Merge "Toggle switch back when user dismisses dialog by tapping screen" into udc-dev
diff --git a/src/com/android/settings/development/RebootConfirmationDialogFragment.java b/src/com/android/settings/development/RebootConfirmationDialogFragment.java
index 2fa0747..45c3697 100644
--- a/src/com/android/settings/development/RebootConfirmationDialogFragment.java
+++ b/src/com/android/settings/development/RebootConfirmationDialogFragment.java
@@ -28,11 +28,9 @@
 import com.android.settings.R;
 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
 
-/**
- * Dialog fragment for reboot confirmation when enabling certain features.
- */
+/** Dialog fragment for reboot confirmation when enabling certain features. */
 public class RebootConfirmationDialogFragment extends InstrumentedDialogFragment
-        implements DialogInterface.OnClickListener {
+        implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
 
     private static final String TAG = "FreeformPrefRebootDlg";
 
@@ -40,18 +38,17 @@
     private final int mCancelButtonId;
     private final RebootConfirmationDialogHost mHost;
 
-    /**
-     * Show an instance of this dialog.
-     */
+    /** Show an instance of this dialog. */
     public static void show(Fragment fragment, int messageId, RebootConfirmationDialogHost host) {
         show(fragment, messageId, R.string.reboot_dialog_reboot_later, host);
     }
 
-    /**
-     * Show an instance of this dialog with cancel button string set as cancelButtonId
-     */
-    public static void show(Fragment fragment, int messageId,
-            int cancelButtonId, RebootConfirmationDialogHost host) {
+    /** Show an instance of this dialog with cancel button string set as cancelButtonId */
+    public static void show(
+            Fragment fragment,
+            int messageId,
+            int cancelButtonId,
+            RebootConfirmationDialogHost host) {
         final FragmentManager manager = fragment.getActivity().getSupportFragmentManager();
         if (manager.findFragmentByTag(TAG) == null) {
             final RebootConfirmationDialogFragment dialog =
@@ -60,8 +57,8 @@
         }
     }
 
-    private RebootConfirmationDialogFragment(int messageId,
-            int cancelButtonId, RebootConfirmationDialogHost host) {
+    private RebootConfirmationDialogFragment(
+            int messageId, int cancelButtonId, RebootConfirmationDialogHost host) {
         mMessageId = messageId;
         mCancelButtonId = cancelButtonId;
         mHost = host;
@@ -89,4 +86,10 @@
             mHost.onRebootCancelled();
         }
     }
+
+    @Override
+    public void onDismiss(DialogInterface dialog) {
+        super.onDismiss(dialog);
+        mHost.onRebootDialogDismissed();
+    }
 }
diff --git a/src/com/android/settings/development/RebootConfirmationDialogHost.java b/src/com/android/settings/development/RebootConfirmationDialogHost.java
index 65ffbed..1be25a7 100644
--- a/src/com/android/settings/development/RebootConfirmationDialogHost.java
+++ b/src/com/android/settings/development/RebootConfirmationDialogHost.java
@@ -20,22 +20,20 @@
 import android.content.Intent;
 
 /**
- * Host of {@link RebootConfirmationDialogFragment} that provides callback when user
- * interacts with the UI.
+ * Host of {@link RebootConfirmationDialogFragment} that provides callback when user interacts with
+ * the UI.
  */
 public interface RebootConfirmationDialogHost {
-    /**
-     * Called when user made a decision to reboot the device.
-     */
+    /** Called when user made a decision to reboot the device. */
     default void onRebootConfirmed(Context context) {
         // user presses button "Reboot now", reboot the device
         final Intent intent = new Intent(Intent.ACTION_REBOOT);
         context.startActivity(intent);
     }
 
-    /**
-     * Called when user made a decision to cancel the reboot
-     * Default to do nothing
-     */
+    /** Called when user made a decision to cancel the reboot Default to do nothing */
     default void onRebootCancelled() {}
+
+    /** Called when reboot dialog is dismissed Default to do nothing */
+    default void onRebootDialogDismissed() {}
 }
diff --git a/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverController.java b/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverController.java
index 04252fa..1a065a9 100644
--- a/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverController.java
+++ b/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverController.java
@@ -17,6 +17,7 @@
 package com.android.settings.development.graphicsdriver;
 
 import android.content.Context;
+import android.content.Intent;
 import android.os.GraphicsEnvironment;
 import android.os.SystemProperties;
 import android.text.TextUtils;
@@ -33,9 +34,7 @@
 import com.android.settings.development.RebootConfirmationDialogHost;
 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
 
-/**
- * Controller to handle the events when user toggles this developer option switch: Enable ANGLE
- */
+/** Controller to handle the events when user toggles this developer option switch: Enable ANGLE */
 public class GraphicsDriverEnableAngleAsSystemDriverController
         extends DeveloperOptionsPreferenceController
         implements Preference.OnPreferenceChangeListener,
@@ -50,14 +49,15 @@
 
     private final GraphicsDriverSystemPropertiesWrapper mSystemProperties;
 
+    private boolean mShouldToggleSwitchBackOnRebootDialogDismiss;
+
     @VisibleForTesting
     static final String PROPERTY_RO_GFX_ANGLE_SUPPORTED = "ro.gfx.angle.supported";
 
     @VisibleForTesting
     static final String PROPERTY_PERSISTENT_GRAPHICS_EGL = "persist.graphics.egl";
 
-    @VisibleForTesting
-    static final String ANGLE_DRIVER_SUFFIX = "angle";
+    @VisibleForTesting static final String ANGLE_DRIVER_SUFFIX = "angle";
 
     @VisibleForTesting
     static class Injector {
@@ -87,6 +87,10 @@
         super(context);
         mFragment = fragment;
         mSystemProperties = injector.createSystemPropertiesWrapper();
+        // By default, when the reboot dialog is dismissed we want to toggle the switch back.
+        // Exception is when user chooses to reboot now, the switch should keep its current value
+        // and persist its' state over reboot.
+        mShouldToggleSwitchBackOnRebootDialogDismiss = true;
     }
 
     @Override
@@ -108,11 +112,12 @@
     @VisibleForTesting
     void showRebootDialog() {
         RebootConfirmationDialogFragment.show(
-                mFragment, R.string.reboot_dialog_enable_angle_as_system_driver,
-                R.string.cancel, this);
+                mFragment,
+                R.string.reboot_dialog_enable_angle_as_system_driver,
+                R.string.cancel,
+                this);
     }
 
