| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser; | |
| 6 | |
| 7 import android.accounts.Account; | |
| 8 import android.annotation.TargetApi; | |
| 9 import android.content.Context; | |
| 10 import android.content.SharedPreferences; | |
| 11 import android.os.Build; | |
| 12 import android.test.suitebuilder.annotation.SmallTest; | |
| 13 | |
| 14 import org.chromium.base.ContextUtils; | |
| 15 import org.chromium.base.StreamUtil; | |
| 16 import org.chromium.base.ThreadUtils; | |
| 17 import org.chromium.base.test.util.CommandLineFlags; | |
| 18 import org.chromium.base.test.util.MinAndroidSdkLevel; | |
| 19 import org.chromium.chrome.browser.firstrun.FirstRunSignInProcessor; | |
| 20 import org.chromium.chrome.browser.firstrun.FirstRunStatus; | |
| 21 import org.chromium.chrome.browser.init.ChromeBrowserInitializer; | |
| 22 import org.chromium.chrome.browser.signin.AccountIdProvider; | |
| 23 import org.chromium.chrome.test.ChromeTabbedActivityTestBase; | |
| 24 import org.chromium.components.signin.AccountManagerHelper; | |
| 25 import org.chromium.components.signin.ChromeSigninController; | |
| 26 import org.chromium.components.signin.test.util.MockAccountManager; | |
| 27 | |
| 28 import java.io.File; | |
| 29 import java.io.FileInputStream; | |
| 30 import java.io.FileNotFoundException; | |
| 31 import java.io.FileOutputStream; | |
| 32 import java.io.IOException; | |
| 33 import java.io.UnsupportedEncodingException; | |
| 34 | |
| 35 /** | |
| 36 * Android backup tests. | |
| 37 */ | |
| 38 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
| 39 @CommandLineFlags.Remove({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE}) | |
| 40 public class ChromeBackupIntegrationTest extends ChromeTabbedActivityTestBase { | |
| 41 | |
| 42 private static final String GOOGLE_ACCOUNT_TYPE = "com.google"; | |
| 43 private static final String TEST_ACCOUNT_1 = "user1@gmail.com"; | |
| 44 private static final String TEST_ACCOUNT_2 = "user2@gmail.com"; | |
| 45 private Context mTargetContext; | |
| 46 | |
| 47 @Override | |
| 48 public void startMainActivity() throws InterruptedException { | |
| 49 // Do nothing here, the tests need to do some per-test setup before they
start the main | |
| 50 // activity. | |
| 51 } | |
| 52 | |
| 53 private static final class MockAccountIdProvider extends AccountIdProvider { | |
| 54 @Override | |
| 55 public String getAccountId(Context ctx, String accountName) { | |
| 56 return accountName; | |
| 57 } | |
| 58 | |
| 59 @Override | |
| 60 public boolean canBeUsed(Context ctx) { | |
| 61 return true; | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 static class ChromeTestBackupAgent extends ChromeBackupAgent { | |
| 66 ChromeTestBackupAgent(Context context) { | |
| 67 // This is protected in ContextWrapper, so can only be called within
a derived | |
| 68 // class. | |
| 69 attachBaseContext(context); | |
| 70 } | |
| 71 | |
| 72 @Override | |
| 73 protected Account[] getAccounts() { | |
| 74 // ChromeBackupAgent can't use Chrome's account manager, so we overr
ide this to mock | |
| 75 // the existence of the account. | |
| 76 return new Account[] {new Account(TEST_ACCOUNT_1, GOOGLE_ACCOUNT_TYP
E)}; | |
| 77 } | |
| 78 | |
| 79 } | |
| 80 | |
| 81 @Override | |
| 82 public void setUp() throws Exception { | |
| 83 super.setUp(); | |
| 84 clearAppData(); | |
| 85 | |
| 86 mTargetContext = getInstrumentation().getTargetContext(); | |
| 87 | |
| 88 // Create a mock account manager. Although this isn't used by ChromeBack
upAgent it is used | |
| 89 // when we test the result by starting Chrome. | |
| 90 ChromeBackupAgent.allowChromeApplicationForTesting(); | |
| 91 Account account = new Account(TEST_ACCOUNT_1, GOOGLE_ACCOUNT_TYPE); | |
| 92 MockAccountManager accountManager = | |
| 93 new MockAccountManager(mTargetContext, getInstrumentation().getC
ontext(), account); | |
| 94 AccountManagerHelper.overrideAccountManagerHelperForTests(mTargetContext
, accountManager); | |
| 95 | |
| 96 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
| 97 @Override | |
| 98 public void run() { | |
| 99 AccountIdProvider.setInstanceForTest(new MockAccountIdProvider()
); | |
| 100 } | |
| 101 }); | |
| 102 } | |
| 103 | |
| 104 @SmallTest | |
| 105 @MinAndroidSdkLevel(Build.VERSION_CODES.LOLLIPOP) | |
| 106 public void testSimpleRestore() throws InterruptedException, IOException { | |
| 107 | |
| 108 // Fake having previously gone through FRE and signed in. | |
| 109 SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); | |
| 110 SharedPreferences.Editor preferenceEditor = prefs.edit(); | |
| 111 preferenceEditor.putBoolean(FirstRunStatus.FIRST_RUN_FLOW_COMPLETE, true
); | |
| 112 preferenceEditor.putBoolean(FirstRunSignInProcessor.FIRST_RUN_FLOW_SIGNI
N_SETUP, true); | |
| 113 | |
| 114 String chromeInputPrefs = | |
| 115 "{\"junk1\":\"abc\", " | |
| 116 + "\"sync\":{ \"has_setup_completed\":\"true\", " | |
| 117 + " \"keep_everything_synced\":\"false\", " | |
| 118 + " \"passwords\":\"true\", " | |
| 119 + " \"junk2\":\"xxx\"" | |
| 120 + " }}"; | |
| 121 | |
| 122 writeTestChromePrefs(chromeInputPrefs); | |
| 123 | |
| 124 // Set up the mocked account as the signed in account. | |
| 125 preferenceEditor.putString(ChromeSigninController.SIGNED_IN_ACCOUNT_KEY,
TEST_ACCOUNT_1); | |
| 126 preferenceEditor.apply(); | |
| 127 | |
| 128 // Run Chrome's restore code. | |
| 129 new ChromeTestBackupAgent(mTargetContext).onRestoreFinished(); | |
| 130 | |
| 131 // Start Chrome and check that it signs in. | |
| 132 startMainActivityFromLauncher(); | |
| 133 | |
| 134 assertTrue(ChromeSigninController.get(mTargetContext).isSignedIn()); | |
| 135 assertEquals(TEST_ACCOUNT_1, | |
| 136 ChromeSigninController.get(mTargetContext).getSignedInAccountNam
e()); | |
| 137 | |
| 138 String chromeOutputPrefs = readChromePrefs(); | |
| 139 | |
| 140 assertTrue(chromeOutputPrefs.contains("\"keep_everything_synced\":\"fals
e\"")); | |
| 141 assertTrue(chromeOutputPrefs.contains("\"passwords\":\"true\"")); | |
| 142 assertFalse(chromeOutputPrefs.contains("junk")); | |
| 143 | |
| 144 } | |
| 145 | |
| 146 | |
| 147 @SmallTest | |
| 148 @MinAndroidSdkLevel(Build.VERSION_CODES.LOLLIPOP) | |
| 149 public void testRestoreAccountMissing() throws InterruptedException, IOExcep
tion { | |
| 150 // Fake having previously gone through FRE and signed in. | |
| 151 SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); | |
| 152 SharedPreferences.Editor preferenceEditor = prefs.edit(); | |
| 153 preferenceEditor.putBoolean(FirstRunStatus.FIRST_RUN_FLOW_COMPLETE, true
); | |
| 154 preferenceEditor.putBoolean(FirstRunSignInProcessor.FIRST_RUN_FLOW_SIGNI
N_SETUP, true); | |
| 155 | |
| 156 // Use an account that hasn't been created by the mocks as the signed in
account. | |
| 157 preferenceEditor.putString(ChromeSigninController.SIGNED_IN_ACCOUNT_KEY,
TEST_ACCOUNT_2); | |
| 158 preferenceEditor.apply(); | |
| 159 | |
| 160 String chromeInputPrefs = "{}"; | |
| 161 | |
| 162 writeTestChromePrefs(chromeInputPrefs); | |
| 163 // Run Chrome's restore code. | |
| 164 new ChromeTestBackupAgent(mTargetContext).onRestoreFinished(); | |
| 165 | |
| 166 // Start Chrome. | |
| 167 startMainActivityFromLauncher(); | |
| 168 | |
| 169 // Since the account didn't exist, Chrome should not be signed in. | |
| 170 assertFalse(ChromeSigninController.get(mTargetContext).isSignedIn()); | |
| 171 } | |
| 172 | |
| 173 private void writeTestChromePrefs(String chromeInputPrefs) | |
| 174 throws FileNotFoundException, UnsupportedEncodingException, IOExcept
ion { | |
| 175 FileOutputStream prefsFileWriter = null; | |
| 176 try { | |
| 177 File prefsDir = | |
| 178 mTargetContext.getDir(ChromeBrowserInitializer.PRIVATE_DATA_
DIRECTORY_SUFFIX, | |
| 179 Context.MODE_PRIVATE); | |
| 180 prefsDir = new File(prefsDir, "Default"); | |
| 181 assertTrue(prefsDir.mkdirs()); | |
| 182 File prefsFile = new File(prefsDir, "Preferences"); | |
| 183 prefsFileWriter = new FileOutputStream(prefsFile); | |
| 184 prefsFileWriter.write(chromeInputPrefs.getBytes("UTF-8")); | |
| 185 } finally { | |
| 186 StreamUtil.closeQuietly(prefsFileWriter); | |
| 187 } | |
| 188 } | |
| 189 | |
| 190 private String readChromePrefs() | |
| 191 throws FileNotFoundException, IOException, UnsupportedEncodingExcept
ion { | |
| 192 FileInputStream prefsFileReader = null; | |
| 193 try { | |
| 194 File prefsDir = | |
| 195 mTargetContext.getDir(ChromeBrowserInitializer.PRIVATE_DATA_
DIRECTORY_SUFFIX, | |
| 196 Context.MODE_PRIVATE); | |
| 197 prefsDir = new File(prefsDir, "Default"); | |
| 198 File prefsFile = new File(prefsDir, "Preferences"); | |
| 199 prefsFileReader = new FileInputStream(prefsFile); | |
| 200 int fileLength = (int) prefsFile.length(); | |
| 201 byte[] inputBuffer = new byte[fileLength]; | |
| 202 assertEquals(fileLength, prefsFileReader.read(inputBuffer)); | |
| 203 return new String(inputBuffer, "UTF-8"); | |
| 204 } finally { | |
| 205 StreamUtil.closeQuietly(prefsFileReader); | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 } | |
| OLD | NEW |