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

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/BluetoothChooserDialogTest.java

Issue 2831823003: Convert ChromeActivityTestCaseBase direct children to JUnit4 (Closed)
Patch Set: fix findbug issues Created 3 years, 7 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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; 5 package org.chromium.chrome.browser;
6 6
7 import android.Manifest; 7 import android.Manifest;
8 import android.app.Dialog; 8 import android.app.Dialog;
9 import android.content.Intent; 9 import android.content.Intent;
10 import android.content.pm.PackageManager; 10 import android.content.pm.PackageManager;
11 import android.location.LocationManager; 11 import android.location.LocationManager;
12 import android.support.test.filters.LargeTest; 12 import android.support.test.filters.LargeTest;
13 import android.test.MoreAsserts; 13 import android.test.MoreAsserts;
14 import android.view.View; 14 import android.view.View;
15 import android.widget.Button; 15 import android.widget.Button;
16 import android.widget.ListView; 16 import android.widget.ListView;
17 17
18 import org.junit.After;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Rule;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24
18 import org.chromium.base.ThreadUtils; 25 import org.chromium.base.ThreadUtils;
26 import org.chromium.base.test.util.CommandLineFlags;
19 import org.chromium.base.test.util.RetryOnFailure; 27 import org.chromium.base.test.util.RetryOnFailure;
20 import org.chromium.chrome.R; 28 import org.chromium.chrome.R;
21 import org.chromium.chrome.test.ChromeActivityTestCaseBase; 29 import org.chromium.chrome.test.ChromeActivityTestRule;
30 import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
22 import org.chromium.components.location.LocationUtils; 31 import org.chromium.components.location.LocationUtils;
23 import org.chromium.components.security_state.ConnectionSecurityLevel; 32 import org.chromium.components.security_state.ConnectionSecurityLevel;
24 import org.chromium.content.browser.test.util.Criteria; 33 import org.chromium.content.browser.test.util.Criteria;
25 import org.chromium.content.browser.test.util.CriteriaHelper; 34 import org.chromium.content.browser.test.util.CriteriaHelper;
26 import org.chromium.content.browser.test.util.TouchCommon; 35 import org.chromium.content.browser.test.util.TouchCommon;
27 import org.chromium.ui.base.ActivityWindowAndroid; 36 import org.chromium.ui.base.ActivityWindowAndroid;
28 import org.chromium.ui.base.AndroidPermissionDelegate; 37 import org.chromium.ui.base.AndroidPermissionDelegate;
29 import org.chromium.ui.base.WindowAndroid; 38 import org.chromium.ui.base.WindowAndroid;
30 import org.chromium.ui.base.WindowAndroid.PermissionCallback; 39 import org.chromium.ui.base.WindowAndroid.PermissionCallback;
31 import org.chromium.ui.widget.TextViewWithClickableSpans; 40 import org.chromium.ui.widget.TextViewWithClickableSpans;
32 41
33 import java.util.concurrent.Callable; 42 import java.util.concurrent.Callable;
34 43
35 /** 44 /**
36 * Tests for the BluetoothChooserDialog class. 45 * Tests for the BluetoothChooserDialog class.
37 */ 46 */
47 @RunWith(ChromeJUnit4ClassRunner.class)
38 @RetryOnFailure 48 @RetryOnFailure
39 public class BluetoothChooserDialogTest extends ChromeActivityTestCaseBase<Chrom eActivity> { 49 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE,
50 ChromeActivityTestRule.DISABLE_NETWORK_PREDICTION_FLAG})
51 public class BluetoothChooserDialogTest {
40 /** 52 /**
41 * Works like the BluetoothChooserDialog class, but records calls to native methods instead of 53 * Works like the BluetoothChooserDialog class, but records calls to native methods instead of
42 * calling back to C++. 54 * calling back to C++.
43 */ 55 */
56
57 @Rule
58 public ChromeActivityTestRule<ChromeActivity> mActivityTestRule =
Ted C 2017/04/27 18:05:27 incorrectly inserted between the comment and the c
the real yoland 2017/04/28 02:03:29 sorry, script problem. Changed
59 new ChromeActivityTestRule<>(ChromeActivity.class);
60
44 static class BluetoothChooserDialogWithFakeNatives extends BluetoothChooserD ialog { 61 static class BluetoothChooserDialogWithFakeNatives extends BluetoothChooserD ialog {
45 int mFinishedEventType = -1; 62 int mFinishedEventType = -1;
46 String mFinishedDeviceId; 63 String mFinishedDeviceId;
47 int mRestartSearchCount = 0; 64 int mRestartSearchCount = 0;
48 65
49 BluetoothChooserDialogWithFakeNatives(WindowAndroid windowAndroid, Strin g origin, 66 BluetoothChooserDialogWithFakeNatives(WindowAndroid windowAndroid, Strin g origin,
50 int securityLevel, long nativeBluetoothChooserDialogPtr) { 67 int securityLevel, long nativeBluetoothChooserDialogPtr) {
51 super(windowAndroid, origin, securityLevel, nativeBluetoothChooserDi alogPtr); 68 super(windowAndroid, origin, securityLevel, nativeBluetoothChooserDi alogPtr);
52 } 69 }
53 70
54 @Override 71 @Override
55 void nativeOnDialogFinished( 72 void nativeOnDialogFinished(
56 long nativeBluetoothChooserAndroid, int eventType, String device Id) { 73 long nativeBluetoothChooserAndroid, int eventType, String device Id) {
57 assertEquals(nativeBluetoothChooserAndroid, mNativeBluetoothChooserD ialogPtr); 74 Assert.assertEquals(nativeBluetoothChooserAndroid, mNativeBluetoothC hooserDialogPtr);
58 assertEquals(mFinishedEventType, -1); 75 Assert.assertEquals(mFinishedEventType, -1);
59 mFinishedEventType = eventType; 76 mFinishedEventType = eventType;
60 mFinishedDeviceId = deviceId; 77 mFinishedDeviceId = deviceId;
61 // The native code calls closeDialog() when OnDialogFinished is call ed. 78 // The native code calls closeDialog() when OnDialogFinished is call ed.
62 closeDialog(); 79 closeDialog();
63 } 80 }
64 81
65 @Override 82 @Override
66 void nativeRestartSearch(long nativeBluetoothChooserAndroid) { 83 void nativeRestartSearch(long nativeBluetoothChooserAndroid) {
67 assertTrue(mNativeBluetoothChooserDialogPtr != 0); 84 Assert.assertTrue(mNativeBluetoothChooserDialogPtr != 0);
68 mRestartSearchCount++; 85 mRestartSearchCount++;
69 } 86 }
70 87
71 @Override 88 @Override
72 void nativeShowBluetoothOverviewLink(long nativeBluetoothChooserAndroid) { 89 void nativeShowBluetoothOverviewLink(long nativeBluetoothChooserAndroid) {
73 // We shouldn't be running native functions if the native class has been destroyed. 90 // We shouldn't be running native functions if the native class has been destroyed.
74 assertTrue(mNativeBluetoothChooserDialogPtr != 0); 91 Assert.assertTrue(mNativeBluetoothChooserDialogPtr != 0);
75 } 92 }
76 93
77 @Override 94 @Override
78 void nativeShowBluetoothAdapterOffLink(long nativeBluetoothChooserAndroi d) { 95 void nativeShowBluetoothAdapterOffLink(long nativeBluetoothChooserAndroi d) {
79 // We shouldn't be running native functions if the native class has been destroyed. 96 // We shouldn't be running native functions if the native class has been destroyed.
80 assertTrue(mNativeBluetoothChooserDialogPtr != 0); 97 Assert.assertTrue(mNativeBluetoothChooserDialogPtr != 0);
81 } 98 }
82 99
83 @Override 100 @Override
84 void nativeShowNeedLocationPermissionLink(long nativeBluetoothChooserAnd roid) { 101 void nativeShowNeedLocationPermissionLink(long nativeBluetoothChooserAnd roid) {
85 // We shouldn't be running native functions if the native class has been destroyed. 102 // We shouldn't be running native functions if the native class has been destroyed.
86 assertTrue(mNativeBluetoothChooserDialogPtr != 0); 103 Assert.assertTrue(mNativeBluetoothChooserDialogPtr != 0);
87 } 104 }
88 } 105 }
89 106
90 private ActivityWindowAndroid mWindowAndroid; 107 private ActivityWindowAndroid mWindowAndroid;
91 private FakeLocationUtils mLocationUtils; 108 private FakeLocationUtils mLocationUtils;
92 private BluetoothChooserDialogWithFakeNatives mChooserDialog; 109 private BluetoothChooserDialogWithFakeNatives mChooserDialog;
93 110
94 public BluetoothChooserDialogTest() {
95 super(ChromeActivity.class);
96 }
97
98 // ChromeActivityTestCaseBase: 111 // ChromeActivityTestCaseBase:
99 112
100 @Override 113 @Before
101 protected void setUp() throws Exception { 114 public void setUp() throws Exception {
102 super.setUp(); 115 mActivityTestRule.startMainActivityOnBlankPage();
103 mLocationUtils = new FakeLocationUtils(); 116 mLocationUtils = new FakeLocationUtils();
104 LocationUtils.setFactory(new LocationUtils.Factory() { 117 LocationUtils.setFactory(new LocationUtils.Factory() {
105 @Override 118 @Override
106 public LocationUtils create() { 119 public LocationUtils create() {
107 return mLocationUtils; 120 return mLocationUtils;
108 } 121 }
109 }); 122 });
110 mChooserDialog = createDialog(); 123 mChooserDialog = createDialog();
111 } 124 }
112 125
113 @Override 126 @After
114 protected void tearDown() throws Exception { 127 public void tearDown() throws Exception {
115 LocationUtils.setFactory(null); 128 LocationUtils.setFactory(null);
116 super.tearDown();
117 }
118
119 @Override
120 public void startMainActivity() throws InterruptedException {
121 startMainActivityOnBlankPage();
122 } 129 }
123 130
124 private BluetoothChooserDialogWithFakeNatives createDialog() { 131 private BluetoothChooserDialogWithFakeNatives createDialog() {
125 return ThreadUtils.runOnUiThreadBlockingNoException( 132 return ThreadUtils.runOnUiThreadBlockingNoException(
126 new Callable<BluetoothChooserDialogWithFakeNatives>() { 133 new Callable<BluetoothChooserDialogWithFakeNatives>() {
127 @Override 134 @Override
128 public BluetoothChooserDialogWithFakeNatives call() { 135 public BluetoothChooserDialogWithFakeNatives call() {
129 mWindowAndroid = new ActivityWindowAndroid(getActivity() ); 136 mWindowAndroid = new ActivityWindowAndroid(mActivityTest Rule.getActivity());
130 BluetoothChooserDialogWithFakeNatives dialog = 137 BluetoothChooserDialogWithFakeNatives dialog =
131 new BluetoothChooserDialogWithFakeNatives(mWindo wAndroid, 138 new BluetoothChooserDialogWithFakeNatives(mWindo wAndroid,
132 "https://origin.example.com/", 139 "https://origin.example.com/",
133 ConnectionSecurityLevel.SECURE, 42); 140 ConnectionSecurityLevel.SECURE, 42);
134 dialog.show(); 141 dialog.show();
135 return dialog; 142 return dialog;
136 } 143 }
137 }); 144 });
138 } 145 }
139 146
140 private static void selectItem(final BluetoothChooserDialogWithFakeNatives c hooserDialog, 147 private static void selectItem(final BluetoothChooserDialogWithFakeNatives c hooserDialog,
141 int position) { 148 int position) {
142 final Dialog dialog = chooserDialog.mItemChooserDialog.getDialogForTesti ng(); 149 final Dialog dialog = chooserDialog.mItemChooserDialog.getDialogForTesti ng();
143 final ListView items = (ListView) dialog.findViewById(R.id.items); 150 final ListView items = (ListView) dialog.findViewById(R.id.items);
144 final Button button = (Button) dialog.findViewById(R.id.positive); 151 final Button button = (Button) dialog.findViewById(R.id.positive);
145 152
146 CriteriaHelper.pollUiThread(new Criteria() { 153 CriteriaHelper.pollUiThread(new Criteria() {
147 @Override 154 @Override
148 public boolean isSatisfied() { 155 public boolean isSatisfied() {
149 return items.getChildAt(0) != null; 156 return items.getChildAt(0) != null;
150 } 157 }
151 }); 158 });
152 159
153 assertEquals("Not all items have a view; positions may be incorrect.", 160 Assert.assertEquals("Not all items have a view; positions may be incorre ct.",
154 items.getChildCount(), items.getAdapter().getCount()); 161 items.getChildCount(), items.getAdapter().getCount());
155 162
156 // Verify first item selected gets selected. 163 // Verify first item selected gets selected.
157 TouchCommon.singleClickView(items.getChildAt(position - 1)); 164 TouchCommon.singleClickView(items.getChildAt(position - 1));
158 165
159 CriteriaHelper.pollUiThread(new Criteria() { 166 CriteriaHelper.pollUiThread(new Criteria() {
160 @Override 167 @Override
161 public boolean isSatisfied() { 168 public boolean isSatisfied() {
162 return button.isEnabled(); 169 return button.isEnabled();
163 } 170 }
(...skipping 10 matching lines...) Expand all
174 } 181 }
175 182
176 /** 183 /**
177 * The messages include <*link*> ... </*link*> sections that are used to cre ate clickable spans. 184 * The messages include <*link*> ... </*link*> sections that are used to cre ate clickable spans.
178 * For testing the messages, this function returns the raw string without th e tags. 185 * For testing the messages, this function returns the raw string without th e tags.
179 */ 186 */
180 private static String removeLinkTags(String message) { 187 private static String removeLinkTags(String message) {
181 return message.replaceAll("</?[^>]*link[^>]*>", ""); 188 return message.replaceAll("</?[^>]*link[^>]*>", "");
182 } 189 }
183 190
191 @Test
184 @LargeTest 192 @LargeTest
185 public void testCancel() { 193 public void testCancel() {
186 ItemChooserDialog itemChooser = mChooserDialog.mItemChooserDialog; 194 ItemChooserDialog itemChooser = mChooserDialog.mItemChooserDialog;
187 Dialog dialog = itemChooser.getDialogForTesting(); 195 Dialog dialog = itemChooser.getDialogForTesting();
188 assertTrue(dialog.isShowing()); 196 Assert.assertTrue(dialog.isShowing());
189 197
190 TextViewWithClickableSpans statusView = 198 TextViewWithClickableSpans statusView =
191 (TextViewWithClickableSpans) dialog.findViewById(R.id.status); 199 (TextViewWithClickableSpans) dialog.findViewById(R.id.status);
192 final ListView items = (ListView) dialog.findViewById(R.id.items); 200 final ListView items = (ListView) dialog.findViewById(R.id.items);
193 final Button button = (Button) dialog.findViewById(R.id.positive); 201 final Button button = (Button) dialog.findViewById(R.id.positive);
194 202
195 // Before we add items to the dialog, the 'searching' message should be 203 // Before we add items to the dialog, the 'searching' message should be
196 // showing, the Commit button should be disabled and the list view hidde n. 204 // showing, the Commit button should be disabled and the list view hidde n.
197 assertEquals(removeLinkTags(getActivity().getString(R.string.bluetooth_s earching)), 205 Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getSt ring(
206 R.string.bluetooth_searching)),
198 statusView.getText().toString()); 207 statusView.getText().toString());
199 assertFalse(button.isEnabled()); 208 Assert.assertFalse(button.isEnabled());
200 assertEquals(View.GONE, items.getVisibility()); 209 Assert.assertEquals(View.GONE, items.getVisibility());
201 210
202 dialog.dismiss(); 211 dialog.dismiss();
203 212
204 CriteriaHelper.pollUiThread(new Criteria() { 213 CriteriaHelper.pollUiThread(new Criteria() {
205 @Override 214 @Override
206 public boolean isSatisfied() { 215 public boolean isSatisfied() {
207 return mChooserDialog.mFinishedEventType != -1; 216 return mChooserDialog.mFinishedEventType != -1;
208 } 217 }
209 }); 218 });
210 219
211 assertEquals(BluetoothChooserDialog.DIALOG_FINISHED_CANCELLED, 220 Assert.assertEquals(BluetoothChooserDialog.DIALOG_FINISHED_CANCELLED,
212 mChooserDialog.mFinishedEventType); 221 mChooserDialog.mFinishedEventType);
213 assertEquals("", mChooserDialog.mFinishedDeviceId); 222 Assert.assertEquals("", mChooserDialog.mFinishedDeviceId);
214 } 223 }
215 224
225 @Test
216 @LargeTest 226 @LargeTest
217 public void testSelectItem() { 227 public void testSelectItem() {
218 Dialog dialog = mChooserDialog.mItemChooserDialog.getDialogForTesting(); 228 Dialog dialog = mChooserDialog.mItemChooserDialog.getDialogForTesting();
219 229
220 TextViewWithClickableSpans statusView = 230 TextViewWithClickableSpans statusView =
221 (TextViewWithClickableSpans) dialog.findViewById(R.id.status); 231 (TextViewWithClickableSpans) dialog.findViewById(R.id.status);
222 final View items = dialog.findViewById(R.id.items); 232 final View items = dialog.findViewById(R.id.items);
223 final Button button = (Button) dialog.findViewById(R.id.positive); 233 final Button button = (Button) dialog.findViewById(R.id.positive);
224 final View progress = dialog.findViewById(R.id.progress); 234 final View progress = dialog.findViewById(R.id.progress);
225 235
(...skipping 12 matching lines...) Expand all
238 // Add connected device with signal strength level 1. 248 // Add connected device with signal strength level 1.
239 mChooserDialog.addOrUpdateDevice( 249 mChooserDialog.addOrUpdateDevice(
240 "id-4", "Name 4", true /* isGATTConnected */, 1 /* signa lStrengthLevel */); 250 "id-4", "Name 4", true /* isGATTConnected */, 1 /* signa lStrengthLevel */);
241 } 251 }
242 }); 252 });
243 253
244 // After adding items to the dialog, the help message should be showing, 254 // After adding items to the dialog, the help message should be showing,
245 // the progress spinner should disappear, the Commit button should still 255 // the progress spinner should disappear, the Commit button should still
246 // be disabled (since nothing's selected), and the list view should 256 // be disabled (since nothing's selected), and the list view should
247 // show. 257 // show.
248 assertEquals(removeLinkTags(getActivity().getString(R.string.bluetooth_s earching)), 258 Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getSt ring(
259 R.string.bluetooth_searching)),
249 statusView.getText().toString()); 260 statusView.getText().toString());
250 assertFalse(button.isEnabled()); 261 Assert.assertFalse(button.isEnabled());
251 assertEquals(View.VISIBLE, items.getVisibility()); 262 Assert.assertEquals(View.VISIBLE, items.getVisibility());
252 assertEquals(View.GONE, progress.getVisibility()); 263 Assert.assertEquals(View.GONE, progress.getVisibility());
253 264
254 ItemChooserDialog.ItemAdapter itemAdapter = 265 ItemChooserDialog.ItemAdapter itemAdapter =
255 mChooserDialog.mItemChooserDialog.getItemAdapterForTesting(); 266 mChooserDialog.mItemChooserDialog.getItemAdapterForTesting();
256 assertTrue(itemAdapter.getItem(0).hasSameContents( 267 Assert.assertTrue(itemAdapter.getItem(0).hasSameContents(
257 "id-1", "Name 1", null /* icon */, null /* iconDescription */)); 268 "id-1", "Name 1", null /* icon */, null /* iconDescription */));
258 assertTrue(itemAdapter.getItem(1).hasSameContents("id-2", "Name 2", 269 Assert.assertTrue(itemAdapter.getItem(1).hasSameContents("id-2", "Name 2 ",
259 mChooserDialog.mConnectedIcon, mChooserDialog.mConnectedIconDesc ription)); 270 mChooserDialog.mConnectedIcon, mChooserDialog.mConnectedIconDesc ription));
260 assertTrue(itemAdapter.getItem(2).hasSameContents("id-3", "Name 3", 271 Assert.assertTrue(itemAdapter.getItem(2).hasSameContents("id-3", "Name 3 ",
261 mChooserDialog.mSignalStrengthLevelIcon[1], 272 mChooserDialog.mSignalStrengthLevelIcon[1],
262 getActivity().getResources().getQuantityString( 273 mActivityTestRule.getActivity().getResources().getQuantityString (
263 R.plurals.signal_strength_level_n_bars, 1, 1))); 274 R.plurals.signal_strength_level_n_bars, 1, 1)));
264 // We show the connected icon even if the device has a signal strength. 275 // We show the connected icon even if the device has a signal strength.
265 assertTrue(itemAdapter.getItem(3).hasSameContents("id-4", "Name 4", 276 Assert.assertTrue(itemAdapter.getItem(3).hasSameContents("id-4", "Name 4 ",
266 mChooserDialog.mConnectedIcon, mChooserDialog.mConnectedIconDesc ription)); 277 mChooserDialog.mConnectedIcon, mChooserDialog.mConnectedIconDesc ription));
267 278
268 selectItem(mChooserDialog, 2); 279 selectItem(mChooserDialog, 2);
269 280
270 assertEquals( 281 Assert.assertEquals(
271 BluetoothChooserDialog.DIALOG_FINISHED_SELECTED, mChooserDialog. mFinishedEventType); 282 BluetoothChooserDialog.DIALOG_FINISHED_SELECTED, mChooserDialog. mFinishedEventType);
272 assertEquals("id-2", mChooserDialog.mFinishedDeviceId); 283 Assert.assertEquals("id-2", mChooserDialog.mFinishedDeviceId);
273 } 284 }
274 285
286 @Test
275 @LargeTest 287 @LargeTest
276 public void testNoLocationPermission() { 288 public void testNoLocationPermission() {
277 ItemChooserDialog itemChooser = mChooserDialog.mItemChooserDialog; 289 ItemChooserDialog itemChooser = mChooserDialog.mItemChooserDialog;
278 Dialog dialog = itemChooser.getDialogForTesting(); 290 Dialog dialog = itemChooser.getDialogForTesting();
279 assertTrue(dialog.isShowing()); 291 Assert.assertTrue(dialog.isShowing());
280 292
281 final TextViewWithClickableSpans statusView = 293 final TextViewWithClickableSpans statusView =
282 (TextViewWithClickableSpans) dialog.findViewById(R.id.status); 294 (TextViewWithClickableSpans) dialog.findViewById(R.id.status);
283 final TextViewWithClickableSpans errorView = 295 final TextViewWithClickableSpans errorView =
284 (TextViewWithClickableSpans) dialog.findViewById(R.id.not_found_ message); 296 (TextViewWithClickableSpans) dialog.findViewById(R.id.not_found_ message);
285 final View items = dialog.findViewById(R.id.items); 297 final View items = dialog.findViewById(R.id.items);
286 final Button button = (Button) dialog.findViewById(R.id.positive); 298 final Button button = (Button) dialog.findViewById(R.id.positive);
287 final View progress = dialog.findViewById(R.id.progress); 299 final View progress = dialog.findViewById(R.id.progress);
288 300
289 final TestAndroidPermissionDelegate permissionDelegate = 301 final TestAndroidPermissionDelegate permissionDelegate =
290 new TestAndroidPermissionDelegate(dialog); 302 new TestAndroidPermissionDelegate(dialog);
291 mWindowAndroid.setAndroidPermissionDelegate(permissionDelegate); 303 mWindowAndroid.setAndroidPermissionDelegate(permissionDelegate);
292 304
293 ThreadUtils.runOnUiThreadBlocking(new Runnable() { 305 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
294 @Override 306 @Override
295 public void run() { 307 public void run() {
296 mChooserDialog.notifyDiscoveryState( 308 mChooserDialog.notifyDiscoveryState(
297 BluetoothChooserDialog.DISCOVERY_FAILED_TO_START); 309 BluetoothChooserDialog.DISCOVERY_FAILED_TO_START);
298 } 310 }
299 }); 311 });
300 312
301 assertEquals(removeLinkTags( 313 Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getSt ring(
302 getActivity().getString(R.string.bluetooth_need_loc ation_permission)), 314 R.string.bluetooth_need_location_permission) ),
303 errorView.getText().toString()); 315 errorView.getText().toString());
304 assertEquals(removeLinkTags(getActivity().getString(R.string.bluetooth_a dapter_off_help)), 316 Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getSt ring(
317 R.string.bluetooth_adapter_off_help)),
305 statusView.getText().toString()); 318 statusView.getText().toString());
306 assertFalse(button.isEnabled()); 319 Assert.assertFalse(button.isEnabled());
307 assertEquals(View.VISIBLE, errorView.getVisibility()); 320 Assert.assertEquals(View.VISIBLE, errorView.getVisibility());
308 assertEquals(View.GONE, items.getVisibility()); 321 Assert.assertEquals(View.GONE, items.getVisibility());
309 assertEquals(View.GONE, progress.getVisibility()); 322 Assert.assertEquals(View.GONE, progress.getVisibility());
310 323
311 ThreadUtils.runOnUiThreadBlocking(new Runnable() { 324 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
312 @Override 325 @Override
313 public void run() { 326 public void run() {
314 errorView.getClickableSpans()[0].onClick(errorView); 327 errorView.getClickableSpans()[0].onClick(errorView);
315 } 328 }
316 }); 329 });
317 330
318 // Permission was requested. 331 // Permission was requested.
319 MoreAsserts.assertEquals(permissionDelegate.mPermissionsRequested, 332 MoreAsserts.assertEquals(permissionDelegate.mPermissionsRequested,
320 new String[] {Manifest.permission.ACCESS_COARSE_LOCATION}); 333 new String[] {Manifest.permission.ACCESS_COARSE_LOCATION});
321 assertNotNull(permissionDelegate.mCallback); 334 Assert.assertNotNull(permissionDelegate.mCallback);
322 // Grant permission. 335 // Grant permission.
323 mLocationUtils.mLocationGranted = true; 336 mLocationUtils.mLocationGranted = true;
324 ThreadUtils.runOnUiThreadBlocking(new Runnable() { 337 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
325 @Override 338 @Override
326 public void run() { 339 public void run() {
327 permissionDelegate.mCallback.onRequestPermissionsResult( 340 permissionDelegate.mCallback.onRequestPermissionsResult(
328 new String[] {Manifest.permission.ACCESS_COARSE_LOCATION }, 341 new String[] {Manifest.permission.ACCESS_COARSE_LOCATION },
329 new int[] {PackageManager.PERMISSION_GRANTED}); 342 new int[] {PackageManager.PERMISSION_GRANTED});
330 } 343 }
331 }); 344 });
332 345
333 assertEquals(1, mChooserDialog.mRestartSearchCount); 346 Assert.assertEquals(1, mChooserDialog.mRestartSearchCount);
334 assertEquals(removeLinkTags(getActivity().getString(R.string.bluetooth_s earching)), 347 Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getSt ring(
348 R.string.bluetooth_searching)),
335 statusView.getText().toString()); 349 statusView.getText().toString());
336 mChooserDialog.closeDialog(); 350 mChooserDialog.closeDialog();
337 } 351 }
338 352
353 @Test
339 @LargeTest 354 @LargeTest
340 public void testNoLocationServices() { 355 public void testNoLocationServices() {
341 ItemChooserDialog itemChooser = mChooserDialog.mItemChooserDialog; 356 ItemChooserDialog itemChooser = mChooserDialog.mItemChooserDialog;
342 Dialog dialog = itemChooser.getDialogForTesting(); 357 Dialog dialog = itemChooser.getDialogForTesting();
343 assertTrue(dialog.isShowing()); 358 Assert.assertTrue(dialog.isShowing());
344 359
345 final TextViewWithClickableSpans statusView = 360 final TextViewWithClickableSpans statusView =
346 (TextViewWithClickableSpans) dialog.findViewById(R.id.status); 361 (TextViewWithClickableSpans) dialog.findViewById(R.id.status);
347 final TextViewWithClickableSpans errorView = 362 final TextViewWithClickableSpans errorView =
348 (TextViewWithClickableSpans) dialog.findViewById(R.id.not_found_ message); 363 (TextViewWithClickableSpans) dialog.findViewById(R.id.not_found_ message);
349 final View items = dialog.findViewById(R.id.items); 364 final View items = dialog.findViewById(R.id.items);
350 final Button button = (Button) dialog.findViewById(R.id.positive); 365 final Button button = (Button) dialog.findViewById(R.id.positive);
351 final View progress = dialog.findViewById(R.id.progress); 366 final View progress = dialog.findViewById(R.id.progress);
352 367
353 final TestAndroidPermissionDelegate permissionDelegate = 368 final TestAndroidPermissionDelegate permissionDelegate =
354 new TestAndroidPermissionDelegate(dialog); 369 new TestAndroidPermissionDelegate(dialog);
355 mWindowAndroid.setAndroidPermissionDelegate(permissionDelegate); 370 mWindowAndroid.setAndroidPermissionDelegate(permissionDelegate);
356 371
357 // Grant permissions, and turn off location services. 372 // Grant permissions, and turn off location services.
358 mLocationUtils.mLocationGranted = true; 373 mLocationUtils.mLocationGranted = true;
359 mLocationUtils.mSystemLocationSettingsEnabled = false; 374 mLocationUtils.mSystemLocationSettingsEnabled = false;
360 375
361 ThreadUtils.runOnUiThreadBlocking(new Runnable() { 376 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
362 @Override 377 @Override
363 public void run() { 378 public void run() {
364 mChooserDialog.notifyDiscoveryState( 379 mChooserDialog.notifyDiscoveryState(
365 BluetoothChooserDialog.DISCOVERY_FAILED_TO_START); 380 BluetoothChooserDialog.DISCOVERY_FAILED_TO_START);
366 } 381 }
367 }); 382 });
368 383
369 assertEquals(removeLinkTags( 384 Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getSt ring(
370 getActivity().getString(R.string.bluetooth_need_loc ation_services_on)), 385 R.string.bluetooth_need_location_services_on )),
371 errorView.getText().toString()); 386 errorView.getText().toString());
372 assertEquals(removeLinkTags(getActivity().getString( 387 Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getSt ring(
373 R.string.bluetooth_need_location_permission_help)), 388 R.string.bluetooth_need_location_permission_ help)),
374 statusView.getText().toString()); 389 statusView.getText().toString());
375 assertFalse(button.isEnabled()); 390 Assert.assertFalse(button.isEnabled());
376 assertEquals(View.VISIBLE, errorView.getVisibility()); 391 Assert.assertEquals(View.VISIBLE, errorView.getVisibility());
377 assertEquals(View.GONE, items.getVisibility()); 392 Assert.assertEquals(View.GONE, items.getVisibility());
378 assertEquals(View.GONE, progress.getVisibility()); 393 Assert.assertEquals(View.GONE, progress.getVisibility());
379 394
380 // Turn on Location Services. 395 // Turn on Location Services.
381 mLocationUtils.mSystemLocationSettingsEnabled = true; 396 mLocationUtils.mSystemLocationSettingsEnabled = true;
382 ThreadUtils.runOnUiThreadBlocking(new Runnable() { 397 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
383 @Override 398 @Override
384 public void run() { 399 public void run() {
385 mChooserDialog.mLocationModeBroadcastReceiver.onReceive( 400 mChooserDialog.mLocationModeBroadcastReceiver.onReceive(
386 getActivity(), new Intent(LocationManager.MODE_CHANGED_A CTION)); 401 mActivityTestRule.getActivity(),
402 new Intent(LocationManager.MODE_CHANGED_ACTION));
387 } 403 }
388 }); 404 });
389 405
390 assertEquals(1, mChooserDialog.mRestartSearchCount); 406 Assert.assertEquals(1, mChooserDialog.mRestartSearchCount);
391 assertEquals(removeLinkTags(getActivity().getString(R.string.bluetooth_s earching)), 407 Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getSt ring(
408 R.string.bluetooth_searching)),
392 statusView.getText().toString()); 409 statusView.getText().toString());
393 410
394 mChooserDialog.closeDialog(); 411 mChooserDialog.closeDialog();
395 } 412 }
396 413
397 // TODO(jyasskin): Test when the user denies Chrome the ability to ask for p ermission. 414 // TODO(jyasskin): Test when the user denies Chrome the ability to ask for p ermission.
398 415
416 @Test
399 @LargeTest 417 @LargeTest
400 public void testTurnOnAdapter() { 418 public void testTurnOnAdapter() {
401 final ItemChooserDialog itemChooser = mChooserDialog.mItemChooserDialog; 419 final ItemChooserDialog itemChooser = mChooserDialog.mItemChooserDialog;
402 Dialog dialog = itemChooser.getDialogForTesting(); 420 Dialog dialog = itemChooser.getDialogForTesting();
403 assertTrue(dialog.isShowing()); 421 Assert.assertTrue(dialog.isShowing());
404 422
405 final TextViewWithClickableSpans statusView = 423 final TextViewWithClickableSpans statusView =
406 (TextViewWithClickableSpans) dialog.findViewById(R.id.status); 424 (TextViewWithClickableSpans) dialog.findViewById(R.id.status);
407 final TextViewWithClickableSpans errorView = 425 final TextViewWithClickableSpans errorView =
408 (TextViewWithClickableSpans) dialog.findViewById(R.id.not_found_ message); 426 (TextViewWithClickableSpans) dialog.findViewById(R.id.not_found_ message);
409 final View items = dialog.findViewById(R.id.items); 427 final View items = dialog.findViewById(R.id.items);
410 final Button button = (Button) dialog.findViewById(R.id.positive); 428 final Button button = (Button) dialog.findViewById(R.id.positive);
411 final View progress = dialog.findViewById(R.id.progress); 429 final View progress = dialog.findViewById(R.id.progress);
412 430
413 // Turn off adapter. 431 // Turn off adapter.
414 ThreadUtils.runOnUiThreadBlocking(new Runnable() { 432 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
415 @Override 433 @Override
416 public void run() { 434 public void run() {
417 mChooserDialog.notifyAdapterTurnedOff(); 435 mChooserDialog.notifyAdapterTurnedOff();
418 } 436 }
419 }); 437 });
420 438
421 assertEquals(removeLinkTags(getActivity().getString(R.string.bluetooth_a dapter_off)), 439 Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getSt ring(
440 R.string.bluetooth_adapter_off)),
422 errorView.getText().toString()); 441 errorView.getText().toString());
423 assertEquals(removeLinkTags(getActivity().getString(R.string.bluetooth_a dapter_off_help)), 442 Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getSt ring(
443 R.string.bluetooth_adapter_off_help)),
424 statusView.getText().toString()); 444 statusView.getText().toString());
425 assertFalse(button.isEnabled()); 445 Assert.assertFalse(button.isEnabled());
426 assertEquals(View.VISIBLE, errorView.getVisibility()); 446 Assert.assertEquals(View.VISIBLE, errorView.getVisibility());
427 assertEquals(View.GONE, items.getVisibility()); 447 Assert.assertEquals(View.GONE, items.getVisibility());
428 assertEquals(View.GONE, progress.getVisibility()); 448 Assert.assertEquals(View.GONE, progress.getVisibility());
429 449
430 // Turn on adapter. 450 // Turn on adapter.
431 ThreadUtils.runOnUiThreadBlocking(new Runnable() { 451 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
432 @Override 452 @Override
433 public void run() { 453 public void run() {
434 itemChooser.signalInitializingAdapter(); 454 itemChooser.signalInitializingAdapter();
435 } 455 }
436 }); 456 });
437 457
438 assertEquals(View.GONE, errorView.getVisibility()); 458 Assert.assertEquals(View.GONE, errorView.getVisibility());
439 assertEquals(View.GONE, items.getVisibility()); 459 Assert.assertEquals(View.GONE, items.getVisibility());
440 assertEquals(View.VISIBLE, progress.getVisibility()); 460 Assert.assertEquals(View.VISIBLE, progress.getVisibility());
441 461
442 mChooserDialog.closeDialog(); 462 mChooserDialog.closeDialog();
443 } 463 }
444 464
445 private static class TestAndroidPermissionDelegate implements AndroidPermiss ionDelegate { 465 private static class TestAndroidPermissionDelegate implements AndroidPermiss ionDelegate {
446 Dialog mDialog = null; 466 Dialog mDialog = null;
447 PermissionCallback mCallback = null; 467 PermissionCallback mCallback = null;
448 String[] mPermissionsRequested = null; 468 String[] mPermissionsRequested = null;
449 469
450 public TestAndroidPermissionDelegate(Dialog dialog) { 470 public TestAndroidPermissionDelegate(Dialog dialog) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 } 507 }
488 508
489 public boolean mSystemLocationSettingsEnabled = true; 509 public boolean mSystemLocationSettingsEnabled = true;
490 510
491 @Override 511 @Override
492 public boolean isSystemLocationSettingEnabled() { 512 public boolean isSystemLocationSettingEnabled() {
493 return mSystemLocationSettingsEnabled; 513 return mSystemLocationSettingsEnabled;
494 } 514 }
495 } 515 }
496 } 516 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698