-
     @Override
     public void updateState(Preference preference) {
         // set switch on if "persist.graphics.egl" is "angle" and angle is built in /vendor
@@ -120,8 +125,9 @@
         final String currentGlesDriver =
                 mSystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL, "");
         final boolean isAngle = TextUtils.equals(ANGLE_DRIVER_SUFFIX, currentGlesDriver);
-        final boolean isAngleSupported = TextUtils
-                .equals(mSystemProperties.get(PROPERTY_RO_GFX_ANGLE_SUPPORTED, ""), "true");
+        final boolean isAngleSupported =
+                TextUtils.equals(
+                        mSystemProperties.get(PROPERTY_RO_GFX_ANGLE_SUPPORTED, ""), "true");
         ((SwitchPreference) mPreference).setChecked(isAngle && isAngleSupported);
         ((SwitchPreference) mPreference).setEnabled(isAngleSupported);
     }
@@ -130,8 +136,9 @@
     protected void onDeveloperOptionsSwitchEnabled() {
         // only enable the switch if ro.gfx.angle.supported is true
         // we use ro.gfx.angle.supported to indicate if ANGLE libs are installed under /vendor
-        final boolean isAngleSupported = TextUtils
-                .equals(mSystemProperties.get(PROPERTY_RO_GFX_ANGLE_SUPPORTED, ""), "true");
+        final boolean isAngleSupported =
+                TextUtils.equals(
+                        mSystemProperties.get(PROPERTY_RO_GFX_ANGLE_SUPPORTED, ""), "true");
         ((SwitchPreference) mPreference).setEnabled(isAngleSupported);
     }
 
