1/* 2 * Copyright (C) 2014 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.google.android.apps.common.testing.ui.espresso; 18 19import static com.google.android.apps.common.testing.ui.espresso.Espresso.onData; 20import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView; 21import static com.google.android.apps.common.testing.ui.espresso.Espresso.openActionBarOverflowOrOptionsMenu; 22import static com.google.android.apps.common.testing.ui.espresso.Espresso.openContextualActionModeOverflowMenu; 23import static com.google.android.apps.common.testing.ui.espresso.action.ViewActions.click; 24import static com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions.matches; 25import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withId; 26import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withText; 27import static org.hamcrest.Matchers.allOf; 28import static org.hamcrest.Matchers.anything; 29import static org.hamcrest.Matchers.hasValue; 30import static org.hamcrest.Matchers.instanceOf; 31 32import com.google.android.apps.common.testing.ui.espresso.action.ViewActions; 33import com.google.android.apps.common.testing.ui.testapp.ActionBarTestActivity; 34import com.google.android.apps.common.testing.ui.testapp.MainActivity; 35import com.google.android.apps.common.testing.ui.testapp.R; 36import com.google.android.apps.common.testing.ui.testapp.SendActivity; 37 38import android.content.Context; 39import android.test.ActivityInstrumentationTestCase2; 40import android.test.suitebuilder.annotation.LargeTest; 41import android.view.View; 42import android.view.inputmethod.InputMethodManager; 43 44import org.hamcrest.Matcher; 45 46import java.util.Map; 47import java.util.concurrent.atomic.AtomicBoolean; 48 49/** 50 * Tests Espresso top level (i.e. ones not specific to a view) actions like pressBack and 51 * closeSoftKeyboard. 52 */ 53@LargeTest 54public class EspressoTest extends ActivityInstrumentationTestCase2<MainActivity> { 55 @SuppressWarnings("deprecation") 56 public EspressoTest() { 57 // Supporting froyo. 58 super("com.google.android.apps.common.testing.ui.testapp", MainActivity.class); 59 } 60 61 @Override 62 public void setUp() throws Exception { 63 super.setUp(); 64 getActivity(); 65 } 66 67 @SuppressWarnings("unchecked") 68 public void testOpenOverflowInActionMode() { 69 onData(allOf(instanceOf(Map.class), hasValue(ActionBarTestActivity.class.getSimpleName()))) 70 .perform(click()); 71 openContextualActionModeOverflowMenu(); 72 onView(withText("Key")) 73 .perform(click()); 74 onView(withId(R.id.text_action_bar_result)) 75 .check(matches(withText("Key"))); 76 } 77 78 @SuppressWarnings("unchecked") 79 public void testOpenOverflowFromActionBar() { 80 onData(allOf(instanceOf(Map.class), hasValue(ActionBarTestActivity.class.getSimpleName()))) 81 .perform(click()); 82 onView(withId(R.id.hide_contextual_action_bar)) 83 .perform(click()); 84 openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext()); 85 onView(withText("World")) 86 .perform(click()); 87 onView(withId(R.id.text_action_bar_result)) 88 .check(matches(withText("World"))); 89 } 90 91 @SuppressWarnings("unchecked") 92 public void testCloseSoftKeyboard() { 93 onData(allOf(instanceOf(Map.class), hasValue(SendActivity.class.getSimpleName()))) 94 .perform(click()); 95 96 onView(withId(R.id.enter_data_edit_text)).perform(new ViewAction() { 97 @Override 98 public Matcher<View> getConstraints() { 99 return anything(); 100 } 101 102 @Override 103 public void perform(UiController uiController, View view) { 104 InputMethodManager imm = (InputMethodManager) getInstrumentation().getTargetContext() 105 .getSystemService(Context.INPUT_METHOD_SERVICE); 106 imm.showSoftInput(view, 0); 107 uiController.loopMainThreadUntilIdle(); 108 } 109 110 @Override 111 public String getDescription() { 112 return "show soft input"; 113 } 114 }); 115 116 onView(withId(R.id.enter_data_edit_text)).perform(ViewActions.closeSoftKeyboard()); 117 } 118 119 public void testSetFailureHandler() { 120 final AtomicBoolean handled = new AtomicBoolean(false); 121 Espresso.setFailureHandler(new FailureHandler() { 122 @Override 123 public void handle(Throwable error, Matcher<View> viewMatcher) { 124 handled.set(true); 125 } 126 }); 127 onView(withText("does not exist")).perform(click()); 128 assertTrue(handled.get()); 129 } 130 131 public void testRegisterResourceWithNullName() { 132 try { 133 Espresso.registerIdlingResources(new IdlingResource() { 134 @Override 135 public boolean isIdleNow() { 136 return true; 137 } 138 139 @Override 140 public String getName() { 141 return null; 142 } 143 144 @Override 145 public void registerIdleTransitionCallback(ResourceCallback callback) { 146 // ignore 147 } 148 }); 149 fail("Should have thrown NPE"); 150 } catch (NullPointerException expected) {} 151 } 152} 153