1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file. 42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)package org.chromium.content.browser; 62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)import android.test.InstrumentationTestCase; 82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)import android.test.suitebuilder.annotation.MediumTest; 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)import org.chromium.base.CommandLine; 115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import org.chromium.base.library_loader.LibraryLoader; 125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import org.chromium.base.library_loader.ProcessInitException; 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)import org.chromium.base.test.util.Feature; 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)import org.chromium.content_shell_apk.ContentShellActivity; 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)import org.chromium.content_shell_apk.ContentShellApplication; 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/** 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Test class for command lines. 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) */ 20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)public class ContentCommandLineTest extends InstrumentationTestCase { 212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // A reference command line. Note that switch2 is [brea\d], switch3 is [and "butter"], 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // and switch4 is [a "quoted" 'food'!] 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) static final String INIT_SWITCHES[] = { "init_command", "--SWITCH", "Arg", 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) "--switch2=brea\\d", "--switch3=and \"butter\"", 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) "--switch4=a \"quoted\" 'food'!", 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) "--", "--actually_an_arg" }; 272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // The same command line, but in quoted string format. 292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) static final char INIT_SWITCHES_BUFFER[] = 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ("init_command --SWITCH Arg --switch2=brea\\d --switch3=\"and \\\"butt\"er\\\" " 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) + "--switch4='a \"quoted\" \\'food\\'!' " 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) + "-- --actually_an_arg").toCharArray(); 332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) static final String CL_ADDED_SWITCH = "zappo-dappo-doggy-trainer"; 352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) static final String CL_ADDED_SWITCH_2 = "username"; 362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) static final String CL_ADDED_VALUE_2 = "bozo"; 372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) @Override 392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public void setUp() throws Exception { 402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CommandLine.reset(); 412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void loadJni() { 442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertFalse(CommandLine.getInstance().isNativeImplementation()); 452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) getInstrumentation().runOnMainSync(new Runnable() { 462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) @Override 472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public void run() { 482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ContentShellApplication.initializeApplicationParameters(); 492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) try { 50c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch LibraryLoader.ensureInitialized(); 512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } catch (ProcessInitException e) { 522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) throw new Error(e); 532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) }); 562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertTrue(CommandLine.getInstance().isNativeImplementation()); 572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void checkInitSwitches() { 602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CommandLine cl = CommandLine.getInstance(); 612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertFalse(cl.hasSwitch("init_command")); 622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertFalse(cl.hasSwitch("switch")); 632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertTrue(cl.hasSwitch("SWITCH")); 642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertFalse(cl.hasSwitch("--SWITCH")); 652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertFalse(cl.hasSwitch("Arg")); 662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertFalse(cl.hasSwitch("actually_an_arg")); 672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertEquals("brea\\d", cl.getSwitchValue("switch2")); 682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertEquals("and \"butter\"", cl.getSwitchValue("switch3")); 692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertEquals("a \"quoted\" 'food'!", cl.getSwitchValue("switch4")); 702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertNull(cl.getSwitchValue("SWITCH")); 712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertNull(cl.getSwitchValue("non-existant")); 722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void checkSettingThenGetting() { 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CommandLine cl = CommandLine.getInstance(); 762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Add a plain switch. 782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertFalse(cl.hasSwitch(CL_ADDED_SWITCH)); 792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) cl.appendSwitch(CL_ADDED_SWITCH); 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertTrue(cl.hasSwitch(CL_ADDED_SWITCH)); 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Add a switch paired with a value. 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertFalse(cl.hasSwitch(CL_ADDED_SWITCH_2)); 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertNull(cl.getSwitchValue(CL_ADDED_SWITCH_2)); 852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) cl.appendSwitchWithValue(CL_ADDED_SWITCH_2, CL_ADDED_VALUE_2); 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertTrue(CL_ADDED_VALUE_2.equals(cl.getSwitchValue(CL_ADDED_SWITCH_2))); 872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Append a few new things. 892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) final String switchesAndArgs[] = { "dummy", "--superfast", "--speed=turbo" }; 902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertFalse(cl.hasSwitch("dummy")); 912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertFalse(cl.hasSwitch("superfast")); 922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertNull(cl.getSwitchValue("speed")); 932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) cl.appendSwitchesAndArguments(switchesAndArgs); 942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertFalse(cl.hasSwitch("dummy")); 952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertFalse(cl.hasSwitch("command")); 962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertTrue(cl.hasSwitch("superfast")); 972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertTrue("turbo".equals(cl.getSwitchValue("speed"))); 982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void checkAppendedSwitchesPassedThrough() { 1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CommandLine cl = CommandLine.getInstance(); 1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertTrue(cl.hasSwitch(CL_ADDED_SWITCH)); 1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertTrue(cl.hasSwitch(CL_ADDED_SWITCH_2)); 1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) assertTrue(CL_ADDED_VALUE_2.equals(cl.getSwitchValue(CL_ADDED_SWITCH_2))); 1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) @MediumTest 1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) @Feature({"Android-AppBase"}) 1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public void testJavaNativeTransition() { 1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CommandLine.init(INIT_SWITCHES); 1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) checkInitSwitches(); 1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) loadJni(); 1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) checkInitSwitches(); 1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) checkSettingThenGetting(); 1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) @MediumTest 1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) @Feature({"Android-AppBase"}) 1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public void testJavaNativeTransitionAfterAppends() { 1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CommandLine.init(INIT_SWITCHES); 1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) checkInitSwitches(); 1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) checkSettingThenGetting(); 1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) loadJni(); 1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) checkInitSwitches(); 1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) checkAppendedSwitchesPassedThrough(); 1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) @MediumTest 1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) @Feature({"Android-AppBase"}) 1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public void testNativeInitialization() { 1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CommandLine.init(null); 1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) loadJni(); 1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Drop the program name for use with appendSwitchesAndArguments. 1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) String[] args = new String[INIT_SWITCHES.length - 1]; 1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) System.arraycopy(INIT_SWITCHES, 1, args, 0, args.length); 1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CommandLine.getInstance().appendSwitchesAndArguments(args); 1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) checkInitSwitches(); 1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) checkSettingThenGetting(); 1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) @MediumTest 1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) @Feature({"Android-AppBase"}) 1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public void testFileInitialization() { 1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CommandLine.initFromFile(ContentShellActivity.COMMAND_LINE_FILE); 1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) loadJni(); 1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) checkSettingThenGetting(); 1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 149