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