@@ -145,9 +152,7 @@
         ((SwitchPreference) mPreference).setEnabled(false);
     }
 
-    @Override
-    public void onRebootCancelled() {
-        // if user presses button "Cancel", do not reboot the device, and toggles switch back
+    void toggleSwitchBack() {
         final String currentGlesDriver =
                 mSystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL, "");
         if (TextUtils.equals(ANGLE_DRIVER_SUFFIX, currentGlesDriver)) {
@@ -169,4 +174,40 @@
         // if persist.graphics.egl holds values other than the above two, log error message
         Log.e(TAG, "Invalid persist.graphics.egl property value");
     }
+
+    @VisibleForTesting
+    void rebootDevice(Context context) {
+        final Intent intent = new Intent(Intent.ACTION_REBOOT);
+        context.startActivity(intent);
+    }
+
+    @Override
+    public void onRebootConfirmed(Context context) {
+        // User chooses to reboot now, do not toggle switch back
+        mShouldToggleSwitchBackOnRebootDialogDismiss = false;
+
+        // Reboot the device
+        rebootDevice(context);
+    }
+
+    @Override
+    public void onRebootCancelled() {
+        // User chooses to cancel reboot, toggle switch back
+        mShouldToggleSwitchBackOnRebootDialogDismiss = true;
+    }
+
+    @Override
+    public void onRebootDialogDismissed() {
+        // If reboot dialog is dismissed either from
+        // 1) User clicks cancel
+        // 2) User taps phone screen area outside of reboot dialog
+        // do not reboot the device, and toggles switch back.
+        if (mShouldToggleSwitchBackOnRebootDialogDismiss) {
+            toggleSwitchBack();
+        }
+
+        // Reset the flag so that the default option is to toggle switch back
+        // on reboot dialog dismissed.
+        mShouldToggleSwitchBackOnRebootDialogDismiss = true;
+    }
 }
diff --git a/tests/unit/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverControllerJUnitTest.java b/tests/unit/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverControllerJUnitTest.java
index 3f85535..c23292a 100644
--- a/tests/unit/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverControllerJUnitTest.java
+++ b/tests/unit/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverControllerJUnitTest.java
@@ -56,11 +56,39 @@
 
     private GraphicsDriverEnableAngleAsSystemDriverController mController;
 
-    @Mock
-    private DevelopmentSettingsDashboardFragment mFragment;
+    // Signal to wait for SystemProperty values changed
+    private class PropertyChangeSignal {
+        private CountDownLatch mCountDownLatch;
 
-    @Mock
-    private GraphicsDriverSystemPropertiesWrapper mSystemPropertiesMock;
+        private Runnable mCountDownJob;
+
+        PropertyChangeSignal() {
+            mCountDownLatch = new CountDownLatch(1);
+            mCountDownJob =
+                    new Runnable() {
+                        @Override
+                        public void run() {
+                            mCountDownLatch.countDown();
+                        }
+                    };
+        }
+
+        public Runnable getCountDownJob() {
+            return mCountDownJob;
+        }
+
+        public void wait(int timeoutInMilliSeconds) {
+            try {
+                mCountDownLatch.await(timeoutInMilliSeconds, TimeUnit.MILLISECONDS);
+            } catch (InterruptedException e) {
+                Assert.fail(e.getMessage());
+            }
+        }
+    }
+
+    @Mock private DevelopmentSettingsDashboardFragment mFragment;
+
+    @Mock private GraphicsDriverSystemPropertiesWrapper mSystemPropertiesMock;
 
     @Before
     public void setUp() {
@@ -76,18 +104,27 @@
         // so we can force the SystemProperties with values we need to run tests.
         // 2) Override the showRebootDialog() to do nothing.
         // We do not need to pop up the reboot dialog in the test.
+        // 3) Override the rebootDevice() to do nothing.
         mController = new GraphicsDriverEnableAngleAsSystemDriverController(
-            mContext, mFragment, new Injector(){
-                @Override
-                public GraphicsDriverSystemPropertiesWrapper createSystemPropertiesWrapper() {
-                    return mSystemPropertiesMock;
-                }
-            }) {
-                @Override
-                void showRebootDialog() {
-                    // do nothing
-                }
-        };
+                mContext,
+                mFragment,
+                new Injector() {
+                    @Override
+                    public GraphicsDriverSystemPropertiesWrapper
+                            createSystemPropertiesWrapper() {
+                            return mSystemPropertiesMock;
+                        }
+                    }) {
+                    @Override
+                    void showRebootDialog() {
+                        // do nothing
+                    }
+
+                    @Override
+                    void rebootDevice(Context context) {
+                        // do nothing
+                    }
+                };
 
         final PreferenceManager preferenceManager = new PreferenceManager(mContext);
         final PreferenceScreen screen = preferenceManager.createPreferenceScreen(mContext);
