The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2007 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 android.test; |
| 18 | |
| 19 | import android.content.Intent; |
| 20 | import android.net.Uri; |
| 21 | import com.google.android.collect.Lists; |
| 22 | import junit.framework.Test; |
| 23 | import junit.framework.TestCase; |
| 24 | import junit.framework.TestSuite; |
| 25 | |
| 26 | import java.util.List; |
| 27 | |
| 28 | /** |
| 29 | * @hide - This is part of a framework that is under development and should not be used for |
| 30 | * active development. |
| 31 | */ |
| 32 | public class TestBrowserControllerImpl implements TestBrowserController { |
| 33 | |
| 34 | static final String TEST_RUNNER_ACTIVITY_CLASS_NAME = |
| 35 | "com.android.testharness.TestRunnerActivity"; |
| 36 | |
| 37 | private TestSuite mTestSuite; |
| 38 | private TestBrowserView mTestBrowserView; |
| 39 | private static final int RUN_ALL_INDEX = 0; |
| 40 | private String mTargetBrowserActivityClassName; |
| 41 | private String mTargetPackageName; |
| 42 | |
| 43 | public void setTargetPackageName(String targetPackageName) { |
| 44 | mTargetPackageName = targetPackageName; |
| 45 | } |
| 46 | |
| 47 | public Intent getIntentForTestAt(int position) { |
| 48 | Intent intent = new Intent(); |
| 49 | intent.setAction(Intent.ACTION_RUN); |
| 50 | // We must add the following two flags to make sure that we create a new activity when |
| 51 | // we browse nested test suites. |
| 52 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 53 | intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); |
| 54 | |
| 55 | String packageName = getDefaultPackageNameForTestRunner(); |
| 56 | String className = ""; |
| 57 | String testName = ""; |
| 58 | if (shouldAllTestsBeRun(position)) { |
| 59 | testName = mTestSuite.getName(); |
| 60 | className = TEST_RUNNER_ACTIVITY_CLASS_NAME; |
| 61 | } else { |
| 62 | Test test = TestCaseUtil.getTestAtIndex(mTestSuite, position - 1); |
| 63 | if (TestSuite.class.isAssignableFrom(test.getClass())) { |
| 64 | TestSuite testSuite = (TestSuite) test; |
| 65 | testName = testSuite.getName(); |
| 66 | className = mTargetBrowserActivityClassName; |
| 67 | packageName = mTargetPackageName; |
| 68 | } else if (TestCase.class.isAssignableFrom(test.getClass())) { |
| 69 | TestCase testCase = (TestCase) test; |
| 70 | testName = testCase.getClass().getName(); |
| 71 | className = TEST_RUNNER_ACTIVITY_CLASS_NAME; |
| 72 | String testMethodName = testCase.getName(); |
| 73 | intent.putExtra(BUNDLE_EXTRA_TEST_METHOD_NAME, testMethodName); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | intent.setClassName(packageName, className); |
| 78 | intent.setData(Uri.parse(testName)); |
| 79 | |
| 80 | return intent; |
| 81 | } |
| 82 | |
| 83 | private String getDefaultPackageNameForTestRunner() { |
| 84 | return TEST_RUNNER_ACTIVITY_CLASS_NAME.substring(0, |
| 85 | TEST_RUNNER_ACTIVITY_CLASS_NAME.lastIndexOf(".")); |
| 86 | } |
| 87 | |
| 88 | private boolean shouldAllTestsBeRun(int position) { |
| 89 | return position == RUN_ALL_INDEX; |
| 90 | } |
| 91 | |
| 92 | public void setTestSuite(TestSuite testSuite) { |
| 93 | mTestSuite = testSuite; |
| 94 | |
| 95 | List<String> testCaseNames = Lists.newArrayList(); |
| 96 | testCaseNames.add("Run All"); |
| 97 | testCaseNames.addAll(TestCaseUtil.getTestCaseNames(testSuite, false)); |
| 98 | |
| 99 | mTestBrowserView.setTestNames(testCaseNames); |
| 100 | } |
| 101 | |
| 102 | public void registerView(TestBrowserView testBrowserView) { |
| 103 | mTestBrowserView = testBrowserView; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | public void setTargetBrowserActivityClassName(String targetBrowserActivityClassName) { |
| 108 | mTargetBrowserActivityClassName = targetBrowserActivityClassName; |
| 109 | } |
| 110 | } |