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.browser;
7 import android.test.InstrumentationTestCase;
8 import android.test.suitebuilder.annotation.MediumTest;
10 import org.chromium.base.CommandLine;
11 import org.chromium.base.library_loader.LibraryLoader;
12 import org.chromium.base.library_loader.ProcessInitException;
13 import org.chromium.base.test.util.Feature;
14 import org.chromium.content_shell_apk.ContentShellActivity;
15 import org.chromium.content_shell_apk.ContentShellApplication;
18 * Test class for command lines.
20 public class ContentCommandLineTest extends InstrumentationTestCase {
21 // A reference command line. Note that switch2 is [brea\d], switch3 is [and "butter"],
22 // and switch4 is [a "quoted" 'food'!]
23 static final String INIT_SWITCHES[] = { "init_command", "--SWITCH", "Arg",
24 "--switch2=brea\\d", "--switch3=and \"butter\"",
25 "--switch4=a \"quoted\" 'food'!",
26 "--", "--actually_an_arg" };
28 // The same command line, but in quoted string format.
29 static final char INIT_SWITCHES_BUFFER[] =
30 ("init_command --SWITCH Arg --switch2=brea\\d --switch3=\"and \\\"butt\"er\\\" "
31 + "--switch4='a \"quoted\" \\'food\\'!' "
32 + "-- --actually_an_arg").toCharArray();
34 static final String CL_ADDED_SWITCH = "zappo-dappo-doggy-trainer";
35 static final String CL_ADDED_SWITCH_2 = "username";
36 static final String CL_ADDED_VALUE_2 = "bozo";
39 public void setUp() throws Exception {
44 assertFalse(CommandLine.getInstance().isNativeImplementation());
45 getInstrumentation().runOnMainSync(new Runnable() {
48 ContentShellApplication.initializeApplicationParameters();
50 LibraryLoader.ensureInitialized();
51 } catch (ProcessInitException e) {
56 assertTrue(CommandLine.getInstance().isNativeImplementation());
59 void checkInitSwitches() {
60 CommandLine cl = CommandLine.getInstance();
61 assertFalse(cl.hasSwitch("init_command"));
62 assertFalse(cl.hasSwitch("switch"));
63 assertTrue(cl.hasSwitch("SWITCH"));
64 assertFalse(cl.hasSwitch("--SWITCH"));
65 assertFalse(cl.hasSwitch("Arg"));
66 assertFalse(cl.hasSwitch("actually_an_arg"));
67 assertEquals("brea\\d", cl.getSwitchValue("switch2"));
68 assertEquals("and \"butter\"", cl.getSwitchValue("switch3"));
69 assertEquals("a \"quoted\" 'food'!", cl.getSwitchValue("switch4"));
70 assertNull(cl.getSwitchValue("SWITCH"));
71 assertNull(cl.getSwitchValue("non-existant"));
74 void checkSettingThenGetting() {
75 CommandLine cl = CommandLine.getInstance();
77 // Add a plain switch.
78 assertFalse(cl.hasSwitch(CL_ADDED_SWITCH));
79 cl.appendSwitch(CL_ADDED_SWITCH);
80 assertTrue(cl.hasSwitch(CL_ADDED_SWITCH));
82 // Add a switch paired with a value.
83 assertFalse(cl.hasSwitch(CL_ADDED_SWITCH_2));
84 assertNull(cl.getSwitchValue(CL_ADDED_SWITCH_2));
85 cl.appendSwitchWithValue(CL_ADDED_SWITCH_2, CL_ADDED_VALUE_2);
86 assertTrue(CL_ADDED_VALUE_2.equals(cl.getSwitchValue(CL_ADDED_SWITCH_2)));
88 // Append a few new things.
89 final String switchesAndArgs[] = { "dummy", "--superfast", "--speed=turbo" };
90 assertFalse(cl.hasSwitch("dummy"));
91 assertFalse(cl.hasSwitch("superfast"));
92 assertNull(cl.getSwitchValue("speed"));
93 cl.appendSwitchesAndArguments(switchesAndArgs);
94 assertFalse(cl.hasSwitch("dummy"));
95 assertFalse(cl.hasSwitch("command"));
96 assertTrue(cl.hasSwitch("superfast"));
97 assertTrue("turbo".equals(cl.getSwitchValue("speed")));
100 void checkAppendedSwitchesPassedThrough() {
101 CommandLine cl = CommandLine.getInstance();
102 assertTrue(cl.hasSwitch(CL_ADDED_SWITCH));
103 assertTrue(cl.hasSwitch(CL_ADDED_SWITCH_2));
104 assertTrue(CL_ADDED_VALUE_2.equals(cl.getSwitchValue(CL_ADDED_SWITCH_2)));
108 @Feature({"Android-AppBase"})
109 public void testJavaNativeTransition() {
110 CommandLine.init(INIT_SWITCHES);
114 checkSettingThenGetting();
118 @Feature({"Android-AppBase"})
119 public void testJavaNativeTransitionAfterAppends() {
120 CommandLine.init(INIT_SWITCHES);
122 checkSettingThenGetting();
125 checkAppendedSwitchesPassedThrough();
129 @Feature({"Android-AppBase"})
130 public void testNativeInitialization() {
131 CommandLine.init(null);
133 // Drop the program name for use with appendSwitchesAndArguments.
134 String[] args = new String[INIT_SWITCHES.length - 1];
135 System.arraycopy(INIT_SWITCHES, 1, args, 0, args.length);
136 CommandLine.getInstance().appendSwitchesAndArguments(args);
138 checkSettingThenGetting();
142 @Feature({"Android-AppBase"})
143 public void testFileInitialization() {
144 CommandLine.initFromFile(ContentShellActivity.COMMAND_LINE_FILE);
146 checkSettingThenGetting();