content: /*
   * Copyright (C) 2014 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 androidx.test.ui.app;
  
  import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
  import static androidx.test.espresso.Espresso.onView;
  import static androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;
  import static androidx.test.espresso.Espresso.openContextualActionModeOverflowMenu;
  import static androidx.test.espresso.action.ViewActions.click;
  import static androidx.test.espresso.assertion.ViewAssertions.matches;
  import static androidx.test.espresso.matcher.ViewMatchers.withId;
  import static androidx.test.espresso.matcher.ViewMatchers.withText;
  
  import androidx.test.core.app.ActivityScenario;
  import androidx.test.ext.junit.runners.AndroidJUnit4;
  import androidx.test.filters.LargeTest;
  import org.junit.Before;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  
  /**
   * Demonstrates Espresso with action bar and contextual action mode. {@link
   * openActionBarOverflowOrOptionsMenu()} opens the overflow menu from an action bar. {@link
   * openContextualActionModeOverflowMenu()} opens the overflow menu from an contextual action mode.
   */
  @RunWith(AndroidJUnit4.class)
  @LargeTest
  public class ActionBarTest {
  
    @Before
    public void setUp() throws Exception {
      // Espresso will not launch our activity for us, we must launch it via ActivityScenario.launch.
      ActivityScenario.launch(ActionBarTestActivity.class);
    }
  
    @Test
    @SuppressWarnings("unchecked")
    public void testClickActionBarItem() {
      onView(withId(R.id.hide_contextual_action_bar))
        .perform(click());
  
      onView(withId(R.id.action_save))
        .perform(click());
  
      onView(withId(R.id.text_action_bar_result))
        .check(matches(withText("Save")));
    }
  
    @Test
    @SuppressWarnings("unchecked")
    public void testClickActionModeItem() {
      onView(withId(R.id.show_contextual_action_bar))
        .perform(click());
  
      onView((withId(R.id.action_lock)))
        .perform(click());
  
      onView(withId(R.id.text_action_bar_result))
        .check(matches(withText("Lock")));
    }
  
    @Test
    @SuppressWarnings("unchecked")
    public void testActionBarOverflow() {
      onView(withId(R.id.hide_contextual_action_bar))
        .perform(click());
  
      // Open the overflow menu from action bar
      openActionBarOverflowOrOptionsMenu(getApplicationContext());
  
      onView(withText("World"))
        .perform(click());
  
      onView(withId(R.id.text_action_bar_result))
        .check(matches(withText("World")));
    }
  
    @Test
    @SuppressWarnings("unchecked")
    public void testActionModeOverflow() {
      onView(withId(R.id.show_contextual_action_bar))
        .perform(click());
  
      // Open the overflow menu from contextual action mode.
      openContextualActionModeOverflowMenu();
  
      onView(withText("Key"))
        .perform(click());
  
      onView(withId(R.id.text_action_bar_result))
        .check(matches(withText("Key")));
    }
  }
  