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 android.content.Context; | 7 import android.content.Context; |
| 8 import android.support.test.InstrumentationRegistry; |
8 import android.support.test.filters.SmallTest; | 9 import android.support.test.filters.SmallTest; |
9 import android.test.InstrumentationTestCase; | |
10 import android.test.UiThreadTest; | 10 import android.test.UiThreadTest; |
11 | 11 |
| 12 import org.junit.Assert; |
| 13 import org.junit.Test; |
| 14 import org.junit.runner.RunWith; |
| 15 |
12 import org.chromium.base.CommandLine; | 16 import org.chromium.base.CommandLine; |
13 import org.chromium.base.ContextUtils; | 17 import org.chromium.base.ContextUtils; |
14 import org.chromium.base.test.util.AdvancedMockContext; | 18 import org.chromium.base.test.util.AdvancedMockContext; |
15 import org.chromium.base.test.util.Feature; | 19 import org.chromium.base.test.util.Feature; |
| 20 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
16 | 21 |
17 /** | 22 /** |
18 * Tests "Usage and Crash reporting" preferences. | 23 * Tests "Usage and Crash reporting" preferences. |
19 */ | 24 */ |
20 public class PrivacyPreferencesManagerTest extends InstrumentationTestCase { | 25 @RunWith(ChromeJUnit4ClassRunner.class) |
21 | 26 public class PrivacyPreferencesManagerTest { |
22 private static final boolean CONNECTED = true; | 27 private static final boolean CONNECTED = true; |
23 private static final boolean DISCONNECTED = false; | 28 private static final boolean DISCONNECTED = false; |
24 | 29 |
25 private static final boolean WIFI_ON = true; | 30 private static final boolean WIFI_ON = true; |
26 private static final boolean WIFI_OFF = false; | 31 private static final boolean WIFI_OFF = false; |
27 | 32 |
28 private static final boolean METRICS_UPLOAD_OK = true; | 33 private static final boolean METRICS_UPLOAD_OK = true; |
29 private static final boolean METRICS_UPLOAD_NOT_PERMITTED = false; | 34 private static final boolean METRICS_UPLOAD_NOT_PERMITTED = false; |
30 | 35 |
31 private static final boolean CRASH_NETWORK_OK = true; | 36 private static final boolean CRASH_NETWORK_OK = true; |
32 private static final boolean CRASH_NETWORK_NOT_PERMITTED = false; | 37 private static final boolean CRASH_NETWORK_NOT_PERMITTED = false; |
33 | 38 |
34 private static final boolean METRIC_REPORTING_ENABLED = true; | 39 private static final boolean METRIC_REPORTING_ENABLED = true; |
35 private static final boolean METRIC_REPORTING_DISABLED = false; | 40 private static final boolean METRIC_REPORTING_DISABLED = false; |
36 | 41 |
37 // Perform the same test a few times to make sure any sort of | 42 // Perform the same test a few times to make sure any sort of |
38 // caching still works. | 43 // caching still works. |
39 private static final int REPS = 3; | 44 private static final int REPS = 3; |
40 | 45 |
| 46 @Test |
41 @SmallTest | 47 @SmallTest |
42 @Feature({"Android-AppBase"}) | 48 @Feature({"Android-AppBase"}) |
43 @UiThreadTest | 49 @UiThreadTest |
44 public void testAllowCrashDumpUploadNowCellDev() { | 50 public void testAllowCrashDumpUploadNowCellDev() { |
45 CommandLine.init(null); | 51 CommandLine.init(null); |
46 runTest(CONNECTED, WIFI_ON, METRIC_REPORTING_ENABLED, METRICS_UPLOAD_OK,
CRASH_NETWORK_OK); | 52 runTest(CONNECTED, WIFI_ON, METRIC_REPORTING_ENABLED, METRICS_UPLOAD_OK,
CRASH_NETWORK_OK); |
47 runTest(CONNECTED, WIFI_OFF, METRIC_REPORTING_ENABLED, METRICS_UPLOAD_OK
, | 53 runTest(CONNECTED, WIFI_OFF, METRIC_REPORTING_ENABLED, METRICS_UPLOAD_OK
, |
48 CRASH_NETWORK_NOT_PERMITTED); | 54 CRASH_NETWORK_NOT_PERMITTED); |
49 runTest(DISCONNECTED, WIFI_OFF, METRIC_REPORTING_ENABLED, METRICS_UPLOAD
_NOT_PERMITTED, | 55 runTest(DISCONNECTED, WIFI_OFF, METRIC_REPORTING_ENABLED, METRICS_UPLOAD
_NOT_PERMITTED, |
50 CRASH_NETWORK_NOT_PERMITTED); | 56 CRASH_NETWORK_NOT_PERMITTED); |
51 | 57 |
52 runTest(CONNECTED, WIFI_ON, METRIC_REPORTING_DISABLED, METRICS_UPLOAD_NO
T_PERMITTED, | 58 runTest(CONNECTED, WIFI_ON, METRIC_REPORTING_DISABLED, METRICS_UPLOAD_NO
T_PERMITTED, |
53 CRASH_NETWORK_OK); | 59 CRASH_NETWORK_OK); |
54 runTest(CONNECTED, WIFI_OFF, METRIC_REPORTING_DISABLED, METRICS_UPLOAD_N
OT_PERMITTED, | 60 runTest(CONNECTED, WIFI_OFF, METRIC_REPORTING_DISABLED, METRICS_UPLOAD_N
OT_PERMITTED, |
55 CRASH_NETWORK_NOT_PERMITTED); | 61 CRASH_NETWORK_NOT_PERMITTED); |
56 runTest(DISCONNECTED, WIFI_OFF, METRIC_REPORTING_DISABLED, METRICS_UPLOA
D_NOT_PERMITTED, | 62 runTest(DISCONNECTED, WIFI_OFF, METRIC_REPORTING_DISABLED, METRICS_UPLOA
D_NOT_PERMITTED, |
57 CRASH_NETWORK_NOT_PERMITTED); | 63 CRASH_NETWORK_NOT_PERMITTED); |
58 } | 64 } |
59 | 65 |
60 private void runTest(boolean isConnected, boolean wifiOn, boolean isMetricsR
eportingEnabled, | 66 private void runTest(boolean isConnected, boolean wifiOn, boolean isMetricsR
eportingEnabled, |
61 boolean expectedMetricsUploadPermitted, | 67 boolean expectedMetricsUploadPermitted, |
62 boolean expectedNetworkAvailableForCrashUploads) { | 68 boolean expectedNetworkAvailableForCrashUploads) { |
63 PermissionContext context = new PermissionContext(getInstrumentation().g
etTargetContext()); | 69 PermissionContext context = new PermissionContext( |
| 70 InstrumentationRegistry.getInstrumentation().getTargetContext())
; |
64 ContextUtils.initApplicationContextForTests(context.getApplicationContex
t()); | 71 ContextUtils.initApplicationContextForTests(context.getApplicationContex
t()); |
65 PrivacyPreferencesManager preferenceManager = new MockPrivacyPreferences
Manager( | 72 PrivacyPreferencesManager preferenceManager = new MockPrivacyPreferences
Manager( |
66 context, isConnected, wifiOn, isMetricsReportingEnabled); | 73 context, isConnected, wifiOn, isMetricsReportingEnabled); |
67 | 74 |
68 for (int i = 0; i < REPS; i++) { | 75 for (int i = 0; i < REPS; i++) { |
69 String state = String.format("[connected = %b, wifi = %b, reporting
= %b]", isConnected, | 76 String state = String.format("[connected = %b, wifi = %b, reporting
= %b]", isConnected, |
70 wifiOn, isMetricsReportingEnabled); | 77 wifiOn, isMetricsReportingEnabled); |
71 String msg = String.format("Metrics reporting should be %1$b for %2$
s", | 78 String msg = String.format("Metrics reporting should be %1$b for %2$
s", |
72 expectedMetricsUploadPermitted, state); | 79 expectedMetricsUploadPermitted, state); |
73 assertEquals(msg, expectedMetricsUploadPermitted, | 80 Assert.assertEquals(msg, expectedMetricsUploadPermitted, |
74 preferenceManager.isMetricsUploadPermitted()); | 81 preferenceManager.isMetricsUploadPermitted()); |
75 | 82 |
76 msg = String.format("Crash reporting should be %1$b for wifi %2$s", | 83 msg = String.format("Crash reporting should be %1$b for wifi %2$s", |
77 expectedNetworkAvailableForCrashUploads, wifiOn); | 84 expectedNetworkAvailableForCrashUploads, wifiOn); |
78 assertEquals(msg, expectedNetworkAvailableForCrashUploads, | 85 Assert.assertEquals(msg, expectedNetworkAvailableForCrashUploads, |
79 preferenceManager.isNetworkAvailableForCrashUploads()); | 86 preferenceManager.isNetworkAvailableForCrashUploads()); |
80 } | 87 } |
81 } | 88 } |
82 | 89 |
83 private static class MockPrivacyPreferencesManager extends PrivacyPreference
sManager { | 90 private static class MockPrivacyPreferencesManager extends PrivacyPreference
sManager { |
84 private final boolean mIsConnected; | 91 private final boolean mIsConnected; |
85 private final boolean mIsWifi; | 92 private final boolean mIsWifi; |
86 | 93 |
87 MockPrivacyPreferencesManager(Context context, boolean isConnected, bool
ean isWifi, | 94 MockPrivacyPreferencesManager(Context context, boolean isConnected, bool
ean isWifi, |
88 boolean isMetricsReportingEnabled) { | 95 boolean isMetricsReportingEnabled) { |
(...skipping 18 matching lines...) Expand all Loading... |
107 private static class PermissionContext extends AdvancedMockContext { | 114 private static class PermissionContext extends AdvancedMockContext { |
108 public PermissionContext(Context targetContext) { | 115 public PermissionContext(Context targetContext) { |
109 super(targetContext); | 116 super(targetContext); |
110 } | 117 } |
111 | 118 |
112 @Override | 119 @Override |
113 public Object getSystemService(String name) { | 120 public Object getSystemService(String name) { |
114 if (Context.CONNECTIVITY_SERVICE.equals(name)) { | 121 if (Context.CONNECTIVITY_SERVICE.equals(name)) { |
115 return null; | 122 return null; |
116 } | 123 } |
117 fail("Should not ask for any other service than the ConnectionManage
r."); | 124 Assert.fail("Should not ask for any other service than the Connectio
nManager."); |
118 return super.getSystemService(name); | 125 return super.getSystemService(name); |
119 } | 126 } |
120 } | 127 } |
121 } | 128 } |
OLD | NEW |