Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(130)

Side by Side Diff: chrome/browser/ui/webui/chromeos/keyboard_overlay_ui_browsertest.cc

Issue 2815213003: Add test to check deprecated accelerator do not have keyboard overlay. (Closed)
Patch Set: Fix nits in patch 1. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 #include "ash/accelerators/accelerator_table.h" 5 #include "ash/accelerators/accelerator_table.h"
6 #include "chrome/browser/ui/browser.h" 6 #include "chrome/browser/ui/browser.h"
7 #include "chrome/browser/ui/tabs/tab_strip_model.h" 7 #include "chrome/browser/ui/tabs/tab_strip_model.h"
8 #include "chrome/common/url_constants.h" 8 #include "chrome/common/url_constants.h"
9 #include "chrome/test/base/in_process_browser_test.h" 9 #include "chrome/test/base/in_process_browser_test.h"
10 #include "chrome/test/base/ui_test_utils.h" 10 #include "chrome/test/base/ui_test_utils.h"
(...skipping 15 matching lines...) Expand all
26 "didPaint", base::Bind(&TestWebUIMessageHandler::HandleDidPaint, 26 "didPaint", base::Bind(&TestWebUIMessageHandler::HandleDidPaint,
27 base::Unretained(this))); 27 base::Unretained(this)));
28 } 28 }
29 29
30 private: 30 private:
31 void HandleDidPaint(const base::ListValue*) {} 31 void HandleDidPaint(const base::ListValue*) {}
32 32
33 DISALLOW_COPY_AND_ASSIGN(TestWebUIMessageHandler); 33 DISALLOW_COPY_AND_ASSIGN(TestWebUIMessageHandler);
34 }; 34 };
35 35
36 content::WebContents* StartKeyboardOverlayUI(Browser* browser) {
37 ui_test_utils::NavigateToURL(browser,
38 GURL(chrome::kChromeUIKeyboardOverlayURL));
39 content::WebContents* web_contents =
40 browser->tab_strip_model()->GetActiveWebContents();
41 web_contents->GetWebUI()->AddMessageHandler(
42 base::MakeUnique<TestWebUIMessageHandler>());
43 return web_contents;
44 }
45
46 bool IsDisplayUIScalingEnabled(content::WebContents* web_contents) {
47 bool is_display_ui_scaling_enabled;
48 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
49 web_contents,
50 "domAutomationController.send(isDisplayUIScalingEnabled());",
51 &is_display_ui_scaling_enabled));
52 return is_display_ui_scaling_enabled;
53 }
54
55 // Skip some accelerators in the tests:
56 // 1. If the accelerator has no modifier, i.e. ui::EF_NONE, or for "Caps
57 // Lock", such as ui::VKEY_MENU and ui::VKEY_LWIN, the logic to show it on
58 // the keyboard overlay is not by the mapping of
59 // keyboardOverlayData['shortcut'], so it can not be tested by this test.
60 // 2. If it has debug modifiers: ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN |
61 // ui::EF_SHIFT_DOWN
62 bool ShouldSkip(const ash::AcceleratorData& accelerator) {
63 return accelerator.keycode == ui::VKEY_MENU ||
64 accelerator.keycode == ui::VKEY_LWIN ||
65 accelerator.modifiers == ui::EF_NONE ||
66 accelerator.modifiers ==
67 (ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN | ui::EF_SHIFT_DOWN);
68 }
69
70 std::string KeyboardCodeToLabel(const ash::AcceleratorData& accelerator,
71 content::WebContents* web_contents) {
72 std::string label;
73 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
74 web_contents,
75 "domAutomationController.send("
76 " (function(number) {"
77 " if (!!KEYCODE_TO_LABEL[number]) {"
78 " return KEYCODE_TO_LABEL[number];"
79 " } else {"
80 " return 'NONE';"
81 " }"
82 " })(" +
83 std::to_string(static_cast<unsigned int>(accelerator.keycode)) +
84 " )"
85 ");",
86 &label));
87 if (label == "NONE") {
88 label = base::ToLowerASCII(static_cast<char>(
89 LocatedToNonLocatedKeyboardCode(accelerator.keycode)));
90 }
91 return label;
92 }
93
94 std::string GenerateShortcutKey(const ash::AcceleratorData& accelerator,
95 content::WebContents* web_contents) {
96 std::string shortcut = KeyboardCodeToLabel(accelerator, web_contents);
97 // The order of the "if" conditions should not be changed because the
98 // modifiers are expected to be alphabetical sorted in the generated
99 // shortcut.
100 if (accelerator.modifiers & ui::EF_ALT_DOWN)
101 shortcut.append("<>ALT");
102 if (accelerator.modifiers & ui::EF_CONTROL_DOWN)
103 shortcut.append("<>CTRL");
104 if (accelerator.modifiers & ui::EF_COMMAND_DOWN)
105 shortcut.append("<>SEARCH");
106 if (accelerator.modifiers & ui::EF_SHIFT_DOWN)
107 shortcut.append("<>SHIFT");
108 return shortcut;
109 }
110
111 bool ContainsShortcut(const std::string& shortcut,
112 content::WebContents* web_contents) {
113 bool contains;
114 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
115 web_contents,
116 "domAutomationController.send("
117 " !!keyboardOverlayData['shortcut']['" + shortcut + "']"
118 ");",
119 &contains));
120 return contains;
121 }
122
36 } // namespace 123 } // namespace
37 124
38 using KeyboardOverlayUIBrowserTest = InProcessBrowserTest; 125 using KeyboardOverlayUIBrowserTest = InProcessBrowserTest;
39 126
40 IN_PROC_BROWSER_TEST_F(KeyboardOverlayUIBrowserTest, 127 IN_PROC_BROWSER_TEST_F(KeyboardOverlayUIBrowserTest,
41 ShouldHaveKeyboardOverlay) { 128 AcceleratorsShouldHaveKeyboardOverlay) {
42 ui_test_utils::NavigateToURL(browser(), 129 content::WebContents* const web_contents = StartKeyboardOverlayUI(browser());
43 GURL(chrome::kChromeUIKeyboardOverlayURL)); 130 const bool is_display_ui_scaling_enabled =
44 content::WebContents* web_contents = 131 IsDisplayUIScalingEnabled(web_contents);
45 browser()->tab_strip_model()->GetActiveWebContents();
46 web_contents->GetWebUI()->AddMessageHandler(
47 base::MakeUnique<TestWebUIMessageHandler>());
48
49 bool is_display_ui_scaling_enabled;
50 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
51 web_contents,
52 "domAutomationController.send(isDisplayUIScalingEnabled());",
53 &is_display_ui_scaling_enabled));
54
55 for (size_t i = 0; i < ash::kAcceleratorDataLength; ++i) { 132 for (size_t i = 0; i < ash::kAcceleratorDataLength; ++i) {
56 const ash::AcceleratorData& entry = ash::kAcceleratorData[i]; 133 const ash::AcceleratorData& entry = ash::kAcceleratorData[i];
57 // Skip some accelerators in this test: 134 if (ShouldSkip(entry))
58 // 1. If the accelerator has no modifier, i.e. ui::EF_NONE, or for "Caps
59 // Lock", such as ui::VKEY_MENU and ui::VKEY_LWIN, the logic to show it on
60 // the keyboard overlay is not by the mapping of
61 // keyboardOverlayData['shortcut'], so it can not be tested by this test.
62 // 2. If it has debug modifiers: ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN |
63 // ui::EF_SHIFT_DOWN
64 if (entry.keycode == ui::VKEY_MENU ||
65 entry.keycode == ui::VKEY_LWIN ||
66 entry.modifiers == ui::EF_NONE ||
67 entry.modifiers ==
68 (ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN | ui::EF_SHIFT_DOWN)) {
69 continue; 135 continue;
136
137 const std::string shortcut = GenerateShortcutKey(entry, web_contents);
138 if (!is_display_ui_scaling_enabled) {
139 if (shortcut == "-<>CTRL<>SHIFT" || shortcut == "+<>CTRL<>SHIFT" ||
140 shortcut == "0<>CTRL<>SHIFT") {
141 continue;
142 }
70 } 143 }
71 144
72 std::string shortcut; 145 EXPECT_TRUE(ContainsShortcut(shortcut, web_contents))
73 ASSERT_TRUE(content::ExecuteScriptAndExtractString( 146 << "Please add the new accelerators to keyboard "
74 web_contents, 147 "overlay. Add one entry '" +
75 "domAutomationController.send(" 148 shortcut +
76 " (function(number) {" 149 "' in the 'shortcut' section"
77 " if (!!KEYCODE_TO_LABEL[number]) {" 150 " at the bottom of the file of "
78 " return KEYCODE_TO_LABEL[number];" 151 "'/chrome/browser/resources/chromeos/"
79 " } else {" 152 "keyboard_overlay_data.js'. Please keep it in "
80 " return 'NONE';" 153 "alphabetical order.";
81 " }"
82 " })(" + std::to_string(static_cast<unsigned int>(entry.keycode)) + ")"
83 ");",
84 &shortcut));
85 if (shortcut == "NONE") {
86 shortcut = base::ToLowerASCII(
87 static_cast<char>(LocatedToNonLocatedKeyboardCode(entry.keycode)));
88 }
89
90 // The order of the "if" conditions should not be changed because the
91 // modifiers are expected to be alphabetical sorted in the generated
92 // shortcut.
93 if (entry.modifiers & ui::EF_ALT_DOWN)
94 shortcut.append("<>ALT");
95 if (entry.modifiers & ui::EF_CONTROL_DOWN)
96 shortcut.append("<>CTRL");
97 if (entry.modifiers & ui::EF_COMMAND_DOWN)
98 shortcut.append("<>SEARCH");
99 if (entry.modifiers & ui::EF_SHIFT_DOWN)
100 shortcut.append("<>SHIFT");
101
102 if (!is_display_ui_scaling_enabled) {
103 if (shortcut == "-<>CTRL<>SHIFT" || shortcut == "+<>CTRL<>SHIFT" ||
104 shortcut == "0<>CTRL<>SHIFT")
105 continue;
106 }
107
108 bool contains;
109 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
110 web_contents,
111 "domAutomationController.send("
112 " !!keyboardOverlayData['shortcut']['" + shortcut + "']"
113 ");",
114 &contains));
115 ASSERT_TRUE(contains) << "Please add the new accelerators to keyboard "
116 "overlay. Add one entry '" +
117 shortcut +
118 "' in the 'shortcut' section"
119 " at the bottom of the file of "
120 "'/chrome/browser/resources/chromeos/"
121 "keyboard_overlay_data.js'. Please keep it in "
122 "alphabetical order.";
123 } 154 }
124 } 155 }
156
157 IN_PROC_BROWSER_TEST_F(KeyboardOverlayUIBrowserTest,
158 DeprecatedAcceleratorsShouldNotHaveKeyboardOverlay) {
159 content::WebContents* const web_contents = StartKeyboardOverlayUI(browser());
160 for (size_t i = 0; i < ash::kDeprecatedAcceleratorsLength; ++i) {
161 const ash::AcceleratorData& entry = ash::kDeprecatedAccelerators[i];
162 if (ShouldSkip(entry))
163 continue;
164
165 const std::string shortcut = GenerateShortcutKey(entry, web_contents);
166 EXPECT_FALSE(ContainsShortcut(shortcut, web_contents))
167 << "Please remove the deprecated accelerator '" + shortcut +
168 "' from the 'shortcut' section"
169 " at the bottom of the file of "
170 "'/chrome/browser/resources/chromeos/"
171 "keyboard_overlay_data.js'.";
172 }
173 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698