| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser; | 5 package org.chromium.chrome.browser; |
| 6 | 6 |
| 7 import static org.hamcrest.CoreMatchers.containsString; |
| 7 import static org.hamcrest.CoreMatchers.equalTo; | 8 import static org.hamcrest.CoreMatchers.equalTo; |
| 8 import static org.hamcrest.CoreMatchers.hasItem; | 9 import static org.hamcrest.CoreMatchers.not; |
| 9 import static org.junit.Assert.assertFalse; | 10 import static org.hamcrest.CoreMatchers.nullValue; |
| 11 |
| 10 import static org.junit.Assert.assertThat; | 12 import static org.junit.Assert.assertThat; |
| 11 import static org.junit.Assert.assertTrue; | |
| 12 import static org.mockito.ArgumentMatchers.any; | |
| 13 import static org.mockito.ArgumentMatchers.anyInt; | |
| 14 import static org.mockito.ArgumentMatchers.anyString; | |
| 15 import static org.mockito.Mockito.doNothing; | |
| 16 import static org.mockito.Mockito.doReturn; | |
| 17 import static org.mockito.Mockito.mock; | |
| 18 import static org.mockito.Mockito.never; | |
| 19 import static org.mockito.Mockito.spy; | |
| 20 import static org.mockito.Mockito.times; | |
| 21 import static org.mockito.Mockito.verify; | |
| 22 import static org.mockito.Mockito.verifyNoMoreInteractions; | |
| 23 import static org.mockito.Mockito.when; | |
| 24 | 13 |
| 25 import android.app.backup.BackupDataInput; | 14 import android.accounts.Account; |
| 26 import android.app.backup.BackupDataOutput; | 15 import android.accounts.AccountManager; |
| 27 import android.content.Context; | 16 import android.content.Context; |
| 28 import android.content.SharedPreferences; | 17 import android.content.SharedPreferences; |
| 29 import android.os.ParcelFileDescriptor; | |
| 30 | 18 |
| 19 import org.chromium.base.ContextUtils; |
| 20 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 31 import org.junit.Before; | 21 import org.junit.Before; |
| 32 import org.junit.Test; | 22 import org.junit.Test; |
| 33 import org.junit.runner.RunWith; | 23 import org.junit.runner.RunWith; |
| 34 import org.mockito.invocation.InvocationOnMock; | |
| 35 import org.mockito.stubbing.Answer; | |
| 36 import org.robolectric.RuntimeEnvironment; | 24 import org.robolectric.RuntimeEnvironment; |
| 37 import org.robolectric.annotation.Config; | 25 import org.robolectric.annotation.Config; |
| 38 | 26 |
| 39 import org.chromium.base.BaseChromiumApplication; | 27 import java.io.ByteArrayInputStream; |
| 40 import org.chromium.base.ContextUtils; | 28 import java.io.ByteArrayOutputStream; |
| 41 import org.chromium.base.library_loader.ProcessInitException; | |
| 42 import org.chromium.chrome.browser.firstrun.FirstRunSignInProcessor; | |
| 43 import org.chromium.chrome.browser.firstrun.FirstRunStatus; | |
| 44 import org.chromium.components.signin.ChromeSigninController; | |
| 45 import org.chromium.testing.local.LocalRobolectricTestRunner; | |
| 46 | |
| 47 import java.io.File; | 29 import java.io.File; |
| 48 import java.io.FileInputStream; | |
| 49 import java.io.FileNotFoundException; | 30 import java.io.FileNotFoundException; |
| 50 import java.io.IOException; | 31 import java.io.InputStream; |
| 51 import java.io.ObjectInputStream; | 32 import java.io.OutputStream; |
| 52 import java.util.ArrayList; | 33 import java.io.UnsupportedEncodingException; |
| 53 import java.util.Arrays; | |
| 54 | 34 |
| 55 /** | 35 /** |
| 56 * Unit tests for {@link org.chromium.chrome.browser.ChromeBackupAgent}. | 36 * Unit tests for {@link org.chromium.chrome.browser.ChromeBackupAgent}. |
| 57 */ | 37 */ |
| 58 @RunWith(LocalRobolectricTestRunner.class) | 38 @RunWith(LocalRobolectricTestRunner.class) |
| 59 @Config(manifest = Config.NONE, application = BaseChromiumApplication.class) | 39 @Config(manifest = Config.NONE) |
| 60 public class ChromeBackupAgentTest { | 40 public class ChromeBackupAgentTest { |
| 61 private Context mContext; | |
| 62 private ChromeBackupAgent mAgent; | |
| 63 | 41 |
| 64 private void setUpTestPrefs(SharedPreferences prefs) { | 42 static class ChromeTestBackupAgent extends ChromeBackupAgent { |
| 65 SharedPreferences.Editor editor = prefs.edit(); | 43 |
| 66 editor.putBoolean(FirstRunStatus.FIRST_RUN_FLOW_COMPLETE, true); | 44 private ByteArrayInputStream mInputStream; |
| 67 editor.putBoolean(FirstRunSignInProcessor.FIRST_RUN_FLOW_SIGNIN_SETUP, f
alse); | 45 private ByteArrayOutputStream mOutputStream; |
| 68 editor.putString(ChromeSigninController.SIGNED_IN_ACCOUNT_KEY, "user1"); | 46 |
| 69 editor.apply(); | 47 ChromeTestBackupAgent(byte[] mChromeInputPrefs) { |
| 48 // This is protected in ContextWrapper, so can only be called within
a derived |
| 49 // class. |
| 50 attachBaseContext(RuntimeEnvironment.application); |
| 51 mInputStream = new ByteArrayInputStream(mChromeInputPrefs); |
| 52 mOutputStream = new ByteArrayOutputStream(); |
| 53 } |
| 54 |
| 55 @Override |
| 56 protected InputStream openInputStream(File prefsFile) throws FileNotFoun
dException { |
| 57 return mInputStream; |
| 58 |
| 59 } |
| 60 |
| 61 @Override |
| 62 protected OutputStream openOutputStream(File prefsFile) throws FileNotFo
undException { |
| 63 return mOutputStream; |
| 64 } |
| 65 |
| 66 @Override |
| 67 public File getDir(String name, int mode) { |
| 68 return null; |
| 69 } |
| 70 |
| 71 @Override |
| 72 protected long getFileLength(File prefsFile) { |
| 73 return mInputStream.available(); |
| 74 } |
| 75 |
| 76 byte[] getOutputData() { |
| 77 return mOutputStream.toByteArray(); |
| 78 } |
| 70 } | 79 } |
| 71 | 80 |
| 72 @Before | 81 @Before |
| 73 public void setUp() throws ProcessInitException { | 82 public void setUp() throws Exception { |
| 74 // Set up the context. | 83 ContextUtils.initApplicationContextForTests(RuntimeEnvironment.applicati
on); |
| 75 mContext = RuntimeEnvironment.application.getApplicationContext(); | 84 AccountManager manager = (AccountManager) RuntimeEnvironment.application
.getSystemService( |
| 76 ContextUtils.initApplicationContextForTests(mContext); | 85 Context.ACCOUNT_SERVICE); |
| 77 | 86 manager.addAccountExplicitly(new Account("user1", "dummy"), null, null); |
| 78 // Override the native calls. | 87 manager.addAccountExplicitly(new Account("user2", "dummy"), null, null); |
| 79 mAgent = spy(new ChromeBackupAgent()); | |
| 80 doReturn(new String[] {"pref1"}).when(mAgent).nativeGetBoolBackupNames()
; | |
| 81 doReturn(new boolean[] {true}).when(mAgent).nativeGetBoolBackupValues(); | |
| 82 doNothing().when(mAgent).nativeSetBoolBackupPrefs( | |
| 83 any(String[].class), any(boolean[].class)); | |
| 84 | |
| 85 // Mock initializing the browser | |
| 86 doReturn(true).when(mAgent).initializeBrowser(any(Context.class)); | |
| 87 } | 88 } |
| 88 | 89 |
| 89 /** | |
| 90 * Test method for {@link ChromeBackupAgent#onBackup} testing first backup | |
| 91 * | |
| 92 * @throws ProcessInitException | |
| 93 */ | |
| 94 @Test | 90 @Test |
| 95 @SuppressWarnings("unchecked") | 91 public void testOnRestoreFinished() throws UnsupportedEncodingException { |
| 96 public void testOnBackup_firstBackup() throws FileNotFoundException, IOExcep
tion, | 92 SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences(); |
| 97 ClassNotFoundException, Proces
sInitException { | 93 SharedPreferences.Editor editor = sharedPrefs.edit(); |
| 98 // Mock the backup data. | 94 editor.putBoolean("metrics_reporting", false); |
| 99 BackupDataOutput backupData = mock(BackupDataOutput.class); | 95 editor.putString("google.services.username", "user1"); |
| 96 editor.putString("junk", "junk"); |
| 97 editor.apply(); |
| 100 | 98 |
| 101 // Create a state file. | 99 String chromeInputPrefs = |
| 102 File stateFile1 = File.createTempFile("Test", ""); | 100 "{\"junk1\":\"abc\", " |
| 103 ParcelFileDescriptor newState = | 101 + "\"sync\":{ \"has_setup_completed\":\"true\", " |
| 104 ParcelFileDescriptor.open(stateFile1, ParcelFileDescriptor.parse
Mode("w")); | 102 + " \"keep_everything_synced\":\"false\", " |
| 103 + " \"junk2\":\"xxx\"" |
| 104 + " }}"; |
| 105 byte[] chromePrefsBuffer = chromeInputPrefs.getBytes("UTF-8"); |
| 106 ChromeTestBackupAgent chromeTestBackupAgent = new ChromeTestBackupAgent(
chromePrefsBuffer); |
| 107 chromeTestBackupAgent.onRestoreFinished(); |
| 105 | 108 |
| 106 // Set up some preferences to back up. | 109 String chromeOutputPrefs = new String(chromeTestBackupAgent.getOutputDat
a(), "UTF-8"); |
| 107 SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); | |
| 108 setUpTestPrefs(prefs); | |
| 109 | 110 |
| 110 // Run the test function. | 111 // Check that we have only restored the correct Chrome preferences |
| 111 mAgent.onBackup(null, backupData, newState); | 112 assertThat(chromeOutputPrefs, containsString("\"has_setup_completed\":\"
true\"")); |
| 113 assertThat(chromeOutputPrefs, containsString("\"keep_everything_synced\"
:\"false\"")); |
| 114 assertThat(chromeOutputPrefs, not(containsString("junk"))); |
| 112 | 115 |
| 113 // Check that the right things were written to the backup | |
| 114 verify(backupData).writeEntityHeader("native.pref1", 1); | |
| 115 verify(backupData) | |
| 116 .writeEntityHeader("AndroidDefault." + FirstRunStatus.FIRST_RUN_
FLOW_COMPLETE, 1); | |
| 117 verify(backupData, times(2)).writeEntityData(new byte[] {1}, 1); | |
| 118 verify(backupData) | |
| 119 .writeEntityHeader( | |
| 120 "AndroidDefault." + FirstRunSignInProcessor.FIRST_RUN_FL
OW_SIGNIN_SETUP, 1); | |
| 121 verify(backupData).writeEntityData(new byte[] {0}, 1); | |
| 122 byte[] unameBytes = "user1".getBytes(); | |
| 123 verify(backupData) | |
| 124 .writeEntityHeader("AndroidDefault." + ChromeSigninController.SI
GNED_IN_ACCOUNT_KEY, | |
| 125 unameBytes.length); | |
| 126 verify(backupData).writeEntityData(unameBytes, unameBytes.length); | |
| 127 | 116 |
| 128 newState.close(); | 117 // Check that we have only restored the correct Android preferences |
| 118 assertThat(sharedPrefs.getBoolean("metrics_reporting", true), equalTo(fa
lse)); |
| 119 assertThat(sharedPrefs.getString("google.services.username", null), null
Value()); |
| 120 assertThat(sharedPrefs.getString("junk", null), nullValue()); |
| 129 | 121 |
| 130 // Check that the state was saved correctly | 122 // Check that the preferences for which there is special code are correc
t |
| 131 ObjectInputStream newStateStream = new ObjectInputStream(new FileInputSt
ream(stateFile1)); | 123 assertThat(sharedPrefs.getString("first_run_signin_account_name", null),
equalTo("user1")); |
| 132 ArrayList<String> names = (ArrayList<String>) newStateStream.readObject(
); | |
| 133 assertThat(names.size(), equalTo(4)); | |
| 134 assertThat(names, hasItem("native.pref1")); | |
| 135 assertThat(names, hasItem("AndroidDefault." + FirstRunStatus.FIRST_RUN_F
LOW_COMPLETE)); | |
| 136 assertThat(names, | |
| 137 hasItem("AndroidDefault." + FirstRunSignInProcessor.FIRST_RUN_FL
OW_SIGNIN_SETUP)); | |
| 138 assertThat( | |
| 139 names, hasItem("AndroidDefault." + ChromeSigninController.SIGNED
_IN_ACCOUNT_KEY)); | |
| 140 ArrayList<byte[]> values = (ArrayList<byte[]>) newStateStream.readObject
(); | |
| 141 assertThat(values.size(), equalTo(4)); | |
| 142 assertThat(values, hasItem(unameBytes)); | |
| 143 assertThat(values, hasItem(new byte[] {0})); | |
| 144 assertThat(values, hasItem(new byte[] {1})); | |
| 145 // Make sure that there are no extra objects. | |
| 146 assertThat(newStateStream.available(), equalTo(0)); | |
| 147 | |
| 148 // Tidy up. | |
| 149 newStateStream.close(); | |
| 150 stateFile1.delete(); | |
| 151 } | 124 } |
| 152 | 125 |
| 153 /** | |
| 154 * Test method for {@link ChromeBackupAgent#onBackup} a second backup with t
he same data | |
| 155 */ | |
| 156 @Test | 126 @Test |
| 157 @SuppressWarnings("unchecked") | 127 public void testOnRestoreFinishedNoUser() throws UnsupportedEncodingExceptio
n { |
| 158 public void testOnBackup_duplicateBackup() | 128 SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences(); |
| 159 throws FileNotFoundException, IOException, ClassNotFoundException { | 129 SharedPreferences.Editor editor = sharedPrefs.edit(); |
| 160 // Mock the backup data. | 130 editor.putBoolean("metrics_reporting", false); |
| 161 BackupDataOutput backupData = mock(BackupDataOutput.class); | 131 editor.putString("junk", "junk"); |
| 132 editor.apply(); |
| 162 | 133 |
| 163 // Create a state file. | 134 String chromeInputPrefs = |
| 164 File stateFile1 = File.createTempFile("Test", ""); | 135 "{\"junk1\":\"abc\", " |
| 165 ParcelFileDescriptor newState = | 136 + "\"sync\":{ \"has_setup_completed\":\"true\", " |
| 166 ParcelFileDescriptor.open(stateFile1, ParcelFileDescriptor.parse
Mode("w")); | 137 + " \"keep_everything_synced\":\"false\", " |
| 138 + " \"junk2\":\"xxx\"" |
| 139 + " }}"; |
| 140 byte[] chromePrefsBuffer = chromeInputPrefs.getBytes("UTF-8"); |
| 141 ChromeTestBackupAgent chromeTestBackupAgent = new ChromeTestBackupAgent(
chromePrefsBuffer); |
| 142 chromeTestBackupAgent.onRestoreFinished(); |
| 167 | 143 |
| 168 // Set up some preferences to back up. | 144 // Check that we haven't restored any Chrome preferences |
| 169 SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); | 145 String chromeOutputPrefs = new String(chromeTestBackupAgent.getOutputDat
a(), "UTF-8"); |
| 170 setUpTestPrefs(prefs); | 146 assertThat(chromeOutputPrefs, equalTo("")); |
| 171 | 147 |
| 172 // Do a first backup. | 148 // Check that we haven't restored any Android preferences |
| 173 mAgent.onBackup(null, backupData, newState); | 149 assertThat(sharedPrefs.getBoolean("metrics_reporting", true), equalTo(tr
ue)); |
| 174 | 150 assertThat(sharedPrefs.getString("google.services.username", null), null
Value()); |
| 175 // Minimal check on first backup, this isn't the test here. | 151 assertThat(sharedPrefs.getString("junk", null), nullValue()); |
| 176 verify(backupData, times(4)).writeEntityHeader(anyString(), anyInt()); | 152 assertThat(sharedPrefs.getString("first_run_signin_account_name", null),
nullValue()); |
| 177 verify(backupData, times(4)).writeEntityData(any(byte[].class), anyInt()
); | |
| 178 | |
| 179 newState.close(); | |
| 180 | |
| 181 ParcelFileDescriptor oldState = | |
| 182 ParcelFileDescriptor.open(stateFile1, ParcelFileDescriptor.parse
Mode("r")); | |
| 183 File stateFile2 = File.createTempFile("Test", ""); | |
| 184 newState = ParcelFileDescriptor.open(stateFile2, ParcelFileDescriptor.pa
rseMode("w")); | |
| 185 | |
| 186 // Try a second backup without changing any data | |
| 187 mAgent.onBackup(oldState, backupData, newState); | |
| 188 | |
| 189 // Check that the second backup didn't write anything. | |
| 190 verifyNoMoreInteractions(backupData); | |
| 191 | |
| 192 oldState.close(); | |
| 193 newState.close(); | |
| 194 | |
| 195 // The two state files should contain identical data. | |
| 196 ObjectInputStream oldStateStream = new ObjectInputStream(new FileInputSt
ream(stateFile1)); | |
| 197 ArrayList<String> oldNames = (ArrayList<String>) oldStateStream.readObje
ct(); | |
| 198 ArrayList<byte[]> oldValues = (ArrayList<byte[]>) oldStateStream.readObj
ect(); | |
| 199 ObjectInputStream newStateStream = new ObjectInputStream(new FileInputSt
ream(stateFile2)); | |
| 200 ArrayList<String> newNames = (ArrayList<String>) newStateStream.readObje
ct(); | |
| 201 ArrayList<byte[]> newValues = (ArrayList<byte[]>) newStateStream.readObj
ect(); | |
| 202 assertThat(newNames, equalTo(oldNames)); | |
| 203 assertTrue(Arrays.deepEquals(newValues.toArray(), oldValues.toArray())); | |
| 204 assertThat(newStateStream.available(), equalTo(0)); | |
| 205 | |
| 206 // Tidy up. | |
| 207 oldStateStream.close(); | |
| 208 newStateStream.close(); | |
| 209 stateFile1.delete(); | |
| 210 stateFile2.delete(); | |
| 211 } | 153 } |
| 212 | 154 |
| 213 /** | |
| 214 * Test method for {@link ChromeBackupAgent#onBackup} a second backup with d
ifferent data | |
| 215 */ | |
| 216 @Test | 155 @Test |
| 217 @SuppressWarnings("unchecked") | 156 public void testOnRestoreFinishedWrongUser() throws UnsupportedEncodingExcep
tion { |
| 218 public void testOnBackup_dataChanged() | 157 SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences(); |
| 219 throws FileNotFoundException, IOException, ClassNotFoundException { | 158 SharedPreferences.Editor editor = sharedPrefs.edit(); |
| 220 // Mock the backup data. | 159 editor.putBoolean("metrics_reporting", false); |
| 221 BackupDataOutput backupData = mock(BackupDataOutput.class); | 160 editor.putString("google.services.username", "wrong_user"); |
| 222 | 161 editor.putString("junk", "junk"); |
| 223 // Create a state file. | |
| 224 File stateFile1 = File.createTempFile("Test", ""); | |
| 225 ParcelFileDescriptor newState = | |
| 226 ParcelFileDescriptor.open(stateFile1, ParcelFileDescriptor.parse
Mode("w")); | |
| 227 | |
| 228 // Set up some preferences to back up. | |
| 229 SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); | |
| 230 setUpTestPrefs(prefs); | |
| 231 | |
| 232 // Do a first backup. | |
| 233 mAgent.onBackup(null, backupData, newState); | |
| 234 | |
| 235 // Minimal check on first backup, this isn't the test here. | |
| 236 verify(backupData, times(4)).writeEntityHeader(anyString(), anyInt()); | |
| 237 verify(backupData, times(4)).writeEntityData(any(byte[].class), anyInt()
); | |
| 238 | |
| 239 newState.close(); | |
| 240 | |
| 241 ParcelFileDescriptor oldState = | |
| 242 ParcelFileDescriptor.open(stateFile1, ParcelFileDescriptor.parse
Mode("r")); | |
| 243 File stateFile2 = File.createTempFile("Test", ""); | |
| 244 newState = ParcelFileDescriptor.open(stateFile2, ParcelFileDescriptor.pa
rseMode("w")); | |
| 245 | |
| 246 // Change some data. | |
| 247 SharedPreferences.Editor editor = prefs.edit(); | |
| 248 editor.putBoolean(FirstRunSignInProcessor.FIRST_RUN_FLOW_SIGNIN_SETUP, t
rue); | |
| 249 editor.apply(); | 162 editor.apply(); |
| 250 | 163 |
| 251 // Do a second backup. | 164 String chromeInputPrefs = |
| 252 mAgent.onBackup(oldState, backupData, newState); | 165 "{\"junk1\":\"abc\", " |
| 166 + "\"sync\":{ \"has_setup_completed\":\"true\", " |
| 167 + " \"keep_everything_synced\":\"false\", " |
| 168 + " \"junk2\":\"xxx\"" |
| 169 + " }}"; |
| 170 byte[] chromePrefsBuffer = chromeInputPrefs.getBytes("UTF-8"); |
| 171 ChromeTestBackupAgent chromeTestBackupAgent = new ChromeTestBackupAgent(
chromePrefsBuffer); |
| 172 chromeTestBackupAgent.onRestoreFinished(); |
| 253 | 173 |
| 254 // Check that the second backup wrote something. | 174 // Check that we haven't restored any Chrome preferences |
| 255 verify(backupData, times(8)).writeEntityHeader(anyString(), anyInt()); | 175 String chromeOutputPrefs = new String(chromeTestBackupAgent.getOutputDat
a(), "UTF-8"); |
| 256 verify(backupData, times(8)).writeEntityData(any(byte[].class), anyInt()
); | 176 assertThat(chromeOutputPrefs, equalTo("")); |
| 257 | 177 |
| 258 oldState.close(); | 178 // Check that we haven't restored any Android preferences |
| 259 newState.close(); | 179 assertThat(sharedPrefs.getBoolean("metrics_reporting", true), equalTo(tr
ue)); |
| 260 | 180 assertThat(sharedPrefs.getString("google.services.username", null), null
Value()); |
| 261 // the two state files should contain different data (although the names
are unchanged). | 181 assertThat(sharedPrefs.getString("junk", null), nullValue()); |
| 262 ObjectInputStream oldStateStream = new ObjectInputStream(new FileInputSt
ream(stateFile1)); | 182 assertThat(sharedPrefs.getString("first_run_signin_account_name", null),
nullValue()); |
| 263 ArrayList<String> oldNames = (ArrayList<String>) oldStateStream.readObje
ct(); | |
| 264 ArrayList<byte[]> oldValues = (ArrayList<byte[]>) oldStateStream.readObj
ect(); | |
| 265 ObjectInputStream newStateStream = new ObjectInputStream(new FileInputSt
ream(stateFile2)); | |
| 266 ArrayList<String> newNames = (ArrayList<String>) newStateStream.readObje
ct(); | |
| 267 ArrayList<byte[]> newValues = (ArrayList<byte[]>) newStateStream.readObj
ect(); | |
| 268 assertThat(newNames, equalTo(oldNames)); | |
| 269 assertFalse(Arrays.deepEquals(newValues.toArray(), oldValues.toArray()))
; | |
| 270 assertThat(newStateStream.available(), equalTo(0)); | |
| 271 | |
| 272 // Tidy up. | |
| 273 oldStateStream.close(); | |
| 274 newStateStream.close(); | |
| 275 stateFile1.delete(); | |
| 276 stateFile2.delete(); | |
| 277 } | |
| 278 | |
| 279 private BackupDataInput createMockBackupData() throws IOException { | |
| 280 // Mock the backup data | |
| 281 BackupDataInput backupData = mock(BackupDataInput.class); | |
| 282 | |
| 283 final String[] keys = {"native.pref1", "native.pref2", | |
| 284 "AndroidDefault." + FirstRunStatus.FIRST_RUN_FLOW_COMPLETE, "And
roidDefault.junk", | |
| 285 "AndroidDefault." + ChromeSigninController.SIGNED_IN_ACCOUNT_KEY
}; | |
| 286 byte[] unameBytes = "user1".getBytes(); | |
| 287 final byte[][] values = {{0}, {1}, {1}, {23, 42}, unameBytes}; | |
| 288 when(backupData.getKey()).thenAnswer(new Answer<String>() { | |
| 289 private int mPos = 0; | |
| 290 | |
| 291 @Override | |
| 292 public String answer(InvocationOnMock invocation) throws Throwable { | |
| 293 return keys[mPos++]; | |
| 294 } | |
| 295 }); | |
| 296 | |
| 297 when(backupData.getDataSize()).thenAnswer(new Answer<Integer>() { | |
| 298 private int mPos = 0; | |
| 299 | |
| 300 @Override | |
| 301 public Integer answer(InvocationOnMock invocation) throws Throwable
{ | |
| 302 return values[mPos++].length; | |
| 303 } | |
| 304 }); | |
| 305 | |
| 306 when(backupData.readEntityData(any(byte[].class), anyInt(), anyInt())) | |
| 307 .thenAnswer(new Answer<Integer>() { | |
| 308 private int mPos = 0; | |
| 309 | |
| 310 @Override | |
| 311 public Integer answer(InvocationOnMock invocation) throws Th
rowable { | |
| 312 // TODO(aberent): Auto-generated method stub | |
| 313 byte[] buffer = invocation.getArgument(0); | |
| 314 for (int i = 0; i < values[mPos].length; i++) { | |
| 315 buffer[i] = values[mPos][i]; | |
| 316 } | |
| 317 return values[mPos++].length; | |
| 318 } | |
| 319 }); | |
| 320 | |
| 321 when(backupData.readNextHeader()).thenAnswer(new Answer<Boolean>() { | |
| 322 private int mPos = 0; | |
| 323 | |
| 324 @Override | |
| 325 public Boolean answer(InvocationOnMock invocation) throws Throwable
{ | |
| 326 return mPos++ < 5; | |
| 327 } | |
| 328 }); | |
| 329 return backupData; | |
| 330 } | |
| 331 | |
| 332 /** | |
| 333 * Test method for {@link ChromeBackupAgent#onRestore}. | |
| 334 * | |
| 335 * @throws IOException | |
| 336 * @throws ClassNotFoundException | |
| 337 */ | |
| 338 @Test | |
| 339 public void testOnRestore_normal() throws IOException, ClassNotFoundExceptio
n { | |
| 340 BackupDataInput backupData = createMockBackupData(); | |
| 341 | |
| 342 doReturn(true).when(mAgent).accountExistsOnDevice(any(String.class)); | |
| 343 | |
| 344 // Create a state file. | |
| 345 File stateFile = File.createTempFile("Test", ""); | |
| 346 ParcelFileDescriptor newState = | |
| 347 ParcelFileDescriptor.open(stateFile, ParcelFileDescriptor.parseM
ode("w")); | |
| 348 | |
| 349 // Do a restore. | |
| 350 mAgent.onRestore(backupData, 0, newState); | |
| 351 SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); | |
| 352 assertTrue(prefs.getBoolean(FirstRunStatus.FIRST_RUN_FLOW_COMPLETE, fals
e)); | |
| 353 assertFalse(prefs.contains("junk")); | |
| 354 verify(mAgent).nativeSetBoolBackupPrefs( | |
| 355 new String[] {"pref1", "pref2"}, new boolean[] {false, true}); | |
| 356 } | |
| 357 | |
| 358 /** | |
| 359 * Test method for {@link ChromeBackupAgent#onRestore} for a user that doesn
't exist on the | |
| 360 * device | |
| 361 * | |
| 362 * @throws IOException | |
| 363 * @throws ClassNotFoundException | |
| 364 */ | |
| 365 @Test | |
| 366 public void testOnRestore_badUser() throws IOException, ClassNotFoundExcepti
on { | |
| 367 BackupDataInput backupData = createMockBackupData(); | |
| 368 | |
| 369 doReturn(false).when(mAgent).accountExistsOnDevice(any(String.class)); | |
| 370 | |
| 371 // Create a state file. | |
| 372 File stateFile = File.createTempFile("Test", ""); | |
| 373 ParcelFileDescriptor newState = | |
| 374 ParcelFileDescriptor.open(stateFile, ParcelFileDescriptor.parseM
ode("w")); | |
| 375 | |
| 376 // Do a restore. | |
| 377 mAgent.onRestore(backupData, 0, newState); | |
| 378 SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); | |
| 379 assertFalse(prefs.contains(FirstRunStatus.FIRST_RUN_FLOW_COMPLETE)); | |
| 380 verify(mAgent, never()).nativeSetBoolBackupPrefs(any(String[].class), an
y(boolean[].class)); | |
| 381 } | 183 } |
| 382 } | 184 } |
| OLD | NEW |