Merge "Using a common class for loading drawables and handling various badging" into oc-dev
diff --git a/res/xml/accessibility_settings.xml b/res/xml/accessibility_settings.xml
index 795d35e..02b9949 100644
--- a/res/xml/accessibility_settings.xml
+++ b/res/xml/accessibility_settings.xml
@@ -57,11 +57,6 @@
             android:title="@string/accessibility_screen_magnification_title"
             android:icon="@mipmap/ic_accessibility_magnification" />
 
-        <Preference
-                android:fragment="com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment"
-                android:key="daltonizer_preference_screen"
-                android:title="@string/accessibility_display_daltonizer_preference_title" />
-
         <SwitchPreference
                 android:key="toggle_large_pointer_icon"
                 android:title="@string/accessibility_toggle_large_pointer_icon_title" />
@@ -118,6 +113,11 @@
                 android:key="toggle_high_text_contrast_preference"
                 android:title="@string/accessibility_toggle_high_text_contrast_preference_title" />
 
+        <Preference
+                android:fragment="com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment"
+                android:key="daltonizer_preference_screen"
+                android:title="@string/accessibility_display_daltonizer_preference_title" />
+
         <SwitchPreference
                 android:key="toggle_inversion_preference"
                 android:title="@string/accessibility_display_inversion_preference_title"
diff --git a/src/com/android/settings/DisplaySettings.java b/src/com/android/settings/DisplaySettings.java
index 1230ae5..b29f8a0 100644
--- a/src/com/android/settings/DisplaySettings.java
+++ b/src/com/android/settings/DisplaySettings.java
@@ -104,7 +104,7 @@
         controllers.add(new VrDisplayPreferenceController(context));
         controllers.add(new WallpaperPreferenceController(context));
         controllers.add(new ThemePreferenceController(context));
-        controllers.add(new BrightnessLevelPreferenceController(context));
+        controllers.add(new BrightnessLevelPreferenceController(context, lifecycle));
         return controllers;
     }
 
