1 /* 2 * Copyright (C) 2014 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 17 package com.android.inputmethod.latin.accounts; 18 19 import android.accounts.AccountManager; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.SharedPreferences; 23 import android.preference.PreferenceManager; 24 import android.test.AndroidTestCase; 25 26 import com.android.inputmethod.latin.settings.LocalSettingsConstants; 27 28 /** 29 * Tests for {@link AccountsChangedReceiver}. 30 */ 31 public class AccountsChangedReceiverTests extends AndroidTestCase { 32 private static final String ACCOUNT_1 = "account1 (at) example.com"; 33 private static final String ACCOUNT_2 = "account2 (at) example.com"; 34 35 private SharedPreferences mPrefs; 36 private String mLastKnownAccount = null; 37 38 @Override 39 protected void setUp() throws Exception { 40 super.setUp(); 41 mPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); 42 // Keep track of the current account so that we restore it when the test finishes. 43 mLastKnownAccount = mPrefs.getString(LocalSettingsConstants.PREF_ACCOUNT_NAME, null); 44 } 45 46 @Override 47 protected void tearDown() throws Exception { 48 super.tearDown(); 49 // Restore the account that was present before running the test. 50 updateAccountName(mLastKnownAccount); 51 } 52 53 public void testUnknownIntent() { 54 updateAccountName(ACCOUNT_1); 55 AccountsChangedReceiver reciever = new AccountsChangedReceiver(); 56 reciever.onReceive(getContext(), new Intent("some-random-action")); 57 // Account should *not* be removed from preferences. 58 assertAccountName(ACCOUNT_1); 59 } 60 61 public void testAccountRemoved() { 62 updateAccountName(ACCOUNT_1); 63 AccountsChangedReceiver reciever = new AccountsChangedReceiver() { 64 @Override 65 protected String[] getAccountsForLogin(Context context) { 66 return new String[] {ACCOUNT_2}; 67 } 68 }; 69 reciever.onReceive(getContext(), new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION)); 70 // Account should be removed from preferences. 71 assertAccountName(null); 72 } 73 74 public void testAccountRemoved_noAccounts() { 75 updateAccountName(ACCOUNT_2); 76 AccountsChangedReceiver reciever = new AccountsChangedReceiver() { 77 @Override 78 protected String[] getAccountsForLogin(Context context) { 79 return new String[0]; 80 } 81 }; 82 reciever.onReceive(getContext(), new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION)); 83 // Account should be removed from preferences. 84 assertAccountName(null); 85 } 86 87 public void testAccountNotRemoved() { 88 updateAccountName(ACCOUNT_2); 89 AccountsChangedReceiver reciever = new AccountsChangedReceiver() { 90 @Override 91 protected String[] getAccountsForLogin(Context context) { 92 return new String[] {ACCOUNT_1, ACCOUNT_2}; 93 } 94 }; 95 reciever.onReceive(getContext(), new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION)); 96 // Account should *not* be removed from preferences. 97 assertAccountName(ACCOUNT_2); 98 } 99 100 private void updateAccountName(String accountName) { 101 if (accountName == null) { 102 mPrefs.edit().remove(LocalSettingsConstants.PREF_ACCOUNT_NAME).apply(); 103 } else { 104 mPrefs.edit().putString(LocalSettingsConstants.PREF_ACCOUNT_NAME, accountName).apply(); 105 } 106 } 107 108 private void assertAccountName(String expectedAccountName) { 109 assertEquals(expectedAccountName, 110 mPrefs.getString(LocalSettingsConstants.PREF_ACCOUNT_NAME, null)); 111 } 112 } 113