@@ -99,58 +136,42 @@
 
     @Test
     public void onPreferenceChange_switchOn_shouldEnableAngleAsSystemDriver() {
+        // Step 1: toggle the switch "Enable ANGLE" on
         // Add a callback when SystemProperty changes.
         // This allows the thread to wait until
         // GpuService::toggleAngleAsSystemDriver() updates the persist.graphics.egl.
-        final CountDownLatch countDownLatch = new CountDownLatch(1);
-        Runnable countDown = new Runnable() {
-            @Override
-            public void run() {
-                countDownLatch.countDown();
-            }
-        };
-        SystemProperties.addChangeCallback(countDown);
-
-        // Test onPreferenceChange(true) updates the persist.graphics.egl to "angle"
+        PropertyChangeSignal propertyChangeSignal = new PropertyChangeSignal();
+        SystemProperties.addChangeCallback(propertyChangeSignal.getCountDownJob());
         mController.onPreferenceChange(mPreference, true);
-        try {
-            countDownLatch.await(100, TimeUnit.MILLISECONDS);
-        } catch (InterruptedException e) {
-            Assert.fail(e.getMessage());
-        }
+        propertyChangeSignal.wait(100);
+
+        // Step 2: verify results
         final String systemEGLDriver = SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL);
         assertThat(systemEGLDriver).isEqualTo(ANGLE_DRIVER_SUFFIX);
 
+        // Step 3: clean up
         // Done with the test, remove the callback
-        SystemProperties.removeChangeCallback(countDown);
+        SystemProperties.removeChangeCallback(propertyChangeSignal.getCountDownJob());
     }
 
     @Test
     public void onPreferenceChange_switchOff_shouldDisableAngleAsSystemDriver() {
+        // Step 1: toggle the switch "Enable ANGLE" off
         // Add a callback when SystemProperty changes.
         // This allows the thread to wait until
         // GpuService::toggleAngleAsSystemDriver() updates the persist.graphics.egl.
-        final CountDownLatch countDownLatch = new CountDownLatch(1);
-        Runnable countDown = new Runnable() {
-            @Override
-            public void run() {
-                countDownLatch.countDown();
-            }
-        };
-        SystemProperties.addChangeCallback(countDown);
-
-        // Test onPreferenceChange(false) updates the persist.graphics.egl to ""
+        PropertyChangeSignal propertyChangeSignal = new PropertyChangeSignal();
+        SystemProperties.addChangeCallback(propertyChangeSignal.getCountDownJob());
         mController.onPreferenceChange(mPreference, false);
-        try {
-            countDownLatch.await(100, TimeUnit.MILLISECONDS);
-        } catch (InterruptedException e) {
-            Assert.fail(e.getMessage());
-        }
+        propertyChangeSignal.wait(100);
+
+        // Step 2: verify results
         final String systemEGLDriver = SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL);
         assertThat(systemEGLDriver).isEqualTo("");
 
