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