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 com.android.internal.R; 20 21 import android.app.ListActivity; 22 import android.content.Intent; 23 import android.os.Bundle; 24 import android.util.Log; 25 import android.view.View; 26 import android.widget.AdapterView; 27 import android.widget.ArrayAdapter; 28 import junit.framework.Test; 29 import junit.framework.TestSuite; 30 31 import java.util.List; 32 33 /** 34 * @hide - This is part of a framework that is under development and should not be used for 35 * active development. 36 */ 37 public abstract class TestBrowserActivity extends ListActivity 38 implements android.test.TestBrowserView, AdapterView.OnItemClickListener, 39 TestSuiteProvider { 40 41 private TestBrowserController mTestBrowserController; 42 public static final String BUNDLE_EXTRA_PACKAGE = "package"; 43 44 @Override 45 protected void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 48 getListView().setOnItemClickListener(this); 49 50 mTestBrowserController = ServiceLocator.getTestBrowserController(); 51 mTestBrowserController.setTargetPackageName(getPackageName()); 52 mTestBrowserController.registerView(this); 53 mTestBrowserController.setTargetBrowserActivityClassName(this.getClass().getName()); 54 55 // Apk paths used to search for test classes when using TestSuiteBuilders. 56 String[] apkPaths = {getPackageCodePath()}; 57 ClassPathPackageInfoSource.setApkPaths(apkPaths); 58 } 59 60 @Override 61 protected void onStart() { 62 super.onStart(); 63 TestSuite testSuite = getTestSuiteToBrowse(); 64 mTestBrowserController.setTestSuite(testSuite); 65 66 String name = testSuite.getName(); 67 if (name != null) { 68 setTitle(name.substring(name.lastIndexOf(".") + 1)); 69 } 70 } 71 72 /** 73 * Subclasses will override this method and return the TestSuite specific to their .apk. 74 * When this method is invoked due to an intent fired from 75 * {@link #onItemClick(android.widget.AdapterView, android.view.View, int, long)} then get the 76 * targeted TestSuite from the intent. 77 * 78 * @return testSuite to browse 79 */ 80 @SuppressWarnings("unchecked") 81 private TestSuite getTestSuiteToBrowse() { 82 Intent intent = getIntent(); 83 if (Intent.ACTION_RUN.equals(intent.getAction())) { 84 String testClassName = intent.getData().toString(); 85 86 try { 87 Class<Test> testClass = (Class<Test>) getClassLoader().loadClass(testClassName); 88 return TestCaseUtil.createTestSuite(testClass); 89 } catch (ClassNotFoundException e) { 90 Log.e("TestBrowserActivity", "ClassNotFoundException for " + testClassName, e); 91 throw new RuntimeException(e); 92 } catch (IllegalAccessException e) { 93 Log.e("TestBrowserActivity", "IllegalAccessException for " + testClassName, e); 94 throw new RuntimeException(e); 95 } catch (InstantiationException e) { 96 Log.e("TestBrowserActivity", "InstantiationException for " + testClassName, e); 97 throw new RuntimeException(e); 98 } 99 } else { 100 // get test classes to browwes from subclass 101 return getTopTestSuite(); 102 } 103 104 } 105 106 public TestSuite getTestSuite() { 107 return getTopTestSuite(); 108 } 109 110 /** 111 * @return A TestSuite that should be run for a given application. 112 */ 113 public abstract TestSuite getTopTestSuite(); 114 115 public void onItemClick(AdapterView parent, View v, int position, long id) { 116 Intent intent = mTestBrowserController.getIntentForTestAt(position); 117 intent.putExtra(BUNDLE_EXTRA_PACKAGE, getPackageName()); 118 startActivity(intent); 119 } 120 121 public void setTestNames(List<String> testNames) { 122 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, 123 R.layout.test_list_item, testNames); 124 setListAdapter(arrayAdapter); 125 } 126 } 127 128