Commit afa20771 authored by aberent's avatar aberent Committed by Commit bot

[Android backup] Sync settings, and new to old version restore

Backup and restore the sync settings. Also enable restore to an
older version of Chrome than the backed up version.

BUG=616477
BUG=613205

Review-Url: https://codereview.chromium.org/2047473003
Cr-Commit-Position: refs/heads/master@{#398267}
parent 7cac6030
......@@ -103,6 +103,7 @@ by a child template that "extends" this file.
android:largeHeap="false"
android:allowBackup="true"
android:fullBackupContent="@xml/chromebackupscheme"
android:restoreAnyVersion="true"
{% block android_backup_agent %}
android:backupAgent="org.chromium.chrome.browser.ChromeBackupAgent"
{% endblock %}
......
......@@ -5,4 +5,5 @@
<full-backup-content>
<include domain="sharedpref" path="{{manifest_package}}_preferences.xml"/>
<include domain="root" path="app_chrome/Default/Preferences"/>
</full-backup-content>
\ No newline at end of file
......@@ -12,16 +12,25 @@ import android.os.Build;
import android.test.suitebuilder.annotation.SmallTest;
import org.chromium.base.ContextUtils;
import org.chromium.base.StreamUtil;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.MinAndroidSdkLevel;
import org.chromium.chrome.browser.firstrun.FirstRunSignInProcessor;
import org.chromium.chrome.browser.firstrun.FirstRunStatus;
import org.chromium.chrome.browser.init.ChromeBrowserInitializer;
import org.chromium.chrome.browser.signin.AccountIdProvider;
import org.chromium.chrome.test.ChromeTabbedActivityTestBase;
import org.chromium.sync.signin.AccountManagerHelper;
import org.chromium.sync.signin.ChromeSigninController;
import org.chromium.sync.test.util.MockAccountManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
/**
* Android backup tests.
*/
......@@ -63,7 +72,7 @@ public class ChromeBackupIntegrationTest extends ChromeTabbedActivityTestBase {
protected Account[] getAccounts() {
// ChromeBackupAgent can't use Chrome's account manager, so we override this to mock
// the existence of the account.
return new Account[]{new Account(TEST_ACCOUNT_1, GOOGLE_ACCOUNT_TYPE)};
return new Account[] {new Account(TEST_ACCOUNT_1, GOOGLE_ACCOUNT_TYPE)};
}
}
......@@ -87,7 +96,7 @@ public class ChromeBackupIntegrationTest extends ChromeTabbedActivityTestBase {
@SmallTest
@MinAndroidSdkLevel(Build.VERSION_CODES.LOLLIPOP)
public void testSimpleRestore() throws InterruptedException {
public void testSimpleRestore() throws InterruptedException, IOException {
// Fake having previously gone through FRE and signed in.
SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
......@@ -95,6 +104,16 @@ public class ChromeBackupIntegrationTest extends ChromeTabbedActivityTestBase {
preferenceEditor.putBoolean(FirstRunStatus.FIRST_RUN_FLOW_COMPLETE, true);
preferenceEditor.putBoolean(FirstRunSignInProcessor.FIRST_RUN_FLOW_SIGNIN_SETUP, true);
String chromeInputPrefs =
"{\"junk1\":\"abc\", "
+ "\"sync\":{ \"has_setup_completed\":\"true\", "
+ " \"keep_everything_synced\":\"false\", "
+ " \"passwords\":\"true\", "
+ " \"junk2\":\"xxx\""
+ " }}";
writeTestChromePrefs(chromeInputPrefs);
// Set up the mocked account as the signed in account.
preferenceEditor.putString(ChromeSigninController.SIGNED_IN_ACCOUNT_KEY, TEST_ACCOUNT_1);
preferenceEditor.commit();
......@@ -108,11 +127,19 @@ public class ChromeBackupIntegrationTest extends ChromeTabbedActivityTestBase {
assertTrue(ChromeSigninController.get(mTargetContext).isSignedIn());
assertEquals(TEST_ACCOUNT_1,
ChromeSigninController.get(mTargetContext).getSignedInAccountName());
String chromeOutputPrefs = readChromePrefs();
assertTrue(chromeOutputPrefs.contains("\"keep_everything_synced\":\"false\""));
assertTrue(chromeOutputPrefs.contains("\"passwords\":\"true\""));
assertFalse(chromeOutputPrefs.contains("junk"));
}
@SmallTest
@MinAndroidSdkLevel(Build.VERSION_CODES.LOLLIPOP)
public void testRestoreAccountMissing() throws InterruptedException {
public void testRestoreAccountMissing() throws InterruptedException, IOException {
// Fake having previously gone through FRE and signed in.
SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
SharedPreferences.Editor preferenceEditor = prefs.edit();
......@@ -123,6 +150,9 @@ public class ChromeBackupIntegrationTest extends ChromeTabbedActivityTestBase {
preferenceEditor.putString(ChromeSigninController.SIGNED_IN_ACCOUNT_KEY, TEST_ACCOUNT_2);
preferenceEditor.commit();
String chromeInputPrefs = "{}";
writeTestChromePrefs(chromeInputPrefs);
// Run Chrome's restore code.
new ChromeTestBackupAgent(mTargetContext).onRestoreFinished();
......@@ -133,4 +163,40 @@ public class ChromeBackupIntegrationTest extends ChromeTabbedActivityTestBase {
assertFalse(ChromeSigninController.get(mTargetContext).isSignedIn());
}
private void writeTestChromePrefs(String chromeInputPrefs)
throws FileNotFoundException, UnsupportedEncodingException, IOException {
FileOutputStream prefsFileWriter = null;
try {
File prefsDir =
mTargetContext.getDir(ChromeBrowserInitializer.PRIVATE_DATA_DIRECTORY_SUFFIX,
Context.MODE_PRIVATE);
prefsDir = new File(prefsDir, "Default");
assertTrue(prefsDir.mkdirs());
File prefsFile = new File(prefsDir, "Preferences");
prefsFileWriter = new FileOutputStream(prefsFile);
prefsFileWriter.write(chromeInputPrefs.getBytes("UTF-8"));
} finally {
StreamUtil.closeQuietly(prefsFileWriter);
}
}
private String readChromePrefs()
throws FileNotFoundException, IOException, UnsupportedEncodingException {
FileInputStream prefsFileReader = null;
try {
File prefsDir =
mTargetContext.getDir(ChromeBrowserInitializer.PRIVATE_DATA_DIRECTORY_SUFFIX,
Context.MODE_PRIVATE);
prefsDir = new File(prefsDir, "Default");
File prefsFile = new File(prefsDir, "Preferences");
prefsFileReader = new FileInputStream(prefsFile);
int fileLength = (int) prefsFile.length();
byte[] inputBuffer = new byte[fileLength];
assertEquals(fileLength, prefsFileReader.read(inputBuffer));
return new String(inputBuffer, "UTF-8");
} finally {
StreamUtil.closeQuietly(prefsFileReader);
}
}
}
......@@ -5,8 +5,10 @@
package org.chromium.chrome.browser;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.containsString;
import android.accounts.Account;
import android.accounts.AccountManager;
......@@ -21,6 +23,14 @@ import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.annotation.Config;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
/**
* Unit tests for {@link org.chromium.chrome.browser.ChromeBackupAgent}.
*/
......@@ -29,10 +39,41 @@ import org.robolectric.annotation.Config;
public class ChromeBackupAgentTest {
static class ChromeTestBackupAgent extends ChromeBackupAgent {
ChromeTestBackupAgent() {
private ByteArrayInputStream mInputStream;
private ByteArrayOutputStream mOutputStream;
ChromeTestBackupAgent(byte[] mChromeInputPrefs) {
// This is protected in ContextWrapper, so can only be called within a derived
// class.
attachBaseContext(Robolectric.application);
mInputStream = new ByteArrayInputStream(mChromeInputPrefs);
mOutputStream = new ByteArrayOutputStream();
}
@Override
protected InputStream openInputStream(File prefsFile) throws FileNotFoundException {
return mInputStream;
}
@Override
protected OutputStream openOutputStream(File prefsFile) throws FileNotFoundException {
return mOutputStream;
}
@Override
public File getDir(String name, int mode) {
return null;
}
@Override
protected long getFileLength(File prefsFile) {
return mInputStream.available();
}
byte[] getOutputData() {
return mOutputStream.toByteArray();
}
}
......@@ -46,7 +87,7 @@ public class ChromeBackupAgentTest {
}
@Test
public void testOnRestoreFinished() {
public void testOnRestoreFinished() throws UnsupportedEncodingException {
SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences();
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putBoolean("crash_dump_upload", false);
......@@ -54,9 +95,25 @@ public class ChromeBackupAgentTest {
editor.putString("junk", "junk");
editor.commit();
new ChromeTestBackupAgent().onRestoreFinished();
String chromeInputPrefs =
"{\"junk1\":\"abc\", "
+ "\"sync\":{ \"has_setup_completed\":\"true\", "
+ " \"keep_everything_synced\":\"false\", "
+ " \"junk2\":\"xxx\""
+ " }}";
byte[] chromePrefsBuffer = chromeInputPrefs.getBytes("UTF-8");
ChromeTestBackupAgent chromeTestBackupAgent = new ChromeTestBackupAgent(chromePrefsBuffer);
chromeTestBackupAgent.onRestoreFinished();
String chromeOutputPrefs = new String(chromeTestBackupAgent.getOutputData(), "UTF-8");
// Check that we have only restored the correct preferences
// Check that we have only restored the correct Chrome preferences
assertThat(chromeOutputPrefs, containsString("\"has_setup_completed\":\"true\""));
assertThat(chromeOutputPrefs, containsString("\"keep_everything_synced\":\"false\""));
assertThat(chromeOutputPrefs, not(containsString("junk")));
// Check that we have only restored the correct Android preferences
assertThat(sharedPrefs.getBoolean("crash_dump_upload", true), equalTo(false));
assertThat(sharedPrefs.getString("google.services.username", null), nullValue());
assertThat(sharedPrefs.getString("junk", null), nullValue());
......@@ -66,16 +123,28 @@ public class ChromeBackupAgentTest {
}
@Test
public void testOnRestoreFinishedNoUser() {
public void testOnRestoreFinishedNoUser() throws UnsupportedEncodingException {
SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences();
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putBoolean("crash_dump_upload", false);
editor.putString("junk", "junk");
editor.commit();
new ChromeTestBackupAgent().onRestoreFinished();
// Check that we haven't restored any preferences
String chromeInputPrefs =
"{\"junk1\":\"abc\", "
+ "\"sync\":{ \"has_setup_completed\":\"true\", "
+ " \"keep_everything_synced\":\"false\", "
+ " \"junk2\":\"xxx\""
+ " }}";
byte[] chromePrefsBuffer = chromeInputPrefs.getBytes("UTF-8");
ChromeTestBackupAgent chromeTestBackupAgent = new ChromeTestBackupAgent(chromePrefsBuffer);
chromeTestBackupAgent.onRestoreFinished();
// Check that we haven't restored any Chrome preferences
String chromeOutputPrefs = new String(chromeTestBackupAgent.getOutputData(), "UTF-8");
assertThat(chromeOutputPrefs, equalTo(""));
// Check that we haven't restored any Android preferences
assertThat(sharedPrefs.getBoolean("crash_dump_upload", true), equalTo(true));
assertThat(sharedPrefs.getString("google.services.username", null), nullValue());
assertThat(sharedPrefs.getString("junk", null), nullValue());
......@@ -83,7 +152,7 @@ public class ChromeBackupAgentTest {
}
@Test
public void testOnRestoreFinishedWrongUser() {
public void testOnRestoreFinishedWrongUser() throws UnsupportedEncodingException {
SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences();
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putBoolean("crash_dump_upload", false);
......@@ -91,9 +160,21 @@ public class ChromeBackupAgentTest {
editor.putString("junk", "junk");
editor.commit();
new ChromeTestBackupAgent().onRestoreFinished();
// Check that we haven't restored any preferences
String chromeInputPrefs =
"{\"junk1\":\"abc\", "
+ "\"sync\":{ \"has_setup_completed\":\"true\", "
+ " \"keep_everything_synced\":\"false\", "
+ " \"junk2\":\"xxx\""
+ " }}";
byte[] chromePrefsBuffer = chromeInputPrefs.getBytes("UTF-8");
ChromeTestBackupAgent chromeTestBackupAgent = new ChromeTestBackupAgent(chromePrefsBuffer);
chromeTestBackupAgent.onRestoreFinished();
// Check that we haven't restored any Chrome preferences
String chromeOutputPrefs = new String(chromeTestBackupAgent.getOutputData(), "UTF-8");
assertThat(chromeOutputPrefs, equalTo(""));
// Check that we haven't restored any Android preferences
assertThat(sharedPrefs.getBoolean("crash_dump_upload", true), equalTo(true));
assertThat(sharedPrefs.getString("google.services.username", null), nullValue());
assertThat(sharedPrefs.getString("junk", null), nullValue());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment