1 // Copyright 2013 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.
5 package org.chromium.content_linker_test_apk;
7 import android.app.Activity;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.os.Bundle;
11 import android.util.Log;
12 import android.view.LayoutInflater;
13 import android.view.View;
15 import org.chromium.base.BaseSwitches;
16 import org.chromium.base.CommandLine;
17 import org.chromium.content.app.LibraryLoader;
18 import org.chromium.content.app.Linker;
19 import org.chromium.content.browser.BrowserStartupController;
20 import org.chromium.content.browser.ContentView;
21 import org.chromium.content.browser.ContentViewClient;
22 import org.chromium.content.common.ProcessInitException;
23 import org.chromium.content_shell.Shell;
24 import org.chromium.content_shell.ShellManager;
25 import org.chromium.ui.base.ActivityWindowAndroid;
26 import org.chromium.ui.base.WindowAndroid;
28 public class ContentLinkerTestActivity extends Activity {
29 public static final String COMMAND_LINE_FILE =
30 "/data/local/tmp/content-linker-test-command-line";
32 private static final String TAG = "ContentLinkerTestActivity";
34 public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs";
36 // Use this on the command-line to simulate a low-memory device, otherwise
37 // a regular device is simulated by this test, independently from what the
38 // target device running the test really is.
39 private static final String LOW_MEMORY_DEVICE = "--low-memory-device";
41 private ShellManager mShellManager;
42 private WindowAndroid mWindowAndroid;
45 public void onCreate(final Bundle savedInstanceState) {
46 super.onCreate(savedInstanceState);
48 // Initializing the command line must occur before loading the library.
49 if (!CommandLine.isInitialized()) {
50 CommandLine.initFromFile(COMMAND_LINE_FILE);
51 String[] commandLineParams = getCommandLineParamsFromIntent(getIntent());
52 if (commandLineParams != null) {
53 CommandLine.getInstance().appendSwitchesAndArguments(commandLineParams);
56 waitForDebuggerIfNeeded();
58 // CommandLine.getInstance().hasSwitch() doesn't work here for some funky
59 // reason, so parse the command-line differently here:
60 boolean hasLowMemoryDeviceSwitch = false;
61 String[] cmdline = CommandLine.getJavaSwitchesOrNull();
63 Log.i(TAG, "Command line is null");
65 Log.i(TAG, "Command line is:");
66 for (int n = 0; n < cmdline.length; ++n) {
67 Log.i(TAG, " '" + cmdline[n] + "'");
68 if (cmdline[n].equals(LOW_MEMORY_DEVICE))
69 hasLowMemoryDeviceSwitch = true;
73 // Determine which kind of device to simulate from the command-line.
74 int memoryDeviceConfig = Linker.MEMORY_DEVICE_CONFIG_NORMAL;
75 if (hasLowMemoryDeviceSwitch)
76 memoryDeviceConfig = Linker.MEMORY_DEVICE_CONFIG_LOW;
77 Linker.setMemoryDeviceConfig(memoryDeviceConfig);
79 // Register the test runner class by name.
80 Linker.setTestRunnerClassName(LinkerTests.class.getName());
82 // Load the library in the browser process, this will also run the test
83 // runner in this process.
85 LibraryLoader.ensureInitialized();
86 } catch (ProcessInitException e) {
87 Log.i(TAG, "Cannot load content_linker_test:" + e);
90 // Now, start a new renderer process by creating a new view.
91 // This will run the test runner in the renderer process.
93 BrowserStartupController.get(getApplicationContext()).initChromiumBrowserProcessForTests();
95 LayoutInflater inflater =
96 (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
97 View view = inflater.inflate(R.layout.test_activity, null);
98 mShellManager = (ShellManager) view.findViewById(R.id.shell_container);
99 mWindowAndroid = new ActivityWindowAndroid(this);
100 mShellManager.setWindow(mWindowAndroid);
102 mShellManager.setStartupUrl("about:blank");
104 BrowserStartupController.get(this).startBrowserProcessesAsync(
105 new BrowserStartupController.StartupCallback() {
107 public void onSuccess(boolean alreadyStarted) {
108 finishInitialization(savedInstanceState);
112 public void onFailure() {
113 initializationFailed();
117 // TODO(digit): Ensure that after the content view is initialized,
118 // the program finishes().
121 private void finishInitialization(Bundle savedInstanceState) {
122 String shellUrl = ShellManager.DEFAULT_SHELL_URL;
123 mShellManager.launchShell(shellUrl);
124 getActiveContentView().setContentViewClient(new ContentViewClient());
127 private void initializationFailed() {
128 Log.e(TAG, "ContentView initialization failed.");
133 protected void onSaveInstanceState(Bundle outState) {
134 super.onSaveInstanceState(outState);
135 mWindowAndroid.saveInstanceState(outState);
138 private void waitForDebuggerIfNeeded() {
139 if (CommandLine.getInstance().hasSwitch(BaseSwitches.WAIT_FOR_JAVA_DEBUGGER)) {
140 Log.e(TAG, "Waiting for Java debugger to connect...");
141 android.os.Debug.waitForDebugger();
142 Log.e(TAG, "Java debugger connected. Resuming execution.");
147 protected void onStop() {
150 ContentView view = getActiveContentView();
151 if (view != null) view.onHide();
155 protected void onStart() {
158 ContentView view = getActiveContentView();
159 if (view != null) view.onShow();
163 public void onActivityResult(int requestCode, int resultCode, Intent data) {
164 super.onActivityResult(requestCode, resultCode, data);
165 mWindowAndroid.onActivityResult(requestCode, resultCode, data);
168 private static String getUrlFromIntent(Intent intent) {
169 return intent != null ? intent.getDataString() : null;
172 private static String[] getCommandLineParamsFromIntent(Intent intent) {
173 return intent != null ? intent.getStringArrayExtra(COMMAND_LINE_ARGS_KEY) : null;
177 * @return The {@link ContentView} owned by the currently visible {@link Shell} or null if one
180 public ContentView getActiveContentView() {
181 if (mShellManager == null)
184 Shell shell = mShellManager.getActiveShell();
188 return shell.getContentView();