1 // Copyright (c) 2012 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.browser; 6 7 import android.test.InstrumentationTestCase; 8 import android.test.suitebuilder.annotation.MediumTest; 9 import android.test.suitebuilder.annotation.SmallTest; 10 11 import org.chromium.base.test.util.Feature; 12 import org.chromium.content.app.LibraryLoader; 13 import org.chromium.content.common.CommandLine; 14 import org.chromium.content.common.ProcessInitException; 15 import org.chromium.content_shell_apk.ContentShellActivity; 16 import org.chromium.content_shell_apk.ContentShellApplication; 17 18 public class CommandLineTest extends InstrumentationTestCase { 19 // A reference command line. Note that switch2 is [brea\d], switch3 is [and "butter"], 20 // and switch4 is [a "quoted" 'food'!] 21 static final String INIT_SWITCHES[] = { "init_command", "--SWITCH", "Arg", 22 "--switch2=brea\\d", "--switch3=and \"butter\"", 23 "--switch4=a \"quoted\" 'food'!", 24 "--", "--actually_an_arg" }; 25 26 // The same command line, but in quoted string format. 27 static final char INIT_SWITCHES_BUFFER[] = 28 ("init_command --SWITCH Arg --switch2=brea\\d --switch3=\"and \\\"butt\"er\\\" " 29 + "--switch4='a \"quoted\" \\'food\\'!' " 30 + "-- --actually_an_arg").toCharArray(); 31 32 static final String CL_ADDED_SWITCH = "zappo-dappo-doggy-trainer"; 33 static final String CL_ADDED_SWITCH_2 = "username"; 34 static final String CL_ADDED_VALUE_2 = "bozo"; 35 36 @Override 37 public void setUp() throws Exception { 38 CommandLine.reset(); 39 } 40 41 void loadJni() { 42 assertFalse(CommandLine.getInstance().isNativeImplementation()); 43 getInstrumentation().runOnMainSync(new Runnable() { 44 @Override 45 public void run() { 46 ContentShellApplication.initializeApplicationParameters(); 47 try { 48 LibraryLoader.ensureInitialized(); 49 } catch (ProcessInitException e) { 50 throw new Error(e); 51 } 52 } 53 }); 54 assertTrue(CommandLine.getInstance().isNativeImplementation()); 55 } 56 57 void checkInitSwitches() { 58 CommandLine cl = CommandLine.getInstance(); 59 assertFalse(cl.hasSwitch("init_command")); 60 assertFalse(cl.hasSwitch("switch")); 61 assertTrue(cl.hasSwitch("SWITCH")); 62 assertFalse(cl.hasSwitch("--SWITCH")); 63 assertFalse(cl.hasSwitch("Arg")); 64 assertFalse(cl.hasSwitch("actually_an_arg")); 65 assertEquals("brea\\d", cl.getSwitchValue("switch2")); 66 assertEquals("and \"butter\"", cl.getSwitchValue("switch3")); 67 assertEquals("a \"quoted\" 'food'!", cl.getSwitchValue("switch4")); 68 assertNull(cl.getSwitchValue("SWITCH")); 69 assertNull(cl.getSwitchValue("non-existant")); 70 } 71 72 void checkSettingThenGetting() { 73 CommandLine cl = CommandLine.getInstance(); 74 75 // Add a plain switch. 76 assertFalse(cl.hasSwitch(CL_ADDED_SWITCH)); 77 cl.appendSwitch(CL_ADDED_SWITCH); 78 assertTrue(cl.hasSwitch(CL_ADDED_SWITCH)); 79 80 // Add a switch paired with a value. 81 assertFalse(cl.hasSwitch(CL_ADDED_SWITCH_2)); 82 assertNull(cl.getSwitchValue(CL_ADDED_SWITCH_2)); 83 cl.appendSwitchWithValue(CL_ADDED_SWITCH_2, CL_ADDED_VALUE_2); 84 assertTrue(CL_ADDED_VALUE_2.equals(cl.getSwitchValue(CL_ADDED_SWITCH_2))); 85 86 // Append a few new things. 87 final String switchesAndArgs[] = { "dummy", "--superfast", "--speed=turbo" }; 88 assertFalse(cl.hasSwitch("dummy")); 89 assertFalse(cl.hasSwitch("superfast")); 90 assertNull(cl.getSwitchValue("speed")); 91 cl.appendSwitchesAndArguments(switchesAndArgs); 92 assertFalse(cl.hasSwitch("dummy")); 93 assertFalse(cl.hasSwitch("command")); 94 assertTrue(cl.hasSwitch("superfast")); 95 assertTrue("turbo".equals(cl.getSwitchValue("speed"))); 96 } 97 98 void checkAppendedSwitchesPassedThrough() { 99 CommandLine cl = CommandLine.getInstance(); 100 assertTrue(cl.hasSwitch(CL_ADDED_SWITCH)); 101 assertTrue(cl.hasSwitch(CL_ADDED_SWITCH_2)); 102 assertTrue(CL_ADDED_VALUE_2.equals(cl.getSwitchValue(CL_ADDED_SWITCH_2))); 103 } 104 105 void checkTokenizer(String[] expected, String toParse) { 106 String[] actual = CommandLine.tokenizeQuotedAruments(toParse.toCharArray()); 107 assertEquals(expected.length, actual.length); 108 for (int i = 0; i < expected.length; ++i) { 109 assertEquals("comparing element " + i, expected[i], actual[i]); 110 } 111 } 112 113 @SmallTest 114 @Feature({"Android-AppBase"}) 115 public void testJavaInitialization() { 116 CommandLine.init(INIT_SWITCHES); 117 checkInitSwitches(); 118 checkSettingThenGetting(); 119 } 120 121 @MediumTest 122 @Feature({"Android-AppBase"}) 123 public void testJavaNativeTransition() { 124 CommandLine.init(INIT_SWITCHES); 125 checkInitSwitches(); 126 loadJni(); 127 checkInitSwitches(); 128 checkSettingThenGetting(); 129 } 130 131 @MediumTest 132 @Feature({"Android-AppBase"}) 133 public void testJavaNativeTransitionAfterAppends() { 134 CommandLine.init(INIT_SWITCHES); 135 checkInitSwitches(); 136 checkSettingThenGetting(); 137 loadJni(); 138 checkInitSwitches(); 139 checkAppendedSwitchesPassedThrough(); 140 } 141 142 @MediumTest 143 @Feature({"Android-AppBase"}) 144 public void testNativeInitialization() { 145 CommandLine.init(null); 146 loadJni(); 147 // Drop the program name for use with appendSwitchesAndArguments. 148 String[] args = new String[INIT_SWITCHES.length - 1]; 149 System.arraycopy(INIT_SWITCHES, 1, args, 0, args.length); 150 CommandLine.getInstance().appendSwitchesAndArguments(args); 151 checkInitSwitches(); 152 checkSettingThenGetting(); 153 } 154 155 @SmallTest 156 @Feature({"Android-AppBase"}) 157 public void testBufferInitialization() { 158 CommandLine.init(CommandLine.tokenizeQuotedAruments(INIT_SWITCHES_BUFFER)); 159 checkInitSwitches(); 160 checkSettingThenGetting(); 161 } 162 163 @SmallTest 164 @Feature({"Android-AppBase"}) 165 public void testArgumentTokenizer() { 166 String toParse = " a\"\\bc de\\\"f g\"\\h ij k\" \"lm"; 167 String[] expected = { "a\\bc de\"f g\\h", 168 "ij", 169 "k lm" }; 170 checkTokenizer(expected, toParse); 171 172 toParse = ""; 173 expected = new String[0]; 174 checkTokenizer(expected, toParse); 175 176 toParse = " \t\n"; 177 checkTokenizer(expected, toParse); 178 179 toParse = " \"a'b\" 'c\"d' \"e\\\"f\" 'g\\'h' \"i\\'j\" 'k\\\"l'" + 180 " m\"n\\'o\"p q'r\\\"s't"; 181 expected = new String[] { "a'b", 182 "c\"d", 183 "e\"f", 184 "g'h", 185 "i\\'j", 186 "k\\\"l", 187 "mn\\'op", 188 "qr\\\"st",}; 189 checkTokenizer(expected, toParse); 190 } 191 192 @MediumTest 193 @Feature({"Android-AppBase"}) 194 public void testFileInitialization() { 195 CommandLine.initFromFile(ContentShellActivity.COMMAND_LINE_FILE); 196 loadJni(); 197 checkSettingThenGetting(); 198 } 199 } 200