SettingsDrawerActivityTest.java revision f1c1bb49f8d9d57b0138dbd4358e32f9ecff87de
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 17package com.android.settingslib.drawer; 18 19import android.app.Activity; 20import android.app.Instrumentation; 21import android.content.Intent; 22import android.support.test.InstrumentationRegistry; 23import android.support.test.filters.SmallTest; 24import android.support.test.rule.ActivityTestRule; 25import android.support.test.runner.AndroidJUnit4; 26 27import com.android.settingslib.R; 28 29import com.google.common.truth.Truth; 30 31import org.junit.Before; 32import org.junit.Rule; 33import org.junit.Test; 34import org.junit.runner.RunWith; 35import org.mockito.MockitoAnnotations; 36 37import static android.support.test.espresso.Espresso.onView; 38import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist; 39import static android.support.test.espresso.assertion.ViewAssertions.matches; 40import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 41import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription; 42import static com.google.common.truth.Truth.assertThat; 43 44@RunWith(AndroidJUnit4.class) 45@SmallTest 46public class SettingsDrawerActivityTest { 47 48 @Rule 49 public ActivityTestRule<TestActivity> mActivityRule = 50 new ActivityTestRule<>(TestActivity.class, true, true); 51 52 @Before 53 public void setUp() throws Exception { 54 MockitoAnnotations.initMocks(this); 55 } 56 57 @Test 58 public void startActivityWithNoExtra_showNoNavUp() { 59 Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); 60 instrumentation.startActivitySync(new Intent(instrumentation.getTargetContext(), 61 TestActivity.class)); 62 63 onView(withContentDescription(com.android.internal.R.string.action_bar_up_description)) 64 .check(doesNotExist()); 65 } 66 67 @Test 68 public void startActivityWithExtraToHideMenu_showNavUp() { 69 Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); 70 Intent intent = new Intent(instrumentation.getTargetContext(), TestActivity.class) 71 .putExtra(TestActivity.EXTRA_SHOW_MENU, false); 72 instrumentation.startActivitySync(intent); 73 74 onView(withContentDescription(com.android.internal.R.string.action_bar_up_description)) 75 .check(doesNotExist()); 76 } 77 78 @Test 79 public void startActivityWithExtraToShowMenu_showNavUp() { 80 Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); 81 Intent intent = new Intent(instrumentation.getTargetContext(), TestActivity.class) 82 .putExtra(TestActivity.EXTRA_SHOW_MENU, true); 83 instrumentation.startActivitySync(intent); 84 85 onView(withContentDescription(com.android.internal.R.string.action_bar_up_description)) 86 .check(matches(isDisplayed())); 87 } 88 89 /** 90 * Test Activity in this test. 91 * 92 * Use this activity because SettingsDrawerActivity hasn't been registered in its 93 * AndroidManifest.xml 94 */ 95 public static class TestActivity extends SettingsDrawerActivity {} 96} 97