1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.phone; 17 18 import static androidx.test.espresso.Espresso.onData; 19 import static androidx.test.espresso.Espresso.onView; 20 import static androidx.test.espresso.action.ViewActions.click; 21 import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist; 22 import static androidx.test.espresso.assertion.ViewAssertions.matches; 23 import static androidx.test.espresso.matcher.ViewMatchers.hasDescendant; 24 import static androidx.test.espresso.matcher.ViewMatchers.isChecked; 25 import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; 26 import static androidx.test.espresso.matcher.ViewMatchers.withId; 27 import static androidx.test.espresso.matcher.ViewMatchers.withText; 28 29 import static com.google.common.truth.Truth.assertThat; 30 31 import android.app.Activity; 32 import android.content.pm.ActivityInfo; 33 import android.provider.Settings.Global; 34 import android.provider.Settings.SettingNotFoundException; 35 36 import androidx.test.espresso.matcher.PreferenceMatchers; 37 import androidx.test.filters.FlakyTest; 38 import androidx.test.rule.ActivityTestRule; 39 40 import junit.framework.AssertionFailedError; 41 42 import org.junit.Before; 43 import org.junit.Rule; 44 import org.junit.Test; 45 46 /** 47 * Espresso tests to check some properties of the dialog that appears when a user 48 * tries to turn on data roaming. 49 */ 50 public class RoamingDialogFragmentTest { 51 52 @Rule 53 public ActivityTestRule<MobileNetworkSettings> mRule = 54 new ActivityTestRule<>(MobileNetworkSettings.class); 55 private Activity mActivity; 56 57 /** 58 * Make sure roaming is off before we start a test since this checks the dialog that only 59 * shows up when we try to turn it on. 60 */ 61 @Before disableRoaming()62 public void disableRoaming() { 63 mActivity = mRule.getActivity(); 64 65 // turn off data roaming if it is on 66 try { 67 onData(PreferenceMatchers.withTitle(R.string.roaming)) 68 .check(matches(hasDescendant(isChecked()))) 69 .perform(click()); 70 } catch (AssertionFailedError e) { 71 // don't click the switch if it is already off. 72 } 73 } 74 75 @FlakyTest 76 @Test dataRoamingDialogPersistsOnRotation()77 public void dataRoamingDialogPersistsOnRotation() { 78 // click on the data roaming preference to trigger warning dialog 79 onData(PreferenceMatchers.withTitle(R.string.roaming)).perform(click()); 80 81 // request both orientations to ensure at least one rotation occurs 82 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 83 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 84 85 // verify the title of the dialog is visible 86 onView(withText(R.string.roaming_alert_title)).check(matches(isDisplayed())); 87 88 } 89 90 @FlakyTest 91 @Test dataRoamingEnabledWhenPositiveButtonClicked()92 public void dataRoamingEnabledWhenPositiveButtonClicked() throws SettingNotFoundException { 93 // click on the data roaming preference to trigger warning dialog 94 onData(PreferenceMatchers.withTitle(R.string.roaming)).perform(click()); 95 96 // click to confirm we want to turn on data roaming 97 onView(withId(android.R.id.button1)).perform(click()); 98 99 // verify that the the setting has actually been changed 100 assertThat(Global.getInt(mActivity.getApplicationContext().getContentResolver(), 101 Global.DATA_ROAMING)).isEqualTo(1); 102 } 103 104 @FlakyTest 105 @Test dialogDismissedOnNegativeButtonClicked()106 public void dialogDismissedOnNegativeButtonClicked() { 107 // click on the data roaming preference to trigger warning dialog 108 onData(PreferenceMatchers.withTitle(R.string.roaming)).perform(click()); 109 110 // click to cancel turning on data roaming 111 onView(withId(android.R.id.button2)).perform(click()); 112 113 // verify the title of the dialog is gone 114 onView(withText(R.string.roaming_alert_title)).check(doesNotExist()); 115 } 116 117 @FlakyTest 118 @Test dataRoamingStaysDisabledWhenDialogCanceled()119 public void dataRoamingStaysDisabledWhenDialogCanceled() throws SettingNotFoundException { 120 // click on the data roaming preference to trigger warning dialog 121 onData(PreferenceMatchers.withTitle(R.string.roaming)).perform(click()); 122 123 // click to cancel turning on data roaming 124 onView(withId(android.R.id.button2)).perform(click()); 125 126 // verify that the the setting has not been changed 127 assertThat(Global.getInt(mActivity.getApplicationContext().getContentResolver(), 128 Global.DATA_ROAMING)).isEqualTo(0); 129 130 } 131 } 132