Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / public / android / javatests / src / org / chromium / content / browser / ContentCommandLineTest.java
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.content.browser;
6
7 import android.test.InstrumentationTestCase;
8 import android.test.suitebuilder.annotation.MediumTest;
9
10 import org.chromium.base.CommandLine;
11 import org.chromium.base.library_loader.LibraryLoader;
12 import org.chromium.base.library_loader.ProcessInitException;
13 import org.chromium.base.test.util.Feature;
14 import org.chromium.content_shell_apk.ContentShellApplication;
15
16 /**
17  * Test class for command lines.
18  */
19 public class ContentCommandLineTest extends InstrumentationTestCase {
20     // A reference command line. Note that switch2 is [brea\d], switch3 is [and "butter"],
21     // and switch4 is [a "quoted" 'food'!]
22     static final String INIT_SWITCHES[] = { "init_command", "--SWITCH", "Arg",
23         "--switch2=brea\\d", "--switch3=and \"butter\"",
24         "--switch4=a \"quoted\" 'food'!",
25         "--", "--actually_an_arg" };
26
27     // The same command line, but in quoted string format.
28     static final char INIT_SWITCHES_BUFFER[] =
29         ("init_command --SWITCH Arg --switch2=brea\\d --switch3=\"and \\\"butt\"er\\\"   "
30         + "--switch4='a \"quoted\" \\'food\\'!' "
31         + "-- --actually_an_arg").toCharArray();
32
33     static final String CL_ADDED_SWITCH = "zappo-dappo-doggy-trainer";
34     static final String CL_ADDED_SWITCH_2 = "username";
35     static final String CL_ADDED_VALUE_2 = "bozo";
36
37     @Override
38     public void setUp() throws Exception {
39         CommandLine.reset();
40     }
41
42     void loadJni() {
43         assertFalse(CommandLine.getInstance().isNativeImplementation());
44         getInstrumentation().runOnMainSync(new Runnable() {
45             @Override
46             public void run() {
47                 ContentShellApplication.initializeApplicationParameters();
48                 try {
49                     LibraryLoader.ensureInitialized();
50                 } catch (ProcessInitException e) {
51                     throw new Error(e);
52                 }
53             }
54         });
55         assertTrue(CommandLine.getInstance().isNativeImplementation());
56     }
57
58     void checkInitSwitches() {
59         CommandLine cl = CommandLine.getInstance();
60         assertFalse(cl.hasSwitch("init_command"));
61         assertFalse(cl.hasSwitch("switch"));
62         assertTrue(cl.hasSwitch("SWITCH"));
63         assertFalse(cl.hasSwitch("--SWITCH"));
64         assertFalse(cl.hasSwitch("Arg"));
65         assertFalse(cl.hasSwitch("actually_an_arg"));
66         assertEquals("brea\\d", cl.getSwitchValue("switch2"));
67         assertEquals("and \"butter\"", cl.getSwitchValue("switch3"));
68         assertEquals("a \"quoted\" 'food'!", cl.getSwitchValue("switch4"));
69         assertNull(cl.getSwitchValue("SWITCH"));
70         assertNull(cl.getSwitchValue("non-existant"));
71     }
72
73     void checkSettingThenGetting() {
74         CommandLine cl = CommandLine.getInstance();
75
76         // Add a plain switch.
77         assertFalse(cl.hasSwitch(CL_ADDED_SWITCH));
78         cl.appendSwitch(CL_ADDED_SWITCH);
79         assertTrue(cl.hasSwitch(CL_ADDED_SWITCH));
80
81         // Add a switch paired with a value.
82         assertFalse(cl.hasSwitch(CL_ADDED_SWITCH_2));
83         assertNull(cl.getSwitchValue(CL_ADDED_SWITCH_2));
84         cl.appendSwitchWithValue(CL_ADDED_SWITCH_2, CL_ADDED_VALUE_2);
85         assertTrue(CL_ADDED_VALUE_2.equals(cl.getSwitchValue(CL_ADDED_SWITCH_2)));
86
87         // Append a few new things.
88         final String switchesAndArgs[] = { "dummy", "--superfast", "--speed=turbo" };
89         assertFalse(cl.hasSwitch("dummy"));
90         assertFalse(cl.hasSwitch("superfast"));
91         assertNull(cl.getSwitchValue("speed"));
92         cl.appendSwitchesAndArguments(switchesAndArgs);
93         assertFalse(cl.hasSwitch("dummy"));
94         assertFalse(cl.hasSwitch("command"));
95         assertTrue(cl.hasSwitch("superfast"));
96         assertTrue("turbo".equals(cl.getSwitchValue("speed")));
97     }
98
99     void checkAppendedSwitchesPassedThrough() {
100         CommandLine cl = CommandLine.getInstance();
101         assertTrue(cl.hasSwitch(CL_ADDED_SWITCH));
102         assertTrue(cl.hasSwitch(CL_ADDED_SWITCH_2));
103         assertTrue(CL_ADDED_VALUE_2.equals(cl.getSwitchValue(CL_ADDED_SWITCH_2)));
104     }
105
106     @MediumTest
107     @Feature({"Android-AppBase"})
108     public void testJavaNativeTransition() {
109         CommandLine.init(INIT_SWITCHES);
110         checkInitSwitches();
111         loadJni();
112         checkInitSwitches();
113         checkSettingThenGetting();
114     }
115
116     @MediumTest
117     @Feature({"Android-AppBase"})
118     public void testJavaNativeTransitionAfterAppends() {
119         CommandLine.init(INIT_SWITCHES);
120         checkInitSwitches();
121         checkSettingThenGetting();
122         loadJni();
123         checkInitSwitches();
124         checkAppendedSwitchesPassedThrough();
125     }
126
127     @MediumTest
128     @Feature({"Android-AppBase"})
129     public void testNativeInitialization() {
130         CommandLine.init(null);
131         loadJni();
132         // Drop the program name for use with appendSwitchesAndArguments.
133         String[] args = new String[INIT_SWITCHES.length - 1];
134         System.arraycopy(INIT_SWITCHES, 1, args, 0, args.length);
135         CommandLine.getInstance().appendSwitchesAndArguments(args);
136         checkInitSwitches();
137         checkSettingThenGetting();
138     }
139
140     @MediumTest
141     @Feature({"Android-AppBase"})
142     public void testFileInitialization() {
143         CommandLine.initFromFile(ContentShellApplication.COMMAND_LINE_FILE);
144         loadJni();
145         checkSettingThenGetting();
146     }
147 }