Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / content / shell / android / linker_test_apk / src / org / chromium / content_linker_test_apk / ContentLinkerTestActivity.java
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.
4
5 package org.chromium.content_linker_test_apk;
6
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;
14
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;
27
28 public class ContentLinkerTestActivity extends Activity {
29     public static final String COMMAND_LINE_FILE =
30             "/data/local/tmp/content-linker-test-command-line";
31
32     private static final String TAG = "ContentLinkerTestActivity";
33
34     public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs";
35
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";
40
41     private ShellManager mShellManager;
42     private WindowAndroid mWindowAndroid;
43
44     @Override
45     public void onCreate(final Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47
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);
54             }
55         }
56         waitForDebuggerIfNeeded();
57
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();
62         if (cmdline == null)
63             Log.i(TAG, "Command line is null");
64         else {
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;
70             }
71         }
72
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);
78
79         // Register the test runner class by name.
80         Linker.setTestRunnerClassName(LinkerTests.class.getName());
81
82         // Load the library in the browser process, this will also run the test
83         // runner in this process.
84         try {
85             LibraryLoader.ensureInitialized();
86         } catch (ProcessInitException e) {
87             Log.i(TAG, "Cannot load content_linker_test:" +  e);
88         }
89
90         // Now, start a new renderer process by creating a new view.
91         // This will run the test runner in the renderer process.
92
93         BrowserStartupController.get(getApplicationContext()).initChromiumBrowserProcessForTests();
94
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);
101
102         mShellManager.setStartupUrl("about:blank");
103
104         BrowserStartupController.get(this).startBrowserProcessesAsync(
105                 new BrowserStartupController.StartupCallback() {
106             @Override
107             public void onSuccess(boolean alreadyStarted) {
108                 finishInitialization(savedInstanceState);
109             }
110
111             @Override
112             public void onFailure() {
113                 initializationFailed();
114             }
115         });
116
117         // TODO(digit): Ensure that after the content view is initialized,
118         // the program finishes().
119     }
120
121     private void finishInitialization(Bundle savedInstanceState) {
122         String shellUrl = ShellManager.DEFAULT_SHELL_URL;
123         mShellManager.launchShell(shellUrl);
124         getActiveContentView().setContentViewClient(new ContentViewClient());
125     }
126
127     private void initializationFailed() {
128         Log.e(TAG, "ContentView initialization failed.");
129         finish();
130     }
131
132     @Override
133     protected void onSaveInstanceState(Bundle outState) {
134         super.onSaveInstanceState(outState);
135         mWindowAndroid.saveInstanceState(outState);
136     }
137
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.");
143         }
144     }
145
146     @Override
147     protected void onStop() {
148         super.onStop();
149
150         ContentView view = getActiveContentView();
151         if (view != null) view.onHide();
152     }
153
154     @Override
155     protected void onStart() {
156         super.onStart();
157
158         ContentView view = getActiveContentView();
159         if (view != null) view.onShow();
160     }
161
162     @Override
163     public void onActivityResult(int requestCode, int resultCode, Intent data) {
164         super.onActivityResult(requestCode, resultCode, data);
165         mWindowAndroid.onActivityResult(requestCode, resultCode, data);
166     }
167
168     private static String getUrlFromIntent(Intent intent) {
169         return intent != null ? intent.getDataString() : null;
170     }
171
172     private static String[] getCommandLineParamsFromIntent(Intent intent) {
173         return intent != null ? intent.getStringArrayExtra(COMMAND_LINE_ARGS_KEY) : null;
174     }
175
176     /**
177      * @return The {@link ContentView} owned by the currently visible {@link Shell} or null if one
178      *         is not showing.
179      */
180     public ContentView getActiveContentView() {
181         if (mShellManager == null)
182             return null;
183
184         Shell shell = mShellManager.getActiveShell();
185         if (shell == null)
186             return null;
187
188         return shell.getContentView();
189     }
190 }