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); |
36 | 52 |
37 // Perform the same test a few times to make sure any sort of | 53 // TODO(yolandyan): Use Junit4 parameters to clean up this test structur
e. |
38 // caching still works. | 54 runTest(CONNECTED, UNMETERED, METRICS_REPORTING_ENABLED, METRICS_UPLOAD_
PERMITTED, |
39 private static final int REPS = 3; | 55 CRASH_NETWORK_AVAILABLE); |
| 56 runTest(CONNECTED, METERED, METRICS_REPORTING_ENABLED, METRICS_UPLOAD_PE
RMITTED, |
| 57 CRASH_NETWORK_UNAVAILABLE); |
| 58 runTest(DISCONNECTED, UNMETERED, METRICS_REPORTING_ENABLED, METRICS_UPLO
AD_NOT_PERMITTED, |
| 59 CRASH_NETWORK_UNAVAILABLE); |
40 | 60 |
41 @SmallTest | 61 runTest(CONNECTED, UNMETERED, METRICS_REPORTING_DISABLED, METRICS_UPLOAD
_NOT_PERMITTED, |
42 @Feature({"Android-AppBase"}) | 62 CRASH_NETWORK_AVAILABLE); |
43 @UiThreadTest | 63 runTest(CONNECTED, METERED, METRICS_REPORTING_DISABLED, METRICS_UPLOAD_N
OT_PERMITTED, |
44 public void testAllowCrashDumpUploadNowCellDev() { | 64 CRASH_NETWORK_UNAVAILABLE); |
45 CommandLine.init(null); | 65 runTest(DISCONNECTED, UNMETERED, METRICS_REPORTING_DISABLED, METRICS_UPL
OAD_NOT_PERMITTED, |
46 runTest(CONNECTED, WIFI_ON, METRIC_REPORTING_ENABLED, METRICS_UPLOAD_OK,
CRASH_NETWORK_OK); | 66 CRASH_NETWORK_UNAVAILABLE); |
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 } | 67 } |
59 | 68 |
60 private void runTest(boolean isConnected, boolean wifiOn, boolean isMetricsR
eportingEnabled, | 69 private void runTest(boolean isConnected, boolean isMetered, boolean isMetri
csReportingEnabled, |
61 boolean expectedMetricsUploadPermitted, | 70 boolean expectedMetricsUploadPermitted, |
62 boolean expectedNetworkAvailableForCrashUploads) { | 71 boolean expectedNetworkAvailableForCrashUploads) { |
63 PermissionContext context = new PermissionContext(getInstrumentation().g
etTargetContext()); | 72 // Mock out the network info accessors. |
64 ContextUtils.initApplicationContextForTests(context.getApplicationContex
t()); | 73 NetworkInfo networkInfo = mock(NetworkInfo.class); |
65 PrivacyPreferencesManager preferenceManager = new MockPrivacyPreferences
Manager( | 74 when(networkInfo.isConnected()).thenReturn(isConnected); |
66 context, isConnected, wifiOn, isMetricsReportingEnabled); | |
67 | 75 |
68 for (int i = 0; i < REPS; i++) { | 76 ConnectivityManager connectivityManager = mock(ConnectivityManager.class
); |
69 String state = String.format("[connected = %b, wifi = %b, reporting
= %b]", isConnected, | 77 when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo)
; |
70 wifiOn, isMetricsReportingEnabled); | 78 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 | 79 |
76 msg = String.format("Crash reporting should be %1$b for wifi %2$s", | 80 Context context = mock(Context.class); |
77 expectedNetworkAvailableForCrashUploads, wifiOn); | 81 when(context.getSystemService(Context.CONNECTIVITY_SERVICE)) |
78 assertEquals(msg, expectedNetworkAvailableForCrashUploads, | 82 .thenReturn(connectivityManager); |
79 preferenceManager.isNetworkAvailableForCrashUploads()); | 83 |
80 } | 84 // Perform other setup. |
| 85 ContextUtils.initApplicationContextForTests(RuntimeEnvironment.applicati
on); |
| 86 PrivacyPreferencesManager preferenceManager = new TestPrivacyPreferences
Manager(context); |
| 87 preferenceManager.setUsageAndCrashReporting(isMetricsReportingEnabled); |
| 88 |
| 89 // Test the usage and crash reporting permission accessors! |
| 90 String state = String.format("[connected = %b, metered = %b, reporting =
%b]", isConnected, |
| 91 isMetered, isMetricsReportingEnabled); |
| 92 String msg = String.format( |
| 93 "Metrics reporting should be %1$b for %2$s", expectedMetricsUplo
adPermitted, state); |
| 94 assertEquals( |
| 95 msg, expectedMetricsUploadPermitted, preferenceManager.isMetrics
UploadPermitted()); |
| 96 |
| 97 msg = String.format("Crash reporting should be %1$b for metered state %2
$s", |
| 98 expectedNetworkAvailableForCrashUploads, isMetered); |
| 99 assertEquals(msg, expectedNetworkAvailableForCrashUploads, |
| 100 preferenceManager.isNetworkAvailableForCrashUploads()); |
81 } | 101 } |
82 | 102 |
83 private static class MockPrivacyPreferencesManager extends PrivacyPreference
sManager { | 103 private static class TestPrivacyPreferencesManager extends PrivacyPreference
sManager { |
84 private final boolean mIsConnected; | 104 TestPrivacyPreferencesManager(Context context) { |
85 private final boolean mIsWifi; | |
86 | |
87 MockPrivacyPreferencesManager(Context context, boolean isConnected, bool
ean isWifi, | |
88 boolean isMetricsReportingEnabled) { | |
89 super(context); | 105 super(context); |
90 mIsConnected = isConnected; | |
91 mIsWifi = isWifi; | |
92 | |
93 setUsageAndCrashReporting(isMetricsReportingEnabled); | |
94 } | 106 } |
95 | 107 |
96 @Override | 108 // Stub out this call, as it could otherwise call into native code. |
97 public boolean isNetworkAvailable() { | 109 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 } | 110 } |
121 } | 111 } |
OLD | NEW |