| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.native_test; | 5 package org.chromium.native_test; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.content.Context; | 8 import android.content.Context; |
| 9 import android.content.Intent; | 9 import android.content.Intent; |
| 10 import android.os.Bundle; | 10 import android.os.Bundle; |
| 11 import android.os.Environment; | 11 import android.os.Environment; |
| 12 import android.os.Handler; | 12 import android.os.Handler; |
| 13 import android.os.Process; |
| 13 | 14 |
| 14 import org.chromium.base.CommandLine; | 15 import org.chromium.base.CommandLine; |
| 15 import org.chromium.base.JNINamespace; | 16 import org.chromium.base.JNINamespace; |
| 16 import org.chromium.base.Log; | 17 import org.chromium.base.Log; |
| 18 import org.chromium.test.reporter.TestStatusReporter; |
| 17 | 19 |
| 18 import java.io.File; | 20 import java.io.File; |
| 21 import java.util.ArrayList; |
| 22 import java.util.Iterator; |
| 19 | 23 |
| 20 /** | 24 /** |
| 21 * Android's NativeActivity is mostly useful for pure-native code. | 25 * Android's NativeActivity is mostly useful for pure-native code. |
| 22 * Our tests need to go up to our own java classes, which is not possible using | 26 * Our tests need to go up to our own java classes, which is not possible using |
| 23 * the native activity class loader. | 27 * the native activity class loader. |
| 24 */ | 28 */ |
| 25 @JNINamespace("testing::android") | 29 @JNINamespace("testing::android") |
| 26 public class NativeTestActivity extends Activity { | 30 public class NativeTestActivity extends Activity { |
| 27 public static final String EXTRA_COMMAND_LINE_FILE = | 31 public static final String EXTRA_COMMAND_LINE_FILE = |
| 28 "org.chromium.native_test.NativeTestActivity.CommandLineFile"; | 32 "org.chromium.native_test.NativeTestActivity.CommandLineFile"; |
| 29 public static final String EXTRA_COMMAND_LINE_FLAGS = | 33 public static final String EXTRA_COMMAND_LINE_FLAGS = |
| 30 "org.chromium.native_test.NativeTestActivity.CommandLineFlags"; | 34 "org.chromium.native_test.NativeTestActivity.CommandLineFlags"; |
| 35 public static final String EXTRA_SHARD = |
| 36 "org.chromium.native_test.NativeTestActivity.Shard"; |
| 31 public static final String EXTRA_STDOUT_FILE = | 37 public static final String EXTRA_STDOUT_FILE = |
| 32 "org.chromium.native_test.NativeTestActivity.StdoutFile"; | 38 "org.chromium.native_test.NativeTestActivity.StdoutFile"; |
| 33 | 39 |
| 34 private static final String TAG = Log.makeTag("native_test"); | 40 private static final String TAG = "cr.native_test"; |
| 35 private static final String EXTRA_RUN_IN_SUB_THREAD = "RunInSubThread"; | 41 private static final String EXTRA_RUN_IN_SUB_THREAD = "RunInSubThread"; |
| 36 // We post a delayed task to run tests so that we do not block onCreate(). | |
| 37 private static final long RUN_TESTS_DELAY_IN_MS = 300; | |
| 38 | 42 |
| 39 private String mCommandLineFilePath; | 43 private String mCommandLineFilePath; |
| 40 private StringBuilder mCommandLineFlags = new StringBuilder(); | 44 private StringBuilder mCommandLineFlags = new StringBuilder(); |
| 45 private TestStatusReporter mReporter; |
| 41 private boolean mRunInSubThread = false; | 46 private boolean mRunInSubThread = false; |
| 42 private boolean mStdoutFifo = false; | 47 private boolean mStdoutFifo = false; |
| 43 private String mStdoutFilePath; | 48 private String mStdoutFilePath; |
| 44 | 49 |
| 45 @Override | 50 @Override |
| 46 public void onCreate(Bundle savedInstanceState) { | 51 public void onCreate(Bundle savedInstanceState) { |
| 47 super.onCreate(savedInstanceState); | 52 super.onCreate(savedInstanceState); |
| 48 CommandLine.init(new String[]{}); | 53 CommandLine.init(new String[]{}); |
| 49 | 54 |
| 50 parseArgumentsFromIntent(getIntent()); | 55 parseArgumentsFromIntent(getIntent()); |
| 56 mReporter = new TestStatusReporter(this); |
| 51 } | 57 } |
| 52 | 58 |
| 53 private void parseArgumentsFromIntent(Intent intent) { | 59 private void parseArgumentsFromIntent(Intent intent) { |
| 54 mCommandLineFilePath = intent.getStringExtra(EXTRA_COMMAND_LINE_FILE); | 60 mCommandLineFilePath = intent.getStringExtra(EXTRA_COMMAND_LINE_FILE); |
| 55 if (mCommandLineFilePath == null) { | 61 if (mCommandLineFilePath == null) { |
| 56 mCommandLineFilePath = ""; | 62 mCommandLineFilePath = ""; |
| 57 } else { | 63 } else { |
| 58 File commandLineFile = new File(mCommandLineFilePath); | 64 File commandLineFile = new File(mCommandLineFilePath); |
| 59 if (!commandLineFile.isAbsolute()) { | 65 if (!commandLineFile.isAbsolute()) { |
| 60 mCommandLineFilePath = Environment.getExternalStorageDirectory()
+ "/" | 66 mCommandLineFilePath = Environment.getExternalStorageDirectory()
+ "/" |
| 61 + mCommandLineFilePath; | 67 + mCommandLineFilePath; |
| 62 } | 68 } |
| 63 Log.i(TAG, "command line file path: %s", mCommandLineFilePath); | 69 Log.i(TAG, "command line file path: %s", mCommandLineFilePath); |
| 64 } | 70 } |
| 65 | 71 |
| 66 String commandLineFlags = intent.getStringExtra(EXTRA_COMMAND_LINE_FLAGS
); | 72 String commandLineFlags = intent.getStringExtra(EXTRA_COMMAND_LINE_FLAGS
); |
| 67 if (commandLineFlags != null) mCommandLineFlags.append(commandLineFlags)
; | 73 if (commandLineFlags != null) mCommandLineFlags.append(commandLineFlags)
; |
| 68 | 74 |
| 69 mRunInSubThread = intent.hasExtra(EXTRA_RUN_IN_SUB_THREAD); | 75 mRunInSubThread = intent.hasExtra(EXTRA_RUN_IN_SUB_THREAD); |
| 70 | 76 |
| 77 ArrayList<String> shard = intent.getStringArrayListExtra(EXTRA_SHARD); |
| 78 if (shard != null) { |
| 79 StringBuilder filterFlag = new StringBuilder(); |
| 80 filterFlag.append("--gtest_filter="); |
| 81 for (Iterator<String> test_iter = shard.iterator(); test_iter.hasNex
t();) { |
| 82 filterFlag.append(test_iter.next()); |
| 83 if (test_iter.hasNext()) { |
| 84 filterFlag.append(":"); |
| 85 } |
| 86 } |
| 87 appendCommandLineFlags(filterFlag.toString()); |
| 88 } |
| 89 |
| 71 mStdoutFilePath = intent.getStringExtra(EXTRA_STDOUT_FILE); | 90 mStdoutFilePath = intent.getStringExtra(EXTRA_STDOUT_FILE); |
| 72 if (mStdoutFilePath == null) { | 91 if (mStdoutFilePath == null) { |
| 73 mStdoutFilePath = new File(getFilesDir(), "test.fifo").getAbsolutePa
th(); | 92 mStdoutFilePath = new File(getFilesDir(), "test.fifo").getAbsolutePa
th(); |
| 74 mStdoutFifo = true; | 93 mStdoutFifo = true; |
| 75 } | 94 } |
| 76 } | 95 } |
| 77 | 96 |
| 78 protected void appendCommandLineFlags(String flags) { | 97 protected void appendCommandLineFlags(String flags) { |
| 79 mCommandLineFlags.append(" ").append(flags); | 98 mCommandLineFlags.append(" ").append(flags); |
| 80 } | 99 } |
| 81 | 100 |
| 82 @Override | 101 @Override |
| 83 public void onStart() { | 102 public void onStart() { |
| 84 super.onStart(); | 103 super.onStart(); |
| 85 | 104 |
| 86 if (mRunInSubThread) { | 105 if (mRunInSubThread) { |
| 87 // Create a new thread and run tests on it. | 106 // Create a new thread and run tests on it. |
| 88 new Thread() { | 107 new Thread() { |
| 89 @Override | 108 @Override |
| 90 public void run() { | 109 public void run() { |
| 91 runTests(); | 110 runTests(); |
| 92 } | 111 } |
| 93 }.start(); | 112 }.start(); |
| 94 } else { | 113 } else { |
| 95 // Post a task to run the tests. This allows us to not block | 114 // Post a task to run the tests. This allows us to not block |
| 96 // onCreate and still run tests on the main thread. | 115 // onCreate and still run tests on the main thread. |
| 97 new Handler().postDelayed(new Runnable() { | 116 new Handler().post(new Runnable() { |
| 98 @Override | 117 @Override |
| 99 public void run() { | 118 public void run() { |
| 100 runTests(); | 119 runTests(); |
| 101 } | 120 } |
| 102 }, RUN_TESTS_DELAY_IN_MS); | 121 }); |
| 103 } | 122 } |
| 104 } | 123 } |
| 105 | 124 |
| 106 private void runTests() { | 125 private void runTests() { |
| 126 mReporter.testRunStarted(Process.myPid()); |
| 107 nativeRunTests(mCommandLineFlags.toString(), mCommandLineFilePath, mStdo
utFilePath, | 127 nativeRunTests(mCommandLineFlags.toString(), mCommandLineFilePath, mStdo
utFilePath, |
| 108 mStdoutFifo, getApplicationContext()); | 128 mStdoutFifo, getApplicationContext()); |
| 109 finish(); | 129 finish(); |
| 130 mReporter.testRunFinished(Process.myPid()); |
| 110 } | 131 } |
| 111 | 132 |
| 112 // Signal a failure of the native test loader to python scripts | 133 // Signal a failure of the native test loader to python scripts |
| 113 // which run tests. For example, we look for | 134 // which run tests. For example, we look for |
| 114 // RUNNER_FAILED build/android/test_package.py. | 135 // RUNNER_FAILED build/android/test_package.py. |
| 115 private void nativeTestFailed() { | 136 private void nativeTestFailed() { |
| 116 Log.e(TAG, "[ RUNNER_FAILED ] could not load native library"); | 137 Log.e(TAG, "[ RUNNER_FAILED ] could not load native library"); |
| 117 } | 138 } |
| 118 | 139 |
| 119 private native void nativeRunTests(String commandLineFlags, String commandLi
neFilePath, | 140 private native void nativeRunTests(String commandLineFlags, String commandLi
neFilePath, |
| 120 String stdoutFilePath, boolean stdoutFifo, Context appContext); | 141 String stdoutFilePath, boolean stdoutFifo, Context appContext); |
| 121 } | 142 } |
| OLD | NEW |