+        // Step 3: clean up
         // Done with the test, remove the callback
-        SystemProperties.removeChangeCallback(countDown);
+        SystemProperties.removeChangeCallback(propertyChangeSignal.getCountDownJob());
     }
 
     @Test
@@ -162,8 +183,7 @@
 
     @Test
     public void updateState_angleNotSupported_PreferenceShouldNotBeChecked() {
-        when(mSystemPropertiesMock.get(eq(PROPERTY_RO_GFX_ANGLE_SUPPORTED), any()))
-                .thenReturn("");
+        when(mSystemPropertiesMock.get(eq(PROPERTY_RO_GFX_ANGLE_SUPPORTED), any())).thenReturn("");
         mController.updateState(mPreference);
         assertThat(mPreference.isChecked()).isFalse();
     }
@@ -191,8 +211,7 @@
             updateState_angleSupported_angleIsNotSystemGLESDriver_PreferenceShouldNotBeChecked() {
         when(mSystemPropertiesMock.get(eq(PROPERTY_RO_GFX_ANGLE_SUPPORTED), any()))
                 .thenReturn("true");
-        when(mSystemPropertiesMock.get(eq(PROPERTY_PERSISTENT_GRAPHICS_EGL), any()))
-                .thenReturn("");
+        when(mSystemPropertiesMock.get(eq(PROPERTY_PERSISTENT_GRAPHICS_EGL), any())).thenReturn("");
         mController.updateState(mPreference);
         assertThat(mPreference.isChecked()).isFalse();
     }
@@ -218,28 +237,18 @@
         // Add a callback when SystemProperty changes.
         // This allows the thread to wait until
         // GpuService::toggleAngleAsSystemDriver() updates the persist.graphics.egl.
-        final CountDownLatch countDownLatch = new CountDownLatch(1);
-        Runnable countDown = new Runnable() {
-            @Override
-            public void run() {
-                countDownLatch.countDown();
-            }
-        };
-        SystemProperties.addChangeCallback(countDown);
+        PropertyChangeSignal propertyChangeSignal1 = new PropertyChangeSignal();
+        SystemProperties.addChangeCallback(propertyChangeSignal1.getCountDownJob());
 
         // Test that onDeveloperOptionSwitchDisabled,
         // persist.graphics.egl updates to ""
         mController.onDeveloperOptionsSwitchDisabled();
-        try {
-            countDownLatch.await(100, TimeUnit.MILLISECONDS);
-        } catch (InterruptedException e) {
-            Assert.fail(e.getMessage());
-        }
+        propertyChangeSignal1.wait(100);
         final String systemEGLDriver = SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL);
         assertThat(systemEGLDriver).isEqualTo("");
 
         // Done with the test, remove the callback
-        SystemProperties.removeChangeCallback(countDown);
+        SystemProperties.removeChangeCallback(propertyChangeSignal1.getCountDownJob());
     }
 
     @Test
@@ -256,70 +265,217 @@
 
     @Test
     public void onRebootCancelled_ToggleSwitchFromOnToOff() {
+        // Step 1: Toggle the "Enable ANGLE" switch on
         // Add a callback when SystemProperty changes.
         // This allows the thread to wait until
         // GpuService::toggleAngleAsSystemDriver() updates the persist.graphics.egl.
-        final CountDownLatch countDownLatch = new CountDownLatch(1);
-        Runnable countDown = new Runnable() {
-            @Override
-            public void run() {
-                countDownLatch.countDown();
-            }
-        };
-        SystemProperties.addChangeCallback(countDown);
+        PropertyChangeSignal propertyChangeSignal1 = new PropertyChangeSignal();
+        SystemProperties.addChangeCallback(propertyChangeSignal1.getCountDownJob());
+        mController.onPreferenceChange(mPreference, true);
+        // Block the following code execution until the "persist.graphics.egl" property value is
+        // changed.
+        propertyChangeSignal1.wait(100);
 
-        // Test that if the current persist.graphics.egl is "angle",
-        // when reboot is cancelled, persist.graphics.egl is changed back to "",
-        // and switch is set to unchecked.
+        // Step 2: Cancel reboot
+        PropertyChangeSignal propertyChangeSignal2 = new PropertyChangeSignal();
+        SystemProperties.addChangeCallback(propertyChangeSignal2.getCountDownJob());
         when(mSystemPropertiesMock.get(eq(PROPERTY_PERSISTENT_GRAPHICS_EGL), any()))
-                .thenReturn(ANGLE_DRIVER_SUFFIX);
+                .thenReturn(SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL));
         mController.onRebootCancelled();
