Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/preferences/privacy/PrivacyPreferencesManagerTest.java

Issue 2751333004: [Crash Reporting] Only upload Chrome crash reports over unmetered networks. (Closed)
Patch Set: Rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.preferences.privacy;
6
7 import android.content.Context;
8 import android.support.test.filters.SmallTest;
9 import android.test.InstrumentationTestCase;
10 import android.test.UiThreadTest;
11
12 import org.chromium.base.CommandLine;
13 import org.chromium.base.ContextUtils;
14 import org.chromium.base.test.util.AdvancedMockContext;
15 import org.chromium.base.test.util.Feature;
16
17 /**
18 * Tests "Usage and Crash reporting" preferences.
19 */
20 public class PrivacyPreferencesManagerTest extends InstrumentationTestCase {
21
22 private static final boolean CONNECTED = true;
23 private static final boolean DISCONNECTED = false;
24
25 private static final boolean WIFI_ON = true;
26 private static final boolean WIFI_OFF = false;
27
28 private static final boolean METRICS_UPLOAD_OK = true;
29 private static final boolean METRICS_UPLOAD_NOT_PERMITTED = false;
30
31 private static final boolean CRASH_NETWORK_OK = true;
32 private static final boolean CRASH_NETWORK_NOT_PERMITTED = false;
33
34 private static final boolean METRIC_REPORTING_ENABLED = true;
35 private static final boolean METRIC_REPORTING_DISABLED = false;
36
37 // Perform the same test a few times to make sure any sort of
38 // caching still works.
39 private static final int REPS = 3;
40
41 @SmallTest
42 @Feature({"Android-AppBase"})
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 }
59
60 private void runTest(boolean isConnected, boolean wifiOn, boolean isMetricsR eportingEnabled,
61 boolean expectedMetricsUploadPermitted,
62 boolean expectedNetworkAvailableForCrashUploads) {
63 PermissionContext context = new PermissionContext(getInstrumentation().g etTargetContext());
64 ContextUtils.initApplicationContextForTests(context.getApplicationContex t());
65 PrivacyPreferencesManager preferenceManager = new MockPrivacyPreferences Manager(
66 context, isConnected, wifiOn, isMetricsReportingEnabled);
67
68 for (int i = 0; i < REPS; i++) {
69 String state = String.format("[connected = %b, wifi = %b, reporting = %b]", isConnected,
70 wifiOn, isMetricsReportingEnabled);
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
76 msg = String.format("Crash reporting should be %1$b for wifi %2$s",
77 expectedNetworkAvailableForCrashUploads, wifiOn);
78 assertEquals(msg, expectedNetworkAvailableForCrashUploads,
79 preferenceManager.isNetworkAvailableForCrashUploads());
80 }
81 }
82
83 private static class MockPrivacyPreferencesManager extends PrivacyPreference sManager {
84 private final boolean mIsConnected;
85 private final boolean mIsWifi;
86
87 MockPrivacyPreferencesManager(Context context, boolean isConnected, bool ean isWifi,
88 boolean isMetricsReportingEnabled) {
89 super(context);
90 mIsConnected = isConnected;
91 mIsWifi = isWifi;
92
93 setUsageAndCrashReporting(isMetricsReportingEnabled);
94 }
95
96 @Override
97 public boolean isNetworkAvailable() {
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 }
121 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698