Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / testing / android / java / src / org / chromium / native_test / ChromeNativeTestActivity.java
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.Handler;
11 import android.util.Log;
12
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;
18
19 /**
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.
23  */
24 public class ChromeNativeTestActivity extends Activity {
25     private static final String TAG = "ChromeNativeTestActivity";
26     private static final String EXTRA_RUN_IN_SUB_THREAD = "RunInSubThread";
27     // We post a delayed task to run tests so that we do not block onCreate().
28     private static final long RUN_TESTS_DELAY_IN_MS = 300;
29
30     @Override
31     public void onCreate(Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33         CommandLine.init(new String[]{});
34
35         // Needed by path_utils_unittest.cc
36         PathUtils.setPrivateDataDirectorySuffix("chrome");
37
38         ResourceExtractor resourceExtractor = ResourceExtractor.get(getApplicationContext());
39         resourceExtractor.setExtractAllPaksAndV8SnapshotForTesting();
40         resourceExtractor.startExtractingResources();
41         resourceExtractor.waitForCompletion();
42
43         // Needed by system_monitor_unittest.cc
44         PowerMonitor.createForTests(this);
45
46         loadLibraries();
47         Bundle extras = this.getIntent().getExtras();
48         if (extras != null && extras.containsKey(EXTRA_RUN_IN_SUB_THREAD)) {
49             // Create a new thread and run tests on it.
50             new Thread() {
51                 @Override
52                 public void run() {
53                     runTests();
54                 }
55             }.start();
56         } else {
57             // Post a task to run the tests. This allows us to not block
58             // onCreate and still run tests on the main thread.
59             new Handler().postDelayed(new Runnable() {
60                 @Override
61                 public void run() {
62                     runTests();
63                 }
64             }, RUN_TESTS_DELAY_IN_MS);
65         }
66     }
67
68     private void runTests() {
69         // This directory is used by build/android/pylib/test_package_apk.py.
70         nativeRunTests(getFilesDir().getAbsolutePath(), getApplicationContext());
71     }
72
73     // Signal a failure of the native test loader to python scripts
74     // which run tests.  For example, we look for
75     // RUNNER_FAILED build/android/test_package.py.
76     private void nativeTestFailed() {
77         Log.e(TAG, "[ RUNNER_FAILED ] could not load native library");
78     }
79
80     private void loadLibraries() {
81         for (String library : NativeLibraries.LIBRARIES) {
82             Log.i(TAG, "loading: " + library);
83             System.loadLibrary(library);
84             Log.i(TAG, "loaded: " + library);
85         }
86     }
87
88     private native void nativeRunTests(String filesDir, Context appContext);
89 }