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.ContentShellApplication;
17 * Test class for command lines.
19 public class ContentCommandLineTest extends InstrumentationTestCase {
20 // A reference command line. Note that switch2 is [brea\d], switch3 is [and "butter"],
21 // and switch4 is [a "quoted" 'food'!]
22 static final String INIT_SWITCHES[] = { "init_command", "--SWITCH", "Arg",
23 "--switch2=brea\\d", "--switch3=and \"butter\"",
24 "--switch4=a \"quoted\" 'food'!",
25 "--", "--actually_an_arg" };
27 // The same command line, but in quoted string format.
28 static final char INIT_SWITCHES_BUFFER[] =
29 ("init_command --SWITCH Arg --switch2=brea\\d --switch3=\"and \\\"butt\"er\\\" "
30 + "--switch4='a \"quoted\" \\'food\\'!' "
31 + "-- --actually_an_arg").toCharArray();
33 static final String CL_ADDED_SWITCH = "zappo-dappo-doggy-trainer";
34 static final String CL_ADDED_SWITCH_2 = "username";
35 static final String CL_ADDED_VALUE_2 = "bozo";
38 public void setUp() throws Exception {
43 assertFalse(CommandLine.getInstance().isNativeImplementation());
44 getInstrumentation().runOnMainSync(new Runnable() {
47 ContentShellApplication.initializeApplicationParameters();
49 LibraryLoader.ensureInitialized();
50 } catch (ProcessInitException e) {
55 assertTrue(CommandLine.getInstance().isNativeImplementation());
58 void checkInitSwitches() {
59 CommandLine cl = CommandLine.getInstance();
60 assertFalse(cl.hasSwitch("init_command"));
61 assertFalse(cl.hasSwitch("switch"));
62 assertTrue(cl.hasSwitch("SWITCH"));
63 assertFalse(cl.hasSwitch("--SWITCH"));
64 assertFalse(cl.hasSwitch("Arg"));
65 assertFalse(cl.hasSwitch("actually_an_arg"));
66 assertEquals("brea\\d", cl.getSwitchValue("switch2"));
67 assertEquals("and \"butter\"", cl.getSwitchValue("switch3"));
68 assertEquals("a \"quoted\" 'food'!", cl.getSwitchValue("switch4"));
69 assertNull(cl.getSwitchValue("SWITCH"));
70 assertNull(cl.getSwitchValue("non-existant"));
73 void checkSettingThenGetting() {
74 CommandLine cl = CommandLine.getInstance();
76 // Add a plain switch.
77 assertFalse(cl.hasSwitch(CL_ADDED_SWITCH));
78 cl.appendSwitch(CL_ADDED_SWITCH);
79 assertTrue(cl.hasSwitch(CL_ADDED_SWITCH));
81 // Add a switch paired with a value.
82 assertFalse(cl.hasSwitch(CL_ADDED_SWITCH_2));
83 assertNull(cl.getSwitchValue(CL_ADDED_SWITCH_2));
84 cl.appendSwitchWithValue(CL_ADDED_SWITCH_2, CL_ADDED_VALUE_2);
85 assertTrue(CL_ADDED_VALUE_2.equals(cl.getSwitchValue(CL_ADDED_SWITCH_2)));
87 // Append a few new things.
88 final String switchesAndArgs[] = { "dummy", "--superfast", "--speed=turbo" };
89 assertFalse(cl.hasSwitch("dummy"));
90 assertFalse(cl.hasSwitch("superfast"));
91 assertNull(cl.getSwitchValue("speed"));
92 cl.appendSwitchesAndArguments(switchesAndArgs);
93 assertFalse(cl.hasSwitch("dummy"));
94 assertFalse(cl.hasSwitch("command"));
95 assertTrue(cl.hasSwitch("superfast"));
96 assertTrue("turbo".equals(cl.getSwitchValue("speed")));
99 void checkAppendedSwitchesPassedThrough() {
100 CommandLine cl = CommandLine.getInstance();
101 assertTrue(cl.hasSwitch(CL_ADDED_SWITCH));
102 assertTrue(cl.hasSwitch(CL_ADDED_SWITCH_2));
103 assertTrue(CL_ADDED_VALUE_2.equals(cl.getSwitchValue(CL_ADDED_SWITCH_2)));
107 @Feature({"Android-AppBase"})
108 public void testJavaNativeTransition() {
109 CommandLine.init(INIT_SWITCHES);
113 checkSettingThenGetting();
117 @Feature({"Android-AppBase"})
118 public void testJavaNativeTransitionAfterAppends() {
119 CommandLine.init(INIT_SWITCHES);
121 checkSettingThenGetting();
124 checkAppendedSwitchesPassedThrough();
128 @Feature({"Android-AppBase"})
129 public void testNativeInitialization() {
130 CommandLine.init(null);
132 // Drop the program name for use with appendSwitchesAndArguments.
133 String[] args = new String[INIT_SWITCHES.length - 1];
134 System.arraycopy(INIT_SWITCHES, 1, args, 0, args.length);
135 CommandLine.getInstance().appendSwitchesAndArguments(args);
137 checkSettingThenGetting();
141 @Feature({"Android-AppBase"})
142 public void testFileInitialization() {
143 CommandLine.initFromFile(ContentShellApplication.COMMAND_LINE_FILE);
145 checkSettingThenGetting();