diff --git a/src/com/android/settings/accessibility/AccessibilitySettings.java b/src/com/android/settings/accessibility/AccessibilitySettings.java
index 1d3e125..44dd353 100644
--- a/src/com/android/settings/accessibility/AccessibilitySettings.java
+++ b/src/com/android/settings/accessibility/AccessibilitySettings.java
@@ -550,6 +550,22 @@
     }
 
     private void updateSystemPreferences() {
+        // Move color inversion and color correction preferences to Display category if device
+        // supports HWC hardware-accelerated color transform.
+        if (isColorTransformAccelerated(getContext())) {
+            PreferenceCategory experimentalCategory =
+                    mCategoryToPrefCategoryMap.get(CATEGORY_EXPERIMENTAL);
+            PreferenceCategory displayCategory =
+                    mCategoryToPrefCategoryMap.get(CATEGORY_DISPLAY);
+            experimentalCategory.removePreference(mToggleInversionPreference);
+            experimentalCategory.removePreference(mDisplayDaltonizerPreferenceScreen);
+            mToggleInversionPreference.setOrder(mToggleLargePointerIconPreference.getOrder());
+            mDisplayDaltonizerPreferenceScreen.setOrder(mToggleInversionPreference.getOrder());
+            mToggleInversionPreference.setSummary(R.string.summary_empty);
+            displayCategory.addPreference(mToggleInversionPreference);
+            displayCategory.addPreference(mDisplayDaltonizerPreferenceScreen);
+        }
+
         // Text contrast.
         mToggleHighTextContrastPreference.setChecked(
                 Settings.Secure.getInt(getContentResolver(),
@@ -601,6 +617,11 @@
         updateAccessibilityShortcut(mAccessibilityShortcutPreferenceScreen);
     }
 
+    private boolean isColorTransformAccelerated(Context context) {
+        return context.getResources()
+                .getBoolean(com.android.internal.R.bool.config_setColorTransformAccelerated);
+    }
+
     private void updateMagnificationSummary(Preference pref) {
         final boolean tripleTapEnabled = Settings.Secure.getInt(getContentResolver(),
                 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED, 0) == 1;
diff --git a/src/com/android/settings/display/BrightnessLevelPreferenceController.java b/src/com/android/settings/display/BrightnessLevelPreferenceController.java
index e0000d6..d012ac3 100644
--- a/src/com/android/settings/display/BrightnessLevelPreferenceController.java
+++ b/src/com/android/settings/display/BrightnessLevelPreferenceController.java
@@ -13,20 +13,79 @@
  */
 package com.android.settings.display;
 
-import static android.provider.Settings.System.SCREEN_BRIGHTNESS;
-
+import android.content.ContentResolver;
 import android.content.Context;
+import android.database.ContentObserver;
+import android.net.Uri;
+import android.os.Handler;
+import android.os.PowerManager;
+import android.os.RemoteException;
+import android.os.ServiceManager;
 import android.provider.Settings;
+import android.provider.Settings.System;
+import android.service.vr.IVrManager;
+import android.support.annotation.VisibleForTesting;
 import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceScreen;
+import android.util.Log;
+
 import com.android.settings.core.PreferenceController;
+import com.android.settings.core.lifecycle.Lifecycle;
+import com.android.settings.core.lifecycle.LifecycleObserver;
+import com.android.settings.core.lifecycle.events.OnPause;
+import com.android.settings.core.lifecycle.events.OnResume;
+
 import java.text.NumberFormat;
 
-public class BrightnessLevelPreferenceController extends PreferenceController {
+public class BrightnessLevelPreferenceController extends PreferenceController implements
+        LifecycleObserver, OnResume, OnPause {
 
+    private static final String TAG = "BrightnessPrefCtrl";
     private static final String KEY_BRIGHTNESS = "brightness";
+    private static final Uri BRIGHTNESS_MODE_URI;
+    private static final Uri BRIGHTNESS_URI;
+    private static final Uri BRIGHTNESS_FOR_VR_URI;
+    private static final Uri BRIGHTNESS_ADJ_URI;
 
-    public BrightnessLevelPreferenceController(Context context) {
+    private final int mMinBrightness;
+    private final int mMaxBrightness;
+    private final int mMinVrBrightness;
+    private final int mMaxVrBrightness;
+    private final ContentResolver mContentResolver;
+
+    private Preference mPreference;
+
+    static {
+        BRIGHTNESS_MODE_URI = System.getUriFor(System.SCREEN_BRIGHTNESS_MODE);
+        BRIGHTNESS_URI = System.getUriFor(System.SCREEN_BRIGHTNESS);
+        BRIGHTNESS_FOR_VR_URI = System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR);
+        BRIGHTNESS_ADJ_URI = System.getUriFor(System.SCREEN_AUTO_BRIGHTNESS_ADJ);
+    }
+
+    private ContentObserver mBrightnessObserver = new ContentObserver(new Handler()) {
+        @Override
+        public void onChange(boolean selfChange) {
+            updatedSummary(mPreference);
+        }
+    };
+
+    public BrightnessLevelPreferenceController(Context context, Lifecycle lifecycle) {
+        this(context, lifecycle, new PowerManagerWrapper(
+                (PowerManager) context.getSystemService(Context.POWER_SERVICE)));
+    }
+
+    @VisibleForTesting
+    public BrightnessLevelPreferenceController(Context context, Lifecycle lifecycle,
+            PowerManagerWrapper powerManagerWrapper) {
         super(context);
+        if (lifecycle != null) {
+            lifecycle.addObserver(this);
+        }
+        mMinBrightness = powerManagerWrapper.getMinimumScreenBrightnessSetting();
+        mMaxBrightness = powerManagerWrapper.getMaximumScreenBrightnessSetting();
+        mMinVrBrightness = powerManagerWrapper.getMinimumScreenBrightnessForVrSetting();
+        mMaxVrBrightness = powerManagerWrapper.getMaximumScreenBrightnessForVrSetting();
+        mContentResolver = mContext.getContentResolver();
     }
 
     @Override
@@ -40,10 +99,66 @@
     }
 
     @Override
-    public void updateState(Preference preference) {
-        final double brightness = Settings.System.getInt(mContext.getContentResolver(),
-            SCREEN_BRIGHTNESS, 0);
-        preference.setSummary(NumberFormat.getPercentInstance().format(brightness / 255));
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        mPreference = screen.findPreference(KEY_BRIGHTNESS);
     }
 
+    @Override
+    public void updateState(Preference preference) {
+        updatedSummary(preference);
+    }
+
+    @Override
+    public void onResume() {
+        mContentResolver.registerContentObserver(BRIGHTNESS_MODE_URI, false, mBrightnessObserver);
+        mContentResolver.registerContentObserver(BRIGHTNESS_URI, false, mBrightnessObserver);
+        mContentResolver.registerContentObserver(BRIGHTNESS_FOR_VR_URI, false, mBrightnessObserver);
+        mContentResolver.registerContentObserver(BRIGHTNESS_ADJ_URI, false, mBrightnessObserver);
+    }
+
+    @Override
+    public void onPause() {
+        mContentResolver.unregisterContentObserver(mBrightnessObserver);
+    }
+
+    private void updatedSummary(Preference preference) {
+        if (preference != null) {
+            preference.setSummary(NumberFormat.getPercentInstance().format(getCurrentBrightness()));
+        }
+    }
+
+    private double getCurrentBrightness() {
+        if (isInVrMode()) {
+            final double value = System.getInt(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR,
+                    mMaxBrightness);
+            return getPercentage(value, mMinVrBrightness, mMaxVrBrightness);
+        }
+        final int brightnessMode = Settings.System.getInt(mContentResolver,
+                System.SCREEN_BRIGHTNESS_MODE, System.SCREEN_BRIGHTNESS_MODE_MANUAL);
+        if (brightnessMode == System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
+            final float value = Settings.System.getFloat(mContentResolver,
+                    System.SCREEN_AUTO_BRIGHTNESS_ADJ, 0);
+            // auto brightness is between -1 and 1
+            return ((value + 1)) / 2;
+        }
+        final double value = Settings.System.getInt(mContentResolver, System.SCREEN_BRIGHTNESS,
+                mMinBrightness);
+        return getPercentage(value, mMinBrightness, mMaxBrightness);
+    }
+
+    private double getPercentage(double value, int min, int max) {
+        return (value - min) / (max - min);
+    }
+
+    @VisibleForTesting
+    boolean isInVrMode() {
+        try {
+            return IVrManager.Stub.asInterface(ServiceManager.getService(Context.VR_SERVICE))
+                    .getVrModeState();
+        } catch (RemoteException e) {
+            Log.e(TAG, "Failed to check vr mode!", e);
+        }
+        return false;
+    }
 }
diff --git a/src/com/android/settings/display/PowerManagerWrapper.java b/src/com/android/settings/display/PowerManagerWrapper.java
new file mode 100644
index 0000000..afa2f33
--- /dev/null
+++ b/src/com/android/settings/display/PowerManagerWrapper.java
@@ -0,0 +1,49 @@
+/*
+ * 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.display;
+
+import android.os.PowerManager;
+
+/**
+ * This class replicates a subset of the android.os.PowerManager. The class exists so that we can
+ * use a thin wrapper around the PowerManager in production code and a mock in tests. We cannot
+ * directly mock or shadow the PowerManager, because some of the methods we rely on are newer than
+ * the API version supported by Robolectric or are hidden.
+ */
+public class PowerManagerWrapper {
+    private final PowerManager mPowerManager;
+
+    public PowerManagerWrapper(PowerManager powerManager) {
+        mPowerManager = powerManager;
+    }
+
+    public int getMinimumScreenBrightnessSetting() {
+        return mPowerManager.getMinimumScreenBrightnessSetting();
+    }
+
+    public int getMaximumScreenBrightnessSetting() {
+        return mPowerManager.getMaximumScreenBrightnessSetting();
+    }
+
+    public int getMinimumScreenBrightnessForVrSetting() {
+        return mPowerManager.getMinimumScreenBrightnessForVrSetting();
+    }
+
+    public int getMaximumScreenBrightnessForVrSetting() {
+        return mPowerManager.getMaximumScreenBrightnessForVrSetting();
+    }
+}
diff --git a/src/com/android/settings/wifi/WifiSettings.java b/src/com/android/settings/wifi/WifiSettings.java
index 01551da..66e8c54 100644
--- a/src/com/android/settings/wifi/WifiSettings.java
+++ b/src/com/android/settings/wifi/WifiSettings.java
@@ -191,8 +191,10 @@
     public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
 
-        getPreferenceManager().setPreferenceComparisonCallback(
-                new PreferenceManager.SimplePreferenceComparisonCallback());
+        // TODO(b/37429702): Add animations and preference comparator back after initial screen is
+        // loaded (ODR).
+        setAnimationAllowed(false);
+
         addPreferencesFromResource(R.xml.wifi_settings);
 
         mConnectedAccessPointPreferenceCategory =
@@ -204,9 +206,6 @@
         mConfigureWifiSettingsPreference = findPreference(PREF_KEY_CONFIGURE_WIFI_SETTINGS);
         mSavedNetworksPreference = findPreference(PREF_KEY_SAVED_NETWORKS);
 
-        // Hide additional settings until access points are shown during onStart
-        showAdditionalSettings(false);
-
         Context prefContext = getPrefContext();
         mAddPreference = new Preference(prefContext);
         mAddPreference.setIcon(R.drawable.ic_menu_add_inset);
@@ -224,17 +223,6 @@
         mBgThread.start();
     }
 
-    // TODO(b/37429702): Figure out how to temporarily disable animations during startup and remove
-    // this method.
-    private void showAdditionalSettings(boolean visible) {
-        mAdditionalSettingsPreferenceCategory.setVisible(visible);
-        mAdditionalSettingsPreferenceCategory.removeAll();
-        if (visible) {
-            mAdditionalSettingsPreferenceCategory.addPreference(mConfigureWifiSettingsPreference);
-            mAdditionalSettingsPreferenceCategory.addPreference(mSavedNetworksPreference);
-        }
-    }
-
     @Override
     public void onDestroy() {
         mBgThread.quit();
@@ -359,8 +347,6 @@
             getView().removeCallbacks(mUpdateAccessPointsRunnable);
             updateAccessPointPreferences();
         }
-
-        showAdditionalSettings(true);
     }
 
     /**
@@ -394,7 +380,6 @@
         mWifiTracker.stopTracking();
         getView().removeCallbacks(mUpdateAccessPointsRunnable);
         getView().removeCallbacks(mHideProgressBarRunnable);
-        showAdditionalSettings(false);
         super.onStop();
     }
 
diff --git a/src/com/android/settings/wifi/details/WifiDetailPreferenceController.java b/src/com/android/settings/wifi/details/WifiDetailPreferenceController.java
index e8bca37..21fda30 100644
--- a/src/com/android/settings/wifi/details/WifiDetailPreferenceController.java
+++ b/src/com/android/settings/wifi/details/WifiDetailPreferenceController.java
@@ -301,6 +301,7 @@
                 Preference pref = new Preference(mPrefContext);
                 pref.setKey(ip);
                 pref.setTitle(ip);
+                pref.setSelectable(false);
                 mIpv6AddressCategory.addPreference(pref);
                 mIpv6AddressCategory.setVisible(true);
             }
diff --git a/tests/robotests/src/com/android/settings/display/BrightnessLevelPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/BrightnessLevelPreferenceControllerTest.java
index 025e1ae..0b33089 100644
--- a/tests/robotests/src/com/android/settings/display/BrightnessLevelPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/display/BrightnessLevelPreferenceControllerTest.java
@@ -16,27 +16,31 @@
 
 package com.android.settings.display;
 
-import static android.provider.Settings.System.SCREEN_BRIGHTNESS;
 import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import android.content.ContentResolver;
 import android.content.Context;
-import android.provider.Settings;
+import android.provider.Settings.System;
 import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceScreen;
 
 import com.android.settings.SettingsRobolectricTestRunner;
 import com.android.settings.TestConfig;
-import com.android.settings.testutils.shadow.SettingsShadowSystemProperties;
 
-import java.text.NumberFormat;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
+import org.robolectric.internal.ShadowExtractor;
+import org.robolectric.shadows.ShadowContentResolver;
 
 @RunWith(SettingsRobolectricTestRunner.class)
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@@ -46,6 +50,10 @@
     @Mock
     private ContentResolver mContentResolver;
     @Mock
+    private PowerManagerWrapper mPowerManager;
+    @Mock
+    private PreferenceScreen mScreen;
+    @Mock
     private Preference mPreference;
 
     private BrightnessLevelPreferenceController mController;
@@ -53,8 +61,15 @@
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
+        when(mContext.getContentResolver()).thenReturn(mContentResolver);
+        when(mPowerManager.getMinimumScreenBrightnessSetting()).thenReturn(0);
+        when(mPowerManager.getMaximumScreenBrightnessSetting()).thenReturn(100);
+        when(mPowerManager.getMinimumScreenBrightnessForVrSetting()).thenReturn(0);
+        when(mPowerManager.getMaximumScreenBrightnessForVrSetting()).thenReturn(100);
+        when(mScreen.findPreference(anyString())).thenReturn(mPreference);
+        mController = spy(new BrightnessLevelPreferenceController(mContext, null, mPowerManager));
+        doReturn(false).when(mController).isInVrMode();
 
-        mController = new BrightnessLevelPreferenceController(mContext);
     }
 
     @Test
@@ -63,14 +78,80 @@
     }
 
     @Test
-    public void updateState_shouldSetSummary() {
-        final NumberFormat formatter = NumberFormat.getPercentInstance();
-        when(mContext.getContentResolver()).thenReturn(mContentResolver);
-        Settings.System.putInt(mContentResolver, SCREEN_BRIGHTNESS, 45);
+    public void onResume_shouldRegisterObserver() {
+        Context context = RuntimeEnvironment.application;
+        BrightnessLevelPreferenceController controller =
+            new BrightnessLevelPreferenceController(context, null, mPowerManager);
+        ShadowContentResolver shadowContentResolver =
+            (ShadowContentResolver) ShadowExtractor.extract(context.getContentResolver());
+
+        controller.onResume();
+
+        assertThat(shadowContentResolver.getContentObservers(
+            System.getUriFor(System.SCREEN_BRIGHTNESS_MODE))).isNotEmpty();
+        assertThat(shadowContentResolver.getContentObservers(
+            System.getUriFor(System.SCREEN_BRIGHTNESS))).isNotEmpty();
+        assertThat(shadowContentResolver.getContentObservers(
+            System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR))).isNotEmpty();
+        assertThat(shadowContentResolver.getContentObservers(
+            System.getUriFor(System.SCREEN_AUTO_BRIGHTNESS_ADJ))).isNotEmpty();
+    }
+
+    @Test
+    public void onPause_shouldUnregisterObserver() {
+        Context context = RuntimeEnvironment.application;
+        BrightnessLevelPreferenceController controller =
+            new BrightnessLevelPreferenceController(context, null, mPowerManager);
+        ShadowContentResolver shadowContentResolver =
+            (ShadowContentResolver) ShadowExtractor.extract(context.getContentResolver());
+
+        controller.displayPreference(mScreen);
+        controller.onResume();
+        controller.onPause();
+
+        assertThat(shadowContentResolver.getContentObservers(
+            System.getUriFor(System.SCREEN_BRIGHTNESS_MODE))).isEmpty();
+        assertThat(shadowContentResolver.getContentObservers(
+            System.getUriFor(System.SCREEN_BRIGHTNESS))).isEmpty();
+        assertThat(shadowContentResolver.getContentObservers(
+            System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR))).isEmpty();
+        assertThat(shadowContentResolver.getContentObservers(
+            System.getUriFor(System.SCREEN_AUTO_BRIGHTNESS_ADJ))).isEmpty();
+    }
+
+    @Test
+    public void updateState_inVrMode_shouldSetSummaryToVrBrightness() {
+        doReturn(true).when(mController).isInVrMode();
+        System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR, 85);
 
         mController.updateState(mPreference);
 
-        verify(mPreference).setSummary(formatter.format(45.0 / 255));
+        verify(mPreference).setSummary("85%");
     }
 
+    @Test
+    public void updateState_autoBrightness_shouldSetSummaryToVrBrightness() {
+        doReturn(false).when(mController).isInVrMode();
+        System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
+            System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
+
+        System.putFloat(mContentResolver, System.SCREEN_AUTO_BRIGHTNESS_ADJ, 0.0f);
+
+        mController.updateState(mPreference);
+
+        verify(mPreference).setSummary("50%");
+    }
+
+    @Test
+    public void updateState_manualBrightness_shouldSetSummaryToVrBrightness() {
+        doReturn(false).when(mController).isInVrMode();
+        System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
+            System.SCREEN_BRIGHTNESS_MODE_MANUAL);
+
+        System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS, 45);
+
+        mController.updateState(mPreference);
+
+        verify(mPreference).setSummary("45%");
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/search/DatabaseIndexingUtilsTest.java b/tests/robotests/src/com/android/settings/search/DatabaseIndexingUtilsTest.java
index f4370fc..fa0170c 100644
--- a/tests/robotests/src/com/android/settings/search/DatabaseIndexingUtilsTest.java
+++ b/tests/robotests/src/com/android/settings/search/DatabaseIndexingUtilsTest.java
@@ -17,35 +17,30 @@
 
 package com.android.settings.search;
 
-import android.content.Context;
+import static com.google.common.truth.Truth.assertThat;
 
+import android.content.Context;
 import android.util.ArrayMap;
+
 import com.android.internal.hardware.AmbientDisplayConfiguration;
 import com.android.settings.SettingsRobolectricTestRunner;
 import com.android.settings.TestConfig;
 import com.android.settings.core.PreferenceController;
-import com.android.settings.display.AutoBrightnessPreferenceController;
-import com.android.settings.gestures.DoubleTapPowerPreferenceController;
-import com.android.settings.gestures.DoubleTapScreenPreferenceController;
-import com.android.settings.gestures.DoubleTwistPreferenceController;
-import com.android.settings.gestures.PickupGesturePreferenceController;
-import com.android.settings.gestures.SwipeToNotificationPreferenceController;
+import com.android.settings.deviceinfo.SystemUpdatePreferenceController;
 import com.android.settings.search2.DatabaseIndexingUtils;
-
 import com.android.settings.search2.IntentPayload;
 import com.android.settings.search2.ResultPayload;
+
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
-import org.robolectric.shadows.ShadowApplication;
 
 import java.util.Map;
 
-import static com.google.common.truth.Truth.assertThat;
-
 @RunWith(SettingsRobolectricTestRunner.class)
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
 public class DatabaseIndexingUtilsTest {
@@ -57,7 +52,7 @@
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
-        mContext = ShadowApplication.getInstance().getApplicationContext();
+        mContext = RuntimeEnvironment.application;
     }
 
     @Test
@@ -74,11 +69,11 @@
 
     @Test
     public void testGetPreferenceControllerUriMap_CompatibleClass_ReturnsValidMap() {
-        String className = "com.android.settings.DisplaySettings";
-
-        Map map = DatabaseIndexingUtils.getPreferenceControllerUriMap(className, mContext);
-        assertThat(map.get("auto_brightness"))
-                .isInstanceOf(AutoBrightnessPreferenceController.class);
+        final String className = "com.android.settings.system.SystemDashboardFragment";
+        final Map<String, PreferenceController> map =
+                DatabaseIndexingUtils.getPreferenceControllerUriMap(className, mContext);
+        assertThat(map.get("system_update_settings"))
+                .isInstanceOf(SystemUpdatePreferenceController.class);
     }
 
     @Test
@@ -106,7 +101,7 @@
                 return new IntentPayload(null);
             }
         };
-        ArrayMap<String,PreferenceController> map = new ArrayMap<>();
+        ArrayMap<String, PreferenceController> map = new ArrayMap<>();
         map.put(key, prefController);
 
         ResultPayload payload = DatabaseIndexingUtils.getPayloadFromUriMap(map, key);
diff --git a/tests/robotests/src/com/android/settings/wifi/details/WifiDetailPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/wifi/details/WifiDetailPreferenceControllerTest.java
index efe0c1c..8d28dee 100644
--- a/tests/robotests/src/com/android/settings/wifi/details/WifiDetailPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/wifi/details/WifiDetailPreferenceControllerTest.java
@@ -54,11 +54,13 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Answers;
+import org.mockito.ArgumentCaptor;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
-import org.robolectric.annotation.Config;
 import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
 
+import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 
@@ -73,6 +75,7 @@
     private static final String SECURITY = "None";
 
     private InetAddress mIpv4Address;
+    private Inet6Address mIpv6Address;
 
     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     private PreferenceScreen mockScreen;
@@ -113,6 +116,12 @@
         try {
             mIpv4Address = InetAddress.getByAddress(
                     new byte[] { (byte) 255, (byte) 255, (byte) 255, (byte) 255 });
+            mIpv6Address = Inet6Address.getByAddress(
+                    "123", /* host */
+                    new byte[] {
+                            (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x11, 0x25,
+                            (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C, (byte) 0xB2},
+                    1  /*scope id */);
         } catch (UnknownHostException e) {
             throw new RuntimeException(e);
         }
@@ -153,6 +162,8 @@
     }
 
     private void setupMockedPreferenceScreen() {
+        when(mockScreen.getPreferenceManager().getContext()).thenReturn(mContext);
+
         when(mockScreen.findPreference(WifiDetailPreferenceController.KEY_CONNECTION_DETAIL_PREF))
                 .thenReturn(mockConnectionDetailPref);
         when(mockScreen.findPreference(WifiDetailPreferenceController.KEY_SIGNAL_STRENGTH_PREF))
@@ -420,4 +431,30 @@
 
         verify(mockActivity).finish();
     }
