• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
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 
9 import org.chromium.base.ApplicationStatus;
10 import org.chromium.base.ContextUtils;
11 import org.chromium.base.Log;
12 import org.chromium.base.PathUtils;
13 import org.chromium.base.PowerMonitor;
14 import org.chromium.base.library_loader.LibraryLoader;
15 import org.chromium.build.NativeLibraries;
16 
17 /** A helper for running native unit tests (i.e., not browser tests) */
18 public class NativeUnitTest extends NativeTest {
19     private static final String TAG = "NativeTest";
20     // This key is only used by Cronet in AOSP to run tests instead of
21     // NativeLibraries.LIBRARIES constant.
22     //
23     // The reason for that is that Cronet translates GN build rules to
24     // AOSP Soong modules, the translation layer is incapable currently
25     // of replicating the logic embedded into GN to replace the temporary
26     // NativeLibraries with the real NativeLibraries at the root of the build
27     // graph, hence we depend on the value of this key to carry the name
28     // of the native library to load.
29     //
30     // Assumption: The code below assumes that the value is exactly a single
31     // library. There is no support for loading multiple libraries through this
32     // key at the moment.
33     private static final String LIBRARY_UNDER_TEST_NAME =
34             "org.chromium.native_test.NativeTestInstrumentationTestRunner.LibraryUnderTest";
35 
36     private static class NativeUnitTestLibraryLoader extends LibraryLoader {
setLibrariesLoaded()37         static void setLibrariesLoaded() {
38             LibraryLoader.setLibrariesLoadedForNativeTests();
39         }
40     }
41 
42     @Override
preCreate(Activity activity)43     public void preCreate(Activity activity) {
44         super.preCreate(activity);
45         // Necessary because NativeUnitTestActivity uses BaseChromiumApplication which does not
46         // initialize ContextUtils.
47         ContextUtils.initApplicationContext(activity.getApplicationContext());
48 
49         // Necessary because BaseChromiumApplication no longer automatically initializes application
50         // tracking.
51         ApplicationStatus.initialize(activity.getApplication());
52 
53         // Needed by path_utils_unittest.cc
54         PathUtils.setPrivateDataDirectorySuffix("chrome");
55 
56         // Needed by system_monitor_unittest.cc
57         PowerMonitor.createForTests();
58 
59         // For NativeActivity based tests, dependency libraries must be loaded before
60         // NativeActivity::OnCreate, otherwise loading android.app.lib_name will fail
61         String libraryToLoad = activity.getIntent().getStringExtra(LIBRARY_UNDER_TEST_NAME);
62         loadLibraries(
63                 libraryToLoad != null ? new String[] {libraryToLoad} : NativeLibraries.LIBRARIES);
64     }
65 
loadLibraries(String[] librariesToLoad)66     private void loadLibraries(String[] librariesToLoad) {
67         LibraryLoader.setEnvForNative();
68         for (String library : librariesToLoad) {
69             // Do not load this library early so that
70             // |LibunwindstackUnwinderAndroidTest.ReparsesMapsOnNewDynamicLibraryLoad| test can
71             // observe the change in /proc/self/maps before and after loading the library.
72             if (library.equals("base_profiler_reparsing_test_support_library")) {
73                 continue;
74             }
75             Log.i(TAG, "loading: %s", library);
76             System.loadLibrary(library);
77             Log.i(TAG, "loaded: %s", library);
78         }
79         NativeUnitTestLibraryLoader.setLibrariesLoaded();
80     }
81 }