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
17package com.android.contacts.common.test;
18
19import static android.os.PowerManager.ACQUIRE_CAUSES_WAKEUP;
20import static android.os.PowerManager.FULL_WAKE_LOCK;
21import static android.os.PowerManager.ON_AFTER_RELEASE;
22
23import android.app.Activity;
24import android.app.Instrumentation;
25import android.content.Context;
26import android.os.PowerManager;
27import android.view.View;
28import android.view.ViewGroup;
29import android.widget.TextView;
30
31import com.google.common.base.Preconditions;
32
33import junit.framework.Assert;
34
35import java.util.ArrayList;
36import java.util.List;
37import java.util.concurrent.Callable;
38import java.util.concurrent.ExecutionException;
39import java.util.concurrent.FutureTask;
40
41import javax.annotation.concurrent.GuardedBy;
42import javax.annotation.concurrent.ThreadSafe;
43
44/** Some utility methods for making integration testing smoother. */
45@ThreadSafe
46public 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(
115                            PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE | PowerManager.FULL_WAKE_LOCK, TAG);
116            mWakeLock.acquire();
117        }
118    }
119
120    /** Release the wake lock previously acquired with {@link #acquireScreenWakeLock(Context)}. */
121    public void releaseScreenWakeLock() {
122        synchronized (mLock) {
123            // We don't use Preconditions to force you to have acquired before release.
124            // This is because we don't want unnecessary exceptions in tearDown() since they'll
125            // typically mask the actual exception that happened during the test.
126            // The other reason is that this method is most likely to be called from tearDown(),
127            // which is invoked within a finally block, so it's not infrequently the case that
128            // the setUp() method fails before getting the lock, at which point we don't want
129            // to fail in tearDown().
130            if (mWakeLock != null) {
131                mWakeLock.release();
132                mWakeLock = null;
133            }
134        }
135    }
136
137    /**
138     * Gets all {@link TextView} objects whose {@link TextView#getText()} contains the given text as
139     * a substring.
140     */
141    public List<TextView> getTextViewsWithString(final Activity activity, final String text)
142            throws Throwable {
143        return getTextViewsWithString(getRootView(activity), text);
144    }
145
146    /**
147     * Gets all {@link TextView} objects whose {@link TextView#getText()} contains the given text as
148     * a substring for the given root view.
149     */
150    public List<TextView> getTextViewsWithString(final View rootView, final String text)
151            throws Throwable {
152        return runOnUiThreadAndGetTheResult(new Callable<List<TextView>>() {
153            @Override
154            public List<TextView> call() throws Exception {
155                List<TextView> matchingViews = new ArrayList<TextView>();
156                for (TextView textView : getAllViews(TextView.class, rootView)) {
157                    if (textView.getText().toString().contains(text)) {
158                        matchingViews.add(textView);
159                    }
160                }
161                return matchingViews;
162            }
163        });
164    }
165
166    /** Find the root view for a given activity. */
167    public static View getRootView(Activity activity) {
168        return activity.findViewById(android.R.id.content).getRootView();
169    }
170
171    /**
172     * Gets a list of all views of a given type, rooted at the given parent.
173     * <p>
174     * This method will recurse down through all {@link ViewGroup} instances looking for
175     * {@link View} instances of the supplied class type. Specifically it will use the
176     * {@link Class#isAssignableFrom(Class)} method as the test for which views to add to the list,
177     * so if you provide {@code View.class} as your type, you will get every view. The parent itself
178     * will be included also, should it be of the right type.
179     * <p>
180     * This call manipulates the ui, and as such should only be called from the application's main
181     * thread.
182     */
183    private static <T extends View> List<T> getAllViews(final Class<T> clazz, final View parent) {
184        List<T> results = new ArrayList<T>();
185        if (parent.getClass().equals(clazz)) {
186            results.add(clazz.cast(parent));
187        }
188        if (parent instanceof ViewGroup) {
189            ViewGroup viewGroup = (ViewGroup) parent;
190            for (int i = 0; i < viewGroup.getChildCount(); ++i) {
191                results.addAll(getAllViews(clazz, viewGroup.getChildAt(i)));
192            }
193        }
194        return results;
195    }
196}
197