1 // Copyright 2014 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.chromium_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.base.library_loader.LibraryLoader;
18 import org.chromium.base.library_loader.Linker;
19 import org.chromium.base.library_loader.ProcessInitException;
20 import org.chromium.content.browser.BrowserStartupController;
21 import org.chromium.content.browser.ContentView;
22 import org.chromium.content.browser.ContentViewClient;
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;
29 * Test activity used for verifying the different configuration options for the ContentLinker.
31 public class ChromiumLinkerTestActivity extends Activity {
32 public static final String COMMAND_LINE_FILE =
33 "/data/local/tmp/chromium-linker-test-command-line";
35 private static final String TAG = "ChromiumLinkerTestActivity";
37 public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs";
39 // Use this on the command-line to simulate a low-memory device, otherwise
40 // a regular device is simulated by this test, independently from what the
41 // target device running the test really is.
42 private static final String LOW_MEMORY_DEVICE = "--low-memory-device";
44 private ShellManager mShellManager;
45 private WindowAndroid mWindowAndroid;
48 public void onCreate(final Bundle savedInstanceState) {
49 super.onCreate(savedInstanceState);
51 // Initializing the command line must occur before loading the library.
52 if (!CommandLine.isInitialized()) {
53 CommandLine.initFromFile(COMMAND_LINE_FILE);
54 String[] commandLineParams = getCommandLineParamsFromIntent(getIntent());
55 if (commandLineParams != null) {
56 CommandLine.getInstance().appendSwitchesAndArguments(commandLineParams);
59 waitForDebuggerIfNeeded();
61 // CommandLine.getInstance().hasSwitch() doesn't work here for some funky
62 // reason, so parse the command-line differently here:
63 boolean hasLowMemoryDeviceSwitch = false;
64 String[] cmdline = CommandLine.getJavaSwitchesOrNull();
66 Log.i(TAG, "Command line is null");
68 Log.i(TAG, "Command line is:");
69 for (int n = 0; n < cmdline.length; ++n) {
70 Log.i(TAG, " '" + cmdline[n] + "'");
71 if (cmdline[n].equals(LOW_MEMORY_DEVICE))
72 hasLowMemoryDeviceSwitch = true;
76 // Determine which kind of device to simulate from the command-line.
77 int memoryDeviceConfig = Linker.MEMORY_DEVICE_CONFIG_NORMAL;
78 if (hasLowMemoryDeviceSwitch)
79 memoryDeviceConfig = Linker.MEMORY_DEVICE_CONFIG_LOW;
80 Linker.setMemoryDeviceConfig(memoryDeviceConfig);
82 // Register the test runner class by name.
83 Linker.setTestRunnerClassName(LinkerTests.class.getName());
85 // Load the library in the browser process, this will also run the test
86 // runner in this process.
88 LibraryLoader.ensureInitialized();
89 } catch (ProcessInitException e) {
90 Log.i(TAG, "Cannot load chromium_linker_test:" + e);
93 // Now, start a new renderer process by creating a new view.
94 // This will run the test runner in the renderer process.
96 BrowserStartupController.get(getApplicationContext()).initChromiumBrowserProcessForTests();
98 LayoutInflater inflater =
99 (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
100 View view = inflater.inflate(R.layout.test_activity, null);
101 mShellManager = (ShellManager) view.findViewById(R.id.shell_container);
102 mWindowAndroid = new ActivityWindowAndroid(this);
103 mShellManager.setWindow(mWindowAndroid);
105 mShellManager.setStartupUrl("about:blank");
108 BrowserStartupController.get(this).startBrowserProcessesAsync(
109 new BrowserStartupController.StartupCallback() {
111 public void onSuccess(boolean alreadyStarted) {
112 finishInitialization(savedInstanceState);
116 public void onFailure() {
117 initializationFailed();
120 } catch (ProcessInitException e) {
121 Log.e(TAG, "Unable to load native library.", e);
125 // TODO(digit): Ensure that after the content view is initialized,
126 // the program finishes().
129 private void finishInitialization(Bundle savedInstanceState) {
130 String shellUrl = ShellManager.DEFAULT_SHELL_URL;
131 mShellManager.launchShell(shellUrl);
132 getActiveContentView().setContentViewClient(new ContentViewClient());
135 private void initializationFailed() {
136 Log.e(TAG, "ContentView initialization failed.");
141 protected void onSaveInstanceState(Bundle outState) {
142 super.onSaveInstanceState(outState);
143 mWindowAndroid.saveInstanceState(outState);
146 private void waitForDebuggerIfNeeded() {
147 if (CommandLine.getInstance().hasSwitch(BaseSwitches.WAIT_FOR_JAVA_DEBUGGER)) {
148 Log.e(TAG, "Waiting for Java debugger to connect...");
149 android.os.Debug.waitForDebugger();
150 Log.e(TAG, "Java debugger connected. Resuming execution.");
155 protected void onStop() {
158 ContentView view = getActiveContentView();
159 if (view != null) view.onHide();
163 protected void onStart() {
166 ContentView view = getActiveContentView();
167 if (view != null) view.onShow();
171 public void onActivityResult(int requestCode, int resultCode, Intent data) {
172 super.onActivityResult(requestCode, resultCode, data);
173 mWindowAndroid.onActivityResult(requestCode, resultCode, data);
176 private static String[] getCommandLineParamsFromIntent(Intent intent) {
177 return intent != null ? intent.getStringArrayExtra(COMMAND_LINE_ARGS_KEY) : null;
181 * @return The {@link ContentView} owned by the currently visible {@link Shell} or null if one
184 public ContentView getActiveContentView() {
185 if (mShellManager == null)
188 Shell shell = mShellManager.getActiveShell();
192 return shell.getContentView();