The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.dumprendertree; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.Instrumentation; |
| 21 | import android.app.Instrumentation.ActivityMonitor; |
| 22 | import android.content.Intent; |
| 23 | |
| 24 | import android.util.Log; |
| 25 | |
| 26 | import android.os.Bundle; |
| 27 | import android.test.ActivityInstrumentationTestCase2; |
| 28 | |
| 29 | import com.android.dumprendertree.TestShellActivity; |
| 30 | import com.android.dumprendertree.TestShellCallback; |
| 31 | |
| 32 | import java.io.InputStream; |
| 33 | import java.io.OutputStream; |
| 34 | import java.io.FileOutputStream; |
| 35 | import java.io.IOException; |
| 36 | |
| 37 | class StreamPipe extends Thread { |
| 38 | InputStream in; |
| 39 | OutputStream out; |
| 40 | |
| 41 | StreamPipe(InputStream in, OutputStream out) { |
| 42 | this.in = in; |
| 43 | this.out = out; |
| 44 | } |
| 45 | |
| 46 | public void run() { |
| 47 | try { |
| 48 | byte[] buf = new byte[1024]; |
| 49 | int nofb = this.in.read(buf); |
| 50 | while (nofb != -1) { |
| 51 | this.out.write(buf, 0, nofb); |
| 52 | nofb = this.in.read(buf); |
| 53 | } |
| 54 | } catch (IOException e) { |
| 55 | e.printStackTrace(); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public class LoadTestsAutoTest extends ActivityInstrumentationTestCase2<TestShellActivity> { |
| 61 | |
| 62 | private final static String LOGTAG = "LoadTest"; |
| 63 | private final static String LOAD_TEST_RESULT = "/sdcard/load_test_result.txt"; |
| 64 | |
| 65 | public LoadTestsAutoTest() { |
| 66 | super("com.android.dumprendertree", TestShellActivity.class); |
| 67 | } |
| 68 | |
| 69 | // This function writes the result of the layout test to |
| 70 | // Am status so that it can be picked up from a script. |
| 71 | public void passOrFailCallback(String file, boolean result) { |
| 72 | Instrumentation inst = getInstrumentation(); |
| 73 | Bundle bundle = new Bundle(); |
| 74 | bundle.putBoolean(file, result); |
| 75 | inst.sendStatus(0, bundle); |
| 76 | } |
| 77 | |
| 78 | // Invokes running of layout tests |
| 79 | // and waits till it has finished running. |
| 80 | public void runTest() { |
| 81 | LayoutTestsAutoRunner runner = (LayoutTestsAutoRunner) getInstrumentation(); |
| 82 | |
| 83 | if (runner.mTestPath == null) { |
| 84 | Log.e(LOGTAG, "No test specified"); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | TestShellActivity activity = (TestShellActivity) getActivity(); |
| 89 | |
| 90 | // Run tests |
| 91 | runTestAndWaitUntilDone(activity, runner.mTestPath, runner.mTimeoutInMillis); |
| 92 | |
| 93 | // TODO(fqian): let am instrumentation pass in the command line, currently |
| 94 | // am instrument does not allow spaces in the command. |
| 95 | runPostShellCommand("/system/bin/dumpsys meminfo"); |
| 96 | |
| 97 | // Kill activity |
| 98 | activity.finish(); |
| 99 | } |
| 100 | |
| 101 | private void runPostShellCommand(String cmd) { |
| 102 | if (cmd == null || cmd.length() == 0) |
| 103 | return; |
| 104 | |
| 105 | try { |
| 106 | // Call dumpsys meminfo |
| 107 | Process proc = Runtime.getRuntime().exec(cmd); |
| 108 | // Append output to LOAD_TEST_RESULT |
| 109 | InputStream input = proc.getInputStream(); |
| 110 | InputStream error = proc.getErrorStream(); |
| 111 | FileOutputStream out = new FileOutputStream(LOAD_TEST_RESULT, true); |
| 112 | |
| 113 | StreamPipe p_in = new StreamPipe(input, out); |
| 114 | StreamPipe p_err = new StreamPipe(error, System.err); |
| 115 | |
| 116 | p_in.start(); |
| 117 | p_err.start(); |
| 118 | |
| 119 | proc.waitFor(); |
| 120 | } catch (IOException e) { |
| 121 | Log.e(LOGTAG, e.getMessage()); |
| 122 | } catch (InterruptedException e) { |
| 123 | Log.e(LOGTAG, e.getMessage()); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // A convenient method to be called by another activity. |
| 128 | private void runTestAndWaitUntilDone(TestShellActivity activity, String url, int timeout) { |
| 129 | activity.setCallback(new TestShellCallback() { |
| 130 | public void finished() { |
| 131 | synchronized (LoadTestsAutoTest.this) { |
| 132 | LoadTestsAutoTest.this.notifyAll(); |
| 133 | } |
| 134 | } |
| 135 | }); |
| 136 | |
| 137 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 138 | intent.setClass(activity, TestShellActivity.class); |
| 139 | intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); |
| 140 | intent.putExtra(TestShellActivity.TEST_URL, url); |
| 141 | intent.putExtra(TestShellActivity.TIMEOUT_IN_MILLIS, timeout); |
| 142 | intent.putExtra(TestShellActivity.RESULT_FILE, LOAD_TEST_RESULT); |
| 143 | activity.startActivity(intent); |
| 144 | |
| 145 | // Wait until done. |
| 146 | synchronized (this) { |
| 147 | try { |
| 148 | this.wait(); |
| 149 | } catch (InterruptedException e) { } |
| 150 | } |
| 151 | } |
| 152 | } |