-        try {
-            countDownLatch.await(100, TimeUnit.MILLISECONDS);
-        } catch (InterruptedException e) {
-            Assert.fail(e.getMessage());
-        }
+        mController.onRebootDialogDismissed();
+        // Block the following code execution until the "persist.graphics.egl" property valye is
+        // changed.
+        propertyChangeSignal2.wait(100);
 
+        // Step 3: Verify results
+        // 1) Test that persist.graphics.egl is changed back to "".
         final String systemEGLDriver = SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL);
         assertThat(systemEGLDriver).isEqualTo("");
+        // 2) Test that the switch is set to unchecked.
         assertThat(mPreference.isChecked()).isFalse();
 
-        // Done with the test, remove the callback.
-        SystemProperties.removeChangeCallback(countDown);
+        // Step 4: Clean up
+        // Done with the test, remove the callback
+        SystemProperties.removeChangeCallback(propertyChangeSignal1.getCountDownJob());
+        SystemProperties.removeChangeCallback(propertyChangeSignal2.getCountDownJob());
     }
 
     @Test
     public void onRebootCancelled_ToggleSwitchFromOffToOn() {
+        // Step 1: Toggle off the switch "Enable ANGLE"
         // Add a callback when SystemProperty changes.
         // This allows the thread to wait until
         // GpuService::toggleAngleAsSystemDriver() updates the persist.graphics.egl.
-        final CountDownLatch countDownLatch = new CountDownLatch(1);
-        Runnable countDown = new Runnable() {
-            @Override
-            public void run() {
-                countDownLatch.countDown();
-            }
-        };
-        SystemProperties.addChangeCallback(countDown);
+        PropertyChangeSignal propertyChangeSignal1 = new PropertyChangeSignal();
+        SystemProperties.addChangeCallback(propertyChangeSignal1.getCountDownJob());
+        mController.onPreferenceChange(mPreference, false);
+        // Block the following code execution until the "persist.graphics.egl" property value is
+        // changed.
+        propertyChangeSignal1.wait(100);
 
-        // Test that if the current persist.graphics.egl is "",
-        // when reboot is cancelled, persist.graphics.egl is changed back to "angle",
-        // and switch is set to checked.
+        // Step 2: Cancel reboot
+        PropertyChangeSignal propertyChangeSignal2 = new PropertyChangeSignal();
+        SystemProperties.addChangeCallback(propertyChangeSignal2.getCountDownJob());
         when(mSystemPropertiesMock.get(eq(PROPERTY_PERSISTENT_GRAPHICS_EGL), any()))
-                .thenReturn("");
+                .thenReturn(SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL));
         mController.onRebootCancelled();
-        try {
-            countDownLatch.await(100, TimeUnit.MILLISECONDS);
-        } catch (InterruptedException e) {
-            Assert.fail(e.getMessage());
-        }
+        mController.onRebootDialogDismissed();
+        // Block the following code execution until the "persist.graphics.egl" property valye is
+        // changed.
+        propertyChangeSignal2.wait(100);
 
+        // Step 3: Verify results
+        // 1) Test that persist.graphics.egl is changed back to "ANGLE"
         final String systemEGLDriver = SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL);
         assertThat(systemEGLDriver).isEqualTo(ANGLE_DRIVER_SUFFIX);
+        // 2) Test that the switch is set to checked
         assertThat(mPreference.isChecked()).isTrue();
 
