ContentCommandLineTest.java revision f2477e01787aa58f445919b809d89e252beef54f
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 5package org.chromium.content.browser; 6 7import android.test.InstrumentationTestCase; 8import android.test.suitebuilder.annotation.MediumTest; 9import android.test.suitebuilder.annotation.SmallTest; 10 11import org.chromium.base.CommandLine; 12import org.chromium.base.test.util.Feature; 13import org.chromium.content.app.LibraryLoader; 14import org.chromium.content.common.ProcessInitException; 15import org.chromium.content_shell_apk.ContentShellActivity; 16import org.chromium.content_shell_apk.ContentShellApplication; 17 18public class ContentCommandLineTest 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 @MediumTest 106 @Feature({"Android-AppBase"}) 107 public void testJavaNativeTransition() { 108 CommandLine.init(INIT_SWITCHES); 109 checkInitSwitches(); 110 loadJni(); 111 checkInitSwitches(); 112 checkSettingThenGetting(); 113 } 114 115 @MediumTest 116 @Feature({"Android-AppBase"}) 117 public void testJavaNativeTransitionAfterAppends() { 118 CommandLine.init(INIT_SWITCHES); 119 checkInitSwitches(); 120 checkSettingThenGetting(); 121 loadJni(); 122 checkInitSwitches(); 123 checkAppendedSwitchesPassedThrough(); 124 } 125 126 @MediumTest 127 @Feature({"Android-AppBase"}) 128 public void testNativeInitialization() { 129 CommandLine.init(null); 130 loadJni(); 131 // Drop the program name for use with appendSwitchesAndArguments. 132 String[] args = new String[INIT_SWITCHES.length - 1]; 133 System.arraycopy(INIT_SWITCHES, 1, args, 0, args.length); 134 CommandLine.getInstance().appendSwitchesAndArguments(args); 135 checkInitSwitches(); 136 checkSettingThenGetting(); 137 } 138 139 @MediumTest 140 @Feature({"Android-AppBase"}) 141 public void testFileInitialization() { 142 CommandLine.initFromFile(ContentShellActivity.COMMAND_LINE_FILE); 143 loadJni(); 144 checkSettingThenGetting(); 145 } 146} 147