Merge "Fix crash in developer settings when backup is not available" into oc-mr1-dev
diff --git a/res/xml/development_prefs.xml b/res/xml/development_prefs.xml
index 8992446..1846a8c 100644
--- a/res/xml/development_prefs.xml
+++ b/res/xml/development_prefs.xml
@@ -210,6 +210,11 @@
             android:entryValues="@array/usb_configuration_values" />
 
         <SwitchPreference
+            android:key="bluetooth_show_devices_without_names"
+            android:title="@string/bluetooth_show_devices_without_names"
+            android:summary="@string/bluetooth_show_devices_without_names_summary"/>
+
+        <SwitchPreference
             android:key="bluetooth_disable_absolute_volume"
             android:title="@string/bluetooth_disable_absolute_volume"
             android:summary="@string/bluetooth_disable_absolute_volume_summary"/>
diff --git a/src/com/android/settings/bluetooth/BluetoothDevicePreference.java b/src/com/android/settings/bluetooth/BluetoothDevicePreference.java
index 7b81018..084b50e 100644
--- a/src/com/android/settings/bluetooth/BluetoothDevicePreference.java
+++ b/src/com/android/settings/bluetooth/BluetoothDevicePreference.java
@@ -54,16 +54,17 @@
     private final UserManager mUserManager;
 
     private AlertDialog mDisconnectDialog;
-
     private String contentDescription = null;
-
+    private DeviceListPreferenceFragment mDeviceListPreferenceFragment;
     /* Talk-back descriptions for various BT icons */
     Resources mResources;
 
-    public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice) {
+    public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice,
+            DeviceListPreferenceFragment deviceListPreferenceFragment) {
         super(context, null);
         mResources = getContext().getResources();
         mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
+        mDeviceListPreferenceFragment = deviceListPreferenceFragment;
 
         if (sDimAlpha == Integer.MIN_VALUE) {
             TypedValue outValue = new TypedValue();
@@ -131,6 +132,11 @@
         // Used to gray out the item
         setEnabled(!mCachedDevice.isBusy());
 
+        // Device is only visible in the UI if it has a valid name besides MAC address or when user
+        // allows showing devices without user-friendly name in developer settings
+        setVisible(mDeviceListPreferenceFragment.shouldShowDevicesWithoutNames()
+                || mCachedDevice.hasHumanReadableName());
+
         // This could affect ordering, so notify that
         notifyHierarchyChanged();
     }
diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java
index bd86c4b..361bc2f 100644
--- a/src/com/android/settings/bluetooth/BluetoothSettings.java
+++ b/src/com/android/settings/bluetooth/BluetoothSettings.java
@@ -140,6 +140,8 @@
             mBluetoothEnabler.resume(getActivity());
         }
         super.onStart();
