| 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.equalTo; | 7 import static org.hamcrest.CoreMatchers.equalTo; |
| 8 import static org.hamcrest.CoreMatchers.not; | 8 import static org.hamcrest.CoreMatchers.not; |
| 9 import static org.hamcrest.CoreMatchers.nullValue; | 9 import static org.hamcrest.CoreMatchers.nullValue; |
| 10 import static org.junit.Assert.assertThat; | 10 import static org.junit.Assert.assertThat; |
| 11 import static org.junit.matchers.JUnitMatchers.containsString; | 11 import static org.junit.matchers.JUnitMatchers.containsString; |
| 12 | 12 |
| 13 import android.accounts.Account; | 13 import android.accounts.Account; |
| 14 import android.accounts.AccountManager; | 14 import android.accounts.AccountManager; |
| 15 import android.content.Context; | 15 import android.content.Context; |
| 16 import android.content.SharedPreferences; | 16 import android.content.SharedPreferences; |
| 17 | 17 |
| 18 import org.chromium.base.ContextUtils; | 18 import org.chromium.base.ContextUtils; |
| 19 import org.chromium.testing.local.LocalRobolectricTestRunner; | 19 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 20 import org.junit.Before; | 20 import org.junit.Before; |
| 21 import org.junit.Test; | 21 import org.junit.Test; |
| 22 import org.junit.runner.RunWith; | 22 import org.junit.runner.RunWith; |
| 23 import org.robolectric.RuntimeEnvironment; | 23 import org.robolectric.Robolectric; |
| 24 import org.robolectric.annotation.Config; | 24 import org.robolectric.annotation.Config; |
| 25 | 25 |
| 26 import java.io.ByteArrayInputStream; | 26 import java.io.ByteArrayInputStream; |
| 27 import java.io.ByteArrayOutputStream; | 27 import java.io.ByteArrayOutputStream; |
| 28 import java.io.File; | 28 import java.io.File; |
| 29 import java.io.FileNotFoundException; | 29 import java.io.FileNotFoundException; |
| 30 import java.io.InputStream; | 30 import java.io.InputStream; |
| 31 import java.io.OutputStream; | 31 import java.io.OutputStream; |
| 32 import java.io.UnsupportedEncodingException; | 32 import java.io.UnsupportedEncodingException; |
| 33 | 33 |
| 34 /** | 34 /** |
| 35 * Unit tests for {@link org.chromium.chrome.browser.ChromeBackupAgent}. | 35 * Unit tests for {@link org.chromium.chrome.browser.ChromeBackupAgent}. |
| 36 */ | 36 */ |
| 37 @RunWith(LocalRobolectricTestRunner.class) | 37 @RunWith(LocalRobolectricTestRunner.class) |
| 38 @Config(manifest = Config.NONE) | 38 @Config(manifest = Config.NONE) |
| 39 public class ChromeBackupAgentTest { | 39 public class ChromeBackupAgentTest { |
| 40 | 40 |
| 41 static class ChromeTestBackupAgent extends ChromeBackupAgent { | 41 static class ChromeTestBackupAgent extends ChromeBackupAgent { |
| 42 | 42 |
| 43 private ByteArrayInputStream mInputStream; | 43 private ByteArrayInputStream mInputStream; |
| 44 private ByteArrayOutputStream mOutputStream; | 44 private ByteArrayOutputStream mOutputStream; |
| 45 | 45 |
| 46 ChromeTestBackupAgent(byte[] mChromeInputPrefs) { | 46 ChromeTestBackupAgent(byte[] mChromeInputPrefs) { |
| 47 // This is protected in ContextWrapper, so can only be called within
a derived | 47 // This is protected in ContextWrapper, so can only be called within
a derived |
| 48 // class. | 48 // class. |
| 49 attachBaseContext(RuntimeEnvironment.application); | 49 attachBaseContext(Robolectric.application); |
| 50 mInputStream = new ByteArrayInputStream(mChromeInputPrefs); | 50 mInputStream = new ByteArrayInputStream(mChromeInputPrefs); |
| 51 mOutputStream = new ByteArrayOutputStream(); | 51 mOutputStream = new ByteArrayOutputStream(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 @Override | 54 @Override |
| 55 protected InputStream openInputStream(File prefsFile) throws FileNotFoun
dException { | 55 protected InputStream openInputStream(File prefsFile) throws FileNotFoun
dException { |
| 56 return mInputStream; | 56 return mInputStream; |
| 57 | 57 |
| 58 } | 58 } |
| 59 | 59 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 72 return mInputStream.available(); | 72 return mInputStream.available(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 byte[] getOutputData() { | 75 byte[] getOutputData() { |
| 76 return mOutputStream.toByteArray(); | 76 return mOutputStream.toByteArray(); |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 | 79 |
| 80 @Before | 80 @Before |
| 81 public void setUp() throws Exception { | 81 public void setUp() throws Exception { |
| 82 ContextUtils.initApplicationContextForTests(RuntimeEnvironment.applicati
on); | 82 ContextUtils.initApplicationContextForTests(Robolectric.application); |
| 83 AccountManager manager = (AccountManager) RuntimeEnvironment.application
.getSystemService( | 83 AccountManager manager = |
| 84 Context.ACCOUNT_SERVICE); | 84 (AccountManager) Robolectric.application.getSystemService(Contex
t.ACCOUNT_SERVICE); |
| 85 manager.addAccountExplicitly(new Account("user1", "dummy"), null, null); | 85 manager.addAccountExplicitly(new Account("user1", "dummy"), null, null); |
| 86 manager.addAccountExplicitly(new Account("user2", "dummy"), null, null); | 86 manager.addAccountExplicitly(new Account("user2", "dummy"), null, null); |
| 87 } | 87 } |
| 88 | 88 |
| 89 @Test | 89 @Test |
| 90 public void testOnRestoreFinished() throws UnsupportedEncodingException { | 90 public void testOnRestoreFinished() throws UnsupportedEncodingException { |
| 91 SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences(); | 91 SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences(); |
| 92 SharedPreferences.Editor editor = sharedPrefs.edit(); | 92 SharedPreferences.Editor editor = sharedPrefs.edit(); |
| 93 editor.putBoolean("crash_dump_upload", false); | 93 editor.putBoolean("crash_dump_upload", false); |
| 94 editor.putString("google.services.username", "user1"); | 94 editor.putString("google.services.username", "user1"); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 String chromeOutputPrefs = new String(chromeTestBackupAgent.getOutputDat
a(), "UTF-8"); | 174 String chromeOutputPrefs = new String(chromeTestBackupAgent.getOutputDat
a(), "UTF-8"); |
| 175 assertThat(chromeOutputPrefs, equalTo("")); | 175 assertThat(chromeOutputPrefs, equalTo("")); |
| 176 | 176 |
| 177 // Check that we haven't restored any Android preferences | 177 // Check that we haven't restored any Android preferences |
| 178 assertThat(sharedPrefs.getBoolean("crash_dump_upload", true), equalTo(tr
ue)); | 178 assertThat(sharedPrefs.getBoolean("crash_dump_upload", true), equalTo(tr
ue)); |
| 179 assertThat(sharedPrefs.getString("google.services.username", null), null
Value()); | 179 assertThat(sharedPrefs.getString("google.services.username", null), null
Value()); |
| 180 assertThat(sharedPrefs.getString("junk", null), nullValue()); | 180 assertThat(sharedPrefs.getString("junk", null), nullValue()); |
| 181 assertThat(sharedPrefs.getString("first_run_signin_account_name", null),
nullValue()); | 181 assertThat(sharedPrefs.getString("first_run_signin_account_name", null),
nullValue()); |
| 182 } | 182 } |
| 183 } | 183 } |
| OLD | NEW |