summaryrefslogtreecommitdiff
path: root/login
diff options
context:
space:
mode:
authorOluwatobi Bashir-Bello <nbashirbello@google.com>2014-05-08 10:00:00 -0400
committerOluwatobi Bashir-Bello <nbashirbello@google.com>2014-05-15 14:11:51 -0400
commit59a7bdad724cc7e4026f5764b775425fd9337169 (patch)
treedbdebaa0a8848d3f8972f31070f2aac76bfdf021 /login
parent9af33dac562a1c609a9c4a8c23604f720711e720 (diff)
downloadcloud-59a7bdad724cc7e4026f5764b775425fd9337169.tar.gz
Google Login - add functionality to managed multiple users.
Change-Id: I1b6e7a49b6751073440f6490b5cc9271860c33ad
Diffstat (limited to 'login')
-rw-r--r--login/login.iml10
-rw-r--r--login/src/com/google/android/login/User.java56
-rw-r--r--login/src/com/google/android/login/Users.java159
-rw-r--r--login/testSrc/com/google/android/login/UsersTest.java208
4 files changed, 433 insertions, 0 deletions
diff --git a/login/login.iml b/login/login.iml
index 1cb9e89..218a960 100644
--- a/login/login.iml
+++ b/login/login.iml
@@ -4,6 +4,7 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+ <sourceFolder url="file://$MODULE_DIR$/testSrc" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
@@ -20,6 +21,15 @@
</orderEntry>
<orderEntry type="module" module-name="openapi" />
<orderEntry type="library" name="Guava" level="project" />
+ <orderEntry type="module-library" scope="TEST">
+ <library>
+ <CLASSES>
+ <root url="jar://$APPLICATION_HOME_DIR$/lib/junit.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+ </orderEntry>
</component>
</module>
diff --git a/login/src/com/google/android/login/User.java b/login/src/com/google/android/login/User.java
new file mode 100644
index 0000000..6514967
--- /dev/null
+++ b/login/src/com/google/android/login/User.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.google.android.login;
+
+/**
+ * Class that represents a single logged in user.
+ */
+public class User {
+ private String email = "";
+ private boolean isActive = false;
+
+ /**
+ * Constructor
+ * @param email Email address of user
+ */
+ public User(String email) {
+ this.email = email;
+ }
+
+ /**
+ * Returns the email address of this user.
+ * @return Email address of user.
+ */
+ public String getEmail() {
+ return email;
+ }
+
+ /**
+ * Returns true if this user is the active user and false otherwise.
+ * @return True if this user is active and false otherwise.
+ */
+ public boolean isActive() {
+ return isActive;
+ }
+
+ /**
+ * Sets this user to active if <code>isActive</code> is true and false otherwise.
+ * @param isActive True if this user should be set to active and false otherwise.
+ */
+ public void setActive(boolean isActive) {
+ this.isActive = isActive;
+ }
+}
diff --git a/login/src/com/google/android/login/Users.java b/login/src/com/google/android/login/Users.java
new file mode 100644
index 0000000..7310b9a
--- /dev/null
+++ b/login/src/com/google/android/login/Users.java
@@ -0,0 +1,159 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.google.android.login;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Manages the list of logged in users.
+ */
+public class Users {
+ private final Map<String, User> allUsers = new HashMap<String, User>();
+ private User activeUser;
+
+ /**
+ * Returns a copy of the map of the current logged in users.
+ * @return Copy of current logged in users.
+ */
+ public Map<String, User> getAllUsers() {
+ synchronized (this) {
+ Map<String, User> clone = new HashMap<String, User>();
+ clone.putAll(allUsers);
+ return clone;
+ }
+ }
+
+ /**
+ * Completely overrides current map of logged in users with <code>users</code>.
+ * @param users Map of users to set.
+ */
+ public void setAllUsers(Map<String, User> users) {
+ synchronized (this) {
+ allUsers.clear();
+ allUsers.putAll(users);
+ }
+ }
+
+ /**
+ * Returns the active user.
+ * @return the active user.
+ */
+ public User getActiveUser() {
+ synchronized (this) {
+ return activeUser;
+ }
+ }
+
+ /**
+ * Sets the active user to <code>userEmail</code> if <code>userEmail</code> is a logged
+ * in user.
+ * @param userEmail The user to be set as active.
+ * @return Returns true if <code>userEmail</code> was successfully set as the active user.
+ * Returns false, if <code>userEmail</code> is not a logged in user.
+ */
+ public boolean setActiveUser(String userEmail) {
+ synchronized (this) {
+ if(!allUsers.containsKey(userEmail)) {
+ return false;
+ }
+
+ if(activeUser != null) {
+ activeUser.setActive(false);
+ }
+
+ activeUser = allUsers.get(userEmail);
+ activeUser.setActive(true);
+ // TODO: Send message out of changed user
+ return true;
+ }
+ }
+
+ /**
+ * Returns the number of logged in users.
+ * @return Number of logged in users.
+ */
+ public int numberOfUsers() {
+ synchronized (this) {
+ return allUsers.size();
+ }
+ }
+
+ /**
+ * Returns true if there is an active user and false otherwise.
+ * @return True if there is an active user and false otherwise.
+ */
+ public boolean isActiveUserAvailable() {
+ synchronized (this) {
+ return activeUser != null;
+ }
+ }
+
+ /**
+ * Adds a user to the list of current users.
+ * The <code>user</code> becomes the active user.
+ * @param user
+ * @return
+ */
+ public boolean addUser(User user) {
+ synchronized (this) {
+ if(allUsers.containsKey(user.getEmail())) {
+ return false;
+ }
+
+ allUsers.put(user.getEmail(), user);
+ setActiveUser(user.getEmail());
+
+ return true;
+ }
+ }
+
+ /**
+ * Remove <code>userEmail</code> from the list of logged in users if <code>userEmail</code> is
+ * a logged in user. If <code>userEmail</code> is the active user, there would no longer be
+ * an active user once <code>userEmail</code> is removed. Another user will have to explicitly
+ * selected for there to be an active user again.
+ * @param userEmail The user to be removed.
+ * @return True if <code>userEmail</code> was successfully removed from the list of
+ * logged in users and false if <code>userEmail</code> is not a logged in user.
+ */
+ public boolean removeUser(String userEmail) {
+ synchronized (this) {
+ if(!allUsers.containsKey(userEmail)) {
+ return false;
+ }
+
+ if(activeUser.getEmail().equals(userEmail)) {
+ activeUser = null;
+ // TODO: Send message out of changed user
+ }
+
+ allUsers.remove(userEmail);
+ return true;
+ }
+ }
+
+ /**
+ * Clears the map of logged in users.
+ */
+ public void removeAllUsers(){
+ synchronized (this) {
+ allUsers.clear();
+ activeUser = null;
+ // TODO: Send message out of changed user
+ }
+ }
+}
diff --git a/login/testSrc/com/google/android/login/UsersTest.java b/login/testSrc/com/google/android/login/UsersTest.java
new file mode 100644
index 0000000..9bbcbfb
--- /dev/null
+++ b/login/testSrc/com/google/android/login/UsersTest.java
@@ -0,0 +1,208 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.google.android.login;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Tests for {@link Users}
+ */
+public class UsersTest extends TestCase {
+ private Users users;
+ User user1;
+ User user2;
+ User user3;
+
+ @Override
+ public void setUp() {
+ users = new Users();
+ user1 = new User("user1");
+ user2 = new User("user2");
+ user3 = new User("user3");
+ }
+
+ @Override
+ public void tearDown() {
+ users = null;
+ user1 = null;
+ user2 = null;
+ user3 = null;
+ }
+
+ /**
+ * Tests that {@link com.google.intellij.login.Users#addUser(User)}
+ * stores the proper users and currently manages the active user.
+ */
+ public void testAddUser() {
+ Assert.assertEquals(0, users.numberOfUsers());
+
+ users.addUser(user1);
+ Assert.assertEquals(user1.getEmail(), users.getActiveUser().getEmail());
+
+ users.addUser(user2);
+ Assert.assertEquals(user2.getEmail(), users.getActiveUser().getEmail());
+ Assert.assertTrue(user2.isActive());
+ Assert.assertFalse(user1.isActive());
+
+ Map<String, User> allUsers = users.getAllUsers();
+ Assert.assertEquals(2, allUsers.size());
+ Assert.assertNotNull(allUsers.get(user1.getEmail()));
+ Assert.assertNotNull(allUsers.get(user2.getEmail()));
+ }
+
+ /**
+ * Tests that {@link com.google.intellij.login.Users#getActiveUser()}
+ * properly manages the active user so that the active user is either the last
+ * added user or the user that has explicitly requested to be active.
+ */
+ public void testGetActiveUser() {
+ Assert.assertNull(users.getActiveUser());
+
+ users.addUser(user1);
+ Assert.assertNotNull(users.getActiveUser());
+ Assert.assertEquals(user1.getEmail(), users.getActiveUser().getEmail());
+
+ users.addUser(user2);
+ Assert.assertEquals(user2.getEmail(), users.getActiveUser().getEmail());
+
+ users.addUser(user3);
+ Assert.assertEquals(user3.getEmail(), users.getActiveUser().getEmail());
+
+ users.removeUser(user2.getEmail());
+ Assert.assertEquals("user3", users.getActiveUser().getEmail());
+
+ users.removeUser(users.getActiveUser().getEmail());
+ Assert.assertNull(users.getActiveUser());
+
+ Assert.assertFalse(users.setActiveUser(user2.getEmail()));
+ Assert.assertTrue(users.setActiveUser(user1.getEmail()));
+ Assert.assertTrue(user1.isActive());
+ }
+
+ /**
+ * Tests that {@link com.google.intellij.login.Users#getAllUsers()}.
+ */
+ public void testGetAllUsers() {
+ Assert.assertEquals(0, users.getAllUsers().size());
+
+ Assert.assertTrue(users.addUser(user1));
+ Assert.assertEquals(1, users.getAllUsers().size());
+
+ Assert.assertTrue(users.addUser(user2));
+ Assert.assertTrue(users.addUser(user3));
+ Assert.assertEquals(3, users.getAllUsers().size());
+
+ Assert.assertFalse(users.addUser(user3));
+
+ Assert.assertTrue(users.removeUser(user1.getEmail()));
+ Assert.assertEquals(2, users.getAllUsers().size());
+
+ users.removeAllUsers();
+ Assert.assertEquals(0, users.getAllUsers().size());
+ }
+
+ /**
+ * Tests {@link com.google.intellij.login.Users#isActiveUserAvailable()}.
+ */
+ public void testIsActiveUserAvailable() {
+ Assert.assertFalse(users.isActiveUserAvailable());
+
+ users.addUser(user1);
+ Assert.assertTrue(users.isActiveUserAvailable());
+
+ users.addUser(user2);
+ users.addUser(user3);
+ Assert.assertTrue(users.isActiveUserAvailable());
+
+ users.removeUser(user2.getEmail());
+ Assert.assertEquals(user3.getEmail(), users.getActiveUser().getEmail());
+
+ users.removeUser(users.getActiveUser().getEmail());
+ Assert.assertNull(users.getActiveUser());
+
+ Assert.assertFalse(users.setActiveUser(user2.getEmail()));
+ Assert.assertTrue(users.setActiveUser(user1.getEmail()));
+ Assert.assertTrue(user1.isActive());
+ }
+
+ /**
+ * Tests {@link com.google.intellij.login.Users#numberOfUsers()}
+ */
+ public void testNumberOfUsers() {
+ Assert.assertEquals(0, users.numberOfUsers());
+
+ users.addUser(user1);
+ Assert.assertEquals(1, users.numberOfUsers());
+
+ users.removeUser(user1.getEmail());
+ Assert.assertEquals(0, users.numberOfUsers());
+
+ users.addUser(user1);
+ users.addUser(user2);
+ Assert.assertEquals(2, users.numberOfUsers());
+ }
+
+ /**
+ * Tests {@link com.google.intellij.login.Users#removeAllUsers()}
+ */
+ public void testRemoveAllUsers() {
+ users.addUser(user1);
+ users.addUser(user2);
+ users.addUser(user3);
+ Assert.assertEquals(3, users.numberOfUsers());
+
+ users.removeAllUsers();
+ Assert.assertEquals(0, users.numberOfUsers());
+ Assert.assertNull(users.getActiveUser());
+ }
+
+ /**
+ * Tests {@link com.google.intellij.login.Users#removeUser()}
+ */
+ public void testRemoveUser() {
+ Assert.assertFalse(users.removeUser(user1.getEmail()));
+
+ users.addUser(user1);
+ users.addUser(user2);
+ Assert.assertEquals(user2.getEmail(), users.getActiveUser().getEmail());
+
+ Assert.assertTrue(users.removeUser(user2.getEmail()));
+ Assert.assertEquals(1, users.numberOfUsers());
+ Assert.assertNull(users.getActiveUser());
+ }
+
+ /**
+ * Tests {@link com.google.intellij.login.Users#setActiveUser()}
+ */
+ public void testSetActiveUser() {
+ users.addUser(user1);
+ users.addUser(user2);
+ users.addUser(user3);
+
+ Assert.assertEquals(user3.getEmail(), users.getActiveUser().getEmail());
+ users.setActiveUser(user1.getEmail());
+ Assert.assertEquals(user1.getEmail(), users.getActiveUser().getEmail());
+
+ Assert.assertFalse(users.setActiveUser("noUser"));
+ Assert.assertTrue(user1.isActive());
+ }
+
+}