LayoutTestsAutoTest.java revision d24b8183b93e781080b2c16c487e60d51c12da31
1/* 2 * Copyright (C) 2008 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.dumprendertree; 18 19import android.app.Activity; 20import android.app.Instrumentation; 21import android.app.Instrumentation.ActivityMonitor; 22import android.content.ContentResolver; 23import android.content.ContentValues; 24import android.content.Intent; 25 26import android.util.Log; 27import android.view.KeyEvent; 28 29import android.os.Bundle; 30import android.os.Message; 31import android.test.ActivityInstrumentationTestCase; 32import android.test.AndroidTestCase; 33import android.test.suitebuilder.annotation.LargeTest; 34 35import com.android.dumprendertree.HTMLHostActivity; 36 37import java.io.BufferedOutputStream; 38import java.io.File; 39import java.io.FileOutputStream; 40import java.io.IOException; 41 42public class LayoutTestsAutoTest extends ActivityInstrumentationTestCase<Menu> { 43 44 private final static String LOGTAG = "LayoutTests"; 45 private final static int DEFAULT_TIMEOUT_IN_MILLIS = 6000; 46 private static String layoutTestDir = null; 47 private static int mTimeoutInMillis = 0; 48 49 public LayoutTestsAutoTest() { 50 super("com.android.dumprendertree", Menu.class); 51 } 52 53 // This function writes the result of the layout test to 54 // Am status so that it can be picked up from a script. 55 public void passOrFailCallback(String file, boolean result) { 56 Instrumentation inst = getInstrumentation(); 57 Bundle bundle = new Bundle(); 58 bundle.putBoolean(file, result); 59 inst.sendStatus(0, bundle); 60 } 61 62 public static void setTimeoutInMillis(int millis) { 63 mTimeoutInMillis = (millis > 0) ? millis : DEFAULT_TIMEOUT_IN_MILLIS; 64 } 65 66 public static void setLayoutTestDir(String name) { 67 if (name == null) 68 throw new AssertionError("Layout test directory cannot be null."); 69 layoutTestDir = HTMLHostActivity.LAYOUT_TESTS_ROOT + name; 70 Log.v("LayoutTestsAutoTest", " Only running the layout tests : " + layoutTestDir); 71 } 72 73 // Invokes running of layout tests 74 // and waits till it has finished running. 75 public void executeLayoutTests(boolean resume) { 76 Instrumentation inst = getInstrumentation(); 77 78 { 79 Activity activity = getActivity(); 80 Intent intent = new Intent(); 81 intent.setClass(activity, HTMLHostActivity.class); 82 intent.putExtra(HTMLHostActivity.RESUME_FROM_CRASH, resume); 83 intent.putExtra(HTMLHostActivity.SINGLE_TEST_MODE, false); 84 intent.putExtra(HTMLHostActivity.TEST_PATH_PREFIX, layoutTestDir); 85 intent.putExtra(HTMLHostActivity.TIMEOUT_IN_MILLIS, mTimeoutInMillis); 86 activity.startActivity(intent); 87 } 88 89 ActivityMonitor htmlHostActivityMonitor = 90 inst.addMonitor("com.android.dumprendertree.HTMLHostActivity", null, false); 91 92 HTMLHostActivity activity = 93 (HTMLHostActivity) htmlHostActivityMonitor.waitForActivity(); 94 95 while (!activity.hasFinishedRunning()) { 96 // Poll every 5 seconds to determine if the layout 97 // tests have finished running 98 try {Thread.sleep(5000); } catch(Exception e){} 99 } 100 101 // Wait few more seconds so that results are 102 // flushed to the /sdcard 103 try {Thread.sleep(5000); } catch(Exception e){} 104 105 // Clean up the HTMLHostActivity activity 106 activity.finish(); 107 } 108 109 public void generateTestList() { 110 try { 111 File tests_list = new File(HTMLHostActivity.LAYOUT_TESTS_LIST_FILE); 112 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tests_list, false)); 113 findTestsRecursively(bos, layoutTestDir); 114 bos.flush(); 115 bos.close(); 116 } catch (Exception e) { 117 Log.e(LOGTAG, "Error when creating test list: " + e.getMessage()); 118 } 119 } 120 121 private void findTestsRecursively(BufferedOutputStream bos, String dir) throws IOException { 122 Log.v(LOGTAG, "Searching tests under " + dir); 123 124 File d = new File(dir); 125 if (!d.isDirectory()) { 126 throw new AssertionError("A directory expected, but got " + dir); 127 } 128 129 String[] files = d.list(); 130 for (int i = 0; i < files.length; i++) { 131 String s = dir + "/" + files[i]; 132 if (FileFilter.ignoreTest(s)) { 133 Log.v(LOGTAG, " Ignoring: " + s); 134 continue; 135 } 136 if (s.toLowerCase().endsWith(".html") 137 || s.toLowerCase().endsWith(".xml")) { 138 bos.write(s.getBytes()); 139 bos.write('\n'); 140 continue; 141 } 142 143 File f = new File(s); 144 if (f.isDirectory()) { 145 findTestsRecursively(bos, s); 146 continue; 147 } 148 149 Log.v(LOGTAG, "Skipping " + s); 150 } 151 } 152 153 // Running all the layout tests at once sometimes 154 // causes the dumprendertree to run out of memory. 155 // So, additional tests are added to run the tests 156 // in chunks. 157 public void startLayoutTests() { 158 try { 159 File tests_list = new File(HTMLHostActivity.LAYOUT_TESTS_LIST_FILE); 160 if (!tests_list.exists()) 161 generateTestList(); 162 } catch (Exception e) { 163 e.printStackTrace(); 164 } 165 166 executeLayoutTests(false); 167 } 168 169 public void resumeLayoutTests() { 170 executeLayoutTests(true); 171 } 172} 173