blob: 29197cf30e2aa1917e377f82b3f7a40236222d43 [file] [log] [blame]
Mustafa Emre Acerb3aa36a82018-05-22 21:44:051// Copyright 2018 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#include "base/command_line.h"
6#include "base/strings/stringprintf.h"
7#include "base/strings/utf_string_conversions.h"
8#include "build/build_config.h"
9#include "chrome/browser/ui/browser.h"
10#include "chrome/browser/ui/tabs/tab_strip_model.h"
11#include "chrome/test/base/in_process_browser_test.h"
12#include "chrome/test/base/interactive_test_utils.h"
13#include "chrome/test/base/ui_test_utils.h"
14#include "content/public/browser/web_contents.h"
15#include "content/public/test/browser_test_utils.h"
16#include "ui/base/window_open_disposition.h"
17
18namespace {
19
20const char kSwitchName[] = "unsafely-treat-insecure-origin-as-secure";
21
22void SimulateTextType(content::WebContents* contents,
23 const char* experiment_id,
24 const char* text) {
25 EXPECT_TRUE(content::ExecuteScript(
26 contents, base::StringPrintf(
27 "var parent = document.getElementById('%s');"
28 "var textarea = parent.getElementsByTagName('textarea')[0];"
29 "textarea.focus();"
30 "textarea.value = `%s`;"
31 "textarea.onchange();",
32 experiment_id, text)));
33}
34
35void ToggleEnableDropdown(content::WebContents* contents,
36 const char* experiment_id,
37 bool enable) {
38 EXPECT_TRUE(content::ExecuteScript(
39 contents,
40 base::StringPrintf(
41 "var k = "
42 "document.getElementById('%s');"
43 "var s = k.getElementsByClassName('experiment-enable-disable')[0];"
44 "s.focus();"
45 "s.selectedIndex = %d;"
46 "s.onchange();",
47 experiment_id, enable ? 1 : 0)));
48}
49
50void SetSwitch(base::CommandLine::SwitchMap* switch_map,
51 const std::string& switch_name,
52 const std::string& switch_value) {
53#if defined(OS_WIN)
54 (*switch_map)[switch_name] = base::ASCIIToUTF16(switch_value.c_str());
55#else
56 (*switch_map)[switch_name] = switch_value;
57#endif
58}
59
60class AboutFlagsBrowserTest : public InProcessBrowserTest {};
61
62// Tests experiments with origin values in chrome://flags page.
63IN_PROC_BROWSER_TEST_F(AboutFlagsBrowserTest, OriginFlag) {
64 ui_test_utils::NavigateToURL(browser(), GURL("chrome://flags"));
65
66 const base::CommandLine::SwitchMap switches =
67 base::CommandLine::ForCurrentProcess()->GetSwitches();
68
69 content::WebContents* contents =
70 browser()->tab_strip_model()->GetActiveWebContents();
71
72 // Type a value in the experiment's textarea. Since the flag state is
73 // "Disabled" by default, command line shouldn't change.
74 SimulateTextType(contents, kSwitchName, "http://example.test");
75 EXPECT_EQ(switches, base::CommandLine::ForCurrentProcess()->GetSwitches());
76
77 // Enable the experiment. Command line should change.
78 ToggleEnableDropdown(contents, kSwitchName, true);
79 base::CommandLine::SwitchMap expected_switches = switches;
80 SetSwitch(&expected_switches, kSwitchName, "http://example.test");
81 EXPECT_EQ(expected_switches,
82 base::CommandLine::ForCurrentProcess()->GetSwitches());
83
84 // Typing while enabled should immediately change the flag.
85 SimulateTextType(contents, kSwitchName, "http://example.test.com");
86 SetSwitch(&expected_switches, kSwitchName, "http://example.test.com");
87 EXPECT_EQ(expected_switches,
88 base::CommandLine::ForCurrentProcess()->GetSwitches());
89
90 // Disable the experiment. Command line switch should be cleared.
91 ToggleEnableDropdown(contents, kSwitchName, false);
92 expected_switches.erase(kSwitchName);
93 EXPECT_EQ(expected_switches,
94 base::CommandLine::ForCurrentProcess()->GetSwitches());
95
96 // Enable again. Command line switch should be added back.
97 ToggleEnableDropdown(contents, kSwitchName, true);
98 SetSwitch(&expected_switches, kSwitchName, "http://example.test.com");
99 EXPECT_EQ(expected_switches,
100 base::CommandLine::ForCurrentProcess()->GetSwitches());
101
102 // Disable again and type. Command line switch should stay cleared.
103 ToggleEnableDropdown(contents, kSwitchName, false);
104 SimulateTextType(contents, kSwitchName, "http://example.test2.com");
105 expected_switches.erase(kSwitchName);
106 EXPECT_EQ(expected_switches,
107 base::CommandLine::ForCurrentProcess()->GetSwitches());
108
109 // Enable one last time. Command line should pick up the last typed value.
110 ToggleEnableDropdown(contents, kSwitchName, true);
111 SetSwitch(&expected_switches, kSwitchName, "http://example.test2.com");
112 EXPECT_EQ(expected_switches,
113 base::CommandLine::ForCurrentProcess()->GetSwitches());
114}
115
116// Tests that only valid http and https origins should be added to the command
117// line when modified from chrome://flags.
118IN_PROC_BROWSER_TEST_F(AboutFlagsBrowserTest, StringFlag) {
119 ui_test_utils::NavigateToURL(browser(), GURL("chrome://flags"));
120
121 const base::CommandLine::SwitchMap switches =
122 base::CommandLine::ForCurrentProcess()->GetSwitches();
123
124 content::WebContents* contents =
125 browser()->tab_strip_model()->GetActiveWebContents();
126
127 const char kValue[] =
128 "http://example.test/path http://example2.test/?query\n"
129 "invalid-value, filesystem:http://example.test.file, "
130 "ws://example3.test http://&^.com";
131
132 ToggleEnableDropdown(contents, kSwitchName, true);
133 SimulateTextType(contents, kSwitchName, kValue);
134 base::CommandLine::SwitchMap expected_switches = switches;
135 SetSwitch(&expected_switches, kSwitchName,
136 "http://example.test,http://example2.test,ws://example3.test");
137 EXPECT_EQ(expected_switches,
138 base::CommandLine::ForCurrentProcess()->GetSwitches());
139}
140
141} // namespace