OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.native_test; | |
6 | |
7 import android.app.Activity; | |
8 import android.content.Context; | |
9 import android.os.Bundle; | |
10 import android.os.Environment; | |
11 import android.os.Handler; | |
12 import android.util.Log; | |
13 | |
14 import org.chromium.base.CommandLine; | |
15 import org.chromium.base.PathUtils; | |
16 import org.chromium.base.PowerMonitor; | |
17 import org.chromium.base.ResourceExtractor; | |
18 import org.chromium.base.library_loader.NativeLibraries; | |
19 | |
20 import java.io.File; | |
21 | |
22 /** | |
23 * Android's NativeActivity is mostly useful for pure-native code. | |
24 * Our tests need to go up to our own java classes, which is not possible using | |
25 * the native activity class loader. | |
26 */ | |
27 public class ChromeNativeTestActivity extends Activity { | |
28 public static final String EXTRA_COMMAND_LINE_FILE = | |
29 "org.chromium.native_test.ChromeNativeTestActivity.CommandLineFile"; | |
30 public static final String EXTRA_COMMAND_LINE_FLAGS = | |
31 "org.chromium.native_test.ChromeNativeTestActivity.CommandLineFlags"
; | |
32 public static final String EXTRA_STDOUT_FILE = | |
33 "org.chromium.native_test.ChromeNativeTestActivity.StdoutFile"; | |
34 | |
35 private static final String TAG = "ChromeNativeTestActivity"; | |
36 private static final String EXTRA_RUN_IN_SUB_THREAD = "RunInSubThread"; | |
37 // We post a delayed task to run tests so that we do not block onCreate(). | |
38 private static final long RUN_TESTS_DELAY_IN_MS = 300; | |
39 | |
40 @Override | |
41 public void onCreate(Bundle savedInstanceState) { | |
42 super.onCreate(savedInstanceState); | |
43 CommandLine.init(new String[]{}); | |
44 | |
45 // Needed by path_utils_unittest.cc | |
46 PathUtils.setPrivateDataDirectorySuffix("chrome"); | |
47 | |
48 ResourceExtractor resourceExtractor = ResourceExtractor.get(getApplicati
onContext()); | |
49 resourceExtractor.setExtractAllPaksAndV8SnapshotForTesting(); | |
50 resourceExtractor.startExtractingResources(); | |
51 resourceExtractor.waitForCompletion(); | |
52 | |
53 // Needed by system_monitor_unittest.cc | |
54 PowerMonitor.createForTests(this); | |
55 | |
56 loadLibraries(); | |
57 Bundle extras = this.getIntent().getExtras(); | |
58 if (extras != null && extras.containsKey(EXTRA_RUN_IN_SUB_THREAD)) { | |
59 // Create a new thread and run tests on it. | |
60 new Thread() { | |
61 @Override | |
62 public void run() { | |
63 runTests(); | |
64 } | |
65 }.start(); | |
66 } else { | |
67 // Post a task to run the tests. This allows us to not block | |
68 // onCreate and still run tests on the main thread. | |
69 new Handler().postDelayed(new Runnable() { | |
70 @Override | |
71 public void run() { | |
72 runTests(); | |
73 } | |
74 }, RUN_TESTS_DELAY_IN_MS); | |
75 } | |
76 } | |
77 | |
78 private void runTests() { | |
79 String commandLineFlags = getIntent().getStringExtra(EXTRA_COMMAND_LINE_
FLAGS); | |
80 if (commandLineFlags == null) commandLineFlags = ""; | |
81 | |
82 String commandLineFilePath = getIntent().getStringExtra(EXTRA_COMMAND_LI
NE_FILE); | |
83 if (commandLineFilePath == null) { | |
84 commandLineFilePath = ""; | |
85 } else { | |
86 File commandLineFile = new File(commandLineFilePath); | |
87 if (!commandLineFile.isAbsolute()) { | |
88 commandLineFilePath = Environment.getExternalStorageDirectory()
+ "/" | |
89 + commandLineFilePath; | |
90 } | |
91 Log.i(TAG, "command line file path: " + commandLineFilePath); | |
92 } | |
93 | |
94 String stdoutFilePath = getIntent().getStringExtra(EXTRA_STDOUT_FILE); | |
95 boolean stdoutFifo = false; | |
96 if (stdoutFilePath == null) { | |
97 stdoutFilePath = new File(getFilesDir(), "test.fifo").getAbsolutePat
h(); | |
98 stdoutFifo = true; | |
99 } | |
100 | |
101 // This directory is used by build/android/pylib/test_package_apk.py. | |
102 nativeRunTests(commandLineFlags, commandLineFilePath, stdoutFilePath, st
doutFifo, | |
103 getApplicationContext()); | |
104 finish(); | |
105 } | |
106 | |
107 // Signal a failure of the native test loader to python scripts | |
108 // which run tests. For example, we look for | |
109 // RUNNER_FAILED build/android/test_package.py. | |
110 private void nativeTestFailed() { | |
111 Log.e(TAG, "[ RUNNER_FAILED ] could not load native library"); | |
112 } | |
113 | |
114 private void loadLibraries() { | |
115 for (String library : NativeLibraries.LIBRARIES) { | |
116 Log.i(TAG, "loading: " + library); | |
117 System.loadLibrary(library); | |
118 Log.i(TAG, "loaded: " + library); | |
119 } | |
120 } | |
121 | |
122 private native void nativeRunTests(String commandLineFlags, String commandLi
neFilePath, | |
123 String stdoutFilePath, boolean stdoutFifo, Context appContext); | |
124 } | |
OLD | NEW |