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.base;
7 import android.test.InstrumentationTestCase;
8 import android.test.suitebuilder.annotation.SmallTest;
10 import org.chromium.base.test.util.Feature;
12 public class CommandLineTest extends InstrumentationTestCase {
13 // A reference command line. Note that switch2 is [brea\d], switch3 is [and "butter"],
14 // and switch4 is [a "quoted" 'food'!]
15 static final String INIT_SWITCHES[] = { "init_command", "--SWITCH", "Arg",
16 "--switch2=brea\\d", "--switch3=and \"butter\"",
17 "--switch4=a \"quoted\" 'food'!",
18 "--", "--actually_an_arg" };
20 // The same command line, but in quoted string format.
21 static final char INIT_SWITCHES_BUFFER[] =
22 ("init_command --SWITCH Arg --switch2=brea\\d --switch3=\"and \\\"butt\"er\\\" "
23 + "--switch4='a \"quoted\" \\'food\\'!' "
24 + "-- --actually_an_arg").toCharArray();
26 static final String CL_ADDED_SWITCH = "zappo-dappo-doggy-trainer";
27 static final String CL_ADDED_SWITCH_2 = "username";
28 static final String CL_ADDED_VALUE_2 = "bozo";
31 public void setUp() throws Exception {
35 void checkInitSwitches() {
36 CommandLine cl = CommandLine.getInstance();
37 assertFalse(cl.hasSwitch("init_command"));
38 assertFalse(cl.hasSwitch("switch"));
39 assertTrue(cl.hasSwitch("SWITCH"));
40 assertFalse(cl.hasSwitch("--SWITCH"));
41 assertFalse(cl.hasSwitch("Arg"));
42 assertFalse(cl.hasSwitch("actually_an_arg"));
43 assertEquals("brea\\d", cl.getSwitchValue("switch2"));
44 assertEquals("and \"butter\"", cl.getSwitchValue("switch3"));
45 assertEquals("a \"quoted\" 'food'!", cl.getSwitchValue("switch4"));
46 assertNull(cl.getSwitchValue("SWITCH"));
47 assertNull(cl.getSwitchValue("non-existant"));
50 void checkSettingThenGetting() {
51 CommandLine cl = CommandLine.getInstance();
53 // Add a plain switch.
54 assertFalse(cl.hasSwitch(CL_ADDED_SWITCH));
55 cl.appendSwitch(CL_ADDED_SWITCH);
56 assertTrue(cl.hasSwitch(CL_ADDED_SWITCH));
58 // Add a switch paired with a value.
59 assertFalse(cl.hasSwitch(CL_ADDED_SWITCH_2));
60 assertNull(cl.getSwitchValue(CL_ADDED_SWITCH_2));
61 cl.appendSwitchWithValue(CL_ADDED_SWITCH_2, CL_ADDED_VALUE_2);
62 assertTrue(CL_ADDED_VALUE_2.equals(cl.getSwitchValue(CL_ADDED_SWITCH_2)));
64 // Append a few new things.
65 final String switchesAndArgs[] = { "dummy", "--superfast", "--speed=turbo" };
66 assertFalse(cl.hasSwitch("dummy"));
67 assertFalse(cl.hasSwitch("superfast"));
68 assertNull(cl.getSwitchValue("speed"));
69 cl.appendSwitchesAndArguments(switchesAndArgs);
70 assertFalse(cl.hasSwitch("dummy"));
71 assertFalse(cl.hasSwitch("command"));
72 assertTrue(cl.hasSwitch("superfast"));
73 assertTrue("turbo".equals(cl.getSwitchValue("speed")));
76 void checkTokenizer(String[] expected, String toParse) {
77 String[] actual = CommandLine.tokenizeQuotedAruments(toParse.toCharArray());
78 assertEquals(expected.length, actual.length);
79 for (int i = 0; i < expected.length; ++i) {
80 assertEquals("comparing element " + i, expected[i], actual[i]);
85 @Feature({"Android-AppBase"})
86 public void testJavaInitialization() {
87 CommandLine.init(INIT_SWITCHES);
89 checkSettingThenGetting();
93 @Feature({"Android-AppBase"})
94 public void testBufferInitialization() {
95 CommandLine.init(CommandLine.tokenizeQuotedAruments(INIT_SWITCHES_BUFFER));
97 checkSettingThenGetting();
101 @Feature({"Android-AppBase"})
102 public void testArgumentTokenizer() {
103 String toParse = " a\"\\bc de\\\"f g\"\\h ij k\" \"lm";
104 String[] expected = { "a\\bc de\"f g\\h",
107 checkTokenizer(expected, toParse);
110 expected = new String[0];
111 checkTokenizer(expected, toParse);
114 checkTokenizer(expected, toParse);
116 toParse = " \"a'b\" 'c\"d' \"e\\\"f\" 'g\\'h' \"i\\'j\" 'k\\\"l'" +
117 " m\"n\\'o\"p q'r\\\"s't";
118 expected = new String[] { "a'b",
126 checkTokenizer(expected, toParse);