1d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha/* 2d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha * Copyright (C) 2014 The Android Open Source Project 3d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha * 4d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha * Licensed under the Apache License, Version 2.0 (the "License"); 5d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha * you may not use this file except in compliance with the License. 6d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha * You may obtain a copy of the License at 7d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha * 8d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha * http://www.apache.org/licenses/LICENSE-2.0 9d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha * 10d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha * Unless required by applicable law or agreed to in writing, software 11d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha * distributed under the License is distributed on an "AS IS" BASIS, 12d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha * See the License for the specific language governing permissions and 14d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha * limitations under the License. 15d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha */ 16d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha 17d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddharthapackage com.android.inputmethod.latin.accounts; 18d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha 19d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddharthaimport android.accounts.AccountManager; 20d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddharthaimport android.content.Context; 21d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddharthaimport android.content.Intent; 22d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddharthaimport android.content.SharedPreferences; 23d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddharthaimport android.preference.PreferenceManager; 24d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddharthaimport android.test.AndroidTestCase; 25d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha 261e10d29bc8975ea45ca5e3bdf1936aa418161bcbSandeep Siddharthaimport com.android.inputmethod.latin.settings.LocalSettingsConstants; 27d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha 28d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha/** 29d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha * Tests for {@link AccountsChangedReceiver}. 30d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha */ 31d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddharthapublic class AccountsChangedReceiverTests extends AndroidTestCase { 32d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha private static final String ACCOUNT_1 = "account1@example.com"; 33d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha private static final String ACCOUNT_2 = "account2@example.com"; 34d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha 35d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha private SharedPreferences mPrefs; 363980675f180cb88dcd2f3bb55cb9231d3bba2c66Sandeep Siddhartha private String mLastKnownAccount = null; 37d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha 38d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha @Override 39d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha protected void setUp() throws Exception { 40d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha super.setUp(); 41d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha mPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); 423980675f180cb88dcd2f3bb55cb9231d3bba2c66Sandeep Siddhartha // Keep track of the current account so that we restore it when the test finishes. 431e10d29bc8975ea45ca5e3bdf1936aa418161bcbSandeep Siddhartha mLastKnownAccount = mPrefs.getString(LocalSettingsConstants.PREF_ACCOUNT_NAME, null); 44d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha } 45d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha 46d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha @Override 47d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha protected void tearDown() throws Exception { 48d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha super.tearDown(); 493980675f180cb88dcd2f3bb55cb9231d3bba2c66Sandeep Siddhartha // Restore the account that was present before running the test. 503980675f180cb88dcd2f3bb55cb9231d3bba2c66Sandeep Siddhartha updateAccountName(mLastKnownAccount); 51d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha } 52d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha 53d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha public void testUnknownIntent() { 54d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha updateAccountName(ACCOUNT_1); 55d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha AccountsChangedReceiver reciever = new AccountsChangedReceiver(); 56d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha reciever.onReceive(getContext(), new Intent("some-random-action")); 57d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha // Account should *not* be removed from preferences. 58d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha assertAccountName(ACCOUNT_1); 59d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha } 60d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha 61d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha public void testAccountRemoved() { 62d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha updateAccountName(ACCOUNT_1); 63d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha AccountsChangedReceiver reciever = new AccountsChangedReceiver() { 64d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha @Override 65d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha protected String[] getAccountsForLogin(Context context) { 66d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha return new String[] {ACCOUNT_2}; 67d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha } 68d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha }; 69d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha reciever.onReceive(getContext(), new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION)); 70d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha // Account should be removed from preferences. 71d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha assertAccountName(null); 72d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha } 73d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha 74d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha public void testAccountRemoved_noAccounts() { 75d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha updateAccountName(ACCOUNT_2); 76d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha AccountsChangedReceiver reciever = new AccountsChangedReceiver() { 77d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha @Override 78d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha protected String[] getAccountsForLogin(Context context) { 79d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha return new String[0]; 80d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha } 81d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha }; 82d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha reciever.onReceive(getContext(), new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION)); 83d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha // Account should be removed from preferences. 84d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha assertAccountName(null); 85d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha } 86d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha 87d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha public void testAccountNotRemoved() { 88d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha updateAccountName(ACCOUNT_2); 89d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha AccountsChangedReceiver reciever = new AccountsChangedReceiver() { 90d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha @Override 91d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha protected String[] getAccountsForLogin(Context context) { 92d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha return new String[] {ACCOUNT_1, ACCOUNT_2}; 93d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha } 94d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha }; 95d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha reciever.onReceive(getContext(), new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION)); 96d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha // Account should *not* be removed from preferences. 97d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha assertAccountName(ACCOUNT_2); 98d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha } 99d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha 100d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha private void updateAccountName(String accountName) { 1013980675f180cb88dcd2f3bb55cb9231d3bba2c66Sandeep Siddhartha if (accountName == null) { 1021e10d29bc8975ea45ca5e3bdf1936aa418161bcbSandeep Siddhartha mPrefs.edit().remove(LocalSettingsConstants.PREF_ACCOUNT_NAME).apply(); 1033980675f180cb88dcd2f3bb55cb9231d3bba2c66Sandeep Siddhartha } else { 1041e10d29bc8975ea45ca5e3bdf1936aa418161bcbSandeep Siddhartha mPrefs.edit().putString(LocalSettingsConstants.PREF_ACCOUNT_NAME, accountName).apply(); 1053980675f180cb88dcd2f3bb55cb9231d3bba2c66Sandeep Siddhartha } 106d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha } 107d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha 108d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha private void assertAccountName(String expectedAccountName) { 1091e10d29bc8975ea45ca5e3bdf1936aa418161bcbSandeep Siddhartha assertEquals(expectedAccountName, 1101e10d29bc8975ea45ca5e3bdf1936aa418161bcbSandeep Siddhartha mPrefs.getString(LocalSettingsConstants.PREF_ACCOUNT_NAME, null)); 111d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha } 112d4aec65f65baf0122cd3d59155307d1ba326f803Sandeep Siddhartha} 113