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

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

Issue 2032273002: If location services are turned off, have the BT chooser prompt the user to turn them on. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: Sync Created 4 years, 6 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.Context; 9 import android.content.Context;
10 import android.content.Intent;
10 import android.content.pm.PackageManager; 11 import android.content.pm.PackageManager;
12 import android.location.LocationManager;
11 import android.test.MoreAsserts; 13 import android.test.MoreAsserts;
12 import android.test.suitebuilder.annotation.SmallTest; 14 import android.test.suitebuilder.annotation.SmallTest;
13 import android.view.View; 15 import android.view.View;
14 import android.widget.Button; 16 import android.widget.Button;
15 import android.widget.ListView; 17 import android.widget.ListView;
16 18
17 import org.chromium.base.ThreadUtils; 19 import org.chromium.base.ThreadUtils;
18 import org.chromium.chrome.R; 20 import org.chromium.chrome.R;
19 import org.chromium.chrome.test.ChromeActivityTestCaseBase; 21 import org.chromium.chrome.test.ChromeActivityTestCaseBase;
20 import org.chromium.components.location.LocationUtils; 22 import org.chromium.components.location.LocationUtils;
(...skipping 13 matching lines...) Expand all
34 * Tests for the BluetoothChooserDialog class. 36 * Tests for the BluetoothChooserDialog class.
35 */ 37 */
36 public class BluetoothChooserDialogTest extends ChromeActivityTestCaseBase<Chrom eActivity> { 38 public class BluetoothChooserDialogTest extends ChromeActivityTestCaseBase<Chrom eActivity> {
37 /** 39 /**
38 * Works like the BluetoothChooserDialog class, but records calls to native methods instead of 40 * Works like the BluetoothChooserDialog class, but records calls to native methods instead of
39 * calling back to C++. 41 * calling back to C++.
40 */ 42 */
41 static class BluetoothChooserDialogWithFakeNatives extends BluetoothChooserD ialog { 43 static class BluetoothChooserDialogWithFakeNatives extends BluetoothChooserD ialog {
42 int mFinishedEventType = -1; 44 int mFinishedEventType = -1;
43 String mFinishedDeviceId; 45 String mFinishedDeviceId;
46 int mRestartSearchCount = 0;
44 47
45 BluetoothChooserDialogWithFakeNatives(WindowAndroid windowAndroid, Strin g origin, 48 BluetoothChooserDialogWithFakeNatives(WindowAndroid windowAndroid, Strin g origin,
46 int securityLevel, long nativeBluetoothChooserDialogPtr) { 49 int securityLevel, long nativeBluetoothChooserDialogPtr) {
47 super(windowAndroid, origin, securityLevel, nativeBluetoothChooserDi alogPtr); 50 super(windowAndroid, origin, securityLevel, nativeBluetoothChooserDi alogPtr);
48 } 51 }
49 52
50 @Override 53 @Override
51 void nativeOnDialogFinished( 54 void nativeOnDialogFinished(
52 long nativeBluetoothChooserAndroid, int eventType, String device Id) { 55 long nativeBluetoothChooserAndroid, int eventType, String device Id) {
53 assertEquals(nativeBluetoothChooserAndroid, mNativeBluetoothChooserD ialogPtr); 56 assertEquals(nativeBluetoothChooserAndroid, mNativeBluetoothChooserD ialogPtr);
54 assertEquals(mFinishedEventType, -1); 57 assertEquals(mFinishedEventType, -1);
55 mFinishedEventType = eventType; 58 mFinishedEventType = eventType;
56 mFinishedDeviceId = deviceId; 59 mFinishedDeviceId = deviceId;
57 } 60 }
58 61
59 @Override 62 @Override
60 void nativeRestartSearch(long nativeBluetoothChooserAndroid) {} 63 void nativeRestartSearch(long nativeBluetoothChooserAndroid) {
64 mRestartSearchCount++;
65 }
61 66
62 @Override 67 @Override
63 void nativeShowBluetoothOverviewLink(long nativeBluetoothChooserAndroid) {} 68 void nativeShowBluetoothOverviewLink(long nativeBluetoothChooserAndroid) {}
64 69
65 @Override 70 @Override
66 void nativeShowBluetoothAdapterOffLink(long nativeBluetoothChooserAndroi d) {} 71 void nativeShowBluetoothAdapterOffLink(long nativeBluetoothChooserAndroi d) {}
67 72
68 @Override 73 @Override
69 void nativeShowNeedLocationPermissionLink(long nativeBluetoothChooserAnd roid) {} 74 void nativeShowNeedLocationPermissionLink(long nativeBluetoothChooserAnd roid) {}
70 } 75 }
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 154
150 CriteriaHelper.pollUiThread(new Criteria() { 155 CriteriaHelper.pollUiThread(new Criteria() {
151 @Override 156 @Override
152 public boolean isSatisfied() { 157 public boolean isSatisfied() {
153 return chooserDialog.mFinishedEventType != -1; 158 return chooserDialog.mFinishedEventType != -1;
154 } 159 }
155 }); 160 });
156 } 161 }
157 162
158 /** 163 /**
159 * The messages include <link> ... </link> or <link1> ... </link1>, <link2> ... </link2> 164 * The messages include <*link*> ... </*link*> sections that are used to cre ate clickable spans.
160 * sections that are used to create clickable spans. For testing the message s, this function 165 * For testing the messages, this function returns the raw string without th e tags.
161 * returns the raw string without the tags.
162 */ 166 */
163 private static String removeLinkTags(String message) { 167 private static String removeLinkTags(String message) {
164 return message.replaceAll("</?link1>", "").replaceAll( 168 return message.replaceAll("</?[^>]*link[^>]*>", "");
165 "</?link2>", "").replaceAll("</?link>", "");
166 } 169 }
167 170
168 @SmallTest 171 @SmallTest
169 public void testCancel() throws InterruptedException { 172 public void testCancel() throws InterruptedException {
170 ItemChooserDialog itemChooser = mChooserDialog.mItemChooserDialog; 173 ItemChooserDialog itemChooser = mChooserDialog.mItemChooserDialog;
171 Dialog dialog = itemChooser.getDialogForTesting(); 174 Dialog dialog = itemChooser.getDialogForTesting();
172 assertTrue(dialog.isShowing()); 175 assertTrue(dialog.isShowing());
173 176
174 TextViewWithClickableSpans statusView = 177 TextViewWithClickableSpans statusView =
175 (TextViewWithClickableSpans) dialog.findViewById(R.id.status); 178 (TextViewWithClickableSpans) dialog.findViewById(R.id.status);
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 // Permission was requested. 282 // Permission was requested.
280 MoreAsserts.assertEquals(permissionDelegate.mPermissionsRequested, 283 MoreAsserts.assertEquals(permissionDelegate.mPermissionsRequested,
281 new String[] {Manifest.permission.ACCESS_COARSE_LOCATION}); 284 new String[] {Manifest.permission.ACCESS_COARSE_LOCATION});
282 assertNotNull(permissionDelegate.mCallback); 285 assertNotNull(permissionDelegate.mCallback);
283 // Grant permission. 286 // Grant permission.
284 mLocationUtils.mLocationGranted = true; 287 mLocationUtils.mLocationGranted = true;
285 ThreadUtils.runOnUiThreadBlocking(new Runnable() { 288 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
286 @Override 289 @Override
287 public void run() { 290 public void run() {
288 permissionDelegate.mCallback.onRequestPermissionsResult( 291 permissionDelegate.mCallback.onRequestPermissionsResult(
289 new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, 292 new String[] {Manifest.permission.ACCESS_COARSE_LOCATION },
290 new int[] {PackageManager.PERMISSION_GRANTED}); 293 new int[] {PackageManager.PERMISSION_GRANTED});
291 } 294 }
292 }); 295 });
293 296
294 assertEquals(removeLinkTags(getActivity().getString(R.string.bluetooth_a dapter_off_help)), 297 assertEquals(1, mChooserDialog.mRestartSearchCount);
298 assertEquals(removeLinkTags(getActivity().getString(R.string.bluetooth_s earching)),
295 statusView.getText().toString()); 299 statusView.getText().toString());
296 300
297 mChooserDialog.closeDialog(); 301 mChooserDialog.closeDialog();
302 }
303
304 @SmallTest
305 public void testNoLocationServices() throws InterruptedException {
306 ItemChooserDialog itemChooser = mChooserDialog.mItemChooserDialog;
307 Dialog dialog = itemChooser.getDialogForTesting();
308 assertTrue(dialog.isShowing());
309
310 final TextViewWithClickableSpans statusView =
311 (TextViewWithClickableSpans) dialog.findViewById(R.id.status);
312 final TextViewWithClickableSpans errorView =
313 (TextViewWithClickableSpans) dialog.findViewById(R.id.not_found_ message);
314 final View items = dialog.findViewById(R.id.items);
315 final Button button = (Button) dialog.findViewById(R.id.positive);
316 final View progress = dialog.findViewById(R.id.progress);
317
318 final TestAndroidPermissionDelegate permissionDelegate =
319 new TestAndroidPermissionDelegate();
320 mWindowAndroid.setAndroidPermissionDelegate(permissionDelegate);
321
322 // Grant permissions, and turn off location services.
323 mLocationUtils.mLocationGranted = true;
324 mLocationUtils.mSystemLocationSettingsEnabled = false;
325
326 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
327 @Override
328 public void run() {
329 mChooserDialog.notifyDiscoveryState(
330 BluetoothChooserDialog.DISCOVERY_FAILED_TO_START);
331 }
332 });
333
334 assertEquals(removeLinkTags(
335 getActivity().getString(R.string.bluetooth_need_loc ation_services_on)),
336 errorView.getText().toString());
337 assertEquals(removeLinkTags(getActivity().getString(
338 R.string.bluetooth_need_location_permission_help)),
339 statusView.getText().toString());
340 assertFalse(button.isEnabled());
341 assertEquals(View.VISIBLE, errorView.getVisibility());
342 assertEquals(View.GONE, items.getVisibility());
343 assertEquals(View.GONE, progress.getVisibility());
344
345 // Turn on Location Services.
346 mLocationUtils.mSystemLocationSettingsEnabled = true;
347 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
348 @Override
349 public void run() {
350 mChooserDialog.mLocationModeBroadcastReceiver.onReceive(
351 getActivity(), new Intent(LocationManager.MODE_CHANGED_A CTION));
352 }
353 });
354
355 assertEquals(1, mChooserDialog.mRestartSearchCount);
356 assertEquals(removeLinkTags(getActivity().getString(R.string.bluetooth_s earching)),
357 statusView.getText().toString());
358
359 mChooserDialog.closeDialog();
298 } 360 }
299 361
300 // TODO(jyasskin): Test when the user denies Chrome the ability to ask for p ermission. 362 // TODO(jyasskin): Test when the user denies Chrome the ability to ask for p ermission.
301 363
302 private static class TestAndroidPermissionDelegate implements AndroidPermiss ionDelegate { 364 private static class TestAndroidPermissionDelegate implements AndroidPermiss ionDelegate {
303 PermissionCallback mCallback = null; 365 PermissionCallback mCallback = null;
304 String[] mPermissionsRequested = null; 366 String[] mPermissionsRequested = null;
305 367
306 @Override 368 @Override
307 public boolean hasPermission(String permission) { 369 public boolean hasPermission(String permission) {
(...skipping 18 matching lines...) Expand all
326 } 388 }
327 } 389 }
328 390
329 private static class FakeLocationUtils extends LocationUtils { 391 private static class FakeLocationUtils extends LocationUtils {
330 public boolean mLocationGranted = false; 392 public boolean mLocationGranted = false;
331 393
332 @Override 394 @Override
333 public boolean hasAndroidLocationPermission(Context context) { 395 public boolean hasAndroidLocationPermission(Context context) {
334 return mLocationGranted; 396 return mLocationGranted;
335 } 397 }
398
399 public boolean mSystemLocationSettingsEnabled = true;
400
401 @Override
402 public boolean isSystemLocationSettingEnabled(Context context) {
403 return mSystemLocationSettingsEnabled;
404 }
336 } 405 }
337 } 406 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698