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