1/*
2 * Copyright (C) 2006 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 android.app.activity;
18
19import android.app.Activity;
20import android.content.Context;
21import android.content.Intent;
22import android.test.AndroidTestCase;
23import android.test.PerformanceTestCase;
24
25public class ActivityTestsBase extends AndroidTestCase
26        implements PerformanceTestCase, LaunchpadActivity.CallingTest {
27    public static final String PERMISSION_GRANTED =
28            "com.android.frameworks.coretests.permission.TEST_GRANTED";
29    public static final String PERMISSION_DENIED =
30            "com.android.frameworks.coretests.permission.TEST_DENIED";
31
32    protected Intent mIntent;
33
34    private PerformanceTestCase.Intermediates mIntermediates;
35    private String mExpecting;
36
37    // Synchronization of activity result.
38    private boolean mFinished;
39    private int mResultCode = 0;
40    private Intent mData;
41    private RuntimeException mResultStack = null;
42
43    @Override
44    protected void setUp() throws Exception {
45        super.setUp();
46        mIntent = new Intent(mContext, LaunchpadActivity.class);
47        mIntermediates = null;
48    }
49
50    @Override
51    protected void tearDown() throws Exception {
52        mIntermediates = null;
53        super.tearDown();
54    }
55
56    public boolean isPerformanceOnly() {
57        return false;
58    }
59
60    public void setInternalIterations(int count) {
61    }
62
63    public void startTiming(boolean realTime) {
64        if (mIntermediates != null) {
65            mIntermediates.startTiming(realTime);
66        }
67    }
68
69    public void addIntermediate(String name) {
70        if (mIntermediates != null) {
71            mIntermediates.addIntermediate(name);
72        }
73    }
74
75    public void addIntermediate(String name, long timeInNS) {
76        if (mIntermediates != null) {
77            mIntermediates.addIntermediate(name, timeInNS);
78        }
79    }
80
81    public void finishTiming(boolean realTime) {
82        if (mIntermediates != null) {
83            mIntermediates.finishTiming(realTime);
84        }
85    }
86
87    public void activityFinished(int resultCode, Intent data, RuntimeException where) {
88        finishWithResult(resultCode, data, where);
89    }
90
91    public Intent editIntent() {
92        return mIntent;
93    }
94
95    public Context getContext() {
96        return mContext;
97    }
98
99    public int startPerformance(Intermediates intermediates) {
100        mIntermediates = intermediates;
101        return 1;
102    }
103
104    public void finishGood() {
105        finishWithResult(Activity.RESULT_OK, null);
106    }
107
108    public void finishBad(String error) {
109        finishWithResult(Activity.RESULT_CANCELED, (new Intent()).setAction(error));
110    }
111
112    public void finishWithResult(int resultCode, Intent data) {
113        RuntimeException where = new RuntimeException("Original error was here");
114        where.fillInStackTrace();
115        finishWithResult(resultCode, data, where);
116    }
117
118    public void finishWithResult(int resultCode, Intent data, RuntimeException where) {
119        synchronized (this) {
120            //System.out.println("*** Activity finished!!");
121            mResultCode = resultCode;
122            mData = data;
123            mResultStack = where;
124            mFinished = true;
125            notifyAll();
126        }
127    }
128
129    public int runLaunchpad(String action) {
130        LaunchpadActivity.setCallingTest(this);
131
132        synchronized (this) {
133            mIntent.setAction(action);
134            mFinished = false;
135            //System.out.println("*** Starting: " + mIntent);
136            mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
137            mContext.startActivity(mIntent);
138        }
139
140        return waitForResultOrThrow(60 * 1000);
141    }
142
143    public int waitForResultOrThrow(int timeoutMs) {
144        return waitForResultOrThrow(timeoutMs, null);
145    }
146
147    public int waitForResultOrThrow(int timeoutMs, String expected) {
148        int res = waitForResult(timeoutMs, expected);
149
150        if (res == Activity.RESULT_CANCELED) {
151            if (mResultStack != null) {
152                throw new RuntimeException(
153                        mData != null ? mData.toString() : "Unable to launch",
154                        mResultStack);
155            } else {
156                throw new RuntimeException(
157                        mData != null ? mData.toString() : "Unable to launch");
158            }
159        }
160        return res;
161    }
162
163    public int waitForResult(int timeoutMs, String expected) {
164        mExpecting = expected;
165
166        long endTime = System.currentTimeMillis() + timeoutMs;
167
168        boolean timeout = false;
169        synchronized (this) {
170            while (!mFinished) {
171                long delay = endTime - System.currentTimeMillis();
172                if (delay < 0) {
173                    timeout = true;
174                    break;
175                }
176
177                try {
178                    wait(delay);
179                } catch (java.lang.InterruptedException e) {
180                    // do nothing
181                }
182            }
183        }
184
185        mFinished = false;
186
187        if (timeout) {
188            mResultCode = Activity.RESULT_CANCELED;
189            onTimeout();
190        }
191        return mResultCode;
192    }
193
194    public int getResultCode() {
195        return mResultCode;
196    }
197
198    public Intent getResultData() {
199        return mData;
200    }
201
202    public RuntimeException getResultStack() {
203        return mResultStack;
204    }
205
206    public void onTimeout() {
207        String msg = mExpecting == null
208                ? "Timeout" : ("Timeout while expecting " + mExpecting);
209        finishWithResult(Activity.RESULT_CANCELED, (new Intent()).setAction(msg));
210    }
211}
212
213