Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2011 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 
     17 package com.android.contacts.util;
     18 
     19 import static android.os.PowerManager.ACQUIRE_CAUSES_WAKEUP;
     20 import static android.os.PowerManager.FULL_WAKE_LOCK;
     21 import static android.os.PowerManager.ON_AFTER_RELEASE;
     22 
     23 import com.google.common.base.Preconditions;
     24 
     25 import android.app.Activity;
     26 import android.app.Instrumentation;
     27 import android.content.Context;
     28 import android.os.PowerManager;
     29 import android.view.View;
     30 import android.view.ViewGroup;
     31 import android.widget.TextView;
     32 
     33 import junit.framework.Assert;
     34 
     35 import java.util.ArrayList;
     36 import java.util.List;
     37 import java.util.concurrent.Callable;
     38 import java.util.concurrent.ExecutionException;
     39 import java.util.concurrent.FutureTask;
     40 
     41 import javax.annotation.concurrent.GuardedBy;
     42 import javax.annotation.concurrent.ThreadSafe;
     43 
     44 /** Some utility methods for making integration testing smoother. */
     45 @ThreadSafe
     46 public class IntegrationTestUtils {
     47     private static final String TAG = "IntegrationTestUtils";
     48 
     49     private final Instrumentation mInstrumentation;
     50     private final Object mLock = new Object();
     51     @GuardedBy("mLock") private PowerManager.WakeLock mWakeLock;
     52 
     53     public IntegrationTestUtils(Instrumentation instrumentation) {
     54         mInstrumentation = instrumentation;
     55     }
     56 
     57     /**
     58      * Find a view by a given resource id, from the given activity, and click it, iff it is
     59      * enabled according to {@link View#isEnabled()}.
     60      */
     61     public void clickButton(final Activity activity, final int buttonResourceId) throws Throwable {
     62         runOnUiThreadAndGetTheResult(new Callable<Void>() {
     63             @Override
     64             public Void call() throws Exception {
     65                 View view = activity.findViewById(buttonResourceId);
     66                 Assert.assertNotNull(view);
     67                 if (view.isEnabled()) {
     68                     view.performClick();
     69                 }
     70                 return null;
     71             }
     72         });
     73     }
     74 
     75     /** Returns the result of running {@link TextView#getText()} on the ui thread. */
     76     public CharSequence getText(final TextView view) throws Throwable {
     77         return runOnUiThreadAndGetTheResult(new Callable<CharSequence>() {
     78             @Override
     79             public CharSequence call() {
     80                 return view.getText();
     81             }
     82         });
     83     }
     84 
     85     // TODO: Move this class and the appropriate documentation into a test library, having checked
     86     // first to see if exactly this code already exists or not.
     87     /**
     88      * Execute a callable on the ui thread, returning its result synchronously.
     89      * <p>
     90      * Waits for an idle sync on the main thread (see {@link Instrumentation#waitForIdle(Runnable)})
     91      * before executing this callable.
     92      */
     93     public <T> T runOnUiThreadAndGetTheResult(Callable<T> callable) throws Throwable {
     94         FutureTask<T> future = new FutureTask<T>(callable);
     95         mInstrumentation.waitForIdle(future);
     96         try {
     97             return future.get();
     98         } catch (ExecutionException e) {
     99             // Unwrap the cause of the exception and re-throw it.
    100             throw e.getCause();
    101         }
    102     }
    103 
    104     /**
    105      * Wake up the screen, useful in tests that want or need the screen to be on.
    106      * <p>
    107      * This is usually called from setUp() for tests that require it.  After calling this method,
    108      * {@link #releaseScreenWakeLock()} must be called, this is usually done from tearDown().
    109      */
    110     public void acquireScreenWakeLock(Context context) {
    111         synchronized (mLock) {
    112             Preconditions.checkState(mWakeLock == null, "mWakeLock was already held");
    113             mWakeLock = ((PowerManager) context.getSystemService(Context.POWER_SERVICE))
    114                     .newWakeLock(ACQUIRE_CAUSES_WAKEUP | ON_AFTER_RELEASE | FULL_WAKE_LOCK, TAG);
    115             mWakeLock.acquire();
    116         }
    117     }
    118 
    119     /** Release the wake lock previously acquired with {@link #acquireScreenWakeLock(Context)}. */
    120     public void releaseScreenWakeLock() {
    121         synchronized (mLock) {
    122             // We don't use Preconditions to force you to have acquired before release.
    123             // This is because we don't want unnecessary exceptions in tearDown() since they'll
    124             // typically mask the actual exception that happened during the test.
    125             // The other reason is that this method is most likely to be called from tearDown(),
    126             // which is invoked within a finally block, so it's not infrequently the case that
    127             // the setUp() method fails before getting the lock, at which point we don't want
    128             // to fail in tearDown().
    129             if (mWakeLock != null) {
    130                 mWakeLock.release();
    131                 mWakeLock = null;
    132             }
    133         }
    134     }
    135 
    136     /**
    137      * Gets all {@link TextView} objects whose {@link TextView#getText()} contains the given text as
    138      * a substring.
    139      */
    140     public List<TextView> getTextViewsWithString(final Activity activity, final String text)
    141             throws Throwable {
    142         return runOnUiThreadAndGetTheResult(new Callable<List<TextView>>() {
    143             @Override
    144             public List<TextView> call() throws Exception {
    145                 List<TextView> matchingViews = new ArrayList<TextView>();
    146                 for (TextView textView : getAllViews(TextView.class, getRootView(activity))) {
    147                     if (textView.getText().toString().contains(text)) {
    148                         matchingViews.add(textView);
    149                     }
    150                 }
    151                 return matchingViews;
    152             }
    153         });
    154     }
    155 
    156     /** Find the root view for a given activity. */
    157     public static View getRootView(Activity activity) {
    158         return activity.findViewById(android.R.id.content).getRootView();
    159     }
    160 
    161     /**
    162      * Gets a list of all views of a given type, rooted at the given parent.
    163      * <p>
    164      * This method will recurse down through all {@link ViewGroup} instances looking for
    165      * {@link View} instances of the supplied class type. Specifically it will use the
    166      * {@link Class#isAssignableFrom(Class)} method as the test for which views to add to the list,
    167      * so if you provide {@code View.class} as your type, you will get every view. The parent itself
    168      * will be included also, should it be of the right type.
    169      * <p>
    170      * This call manipulates the ui, and as such should only be called from the application's main
    171      * thread.
    172      */
    173     private static <T extends View> List<T> getAllViews(final Class<T> clazz, final View parent) {
    174         List<T> results = new ArrayList<T>();
    175         if (parent.getClass().equals(clazz)) {
    176             results.add(clazz.cast(parent));
    177         }
    178         if (parent instanceof ViewGroup) {
    179             ViewGroup viewGroup = (ViewGroup) parent;
    180             for (int i = 0; i < viewGroup.getChildCount(); ++i) {
    181                 results.addAll(getAllViews(clazz, viewGroup.getChildAt(i)));
    182             }
    183         }
    184         return results;
    185     }
    186 }
    187