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