Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Neal Nguyen | 1a44d5d | 2010-01-13 10:42:43 -0800 | [diff] [blame^] | 17 | package android.accounts; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 18 | |
| 19 | import android.test.AndroidTestCase; |
| 20 | import android.test.RenamingDelegatingContext; |
| 21 | import android.test.IsolatedContext; |
| 22 | import android.test.mock.MockContext; |
| 23 | import android.test.mock.MockContentResolver; |
| 24 | import android.content.*; |
| 25 | import android.accounts.Account; |
| 26 | import android.accounts.AccountManagerService; |
| 27 | import android.os.Bundle; |
| 28 | |
| 29 | import java.util.Arrays; |
| 30 | import java.util.Comparator; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 31 | |
| 32 | public class AccountManagerServiceTest extends AndroidTestCase { |
| 33 | @Override |
| 34 | protected void setUp() throws Exception { |
| 35 | final String filenamePrefix = "test."; |
| 36 | MockContentResolver resolver = new MockContentResolver(); |
| 37 | RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext( |
| 38 | new MockContext(), // The context that most methods are delegated to |
| 39 | getContext(), // The context that file methods are delegated to |
| 40 | filenamePrefix); |
| 41 | Context context = new IsolatedContext(resolver, targetContextWrapper); |
| 42 | setContext(context); |
| 43 | } |
| 44 | |
| 45 | public class AccountSorter implements Comparator<Account> { |
| 46 | public int compare(Account object1, Account object2) { |
| 47 | if (object1 == object2) return 0; |
| 48 | if (object1 == null) return 1; |
| 49 | if (object2 == null) return -1; |
Fred Quintana | 3710f39 | 2009-08-13 14:55:02 -0700 | [diff] [blame] | 50 | int result = object1.type.compareTo(object2.type); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 51 | if (result != 0) return result; |
Fred Quintana | 3710f39 | 2009-08-13 14:55:02 -0700 | [diff] [blame] | 52 | return object1.name.compareTo(object2.name); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | public void testCheckAddAccount() throws Exception { |
| 57 | AccountManagerService ams = new AccountManagerService(getContext()); |
| 58 | Account a11 = new Account("account1", "type1"); |
| 59 | Account a21 = new Account("account2", "type1"); |
| 60 | Account a31 = new Account("account3", "type1"); |
| 61 | Account a12 = new Account("account1", "type2"); |
| 62 | Account a22 = new Account("account2", "type2"); |
| 63 | Account a32 = new Account("account3", "type2"); |
Fred Quintana | 3710f39 | 2009-08-13 14:55:02 -0700 | [diff] [blame] | 64 | ams.addAccount(a11, "p11", null); |
| 65 | ams.addAccount(a12, "p12", null); |
| 66 | ams.addAccount(a21, "p21", null); |
| 67 | ams.addAccount(a22, "p22", null); |
| 68 | ams.addAccount(a31, "p31", null); |
| 69 | ams.addAccount(a32, "p32", null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 70 | |
Fred Quintana | 3710f39 | 2009-08-13 14:55:02 -0700 | [diff] [blame] | 71 | Account[] accounts = ams.getAccounts(null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 72 | Arrays.sort(accounts, new AccountSorter()); |
| 73 | assertEquals(6, accounts.length); |
| 74 | assertEquals(a11, accounts[0]); |
| 75 | assertEquals(a21, accounts[1]); |
| 76 | assertEquals(a31, accounts[2]); |
| 77 | assertEquals(a12, accounts[3]); |
| 78 | assertEquals(a22, accounts[4]); |
| 79 | assertEquals(a32, accounts[5]); |
| 80 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 81 | accounts = ams.getAccountsByType("type1" ); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 82 | Arrays.sort(accounts, new AccountSorter()); |
| 83 | assertEquals(3, accounts.length); |
| 84 | assertEquals(a11, accounts[0]); |
| 85 | assertEquals(a21, accounts[1]); |
| 86 | assertEquals(a31, accounts[2]); |
| 87 | |
Fred Quintana | 3710f39 | 2009-08-13 14:55:02 -0700 | [diff] [blame] | 88 | ams.removeAccount(null, a21); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 89 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 90 | accounts = ams.getAccountsByType("type1" ); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 91 | Arrays.sort(accounts, new AccountSorter()); |
| 92 | assertEquals(2, accounts.length); |
| 93 | assertEquals(a11, accounts[0]); |
| 94 | assertEquals(a31, accounts[1]); |
| 95 | } |
| 96 | |
| 97 | public void testPasswords() throws Exception { |
| 98 | AccountManagerService ams = new AccountManagerService(getContext()); |
| 99 | Account a11 = new Account("account1", "type1"); |
| 100 | Account a12 = new Account("account1", "type2"); |
Fred Quintana | 3710f39 | 2009-08-13 14:55:02 -0700 | [diff] [blame] | 101 | ams.addAccount(a11, "p11", null); |
| 102 | ams.addAccount(a12, "p12", null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 103 | |
| 104 | assertEquals("p11", ams.getPassword(a11)); |
| 105 | assertEquals("p12", ams.getPassword(a12)); |
| 106 | |
| 107 | ams.setPassword(a11, "p11b"); |
| 108 | |
| 109 | assertEquals("p11b", ams.getPassword(a11)); |
| 110 | assertEquals("p12", ams.getPassword(a12)); |
| 111 | } |
| 112 | |
| 113 | public void testUserdata() throws Exception { |
| 114 | AccountManagerService ams = new AccountManagerService(getContext()); |
| 115 | Account a11 = new Account("account1", "type1"); |
| 116 | Bundle u11 = new Bundle(); |
| 117 | u11.putString("a", "a_a11"); |
| 118 | u11.putString("b", "b_a11"); |
| 119 | u11.putString("c", "c_a11"); |
| 120 | Account a12 = new Account("account1", "type2"); |
| 121 | Bundle u12 = new Bundle(); |
| 122 | u12.putString("a", "a_a12"); |
| 123 | u12.putString("b", "b_a12"); |
| 124 | u12.putString("c", "c_a12"); |
Fred Quintana | 3710f39 | 2009-08-13 14:55:02 -0700 | [diff] [blame] | 125 | ams.addAccount(a11, "p11", u11); |
| 126 | ams.addAccount(a12, "p12", u12); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 127 | |
| 128 | assertEquals("a_a11", ams.getUserData(a11, "a")); |
| 129 | assertEquals("b_a11", ams.getUserData(a11, "b")); |
| 130 | assertEquals("c_a11", ams.getUserData(a11, "c")); |
| 131 | assertEquals("a_a12", ams.getUserData(a12, "a")); |
| 132 | assertEquals("b_a12", ams.getUserData(a12, "b")); |
| 133 | assertEquals("c_a12", ams.getUserData(a12, "c")); |
| 134 | |
| 135 | ams.setUserData(a11, "b", "b_a11b"); |
| 136 | |
| 137 | assertEquals("a_a11", ams.getUserData(a11, "a")); |
| 138 | assertEquals("b_a11b", ams.getUserData(a11, "b")); |
| 139 | assertEquals("c_a11", ams.getUserData(a11, "c")); |
| 140 | assertEquals("a_a12", ams.getUserData(a12, "a")); |
| 141 | assertEquals("b_a12", ams.getUserData(a12, "b")); |
| 142 | assertEquals("c_a12", ams.getUserData(a12, "c")); |
| 143 | } |
| 144 | |
| 145 | public void testAuthtokens() throws Exception { |
| 146 | AccountManagerService ams = new AccountManagerService(getContext()); |
| 147 | Account a11 = new Account("account1", "type1"); |
| 148 | Account a12 = new Account("account1", "type2"); |
Fred Quintana | 3710f39 | 2009-08-13 14:55:02 -0700 | [diff] [blame] | 149 | ams.addAccount(a11, "p11", null); |
| 150 | ams.addAccount(a12, "p12", null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 151 | |
| 152 | ams.setAuthToken(a11, "att1", "a11_att1"); |
| 153 | ams.setAuthToken(a11, "att2", "a11_att2"); |
| 154 | ams.setAuthToken(a11, "att3", "a11_att3"); |
| 155 | ams.setAuthToken(a12, "att1", "a12_att1"); |
| 156 | ams.setAuthToken(a12, "att2", "a12_att2"); |
| 157 | ams.setAuthToken(a12, "att3", "a12_att3"); |
| 158 | |
| 159 | assertEquals("a11_att1", ams.peekAuthToken(a11, "att1")); |
| 160 | assertEquals("a11_att2", ams.peekAuthToken(a11, "att2")); |
| 161 | assertEquals("a11_att3", ams.peekAuthToken(a11, "att3")); |
| 162 | assertEquals("a12_att1", ams.peekAuthToken(a12, "att1")); |
| 163 | assertEquals("a12_att2", ams.peekAuthToken(a12, "att2")); |
| 164 | assertEquals("a12_att3", ams.peekAuthToken(a12, "att3")); |
| 165 | |
| 166 | ams.setAuthToken(a11, "att3", "a11_att3b"); |
Fred Quintana | 3710f39 | 2009-08-13 14:55:02 -0700 | [diff] [blame] | 167 | ams.invalidateAuthToken(a12.type, "a12_att2"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 168 | |
| 169 | assertEquals("a11_att1", ams.peekAuthToken(a11, "att1")); |
| 170 | assertEquals("a11_att2", ams.peekAuthToken(a11, "att2")); |
| 171 | assertEquals("a11_att3b", ams.peekAuthToken(a11, "att3")); |
| 172 | assertEquals("a12_att1", ams.peekAuthToken(a12, "att1")); |
| 173 | assertNull(ams.peekAuthToken(a12, "att2")); |
| 174 | assertEquals("a12_att3", ams.peekAuthToken(a12, "att3")); |
| 175 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 176 | assertNull(ams.readAuthTokenFromDatabase(a12, "att2")); |
| 177 | } |
Neal Nguyen | 1a44d5d | 2010-01-13 10:42:43 -0800 | [diff] [blame^] | 178 | } |