Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.preferences.privacy; | 5 package org.chromium.chrome.browser.preferences.privacy; |
| 6 | 6 |
| 7 import static org.mockito.Mockito.mock; | |
| 8 import static org.mockito.Mockito.when; | |
| 9 | |
| 7 import android.content.Context; | 10 import android.content.Context; |
| 8 import android.support.test.filters.SmallTest; | 11 import android.net.ConnectivityManager; |
| 9 import android.test.InstrumentationTestCase; | 12 import android.net.NetworkInfo; |
| 10 import android.test.UiThreadTest; | 13 |
| 14 import static org.junit.Assert.assertEquals; | |
| 15 | |
| 16 import org.junit.Test; | |
| 17 import org.junit.runner.RunWith; | |
| 18 import org.robolectric.RuntimeEnvironment; | |
| 19 import org.robolectric.annotation.Config; | |
| 11 | 20 |
| 12 import org.chromium.base.CommandLine; | 21 import org.chromium.base.CommandLine; |
| 13 import org.chromium.base.ContextUtils; | 22 import org.chromium.base.ContextUtils; |
| 14 import org.chromium.base.test.util.AdvancedMockContext; | 23 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 15 import org.chromium.base.test.util.Feature; | |
| 16 | 24 |
| 17 /** | 25 /** |
| 18 * Tests "Usage and Crash reporting" preferences. | 26 * junit tests for {@link PrivacyPreferencesManager}'s handling of "Usage and Cr ash reporting" |
| 27 * preferences. | |
| 19 */ | 28 */ |
| 20 public class PrivacyPreferencesManagerTest extends InstrumentationTestCase { | 29 @RunWith(LocalRobolectricTestRunner.class) |
| 21 | 30 @Config(manifest = Config.NONE) |
| 31 public class PrivacyPreferencesManagerTest { | |
| 32 // Parameters to simulate user- and network-permission state. | |
| 22 private static final boolean CONNECTED = true; | 33 private static final boolean CONNECTED = true; |
| 23 private static final boolean DISCONNECTED = false; | 34 private static final boolean DISCONNECTED = false; |
| 24 | 35 |
| 25 private static final boolean WIFI_ON = true; | 36 private static final boolean METERED = true; |
| 26 private static final boolean WIFI_OFF = false; | 37 private static final boolean UNMETERED = false; |
| 27 | 38 |
| 28 private static final boolean METRICS_UPLOAD_OK = true; | 39 private static final boolean METRICS_REPORTING_ENABLED = true; |
| 40 private static final boolean METRICS_REPORTING_DISABLED = false; | |
| 41 | |
| 42 // Used to set test expectations. | |
| 43 private static final boolean METRICS_UPLOAD_PERMITTED = true; | |
| 29 private static final boolean METRICS_UPLOAD_NOT_PERMITTED = false; | 44 private static final boolean METRICS_UPLOAD_NOT_PERMITTED = false; |
| 30 | 45 |
| 31 private static final boolean CRASH_NETWORK_OK = true; | 46 private static final boolean CRASH_NETWORK_AVAILABLE = true; |
| 32 private static final boolean CRASH_NETWORK_NOT_PERMITTED = false; | 47 private static final boolean CRASH_NETWORK_UNAVAILABLE = false; |
| 33 | 48 |
| 34 private static final boolean METRIC_REPORTING_ENABLED = true; | 49 @Test |
| 35 private static final boolean METRIC_REPORTING_DISABLED = false; | 50 public void testUsageAndCrashReportingAccessors() { |
| 51 CommandLine.init(null); | |
| 52 runTest(CONNECTED, UNMETERED, METRICS_REPORTING_ENABLED, METRICS_UPLOAD_ PERMITTED, | |
|
Maria
2017/03/20 23:23:40
So I recently saw a presentation on junit4 and usi
| |
| 53 CRASH_NETWORK_AVAILABLE); | |
| 54 runTest(CONNECTED, METERED, METRICS_REPORTING_ENABLED, METRICS_UPLOAD_PE RMITTED, | |
| 55 CRASH_NETWORK_UNAVAILABLE); | |
| 56 runTest(DISCONNECTED, UNMETERED, METRICS_REPORTING_ENABLED, METRICS_UPLO AD_NOT_PERMITTED, | |
| 57 CRASH_NETWORK_UNAVAILABLE); | |
| 36 | 58 |
| 37 // Perform the same test a few times to make sure any sort of | 59 runTest(CONNECTED, UNMETERED, METRICS_REPORTING_DISABLED, METRICS_UPLOAD _NOT_PERMITTED, |
| 38 // caching still works. | 60 CRASH_NETWORK_AVAILABLE); |
| 39 private static final int REPS = 3; | 61 runTest(CONNECTED, METERED, METRICS_REPORTING_DISABLED, METRICS_UPLOAD_N OT_PERMITTED, |
| 40 | 62 CRASH_NETWORK_UNAVAILABLE); |
| 41 @SmallTest | 63 runTest(DISCONNECTED, UNMETERED, METRICS_REPORTING_DISABLED, METRICS_UPL OAD_NOT_PERMITTED, |
| 42 @Feature({"Android-AppBase"}) | 64 CRASH_NETWORK_UNAVAILABLE); |
| 43 @UiThreadTest | |
| 44 public void testAllowCrashDumpUploadNowCellDev() { | |
| 45 CommandLine.init(null); | |
| 46 runTest(CONNECTED, WIFI_ON, METRIC_REPORTING_ENABLED, METRICS_UPLOAD_OK, CRASH_NETWORK_OK); | |
| 47 runTest(CONNECTED, WIFI_OFF, METRIC_REPORTING_ENABLED, METRICS_UPLOAD_OK , | |
| 48 CRASH_NETWORK_NOT_PERMITTED); | |
| 49 runTest(DISCONNECTED, WIFI_OFF, METRIC_REPORTING_ENABLED, METRICS_UPLOAD _NOT_PERMITTED, | |
| 50 CRASH_NETWORK_NOT_PERMITTED); | |
| 51 | |
| 52 runTest(CONNECTED, WIFI_ON, METRIC_REPORTING_DISABLED, METRICS_UPLOAD_NO T_PERMITTED, | |
| 53 CRASH_NETWORK_OK); | |
| 54 runTest(CONNECTED, WIFI_OFF, METRIC_REPORTING_DISABLED, METRICS_UPLOAD_N OT_PERMITTED, | |
| 55 CRASH_NETWORK_NOT_PERMITTED); | |
| 56 runTest(DISCONNECTED, WIFI_OFF, METRIC_REPORTING_DISABLED, METRICS_UPLOA D_NOT_PERMITTED, | |
| 57 CRASH_NETWORK_NOT_PERMITTED); | |
| 58 } | 65 } |
| 59 | 66 |
| 60 private void runTest(boolean isConnected, boolean wifiOn, boolean isMetricsR eportingEnabled, | 67 private void runTest(boolean isConnected, boolean isMetered, boolean isMetri csReportingEnabled, |
| 61 boolean expectedMetricsUploadPermitted, | 68 boolean expectedMetricsUploadPermitted, |
| 62 boolean expectedNetworkAvailableForCrashUploads) { | 69 boolean expectedNetworkAvailableForCrashUploads) { |
| 63 PermissionContext context = new PermissionContext(getInstrumentation().g etTargetContext()); | 70 // Mock out the network info accessors. |
| 64 ContextUtils.initApplicationContextForTests(context.getApplicationContex t()); | 71 NetworkInfo networkInfo = mock(NetworkInfo.class); |
| 65 PrivacyPreferencesManager preferenceManager = new MockPrivacyPreferences Manager( | 72 when(networkInfo.isConnected()).thenReturn(isConnected); |
| 66 context, isConnected, wifiOn, isMetricsReportingEnabled); | |
| 67 | 73 |
| 68 for (int i = 0; i < REPS; i++) { | 74 ConnectivityManager connectivityManager = mock(ConnectivityManager.class ); |
| 69 String state = String.format("[connected = %b, wifi = %b, reporting = %b]", isConnected, | 75 when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo) ; |
| 70 wifiOn, isMetricsReportingEnabled); | 76 when(connectivityManager.isActiveNetworkMetered()).thenReturn(isMetered) ; |
| 71 String msg = String.format("Metrics reporting should be %1$b for %2$ s", | |
| 72 expectedMetricsUploadPermitted, state); | |
| 73 assertEquals(msg, expectedMetricsUploadPermitted, | |
| 74 preferenceManager.isMetricsUploadPermitted()); | |
| 75 | 77 |
| 76 msg = String.format("Crash reporting should be %1$b for wifi %2$s", | 78 Context context = mock(Context.class); |
| 77 expectedNetworkAvailableForCrashUploads, wifiOn); | 79 when(context.getSystemService(Context.CONNECTIVITY_SERVICE)) |
| 78 assertEquals(msg, expectedNetworkAvailableForCrashUploads, | 80 .thenReturn(connectivityManager); |
| 79 preferenceManager.isNetworkAvailableForCrashUploads()); | 81 |
| 80 } | 82 // Perform other setup. |
| 83 ContextUtils.initApplicationContextForTests(RuntimeEnvironment.applicati on); | |
| 84 PrivacyPreferencesManager preferenceManager = new TestPrivacyPreferences Manager(context); | |
| 85 preferenceManager.setUsageAndCrashReporting(isMetricsReportingEnabled); | |
| 86 | |
| 87 // Test the usage and crash reporting permission accessors! | |
| 88 String state = String.format("[connected = %b, metered = %b, reporting = %b]", isConnected, | |
| 89 isMetered, isMetricsReportingEnabled); | |
| 90 String msg = String.format( | |
| 91 "Metrics reporting should be %1$b for %2$s", expectedMetricsUplo adPermitted, state); | |
| 92 assertEquals( | |
| 93 msg, expectedMetricsUploadPermitted, preferenceManager.isMetrics UploadPermitted()); | |
| 94 | |
| 95 msg = String.format("Crash reporting should be %1$b for metered state %2 $s", | |
| 96 expectedNetworkAvailableForCrashUploads, isMetered); | |
| 97 assertEquals(msg, expectedNetworkAvailableForCrashUploads, | |
| 98 preferenceManager.isNetworkAvailableForCrashUploads()); | |
| 81 } | 99 } |
| 82 | 100 |
| 83 private static class MockPrivacyPreferencesManager extends PrivacyPreference sManager { | 101 private static class TestPrivacyPreferencesManager extends PrivacyPreference sManager { |
| 84 private final boolean mIsConnected; | 102 TestPrivacyPreferencesManager(Context context) { |
| 85 private final boolean mIsWifi; | |
| 86 | |
| 87 MockPrivacyPreferencesManager(Context context, boolean isConnected, bool ean isWifi, | |
| 88 boolean isMetricsReportingEnabled) { | |
| 89 super(context); | 103 super(context); |
| 90 mIsConnected = isConnected; | |
| 91 mIsWifi = isWifi; | |
| 92 | |
| 93 setUsageAndCrashReporting(isMetricsReportingEnabled); | |
| 94 } | 104 } |
| 95 | 105 |
| 96 @Override | 106 // Stub out this call, as it could otherwise call into native code. |
| 97 public boolean isNetworkAvailable() { | 107 public void syncUsageAndCrashReportingPrefs() {} |
| 98 return mIsConnected; | |
| 99 } | |
| 100 | |
| 101 @Override | |
| 102 public boolean isWiFiOrEthernetNetwork() { | |
| 103 return mIsWifi; | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 private static class PermissionContext extends AdvancedMockContext { | |
| 108 public PermissionContext(Context targetContext) { | |
| 109 super(targetContext); | |
| 110 } | |
| 111 | |
| 112 @Override | |
| 113 public Object getSystemService(String name) { | |
| 114 if (Context.CONNECTIVITY_SERVICE.equals(name)) { | |
| 115 return null; | |
| 116 } | |
| 117 fail("Should not ask for any other service than the ConnectionManage r."); | |
| 118 return super.getSystemService(name); | |
| 119 } | |
| 120 } | 108 } |
| 121 } | 109 } |
| OLD | NEW |