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.onView; 20import static com.google.android.apps.common.testing.ui.espresso.action.ViewActions.click; 21import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withId; 22 23import com.google.android.apps.common.testing.ui.testapp.R; 24import com.google.android.apps.common.testing.ui.testapp.SyncActivity; 25 26import android.os.Handler; 27import android.os.Looper; 28import android.test.ActivityInstrumentationTestCase2; 29import android.test.suitebuilder.annotation.LargeTest; 30 31import java.util.concurrent.FutureTask; 32import java.util.concurrent.atomic.AtomicBoolean; 33 34/** 35 * Test case for {@link AppNotIdleException}. 36 */ 37@LargeTest 38public class AppNotIdleExceptionTest extends ActivityInstrumentationTestCase2<SyncActivity> { 39 40 @SuppressWarnings("deprecation") 41 public AppNotIdleExceptionTest() { 42 // This constructor was deprecated - but we want to support lower API levels. 43 super("com.google.android.apps.common.testing.ui.testapp", SyncActivity.class); 44 } 45 46 @Override 47 public void setUp() throws Exception { 48 super.setUp(); 49 getActivity(); 50 } 51 52 public void testAppIdleException() throws Exception { 53 final AtomicBoolean continueBeingBusy = new AtomicBoolean(true); 54 try { 55 final Handler handler = new Handler(Looper.getMainLooper()); 56 Runnable runnable = new Runnable() { 57 @Override 58 public void run() { 59 if (!continueBeingBusy.get()) { 60 return; 61 } else { 62 handler.post(this); 63 } 64 } 65 }; 66 FutureTask<Void> task = new FutureTask<Void>(runnable, null); 67 handler.post(task); 68 task.get(); // Will Make sure that the first post is sent before we do a lookup. 69 // Request the "hello world!" text by clicking on the request button. 70 onView(withId(R.id.request_button)).perform(click()); 71 fail("Espresso failed to throw AppNotIdleException"); 72 } catch (AppNotIdleException e) { 73 // Do Nothing. Test pass. 74 continueBeingBusy.getAndSet(false); 75 } 76 } 77} 78