RoamingDialog stays open on rotation
Moved the dialog into a fragment instead of using
the AlertBuilder directly in the activity. Fixed jank that
would occur when the device was rotated and the toggle changed
state before actually changing the preference. Wrote integration
test to make sure dialog stays visible on device rotation. The
test is located in the tests folder for the project and can be
run in the same way that is already described in the manifest
file.
Test: Espresso Test
Bug: 32441370
Change-Id: I2880bb719f41f1765bf548e7a3ae363f7108a50d
diff --git a/tests/Android.mk b/tests/Android.mk
index 66df1a8..2feb55a 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -32,6 +32,8 @@
LOCAL_STATIC_JAVA_LIBRARIES := \
android-support-test \
mockito-target \
- legacy-android-test
-
+ legacy-android-test \
+ mockito-target-minus-junit4 \
+ espresso-core \
+ truth-prebuilt
include $(BUILD_PACKAGE)
diff --git a/tests/src/com/android/phone/RoamingDialogFragmentTest.java b/tests/src/com/android/phone/RoamingDialogFragmentTest.java
new file mode 100644
index 0000000..96a3fda
--- /dev/null
+++ b/tests/src/com/android/phone/RoamingDialogFragmentTest.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2016 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.phone;
+
+import android.app.Activity;
+import android.content.pm.ActivityInfo;
+import android.provider.Settings.Global;
+import android.provider.Settings.SettingNotFoundException;
+import android.support.test.espresso.matcher.PreferenceMatchers;
+import android.support.test.rule.ActivityTestRule;
+import com.google.common.truth.Truth;
+import junit.framework.AssertionFailedError;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import static android.support.test.espresso.Espresso.onData;
+import static android.support.test.espresso.Espresso.onView;
+import static android.support.test.espresso.action.ViewActions.click;
+import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
+import static android.support.test.espresso.assertion.ViewAssertions.matches;
+import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant;
+import static android.support.test.espresso.matcher.ViewMatchers.isChecked;
+import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
+import static android.support.test.espresso.matcher.ViewMatchers.withId;
+import static android.support.test.espresso.matcher.ViewMatchers.withText;
+import static com.google.common.truth.Truth.assertThat;
+
+/**
+ * Espresso tests to check some properties of the dialog that appears when a user
+ * tries to turn on data roaming.
+ */
+public class RoamingDialogFragmentTest {
+
+ @Rule
+ public ActivityTestRule<MobileNetworkSettings> mRule =
+ new ActivityTestRule<>(MobileNetworkSettings.class);
+ private Activity mActivity;
+
+ /**
+ * Make sure roaming is off before we start a test since this checks the dialog that only
+ * shows up when we try to turn it on.
+ */
+ @Before
+ public void disableRoaming() {
+ mActivity = mRule.getActivity();
+
+ // turn off data roaming if it is on
+ try {
+ onData(PreferenceMatchers.withTitle(R.string.roaming))
+ .check(matches(hasDescendant(isChecked())))
+ .perform(click());
+ } catch (AssertionFailedError e) {
+ // don't click the switch if it is already off.
+ }
+ }
+
+ @Test
+ public void dataRoamingDialogPersistsOnRotation() {
+ // click on the data roaming preference to trigger warning dialog
+ onData(PreferenceMatchers.withTitle(R.string.roaming)).perform(click());
+
+ // request both orientations to ensure at least one rotation occurs
+ mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
+ mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
+
+ // verify the title of the dialog is visible
+ onView(withText(R.string.roaming_alert_title)).check(matches(isDisplayed()));
+
+ }
+
+ @Test
+ public void dataRoamingEnabledWhenPositiveButtonClicked() throws SettingNotFoundException {
+ // click on the data roaming preference to trigger warning dialog
+ onData(PreferenceMatchers.withTitle(R.string.roaming)).perform(click());
+
+ // click to confirm we want to turn on data roaming
+ onView(withId(android.R.id.button1)).perform(click());
+
+ // verify that the the setting has actually been changed
+ assertThat(Global.getInt(mActivity.getApplicationContext().getContentResolver(),
+ Global.DATA_ROAMING)).isEqualTo(1);
+ }
+
+ @Test
+ public void dialogDismissedOnNegativeButtonClicked() {
+ // click on the data roaming preference to trigger warning dialog
+ onData(PreferenceMatchers.withTitle(R.string.roaming)).perform(click());
+
+ // click to cancel turning on data roaming
+ onView(withId(android.R.id.button2)).perform(click());
+
+ // verify the title of the dialog is gone
+ onView(withText(R.string.roaming_alert_title)).check(doesNotExist());
+ }
+
+ @Test
+ public void dataRoamingStaysDisabledWhenDialogCanceled() throws SettingNotFoundException {
+ // click on the data roaming preference to trigger warning dialog
+ onData(PreferenceMatchers.withTitle(R.string.roaming)).perform(click());
+
+ // click to cancel turning on data roaming
+ onView(withId(android.R.id.button2)).perform(click());
+
+ // verify that the the setting has not been changed
+ assertThat(Global.getInt(mActivity.getApplicationContext().getContentResolver(),
+ Global.DATA_ROAMING)).isEqualTo(0);
+
+ }
+}