OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.browser; | 5 package org.chromium.base; |
6 | 6 |
7 import android.test.InstrumentationTestCase; | 7 import android.test.InstrumentationTestCase; |
8 import android.test.suitebuilder.annotation.MediumTest; | 8 import android.test.suitebuilder.annotation.MediumTest; |
9 import android.test.suitebuilder.annotation.SmallTest; | 9 import android.test.suitebuilder.annotation.SmallTest; |
10 | 10 |
| 11 import org.chromium.base.CommandLine; |
11 import org.chromium.base.test.util.Feature; | 12 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 | 13 |
18 public class CommandLineTest extends InstrumentationTestCase { | 14 public class CommandLineTest extends InstrumentationTestCase { |
19 // A reference command line. Note that switch2 is [brea\d], switch3 is [and
"butter"], | 15 // A reference command line. Note that switch2 is [brea\d], switch3 is [and
"butter"], |
20 // and switch4 is [a "quoted" 'food'!] | 16 // and switch4 is [a "quoted" 'food'!] |
21 static final String INIT_SWITCHES[] = { "init_command", "--SWITCH", "Arg", | 17 static final String INIT_SWITCHES[] = { "init_command", "--SWITCH", "Arg", |
22 "--switch2=brea\\d", "--switch3=and \"butter\"", | 18 "--switch2=brea\\d", "--switch3=and \"butter\"", |
23 "--switch4=a \"quoted\" 'food'!", | 19 "--switch4=a \"quoted\" 'food'!", |
24 "--", "--actually_an_arg" }; | 20 "--", "--actually_an_arg" }; |
25 | 21 |
26 // The same command line, but in quoted string format. | 22 // The same command line, but in quoted string format. |
27 static final char INIT_SWITCHES_BUFFER[] = | 23 static final char INIT_SWITCHES_BUFFER[] = |
28 ("init_command --SWITCH Arg --switch2=brea\\d --switch3=\"and \\\"butt\"
er\\\" " | 24 ("init_command --SWITCH Arg --switch2=brea\\d --switch3=\"and \\\"butt\"
er\\\" " |
29 + "--switch4='a \"quoted\" \\'food\\'!' " | 25 + "--switch4='a \"quoted\" \\'food\\'!' " |
30 + "-- --actually_an_arg").toCharArray(); | 26 + "-- --actually_an_arg").toCharArray(); |
31 | 27 |
32 static final String CL_ADDED_SWITCH = "zappo-dappo-doggy-trainer"; | 28 static final String CL_ADDED_SWITCH = "zappo-dappo-doggy-trainer"; |
33 static final String CL_ADDED_SWITCH_2 = "username"; | 29 static final String CL_ADDED_SWITCH_2 = "username"; |
34 static final String CL_ADDED_VALUE_2 = "bozo"; | 30 static final String CL_ADDED_VALUE_2 = "bozo"; |
35 | 31 |
36 @Override | 32 @Override |
37 public void setUp() throws Exception { | 33 public void setUp() throws Exception { |
38 CommandLine.reset(); | 34 CommandLine.reset(); |
39 } | 35 } |
40 | 36 |
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() { | 37 void checkInitSwitches() { |
58 CommandLine cl = CommandLine.getInstance(); | 38 CommandLine cl = CommandLine.getInstance(); |
59 assertFalse(cl.hasSwitch("init_command")); | 39 assertFalse(cl.hasSwitch("init_command")); |
60 assertFalse(cl.hasSwitch("switch")); | 40 assertFalse(cl.hasSwitch("switch")); |
61 assertTrue(cl.hasSwitch("SWITCH")); | 41 assertTrue(cl.hasSwitch("SWITCH")); |
62 assertFalse(cl.hasSwitch("--SWITCH")); | 42 assertFalse(cl.hasSwitch("--SWITCH")); |
63 assertFalse(cl.hasSwitch("Arg")); | 43 assertFalse(cl.hasSwitch("Arg")); |
64 assertFalse(cl.hasSwitch("actually_an_arg")); | 44 assertFalse(cl.hasSwitch("actually_an_arg")); |
65 assertEquals("brea\\d", cl.getSwitchValue("switch2")); | 45 assertEquals("brea\\d", cl.getSwitchValue("switch2")); |
66 assertEquals("and \"butter\"", cl.getSwitchValue("switch3")); | 46 assertEquals("and \"butter\"", cl.getSwitchValue("switch3")); |
(...skipping 21 matching lines...) Expand all Loading... |
88 assertFalse(cl.hasSwitch("dummy")); | 68 assertFalse(cl.hasSwitch("dummy")); |
89 assertFalse(cl.hasSwitch("superfast")); | 69 assertFalse(cl.hasSwitch("superfast")); |
90 assertNull(cl.getSwitchValue("speed")); | 70 assertNull(cl.getSwitchValue("speed")); |
91 cl.appendSwitchesAndArguments(switchesAndArgs); | 71 cl.appendSwitchesAndArguments(switchesAndArgs); |
92 assertFalse(cl.hasSwitch("dummy")); | 72 assertFalse(cl.hasSwitch("dummy")); |
93 assertFalse(cl.hasSwitch("command")); | 73 assertFalse(cl.hasSwitch("command")); |
94 assertTrue(cl.hasSwitch("superfast")); | 74 assertTrue(cl.hasSwitch("superfast")); |
95 assertTrue("turbo".equals(cl.getSwitchValue("speed"))); | 75 assertTrue("turbo".equals(cl.getSwitchValue("speed"))); |
96 } | 76 } |
97 | 77 |
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) { | 78 void checkTokenizer(String[] expected, String toParse) { |
106 String[] actual = CommandLine.tokenizeQuotedAruments(toParse.toCharArray
()); | 79 String[] actual = CommandLine.tokenizeQuotedAruments(toParse.toCharArray
()); |
107 assertEquals(expected.length, actual.length); | 80 assertEquals(expected.length, actual.length); |
108 for (int i = 0; i < expected.length; ++i) { | 81 for (int i = 0; i < expected.length; ++i) { |
109 assertEquals("comparing element " + i, expected[i], actual[i]); | 82 assertEquals("comparing element " + i, expected[i], actual[i]); |
110 } | 83 } |
111 } | 84 } |
112 | 85 |
113 @SmallTest | 86 @SmallTest |
114 @Feature({"Android-AppBase"}) | 87 @Feature({"Android-AppBase"}) |
115 public void testJavaInitialization() { | 88 public void testJavaInitialization() { |
116 CommandLine.init(INIT_SWITCHES); | 89 CommandLine.init(INIT_SWITCHES); |
117 checkInitSwitches(); | 90 checkInitSwitches(); |
118 checkSettingThenGetting(); | 91 checkSettingThenGetting(); |
119 } | 92 } |
120 | 93 |
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 | 94 @SmallTest |
156 @Feature({"Android-AppBase"}) | 95 @Feature({"Android-AppBase"}) |
157 public void testBufferInitialization() { | 96 public void testBufferInitialization() { |
158 CommandLine.init(CommandLine.tokenizeQuotedAruments(INIT_SWITCHES_BUFFER
)); | 97 CommandLine.init(CommandLine.tokenizeQuotedAruments(INIT_SWITCHES_BUFFER
)); |
159 checkInitSwitches(); | 98 checkInitSwitches(); |
160 checkSettingThenGetting(); | 99 checkSettingThenGetting(); |
161 } | 100 } |
162 | 101 |
163 @SmallTest | 102 @SmallTest |
164 @Feature({"Android-AppBase"}) | 103 @Feature({"Android-AppBase"}) |
(...skipping 16 matching lines...) Expand all Loading... |
181 expected = new String[] { "a'b", | 120 expected = new String[] { "a'b", |
182 "c\"d", | 121 "c\"d", |
183 "e\"f", | 122 "e\"f", |
184 "g'h", | 123 "g'h", |
185 "i\\'j", | 124 "i\\'j", |
186 "k\\\"l", | 125 "k\\\"l", |
187 "mn\\'op", | 126 "mn\\'op", |
188 "qr\\\"st",}; | 127 "qr\\\"st",}; |
189 checkTokenizer(expected, toParse); | 128 checkTokenizer(expected, toParse); |
190 } | 129 } |
191 | |
192 @MediumTest | |
193 @Feature({"Android-AppBase"}) | |
194 public void testFileInitialization() { | |
195 CommandLine.initFromFile(ContentShellActivity.COMMAND_LINE_FILE); | |
196 loadJni(); | |
197 checkSettingThenGetting(); | |
198 } | |
199 } | 130 } |
OLD | NEW |