+
+    @Test
+    public void ipv6AddressPref_shouldHaveHostAddressTextSet() {
+        LinkAddress ipv6Address = new LinkAddress(mIpv6Address, 128);
+
+        mLinkProperties.addLinkAddress(ipv6Address);
+
+        mController.onResume();
+
+        ArgumentCaptor<Preference> preferenceCaptor = ArgumentCaptor.forClass(Preference.class);
+        verify(mockIpv6AddressCategory).addPreference(preferenceCaptor.capture());
+        assertThat(preferenceCaptor.getValue().getTitle()).isEqualTo(mIpv6Address.getHostAddress());
+    }
+
+    @Test
+    public void ipv6AddressPref_shouldNotBeSelectable() {
+        LinkAddress ipv6Address = new LinkAddress(mIpv6Address, 128);
+
+        mLinkProperties.addLinkAddress(ipv6Address);
+
+        mController.onResume();
+
+        ArgumentCaptor<Preference> preferenceCaptor = ArgumentCaptor.forClass(Preference.class);
+        verify(mockIpv6AddressCategory).addPreference(preferenceCaptor.capture());
+        assertThat(preferenceCaptor.getValue().isSelectable()).isFalse();
+    }
 }
diff --git a/tests/unit/src/com/android/settings/RegulatoryInfoDisplayActivityTest.java b/tests/unit/src/com/android/settings/RegulatoryInfoDisplayActivityTest.java
new file mode 100644
index 0000000..70087ae
--- /dev/null
+++ b/tests/unit/src/com/android/settings/RegulatoryInfoDisplayActivityTest.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;
+
+import static android.support.test.espresso.Espresso.onView;
+import static android.support.test.espresso.assertion.ViewAssertions.matches;
+import static android.support.test.espresso.matcher.RootMatchers.isDialog;
+import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
+import static android.support.test.espresso.matcher.ViewMatchers.withId;
+import static junit.framework.Assert.fail;
+
+import android.app.Instrumentation;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.ResolveInfo;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(AndroidJUnit4.class)
+@SmallTest
+public class RegulatoryInfoDisplayActivityTest {
+
+    private Instrumentation mInstrumentation;
+    private Intent mRegulatoryInfoIntent;
+
+    @Before
+    public void setUp() {
+        mInstrumentation = InstrumentationRegistry.getInstrumentation();
+        mRegulatoryInfoIntent = new Intent("android.settings.SHOW_REGULATORY_INFO")
+                .addCategory(Intent.CATEGORY_DEFAULT)
+                .setPackage(mInstrumentation.getTargetContext().getPackageName());
+    }
+
+    @Test
+    public void resolveRegulatoryInfoIntent_intentShouldMatchConfig() {
+        // Load intent from PackageManager and load config from Settings app
+        final Context context = mInstrumentation.getTargetContext();
+
+        final boolean hasRegulatoryInfo = context.getResources()
+                .getBoolean(R.bool.config_show_regulatory_info);
+        final ResolveInfo resolveInfo = mInstrumentation.getTargetContext().getPackageManager()
+                .resolveActivity(mRegulatoryInfoIntent, 0 /* flags */);
+
+        // Check config and intent both enable or both disabled.
+        if (hasRegulatoryInfo && resolveInfo == null) {
+            fail("Config enables regulatory info but there is no handling intent");
+            return;
+        }
+        if (!hasRegulatoryInfo && resolveInfo != null) {
+            fail("Config disables regulatory info but there is at least one handling intent");
+            return;
+        }
+    }
+
+    @Test
+    public void launchRegulatoryInfo_shouldNotCrash() {
+        final Context context = mInstrumentation.getTargetContext();
+        final boolean hasRegulatoryInfo = context.getResources()
+                .getBoolean(R.bool.config_show_regulatory_info);
+
+        if (!hasRegulatoryInfo) {
+            return;
+        }
+        // Launch intent
+        mInstrumentation.startActivitySync(mRegulatoryInfoIntent);
+
+        onView(withId(R.id.regulatoryInfo))
+                .inRoot(isDialog())
+                .check(matches(isDisplayed()));
+    }
+
+}