+        // Always show paired devices regardless whether user-friendly name exists
+        mShowDevicesWithoutNames = true;
         if (isUiRestricted()) {
             getPreferenceScreen().removeAll();
             if (!isUiRestrictedByOnlyAdmin()) {
diff --git a/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.java b/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.java
index ca06e3c..0485e69 100644
--- a/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.java
+++ b/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.java
@@ -19,6 +19,7 @@
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
 import android.os.Bundle;
+import android.os.SystemProperties;
 import android.support.annotation.VisibleForTesting;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceCategory;
@@ -52,6 +53,10 @@
 
     private static final String KEY_BT_SCAN = "bt_scan";
 
+    // Copied from DevelopmentSettings.java
+    private static final String BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY =
+            "persist.bluetooth.showdeviceswithoutnames";
+
     private BluetoothDeviceFilter.Filter mFilter;
 
     @VisibleForTesting
@@ -68,6 +73,8 @@
     final WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference> mDevicePreferenceMap =
             new WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference>();
 
+    boolean mShowDevicesWithoutNames;
+
     DeviceListPreferenceFragment(String restrictedKey) {
         super(restrictedKey);
         mFilter = BluetoothDeviceFilter.ALL_FILTER;
@@ -103,6 +110,8 @@
     @Override
     public void onStart() {
         super.onStart();
+        mShowDevicesWithoutNames = SystemProperties.getBoolean(
+                BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY, false);
         if (mLocalManager == null || isUiRestricted()) return;
 
         mLocalManager.setForegroundActivity(getActivity());
@@ -181,7 +190,7 @@
         BluetoothDevicePreference preference = (BluetoothDevicePreference) getCachedPreference(key);
 
         if (preference == null) {
-            preference = new BluetoothDevicePreference(getPrefContext(), cachedDevice);
+            preference = new BluetoothDevicePreference(getPrefContext(), cachedDevice, this);
             preference.setKey(key);
             mDeviceListGroup.addPreference(preference);
         } else {
@@ -271,4 +280,8 @@
      * Return the key of the {@link PreferenceGroup} that contains the bluetooth devices
      */
     public abstract String getDeviceListKey();
+
+    public boolean shouldShowDevicesWithoutNames() {
+        return mShowDevicesWithoutNames;
+    }
 }
diff --git a/src/com/android/settings/development/DevelopmentSettings.java b/src/com/android/settings/development/DevelopmentSettings.java
index d05991c..c2571ee 100644
--- a/src/com/android/settings/development/DevelopmentSettings.java
+++ b/src/com/android/settings/development/DevelopmentSettings.java
@@ -199,6 +199,10 @@
     private static final String FORCE_RESIZABLE_KEY = "force_resizable_activities";
     private static final String COLOR_TEMPERATURE_KEY = "color_temperature";
 
+    private static final String BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_KEY =
+            "bluetooth_show_devices_without_names";
+    private static final String BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY =
+            "persist.bluetooth.showdeviceswithoutnames";
     private static final String BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_KEY =
             "bluetooth_disable_absolute_volume";
     private static final String BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY =
@@ -282,6 +286,7 @@
     private SwitchPreference mWifiAggressiveHandover;
     private SwitchPreference mMobileDataAlwaysOn;
     private SwitchPreference mTetheringHardwareOffload;
+    private SwitchPreference mBluetoothShowDevicesWithoutNames;
     private SwitchPreference mBluetoothDisableAbsVolume;
     private SwitchPreference mBluetoothEnableInbandRinging;
 
@@ -498,6 +503,8 @@
             mLogpersist = null;
         }
         mUsbConfiguration = addListPreference(USB_CONFIGURATION_KEY);
+        mBluetoothShowDevicesWithoutNames =
+                findAndInitSwitchPref(BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_KEY);
         mBluetoothDisableAbsVolume = findAndInitSwitchPref(BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_KEY);
         mBluetoothEnableInbandRinging = findAndInitSwitchPref(BLUETOOTH_ENABLE_INBAND_RINGING_KEY);
         if (!BluetoothHeadset.isInbandRingingSupported(getContext())) {
@@ -838,6 +845,7 @@
         if (mColorTemperaturePreference != null) {
             updateColorTemperature();
         }
+        updateBluetoothShowDevicesWithoutUserFriendlyNameOptions();
         updateBluetoothDisableAbsVolumeOptions();
         updateBluetoothEnableInbandRingingOptions();
         updateBluetoothA2dpConfigurationValues();
@@ -1471,6 +1479,17 @@
         mWifiManager.setAllowScansWithTraffic(mWifiAllowScansWithTraffic.isChecked() ? 1 : 0);
     }
 
+    private void updateBluetoothShowDevicesWithoutUserFriendlyNameOptions() {
+        updateSwitchPreference(mBluetoothShowDevicesWithoutNames,
+                SystemProperties.getBoolean(
+                        BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY, false));
+    }
+
+    private void writeBluetoothShowDevicesWithoutUserFriendlyNameOptions() {
+        SystemProperties.set(BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY,
+                mBluetoothShowDevicesWithoutNames.isChecked() ? "true" : "false");
+    }
+
     private void updateBluetoothDisableAbsVolumeOptions() {
         updateSwitchPreference(mBluetoothDisableAbsVolume,
                 SystemProperties.getBoolean(BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY, false));
@@ -2535,6 +2554,8 @@
             writeUSBAudioOptions();
         } else if (preference == mForceResizable) {
             writeForceResizableOptions();
+        } else if (preference == mBluetoothShowDevicesWithoutNames) {
+            writeBluetoothShowDevicesWithoutUserFriendlyNameOptions();
         } else if (preference == mBluetoothDisableAbsVolume) {
             writeBluetoothDisableAbsVolumeOptions();
         } else if (preference == mBluetoothEnableInbandRinging) {
diff --git a/src/com/android/settings/location/LocationSettings.java b/src/com/android/settings/location/LocationSettings.java
index dc321e2..34e8cc3 100644
--- a/src/com/android/settings/location/LocationSettings.java
+++ b/src/com/android/settings/location/LocationSettings.java
@@ -211,11 +211,6 @@
                     request.contentDescription);
             pref.setIcon(request.icon);
             pref.setTitle(request.label);
-            if (request.isHighBattery) {
-                pref.setSummary(R.string.location_high_battery_use);
-            } else {
-                pref.setSummary(R.string.location_low_battery_use);
-            }
             pref.setOnPreferenceClickListener(
                     new PackageEntryClickedListener(request.packageName, request.userHandle));
             recentLocationPrefs.add(pref);
diff --git a/tests/anomaly-tester/Android.mk b/tests/anomaly-tester/Android.mk
index beb5d69..ade37db 100644
--- a/tests/anomaly-tester/Android.mk
+++ b/tests/anomaly-tester/Android.mk
@@ -2,6 +2,15 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE_TAGS := tests
+LOCAL_CERTIFICATE := platform
+
+LOCAL_JAVA_LIBRARIES := android.test.runner bouncycastle
+
+LOCAL_STATIC_JAVA_LIBRARIES := \
+    android-support-test \
+    mockito-target \
+    ub-uiautomator \
+    truth-prebuilt \
 
 LOCAL_SRC_FILES := $(call all-subdir-java-files)
 
@@ -9,7 +18,7 @@
 
 LOCAL_PACKAGE_NAME := AnomalyTester
 
-LOCAL_CERTIFICATE := platform
+LOCAL_INSTRUMENTATION_FOR := Settings
 
 LOCAL_USE_AAPT2 := true
 
diff --git a/tests/anomaly-tester/AndroidManifest.xml b/tests/anomaly-tester/AndroidManifest.xml
index 68e2dd7..7893b86 100644
--- a/tests/anomaly-tester/AndroidManifest.xml
+++ b/tests/anomaly-tester/AndroidManifest.xml
@@ -22,11 +22,13 @@
     <uses-permission android:name="android.permission.WAKE_LOCK"/>
     <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
     <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
+
     <application
         android:allowBackup="false"
         android:label="@string/app_name"
         android:supportsRtl="true"
         android:theme="@android:style/Theme.Material.Light.DarkActionBar">
+        <uses-library android:name="android.test.runner" />
         <activity
             android:name=".AnomalyActivity"
             android:exported="true">
@@ -41,4 +43,10 @@
             android:exported="false"/>
     </application>
 
+    <instrumentation
+        android:name="android.support.test.runner.AndroidJUnitRunner"
+        android:targetPackage="com.android.settings"
+        android:label="Settings Test Cases">
+    </instrumentation>
+
 </manifest>
\ No newline at end of file
diff --git a/tests/anomaly-tester/src/com/android/settings/anomaly/tests/BluetoothAnomalyTest.java b/tests/anomaly-tester/src/com/android/settings/anomaly/tests/BluetoothAnomalyTest.java
new file mode 100644
index 0000000..3630ce4
--- /dev/null
+++ b/tests/anomaly-tester/src/com/android/settings/anomaly/tests/BluetoothAnomalyTest.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the
+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+package com.android.settings.anomaly.tests;
+
+import static com.google.common.truth.Truth.assertWithMessage;
+
+import android.app.Instrumentation;
+import android.content.Context;
+import android.content.Intent;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.runner.AndroidJUnit4;
+import android.support.test.uiautomator.By;
+import android.support.test.uiautomator.UiDevice;
+import android.support.test.uiautomator.Until;
+import android.text.format.DateUtils;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Functional test for bluetooth unoptimized scanning anomaly detector
+ *
+ * @see com.android.settings.fuelgauge.anomaly.checker.BluetoothScanAnomalyDetector
+ */
+@RunWith(AndroidJUnit4.class)
+public class BluetoothAnomalyTest {
+    private static final String BATTERY_INTENT = "android.intent.action.POWER_USAGE_SUMMARY";
+    private static final String RES_BT_EDITTEXT =
+            "com.android.settings.anomaly.tester:id/bluetooth_run_time";
+    private static final String RES_BT_BUTTON =
+            "com.android.settings.anomaly.tester:id/bluetooth_button";
+    private static final long TIME_OUT = 3000;
+    private UiDevice mDevice;
+
+    @Before
+    public void setUp() {
+        final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
+        final Context context = instrumentation.getContext();
+        mDevice = UiDevice.getInstance(instrumentation);
+
+        // setup environment
+        TestUtils.setUp(instrumentation);
+        // start anomaly-tester app
+        TestUtils.startAnomalyApp(context, mDevice);
+    }
+
+    @After
+    public void tearDown() {
+        TestUtils.tearDown(InstrumentationRegistry.getInstrumentation());
+    }
+
+    @Test
+    public void testBluetoothAnomaly_longScanningTime_reportAnomaly() throws InterruptedException {
+        // Set running time
+        final long durationMs = DateUtils.SECOND_IN_MILLIS * 15;
+        TestUtils.setEditTextWithValue(mDevice, RES_BT_EDITTEXT, durationMs);
+
+        // Click start button
+        TestUtils.clickButton(mDevice, RES_BT_BUTTON);
+
+        // Wait for its running
+        mDevice.pressHome();
+        TestUtils.wait(mDevice, durationMs);
+
+        // Check it in battery main page
+        Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
+        instrumentation.startActivitySync(new Intent(BATTERY_INTENT));
+        assertWithMessage("Doesn't have bluetooth anomaly").that(
+                mDevice.wait(Until.findObject(By.text("AnomalyTester draining battery")),
+                        TIME_OUT)).isNotNull();
+    }
+
+    @Test
+    public void testBluetoothAnomaly_shortScanningTime_notReport() throws InterruptedException {
+        // Set running time
+        final long durationMs = DateUtils.SECOND_IN_MILLIS;
+        TestUtils.setEditTextWithValue(mDevice, RES_BT_EDITTEXT, durationMs);
+
+        // Click start button
+        TestUtils.clickButton(mDevice, RES_BT_BUTTON);
+
+        // Wait for its running
+        mDevice.pressHome();
+        TestUtils.wait(mDevice, durationMs);
+
+        // Check it in battery main page
+        Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
+        instrumentation.startActivitySync(new Intent(BATTERY_INTENT));
+        assertWithMessage("Shouldn't have bluetooth anomaly").that(
+                mDevice.wait(Until.findObject(By.text("AnomalyTester draining battery")),
+                        TIME_OUT)).isNull();
+    }
+
+}
diff --git a/tests/anomaly-tester/src/com/android/settings/anomaly/tests/TestUtils.java b/tests/anomaly-tester/src/com/android/settings/anomaly/tests/TestUtils.java
new file mode 100644
index 0000000..ac15d77
--- /dev/null
+++ b/tests/anomaly-tester/src/com/android/settings/anomaly/tests/TestUtils.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the
+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+package com.android.settings.anomaly.tests;
+
+import static com.google.common.truth.Truth.assertWithMessage;
+
+import android.app.Instrumentation;
+import android.app.UiAutomation;
+import android.content.Context;
+import android.content.Intent;
+import android.support.test.uiautomator.By;
+import android.support.test.uiautomator.UiDevice;
+import android.support.test.uiautomator.UiObject2;
+import android.support.test.uiautomator.Until;
+
+public class TestUtils {
+    private static final String PACKAGE_NAME = "com.android.settings.anomaly.tester";
+    private static final long TIME_OUT = 3000;
+
+    /**
+     * This method set up the environment for anomaly test
+     *
+     * @param instrumentation to execute command
+     */
+    public static void setUp(Instrumentation instrumentation) {
+        final UiAutomation uiAutomation = instrumentation.getUiAutomation();
+        // pretend unplug and screen off, also reset the battery stats
+        uiAutomation.executeShellCommand("dumpsys battery unplug");
+        uiAutomation.executeShellCommand("dumpsys batterystats enable pretend-screen-off");
+        uiAutomation.executeShellCommand("dumpsys batterystats --reset");
+    }
+
+    /**
+     * This method cleans up all the commands in {@link #setUp(Instrumentation)}
+     *
+     * @param instrumentation to execute command
+     */
+    public static void tearDown(Instrumentation instrumentation) {
+        final UiAutomation uiAutomation = instrumentation.getUiAutomation();
+        // reset unplug and screen-off
+        uiAutomation.executeShellCommand("dumpsys battery reset");
+        uiAutomation.executeShellCommand("dumpsys batterystats disable pretend-screen-off");
+    }
+
+    public static void startAnomalyApp(Context context, UiDevice uiDevice) {
+        final Intent intent = context.getPackageManager().getLaunchIntentForPackage(PACKAGE_NAME);
+        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
+        context.startActivity(intent);
+        uiDevice.wait(Until.hasObject(By.pkg(PACKAGE_NAME).depth(0)), TIME_OUT);
+    }
+
+    /**
+     * Find {@link android.widget.EditText} with {@code res} and set its {@code value}
+     */
+    public static void setEditTextWithValue(UiDevice uiDevice, String res, long value) {
+        final UiObject2 editText = uiDevice.findObject(By.res(res));
+        assertWithMessage("Cannot find editText with res: " + res).that(editText).isNotNull();
+        editText.setText(String.valueOf(value));
+    }
+
+    /**
+     * Find {@link android.widget.Button} with {@code res} and click it
+     */
+    public static void clickButton(UiDevice uiDevice, String res) {
+        final UiObject2 button = uiDevice.findObject(By.res(res));
+        assertWithMessage("Cannot find button with res: " + res).that(button).isNotNull();
+        button.click();
+    }
+
+    /**
+     * Make {@link UiDevice} wait for {@code timeMs}
+     *
+     * @see Thread#sleep(long)
+     */
+    public static void wait(UiDevice uiDevice, long timeMs) throws InterruptedException {
+        uiDevice.waitForIdle();
+        Thread.sleep(timeMs);
+    }
+}
diff --git a/tests/anomaly-tester/src/com/android/settings/anomaly/tests/WakelockAnomalyTest.java b/tests/anomaly-tester/src/com/android/settings/anomaly/tests/WakelockAnomalyTest.java
new file mode 100644
index 0000000..a2f3804
--- /dev/null
+++ b/tests/anomaly-tester/src/com/android/settings/anomaly/tests/WakelockAnomalyTest.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the
+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+package com.android.settings.anomaly.tests;
+
+import static com.google.common.truth.Truth.assertWithMessage;
+
+import android.app.Instrumentation;
+import android.content.Context;
+import android.content.Intent;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.runner.AndroidJUnit4;
+import android.support.test.uiautomator.By;
+import android.support.test.uiautomator.UiDevice;
+import android.support.test.uiautomator.Until;
+import android.text.format.DateUtils;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Functional test for bluetooth unoptimized scanning anomaly detector
+ *
+ * @see com.android.settings.fuelgauge.anomaly.checker.BluetoothScanAnomalyDetector
+ */
+@RunWith(AndroidJUnit4.class)
+public class WakelockAnomalyTest {
+    private static final String BATTERY_INTENT = "android.intent.action.POWER_USAGE_SUMMARY";
+    private static final String RES_WAKELOCK_EDITTEXT =
+            "com.android.settings.anomaly.tester:id/wakelock_run_time";
+    private static final String RES_WAKELOCK_BUTTON =
+            "com.android.settings.anomaly.tester:id/wakelock_button";
+    private static final long TIME_OUT = 3000;
+    private UiDevice mDevice;
+
+    @Before
+    public void setUp() {
+        final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
+        final Context context = instrumentation.getContext();
+        mDevice = UiDevice.getInstance(instrumentation);
+
+        // setup environment
+        TestUtils.setUp(instrumentation);
+        // start anomaly-tester app
+        TestUtils.startAnomalyApp(context, mDevice);
+    }
+
+    @After
+    public void tearDown() {
+        TestUtils.tearDown(InstrumentationRegistry.getInstrumentation());
+    }
+
+    @Test
+    public void testWakelockAnomaly_longTimeWhileRunning_report() throws InterruptedException {
+        // Set running time
+        final long durationMs = DateUtils.SECOND_IN_MILLIS * 15;
+        TestUtils.setEditTextWithValue(mDevice, RES_WAKELOCK_EDITTEXT, durationMs);
+
+        // Click start button
+        TestUtils.clickButton(mDevice, RES_WAKELOCK_BUTTON);
+
+        // Wait for its running
+        mDevice.pressHome();
+        // Sleeping time less than running time, so the app still holding wakelock when we check
+        TestUtils.wait(mDevice, durationMs - TIME_OUT);
+
+        // Check it in battery main page
+        Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
+        instrumentation.startActivitySync(new Intent(BATTERY_INTENT));
+        assertWithMessage("Doesn't have wakelock anomaly").that(
+                mDevice.wait(Until.findObject(By.text("AnomalyTester draining battery")),
+                        TIME_OUT)).isNotNull();
+    }
+
+    @Test
+    public void testWakelockAnomaly_shortTime_notReport() throws InterruptedException {
+        // Set running time
+        final long durationMs = DateUtils.SECOND_IN_MILLIS;
+        TestUtils.setEditTextWithValue(mDevice, RES_WAKELOCK_EDITTEXT, durationMs);
+
+        // Click start button
+        TestUtils.clickButton(mDevice, RES_WAKELOCK_BUTTON);
+
+        // Wait for its running
+        mDevice.pressHome();
+        TestUtils.wait(mDevice, durationMs);
+
+        // Check it in battery main page
+        Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
+        instrumentation.startActivitySync(new Intent(BATTERY_INTENT));
+        assertWithMessage("Shouldn't have wakelock anomaly").that(
+                mDevice.wait(Until.findObject(By.text("AnomalyTester draining battery")),
+                        TIME_OUT)).isNull();
+    }
+
+    @Test
+    public void testWakelockAnomaly_longTimeWhileNotRunning_notReport()
+            throws InterruptedException {
+        // Set running time
+        final long durationMs = DateUtils.SECOND_IN_MILLIS * 10;
+        TestUtils.setEditTextWithValue(mDevice, RES_WAKELOCK_EDITTEXT, durationMs);
+
+        // Click start button
+        TestUtils.clickButton(mDevice, RES_WAKELOCK_BUTTON);
+
+        // Wait for its running
+        mDevice.pressHome();
+        // Wait more time for releasing the wakelock
+        TestUtils.wait(mDevice, durationMs + TIME_OUT);
+
+        // Check it in battery main page
+        Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
+        instrumentation.startActivitySync(new Intent(BATTERY_INTENT));
+        assertWithMessage("Shouldn't have wakelock anomaly").that(
+                mDevice.wait(Until.findObject(By.text("AnomalyTester draining battery")),
+                        TIME_OUT)).isNull();
+    }
+
+}
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDevicePreferenceTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDevicePreferenceTest.java
index b16e5bc..a1db5de 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDevicePreferenceTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDevicePreferenceTest.java
@@ -39,6 +39,8 @@
 import org.robolectric.util.ReflectionHelpers;
 
 import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
@@ -52,6 +54,8 @@
     private Context mContext;
     @Mock
     private CachedBluetoothDevice mCachedBluetoothDevice;
+    @Mock
+    private DeviceListPreferenceFragment mDeviceListPreferenceFragment;
 
     private FakeFeatureFactory mFakeFeatureFactory;
     private MetricsFeatureProvider mMetricsFeatureProvider;
@@ -64,7 +68,8 @@
         FakeFeatureFactory.setupForTest(mContext);
         mFakeFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
         mMetricsFeatureProvider = mFakeFeatureFactory.getMetricsFeatureProvider();
-        mPreference = new BluetoothDevicePreference(mContext, mCachedBluetoothDevice);
+        mPreference = new BluetoothDevicePreference(mContext, mCachedBluetoothDevice,
+                mDeviceListPreferenceFragment);
     }
 
     @Test
@@ -151,4 +156,49 @@
         assertThat(mPreference.getIcon()).isEqualTo(
                 mContext.getDrawable(R.drawable.ic_settings_print));
     }
+
+    @Test
+    public void testVisible_notVisibleThenVisible() {
+        when(mDeviceListPreferenceFragment.shouldShowDevicesWithoutNames()).thenReturn(false);
+        final boolean[] humanReadableName = {false};
+        doAnswer(invocation -> humanReadableName[0]).when(mCachedBluetoothDevice)
+                .hasHumanReadableName();
+        BluetoothDevicePreference preference =
+                new BluetoothDevicePreference(mContext, mCachedBluetoothDevice,
+                        mDeviceListPreferenceFragment);
+        assertThat(preference.isVisible()).isFalse();
+        humanReadableName[0] = true;
+        preference.onDeviceAttributesChanged();
+        assertThat(preference.isVisible()).isTrue();
+    }
+
+    @Test
+    public void testVisible_visibleThenNotVisible() {
+        when(mDeviceListPreferenceFragment.shouldShowDevicesWithoutNames()).thenReturn(false);
+        final boolean[] humanReadableName = {true};
+        doAnswer(invocation -> humanReadableName[0]).when(mCachedBluetoothDevice)
+                .hasHumanReadableName();
+        BluetoothDevicePreference preference =
+                new BluetoothDevicePreference(mContext, mCachedBluetoothDevice,
+                        mDeviceListPreferenceFragment);
+        assertThat(preference.isVisible()).isTrue();
+        humanReadableName[0] = false;
+        preference.onDeviceAttributesChanged();
+        assertThat(preference.isVisible()).isFalse();
+    }
+
+    @Test
+    public void testVisible_alwaysVisibleWhenEnabled() {
+        when(mDeviceListPreferenceFragment.shouldShowDevicesWithoutNames()).thenReturn(true);
+        final boolean[] humanReadableName = {true};
+        doAnswer(invocation -> humanReadableName[0]).when(mCachedBluetoothDevice)
+                .hasHumanReadableName();
+        BluetoothDevicePreference preference =
+                new BluetoothDevicePreference(mContext, mCachedBluetoothDevice,
+                        mDeviceListPreferenceFragment);
+        assertThat(preference.isVisible()).isTrue();
+        humanReadableName[0] = false;
+        preference.onDeviceAttributesChanged();
+        assertThat(preference.isVisible()).isTrue();
+    }
 }