1 /* 2 * Copyright (C) 2016 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.google.android.mobly.snippet.example2; 18 19 import static androidx.test.espresso.Espresso.onView; 20 import static androidx.test.espresso.assertion.ViewAssertions.matches; 21 import static androidx.test.espresso.matcher.ViewMatchers.withId; 22 import static androidx.test.espresso.matcher.ViewMatchers.withText; 23 24 import androidx.test.espresso.action.ViewActions; 25 import androidx.test.rule.ActivityTestRule; 26 import com.google.android.mobly.snippet.Snippet; 27 import com.google.android.mobly.snippet.rpc.Rpc; 28 import org.junit.Rule; 29 30 public class EspressoSnippet implements Snippet { 31 @Rule 32 public ActivityTestRule<MainActivity> mActivityRule = 33 new ActivityTestRule<>(MainActivity.class); 34 35 @Rpc(description="Opens the main activity of the app") startMainActivity()36 public void startMainActivity() { 37 mActivityRule.launchActivity(null /* startIntent */); 38 } 39 40 @Rpc(description="Pushes the main app button, and checks the label if this is the first time.") pushMainButton(boolean checkFirstRun)41 public void pushMainButton(boolean checkFirstRun) { 42 if (checkFirstRun) { 43 onView(withId(R.id.main_text_view)).check(matches(withText("Hello World!"))); 44 } 45 onView(withId(R.id.main_button)).perform(ViewActions.click()); 46 if (checkFirstRun) { 47 onView(withId(R.id.main_text_view)).check(matches(withText("Button pressed 1 times."))); 48 } 49 } 50 51 @Override shutdown()52 public void shutdown() { 53 mActivityRule.getActivity().finish(); 54 } 55 } 56