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.base; 6 7 import android.test.InstrumentationTestCase; 8 import android.test.suitebuilder.annotation.SmallTest; 9 10 import org.chromium.base.test.util.Feature; 11 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" }; 19 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(); 25 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"; 29 30 @Override setUp()31 public void setUp() throws Exception { 32 CommandLine.reset(); 33 } 34 checkInitSwitches()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")); 48 } 49 checkSettingThenGetting()50 void checkSettingThenGetting() { 51 CommandLine cl = CommandLine.getInstance(); 52 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)); 57 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))); 63 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"))); 74 } 75 checkTokenizer(String[] expected, String toParse)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]); 81 } 82 } 83 84 @SmallTest 85 @Feature({"Android-AppBase"}) testJavaInitialization()86 public void testJavaInitialization() { 87 CommandLine.init(INIT_SWITCHES); 88 checkInitSwitches(); 89 checkSettingThenGetting(); 90 } 91 92 @SmallTest 93 @Feature({"Android-AppBase"}) testBufferInitialization()94 public void testBufferInitialization() { 95 CommandLine.init(CommandLine.tokenizeQuotedAruments(INIT_SWITCHES_BUFFER)); 96 checkInitSwitches(); 97 checkSettingThenGetting(); 98 } 99 100 @SmallTest 101 @Feature({"Android-AppBase"}) testArgumentTokenizer()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", 105 "ij", 106 "k lm" }; 107 checkTokenizer(expected, toParse); 108 109 toParse = ""; 110 expected = new String[0]; 111 checkTokenizer(expected, toParse); 112 113 toParse = " \t\n"; 114 checkTokenizer(expected, toParse); 115 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", 119 "c\"d", 120 "e\"f", 121 "g'h", 122 "i\\'j", 123 "k\\\"l", 124 "mn\\'op", 125 "qr\\\"st"}; 126 checkTokenizer(expected, toParse); 127 } 128 } 129