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 android.app.Activity; 19 import android.content.pm.ActivityInfo; 20 import android.provider.Settings.Global; 21 import android.provider.Settings.SettingNotFoundException; 22 import android.support.test.espresso.matcher.PreferenceMatchers; 23 import android.support.test.rule.ActivityTestRule; 24 import android.support.test.filters.FlakyTest; 25 import com.google.common.truth.Truth; 26 import junit.framework.AssertionFailedError; 27 import org.junit.Before; 28 import org.junit.Rule; 29 import org.junit.Test; 30 31 import static android.support.test.espresso.Espresso.onData; 32 import static android.support.test.espresso.Espresso.onView; 33 import static android.support.test.espresso.action.ViewActions.click; 34 import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist; 35 import static android.support.test.espresso.assertion.ViewAssertions.matches; 36 import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant; 37 import static android.support.test.espresso.matcher.ViewMatchers.isChecked; 38 import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 39 import static android.support.test.espresso.matcher.ViewMatchers.withId; 40 import static android.support.test.espresso.matcher.ViewMatchers.withText; 41 import static com.google.common.truth.Truth.assertThat; 42 43 /** 44 * Espresso tests to check some properties of the dialog that appears when a user 45 * tries to turn on data roaming. 46 */ 47 public class RoamingDialogFragmentTest { 48 49 @Rule 50 public ActivityTestRule<MobileNetworkSettings> mRule = 51 new ActivityTestRule<>(MobileNetworkSettings.class); 52 private Activity mActivity; 53 54 /** 55 * Make sure roaming is off before we start a test since this checks the dialog that only 56 * shows up when we try to turn it on. 57 */ 58 @Before disableRoaming()59 public void disableRoaming() { 60 mActivity = mRule.getActivity(); 61 62 // turn off data roaming if it is on 63 try { 64 onData(PreferenceMatchers.withTitle(R.string.roaming)) 65 .check(matches(hasDescendant(isChecked()))) 66 .perform(click()); 67 } catch (AssertionFailedError e) { 68 // don't click the switch if it is already off. 69 } 70 } 71 72 @FlakyTest 73 @Test dataRoamingDialogPersistsOnRotation()74 public void dataRoamingDialogPersistsOnRotation() { 75 // click on the data roaming preference to trigger warning dialog 76 onData(PreferenceMatchers.withTitle(R.string.roaming)).perform(click()); 77 78 // request both orientations to ensure at least one rotation occurs 79 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 80 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 81 82 // verify the title of the dialog is visible 83 onView(withText(R.string.roaming_alert_title)).check(matches(isDisplayed())); 84 85 } 86 87 @FlakyTest 88 @Test dataRoamingEnabledWhenPositiveButtonClicked()89 public void dataRoamingEnabledWhenPositiveButtonClicked() throws SettingNotFoundException { 90 // click on the data roaming preference to trigger warning dialog 91 onData(PreferenceMatchers.withTitle(R.string.roaming)).perform(click()); 92 93 // click to confirm we want to turn on data roaming 94 onView(withId(android.R.id.button1)).perform(click()); 95 96 // verify that the the setting has actually been changed 97 assertThat(Global.getInt(mActivity.getApplicationContext().getContentResolver(), 98 Global.DATA_ROAMING)).isEqualTo(1); 99 } 100 101 @FlakyTest 102 @Test dialogDismissedOnNegativeButtonClicked()103 public void dialogDismissedOnNegativeButtonClicked() { 104 // click on the data roaming preference to trigger warning dialog 105 onData(PreferenceMatchers.withTitle(R.string.roaming)).perform(click()); 106 107 // click to cancel turning on data roaming 108 onView(withId(android.R.id.button2)).perform(click()); 109 110 // verify the title of the dialog is gone 111 onView(withText(R.string.roaming_alert_title)).check(doesNotExist()); 112 } 113 114 @FlakyTest 115 @Test dataRoamingStaysDisabledWhenDialogCanceled()116 public void dataRoamingStaysDisabledWhenDialogCanceled() throws SettingNotFoundException { 117 // click on the data roaming preference to trigger warning dialog 118 onData(PreferenceMatchers.withTitle(R.string.roaming)).perform(click()); 119 120 // click to cancel turning on data roaming 121 onView(withId(android.R.id.button2)).perform(click()); 122 123 // verify that the the setting has not been changed 124 assertThat(Global.getInt(mActivity.getApplicationContext().getContentResolver(), 125 Global.DATA_ROAMING)).isEqualTo(0); 126 127 } 128 } 129