Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.components.variations.firstrun; | 5 package org.chromium.components.variations.firstrun; |
| 6 | 6 |
| 7 import static org.hamcrest.CoreMatchers.equalTo; | 7 import static org.hamcrest.CoreMatchers.equalTo; |
| 8 import static org.junit.Assert.assertFalse; | 8 import static org.junit.Assert.assertFalse; |
| 9 import static org.junit.Assert.assertThat; | 9 import static org.junit.Assert.assertThat; |
| 10 import static org.junit.Assert.assertTrue; | 10 import static org.junit.Assert.assertTrue; |
| 11 import static org.mockito.Mockito.doReturn; | 11 import static org.mockito.Mockito.doReturn; |
| 12 import static org.mockito.Mockito.doThrow; | 12 import static org.mockito.Mockito.doThrow; |
| 13 import static org.mockito.Mockito.mock; | 13 import static org.mockito.Mockito.mock; |
| 14 import static org.mockito.Mockito.never; | 14 import static org.mockito.Mockito.never; |
| 15 import static org.mockito.Mockito.spy; | 15 import static org.mockito.Mockito.spy; |
| 16 import static org.mockito.Mockito.verify; | 16 import static org.mockito.Mockito.verify; |
| 17 import static org.mockito.Mockito.when; | 17 import static org.mockito.Mockito.when; |
| 18 | 18 |
| 19 import android.content.SharedPreferences; | 19 import android.content.SharedPreferences; |
| 20 import android.os.Looper; | 20 import android.os.Looper; |
| 21 import android.util.Base64; | 21 import android.util.Base64; |
| 22 | 22 |
| 23 import org.junit.After; | 23 import org.junit.After; |
| 24 import org.junit.Before; | 24 import org.junit.Before; |
| 25 import org.junit.Test; | 25 import org.junit.Test; |
| 26 import org.junit.runner.RunWith; | 26 import org.junit.runner.RunWith; |
| 27 import org.robolectric.RuntimeEnvironment; | |
| 27 import org.robolectric.annotation.Config; | 28 import org.robolectric.annotation.Config; |
| 28 | 29 |
| 29 import org.chromium.base.ContextUtils; | 30 import org.chromium.base.ContextUtils; |
| 30 import org.chromium.base.ThreadUtils; | 31 import org.chromium.base.ThreadUtils; |
| 31 import org.chromium.testing.local.LocalRobolectricTestRunner; | 32 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 32 | 33 |
| 33 import java.io.ByteArrayInputStream; | 34 import java.io.ByteArrayInputStream; |
| 34 import java.io.IOException; | 35 import java.io.IOException; |
| 35 import java.net.HttpURLConnection; | 36 import java.net.HttpURLConnection; |
| 36 | 37 |
| 37 /** | 38 /** |
| 38 * Tests for VariationsSeedFetcher | 39 * Tests for VariationsSeedFetcher |
| 39 */ | 40 */ |
| 40 @RunWith(LocalRobolectricTestRunner.class) | 41 @RunWith(LocalRobolectricTestRunner.class) |
| 41 @Config(manifest = Config.NONE) | 42 @Config(manifest = Config.NONE) |
| 42 public class VariationsSeedFetcherTest { | 43 public class VariationsSeedFetcherTest { |
| 43 private HttpURLConnection mConnection; | 44 private HttpURLConnection mConnection; |
| 44 private VariationsSeedFetcher mFetcher; | 45 private VariationsSeedFetcher mFetcher; |
| 45 private SharedPreferences mPrefs; | 46 private SharedPreferences mPrefs; |
| 46 | 47 |
| 47 @Before | 48 @Before |
| 48 public void setUp() throws IOException { | 49 public void setUp() throws IOException { |
| 50 ContextUtils.initApplicationContextForTests(RuntimeEnvironment.applicati on); | |
|
mikecase (-- gone --)
2017/03/07 20:29:41
Adding this fixed some errors when running the tes
jwd
2017/03/08 22:44:33
What does this line do? The robolectric doc on Run
mikecase (-- gone --)
2017/03/09 01:50:28
I think RuntimeEnvironment.application is just how
jwd
2017/03/09 15:16:31
Ok, seems fine to add it. The test shouldn't be af
| |
| 49 // Pretend we are not on the UI thread, since the class we are testing i s supposed to run | 51 // Pretend we are not on the UI thread, since the class we are testing i s supposed to run |
| 50 // only on a background thread. | 52 // only on a background thread. |
| 51 ThreadUtils.setUiThread(mock(Looper.class)); | 53 ThreadUtils.setUiThread(mock(Looper.class)); |
| 52 mFetcher = spy(VariationsSeedFetcher.get()); | 54 mFetcher = spy(VariationsSeedFetcher.get()); |
| 53 mConnection = mock(HttpURLConnection.class); | 55 mConnection = mock(HttpURLConnection.class); |
| 54 doReturn(mConnection).when(mFetcher).getServerConnection(); | 56 doReturn(mConnection).when(mFetcher).getServerConnection(); |
| 55 mPrefs = ContextUtils.getAppSharedPreferences(); | 57 mPrefs = ContextUtils.getAppSharedPreferences(); |
| 56 mPrefs.edit().clear().apply(); | 58 mPrefs.edit().clear().apply(); |
| 57 } | 59 } |
| 58 | 60 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 @Test | 125 @Test |
| 124 public void testFetchSeed_IOException() throws IOException { | 126 public void testFetchSeed_IOException() throws IOException { |
| 125 doThrow(new IOException()).when(mConnection).connect(); | 127 doThrow(new IOException()).when(mConnection).connect(); |
| 126 | 128 |
| 127 mFetcher.fetchSeed(); | 129 mFetcher.fetchSeed(); |
| 128 | 130 |
| 129 assertTrue(mPrefs.getBoolean(VariationsSeedFetcher.VARIATIONS_INITIALIZE D_PREF, false)); | 131 assertTrue(mPrefs.getBoolean(VariationsSeedFetcher.VARIATIONS_INITIALIZE D_PREF, false)); |
| 130 assertFalse(VariationsSeedBridge.hasJavaPref(ContextUtils.getApplication Context())); | 132 assertFalse(VariationsSeedBridge.hasJavaPref(ContextUtils.getApplication Context())); |
| 131 } | 133 } |
| 132 } | 134 } |
| OLD | NEW |