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