1/*
2 * Copyright (C) 2010 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.dumprendertree2.scriptsupport;
18
19import android.content.Intent;
20import android.test.ActivityInstrumentationTestCase2;
21import android.util.Log;
22
23import com.android.dumprendertree2.TestsListActivity;
24import com.android.dumprendertree2.forwarder.ForwarderManager;
25
26/**
27 * A class which provides methods that can be invoked by a script running on the host machine to
28 * run the tests.
29 *
30 * It starts a TestsListActivity and does not return until all the tests finish executing.
31 */
32public class Starter extends ActivityInstrumentationTestCase2<TestsListActivity> {
33    private static final String LOG_TAG = "Starter";
34    private boolean mEverythingFinished;
35
36    public Starter() {
37        super(TestsListActivity.class);
38    }
39
40    /**
41     * This method is called from adb to start executing the tests. It doesn't return
42     * until everything is finished so that the script can wait for the end if it needs
43     * to.
44     */
45    public void startLayoutTests() {
46        ScriptTestRunner runner = (ScriptTestRunner)getInstrumentation();
47        String relativePath = runner.getTestsRelativePath();
48
49        ForwarderManager.getForwarderManager().start();
50
51        Intent intent = new Intent();
52        intent.setClassName("com.android.dumprendertree2", "TestsListActivity");
53        intent.setAction(Intent.ACTION_RUN);
54        intent.putExtra(TestsListActivity.EXTRA_TEST_PATH, relativePath);
55        setActivityIntent(intent);
56        getActivity().registerOnEverythingFinishedCallback(new OnEverythingFinishedCallback() {
57            /** This method is safe to call on any thread */
58            @Override
59            public void onFinished() {
60                synchronized (Starter.this) {
61                    mEverythingFinished = true;
62                    Starter.this.notifyAll();
63                }
64            }
65        });
66
67        synchronized (this) {
68            while (!mEverythingFinished) {
69                try {
70                    this.wait();
71                } catch (InterruptedException e) {
72                    Log.e(LOG_TAG, "startLayoutTests()", e);
73                }
74            }
75        }
76
77        ForwarderManager.getForwarderManager().stop();
78    }
79}