summaryrefslogtreecommitdiffstats
path: root/testing/android/java/src/org/chromium/native_test/ChromeNativeTestActivity.java
blob: b8e41c830ddd934d526dbcce435b1c9879da52ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.native_test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import org.chromium.base.PathUtils;
import org.chromium.base.PowerMonitor;
// TODO(cjhopman): This should not refer to content. NativeLibraries should be moved to base.
import org.chromium.content.app.NativeLibraries;

// Android's NativeActivity is mostly useful for pure-native code.
// Our tests need to go up to our own java classes, which is not possible using
// the native activity class loader.
public class ChromeNativeTestActivity extends Activity {
    private static final String TAG = "ChromeNativeTestActivity";
    private static final String EXTRA_RUN_IN_SUB_THREAD = "RunInSubThread";
    // We post a delayed task to run tests so that we do not block onCreate().
    private static final long RUN_TESTS_DELAY_IN_MS = 300;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Needed by path_utils_unittest.cc
        PathUtils.setPrivateDataDirectorySuffix("chrome");

        // Needed by system_monitor_unittest.cc
        PowerMonitor.createForTests(this);

        loadLibraries();
        Bundle extras = this.getIntent().getExtras();
        if (extras != null && extras.containsKey(EXTRA_RUN_IN_SUB_THREAD)) {
            // Create a new thread and run tests on it.
            new Thread() {
                @Override
                public void run() {
                    runTests();
                }
            }.start();
        } else {
            // Post a task to run the tests. This allows us to not block
            // onCreate and still run tests on the main thread.
            new Handler().postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      runTests();
                  }
              }, RUN_TESTS_DELAY_IN_MS);
        }
    }

    private void runTests() {
        // This directory is used by build/android/pylib/test_package_apk.py.
        nativeRunTests(getFilesDir().getAbsolutePath(), getApplicationContext());
    }

    // Signal a failure of the native test loader to python scripts
    // which run tests.  For example, we look for
    // RUNNER_FAILED build/android/test_package.py.
    private void nativeTestFailed() {
        Log.e(TAG, "[ RUNNER_FAILED ] could not load native library");
    }

    private void loadLibraries() {
        for (String library: NativeLibraries.libraries) {
            Log.i(TAG, "loading: " + library);
            System.loadLibrary(library);
            Log.i(TAG, "loaded: " + library);
        }
    }

    private native void nativeRunTests(String filesDir, Context appContext);
}