• 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.os.Bundle;
8 
9 import androidx.fragment.app.FragmentActivity;
10 
11 import java.io.File;
12 
13 /**
14  * An {@link android.app.Activity} for running native browser tests.
15  */
16 public abstract class NativeBrowserTestActivity extends FragmentActivity {
17     private static final String TAG = "NativeTest";
18 
19     private NativeTest mTest = new NativeTest();
20     private boolean mStarted;
21 
22     @Override
onCreate(Bundle savedInstanceState)23     public void onCreate(Bundle savedInstanceState) {
24         mTest.preCreate(this);
25         super.onCreate(savedInstanceState);
26         mTest.postCreate(this);
27         for (String flag : NativeBrowserTest.BROWSER_TESTS_FLAGS) {
28             appendCommandLineFlags(flag);
29         }
30 
31         String userDataDirSwitch = getUserDataDirectoryCommandLineSwitch();
32         if (!userDataDirSwitch.isEmpty()) {
33             String userDataDirFlag = "--" + userDataDirSwitch + "=" + getPrivateDataDirectory();
34             appendCommandLineFlags(userDataDirFlag);
35         }
36     }
37 
38     @Override
onStart()39     public void onStart() {
40         super.onStart();
41 
42         // onStart can be called any number of times see:
43         // https://developer.android.com/guide/components/activities/activity-lifecycle#onstart
44         // We only want to run the test once (or bad things can happen) so bail out if we've
45         // already started.
46         if (mStarted) return;
47 
48         mStarted = true;
49         NativeBrowserTest.deletePrivateDataDirectory(getPrivateDataDirectory());
50         initializeBrowserProcess();
51     }
52 
runTests()53     protected void runTests() {
54         mTest.postStart(this, false);
55     }
56 
appendCommandLineFlags(String flags)57     public void appendCommandLineFlags(String flags) {
58         mTest.appendCommandLineFlags(flags);
59     }
60 
61     /** Returns the test suite's private data directory. */
getPrivateDataDirectory()62     protected abstract File getPrivateDataDirectory();
63 
64     /**
65      * Returns the command line switch used to specify the user data directory.
66      *
67      *  The default implementation returns an empty string, which means no user
68      *  data directory.
69      *  If this method returns a non-empty value, the user data directory will be overridden to be
70      *  the private data directory, which is cleared at the beginning of each test run.
71      *  NOTE: The switch should not start with "--".
72      *  TODO(crbug.com/617734): Solve this problem holistically for Java and C++ at the level of
73      *  DIR_ANDROID_APP_DATA and eliminate the need for this solution.
74      */
getUserDataDirectoryCommandLineSwitch()75     protected String getUserDataDirectoryCommandLineSwitch() {
76         return "";
77     }
78 
79     /** Initializes the browser process.
80      *
81      *  This generally includes loading native libraries and switching to the native command line,
82      *  among other things.
83      */
initializeBrowserProcess()84     protected abstract void initializeBrowserProcess();
85 }
86