SmokeTestRunner.java revision 5ae3dfe59d22457fb232853990ba4131385ff3f6
1/*
2 * Copyright (C) 2012 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.smoketest;
18
19import android.app.ActivityManager;
20import android.app.ActivityManager.ProcessErrorStateInfo;
21import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
23import android.test.InstrumentationTestRunner;
24
25import junit.framework.TestCase;
26import junit.framework.TestSuite;
27
28import java.util.Collection;
29import java.util.HashSet;
30import java.util.List;
31import java.util.Set;
32
33/**
34 * A special test runner which does a test-start for each app in a separate testcase
35 */
36public class SmokeTestRunner extends InstrumentationTestRunner {
37
38    private static final String SUITE_NAME = "Smoke Test Suite";
39
40    /**
41     * Returns a single testcase for each app to launch
42     */
43    @Override
44    public TestSuite getAllTests() {
45        final TestSuite suite = new TestSuite(SUITE_NAME);
46
47        final PackageManager pm = getTargetContext().getPackageManager();
48        final List<ResolveInfo> apps = ProcessErrorsTest.getLauncherActivities(pm);
49
50        // FIXME: figure out some way to control the reported class names for these anonymous
51        // FIXME: class instances.
52
53        final TestCase setupTest = new ProcessErrorsTest() {
54            @Override
55            public void runTest() throws Exception {
56                testSetUpConditions();
57            }
58        };
59        setupTest.setName("testSetUpConditions");
60        suite.addTest(setupTest);
61
62        final TestCase postBootTest = new ProcessErrorsTest() {
63            @Override
64            public void runTest() throws Exception {
65                testNoProcessErrorsAfterBoot();
66            }
67        };
68        postBootTest.setName("testNoProcessErrorsAfterBoot");
69        suite.addTest(postBootTest);
70
71        for (final ResolveInfo app : apps) {
72            final TestCase appTest = new ProcessErrorsTest() {
73                @Override
74                public void runTest() throws Exception {
75                    final Set<ProcessError> errSet = new HashSet<ProcessError>();
76                    final Collection<ProcessErrorStateInfo> errProcs = runOneActivity(app);
77                    if (errProcs != null) {
78                        errSet.addAll(ProcessError.fromCollection(errProcs));
79                    }
80
81                    if (!errSet.isEmpty()) {
82                        fail(String.format("Got %d errors: %s", errSet.size(),
83                                reportWrappedListContents(errSet)));
84                    }
85                }
86            };
87            appTest.setName(app.activityInfo.name);
88            suite.addTest(appTest);
89        }
90
91        return suite;
92    }
93}
94
95