+        // Step 4: Clean up
         // Done with the test, remove the callback.
-        SystemProperties.removeChangeCallback(countDown);
+        SystemProperties.removeChangeCallback(propertyChangeSignal1.getCountDownJob());
+        SystemProperties.removeChangeCallback(propertyChangeSignal2.getCountDownJob());
     }
 
+    @Test
+    public void onRebootDialogDismissed_ToggleSwitchFromOnToOff() {
+        // Step 1: Toggle on the switch "Enable ANGLE"
+        // Add a callback when SystemProperty changes.
+        // This allows the thread to wait until
+        // GpuService::toggleAngleAsSystemDriver() updates the persist.graphics.egl.
+        PropertyChangeSignal propertyChangeSignal1 = new PropertyChangeSignal();
+        SystemProperties.addChangeCallback(propertyChangeSignal1.getCountDownJob());
+        mController.onPreferenceChange(mPreference, true);
+        // Block the following code execution until the "persist.graphics.egl" property value is
+        // changed.
+        propertyChangeSignal1.wait(100);
+
+        // Step 2: Dismiss the reboot dialog
+        PropertyChangeSignal propertyChangeSignal2 = new PropertyChangeSignal();
+        SystemProperties.addChangeCallback(propertyChangeSignal2.getCountDownJob());
+        when(mSystemPropertiesMock.get(eq(PROPERTY_PERSISTENT_GRAPHICS_EGL), any()))
+                .thenReturn(SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL));
+        mController.onRebootDialogDismissed();
+        // Block the following code execution until the "persist.graphics.egl" property valye is
+        // changed.
+        propertyChangeSignal2.wait(100);
+
+        // Step 3: Verify results
+        // 1) Test that persist.graphics.egl is changed back to "".
+        final String systemEGLDriver = SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL);
+        assertThat(systemEGLDriver).isEqualTo("");
+        // 2) Test that the switch is set to unchecked.
+        assertThat(mPreference.isChecked()).isFalse();
+
+        // Step 4: Clean up
+        // Done with the test, remove the callback
+        SystemProperties.removeChangeCallback(propertyChangeSignal1.getCountDownJob());
+        SystemProperties.removeChangeCallback(propertyChangeSignal2.getCountDownJob());
+    }
+
+    @Test
+    public void onRebootDialogDismissed_ToggleSwitchFromOffToOn() {
+        // Step 1: Toggle on the switch "Enable ANGLE"
+        // Add a callback when SystemProperty changes.
+        // This allows the thread to wait until
+        // GpuService::toggleAngleAsSystemDriver() updates the persist.graphics.egl.
+        PropertyChangeSignal propertyChangeSignal1 = new PropertyChangeSignal();
+        SystemProperties.addChangeCallback(propertyChangeSignal1.getCountDownJob());
+        mController.onPreferenceChange(mPreference, false);
+        // Block the following code execution until the "persist.graphics.egl" property value is
+        // changed.
+        propertyChangeSignal1.wait(100);
+
+        // Step 2: Dismiss the reboot dialog
+        PropertyChangeSignal propertyChangeSignal2 = new PropertyChangeSignal();
+        SystemProperties.addChangeCallback(propertyChangeSignal2.getCountDownJob());
+        when(mSystemPropertiesMock.get(eq(PROPERTY_PERSISTENT_GRAPHICS_EGL), any()))
+                .thenReturn(SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL));
+        mController.onRebootDialogDismissed();
+        // Block the following code execution until the "persist.graphics.egl" property valye is
+        // changed.
+        propertyChangeSignal2.wait(100);
+
+        // Step 3: Verify results
+        // 1) Test that persist.graphics.egl is changed back to "ANGLE"
+        final String systemEGLDriver = SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL);
+        assertThat(systemEGLDriver).isEqualTo(ANGLE_DRIVER_SUFFIX);
+        // 2) Test that the switch is set to checked
+        assertThat(mPreference.isChecked()).isTrue();
+
+        // Step 4: Clean up
+        // Done with the test, remove the callback
+        SystemProperties.removeChangeCallback(propertyChangeSignal1.getCountDownJob());
+        SystemProperties.removeChangeCallback(propertyChangeSignal2.getCountDownJob());
+    }
+
+    @Test
+    public void onRebootDialogConfirmed_ToggleSwitchOnRemainsOn() {
+        // Step 1: Toggle on the switch "Enable ANGLE"
+        // Add a callback when SystemProperty changes.
+        // This allows the thread to wait until
+        // GpuService::toggleAngleAsSystemDriver() updates the persist.graphics.egl.
+        PropertyChangeSignal propertyChangeSignal1 = new PropertyChangeSignal();
+        SystemProperties.addChangeCallback(propertyChangeSignal1.getCountDownJob());
+        mController.onPreferenceChange(mPreference, true);
+        // Block the following code execution until the "persist.graphics.egl" property value is
+        // changed.
+        propertyChangeSignal1.wait(100);
+
+        // Step 2: Confirm reboot
+        PropertyChangeSignal propertyChangeSignal2 = new PropertyChangeSignal();
+        SystemProperties.addChangeCallback(propertyChangeSignal2.getCountDownJob());
+        when(mSystemPropertiesMock.get(eq(PROPERTY_PERSISTENT_GRAPHICS_EGL), any()))
+                .thenReturn(SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL));
+        mController.onRebootConfirmed(mContext);
+        mController.onRebootDialogDismissed();
+        // Block the following code execution until the "persist.graphics.egl" property valye is
+        // changed.
+        propertyChangeSignal2.wait(100);
+
+        // Step 3: Verify Results
+        // Test that persist.graphics.egl remains to be "ANGLE"
+        final String systemEGLDriver = SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL);
+        assertThat(systemEGLDriver).isEqualTo(ANGLE_DRIVER_SUFFIX);
+
+        // Step 4: Clean up
+        // Done with the test, remove the callback
+        SystemProperties.removeChangeCallback(propertyChangeSignal1.getCountDownJob());
+        SystemProperties.removeChangeCallback(propertyChangeSignal2.getCountDownJob());
+    }
+
+    @Test
+    public void onRebootDialogConfirmed_ToggleSwitchOffRemainsOff() {
+        // Step 1: Toggle off the switch "Enable ANGLE"
+        // Add a callback when SystemProperty changes.
+        // This allows the thread to wait until
+        // GpuService::toggleAngleAsSystemDriver() updates the persist.graphics.egl.
+        PropertyChangeSignal propertyChangeSignal1 = new PropertyChangeSignal();
+        SystemProperties.addChangeCallback(propertyChangeSignal1.getCountDownJob());
+        mController.onPreferenceChange(mPreference, false);
+        // Block the following code execution until the "persist.graphics.egl" property value is
+        // changed.
+        propertyChangeSignal1.wait(100);
+
+        // Step 2: Confirm reboot
+        PropertyChangeSignal propertyChangeSignal2 = new PropertyChangeSignal();
+        SystemProperties.addChangeCallback(propertyChangeSignal2.getCountDownJob());
+        when(mSystemPropertiesMock.get(eq(PROPERTY_PERSISTENT_GRAPHICS_EGL), any()))
+                .thenReturn(SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL));
+        mController.onRebootConfirmed(mContext);
+        mController.onRebootDialogDismissed();
+        // Block the following code execution until the "persist.graphics.egl" property valye is
+        // changed.
+        propertyChangeSignal2.wait(100);
+
+        // Step 3: Verify Results
+        // Test that persist.graphics.egl remains to be ""
+        final String systemEGLDriver = SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL);
+        assertThat(systemEGLDriver).isEqualTo("");
+
+        // Step 4: Clean up
+        // Done with the test, remove the callback
+        SystemProperties.removeChangeCallback(propertyChangeSignal1.getCountDownJob());
+        SystemProperties.removeChangeCallback(propertyChangeSignal2.getCountDownJob());
+    }
 }