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.
5 package org
.chromium
.native_test
;
7 import android
.app
.Activity
;
8 import android
.content
.Context
;
9 import android
.os
.Bundle
;
10 import android
.os
.Handler
;
11 import android
.util
.Log
;
13 import org
.chromium
.base
.CommandLine
;
14 import org
.chromium
.base
.PathUtils
;
15 import org
.chromium
.base
.PowerMonitor
;
16 import org
.chromium
.base
.ResourceExtractor
;
17 import org
.chromium
.base
.library_loader
.NativeLibraries
;
20 * Android's NativeActivity is mostly useful for pure-native code.
21 * Our tests need to go up to our own java classes, which is not possible using
22 * the native activity class loader.
24 public class ChromeNativeTestActivity
extends Activity
{
25 public static final String EXTRA_COMMAND_LINE_FILE
=
26 "org.chromium.native_test.ChromeNativeTestActivity.CommandLineFile";
27 public static final String EXTRA_COMMAND_LINE_FLAGS
=
28 "org.chromium.native_test.ChromeNativeTestActivity.CommandLineFlags";
30 private static final String TAG
= "ChromeNativeTestActivity";
31 private static final String EXTRA_RUN_IN_SUB_THREAD
= "RunInSubThread";
32 // We post a delayed task to run tests so that we do not block onCreate().
33 private static final long RUN_TESTS_DELAY_IN_MS
= 300;
36 public void onCreate(Bundle savedInstanceState
) {
37 super.onCreate(savedInstanceState
);
38 CommandLine
.init(new String
[]{});
40 // Needed by path_utils_unittest.cc
41 PathUtils
.setPrivateDataDirectorySuffix("chrome");
43 ResourceExtractor resourceExtractor
= ResourceExtractor
.get(getApplicationContext());
44 resourceExtractor
.setExtractAllPaksAndV8SnapshotForTesting();
45 resourceExtractor
.startExtractingResources();
46 resourceExtractor
.waitForCompletion();
48 // Needed by system_monitor_unittest.cc
49 PowerMonitor
.createForTests(this);
52 Bundle extras
= this.getIntent().getExtras();
53 if (extras
!= null && extras
.containsKey(EXTRA_RUN_IN_SUB_THREAD
)) {
54 // Create a new thread and run tests on it.
62 // Post a task to run the tests. This allows us to not block
63 // onCreate and still run tests on the main thread.
64 new Handler().postDelayed(new Runnable() {
69 }, RUN_TESTS_DELAY_IN_MS
);
73 private void runTests() {
74 String commandLineFlags
= getIntent().getStringExtra(EXTRA_COMMAND_LINE_FLAGS
);
75 if (commandLineFlags
== null) commandLineFlags
= "";
77 String commandLineFilePath
= getIntent().getStringExtra(EXTRA_COMMAND_LINE_FILE
);
78 if (commandLineFilePath
== null) commandLineFilePath
= "";
80 // This directory is used by build/android/pylib/test_package_apk.py.
81 nativeRunTests(commandLineFlags
, commandLineFilePath
, getFilesDir().getAbsolutePath(),
82 getApplicationContext());
85 // Signal a failure of the native test loader to python scripts
86 // which run tests. For example, we look for
87 // RUNNER_FAILED build/android/test_package.py.
88 private void nativeTestFailed() {
89 Log
.e(TAG
, "[ RUNNER_FAILED ] could not load native library");
92 private void loadLibraries() {
93 for (String library
: NativeLibraries
.LIBRARIES
) {
94 Log
.i(TAG
, "loading: " + library
);
95 System
.loadLibrary(library
);
96 Log
.i(TAG
, "loaded: " + library
);
100 private native void nativeRunTests(String commandLineFlags
, String commandLineFilePath
,
101 String filesDir
, Context appContext
);