Focus the first/last element when the view is requested for focus by tab traversal.
[chromium-blink-merge.git] / chrome / browser / ui / views / extensions / extension_dialog_interactive_uitest.cc
blob408e0e864c42d31ae4227ecbca65cdf52c13a515
1 // Copyright 2015 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.
5 #include "chrome/browser/ui/views/extensions/extension_dialog.h"
7 #include "chrome/browser/extensions/extension_browsertest.h"
8 #include "chrome/browser/extensions/extension_view_host.h"
9 #include "chrome/browser/ui/browser_window.h"
10 #include "content/public/test/browser_test_utils.h"
11 #include "extensions/test/extension_test_message_listener.h"
12 #include "ui/base/test/ui_controls.h"
14 using ExtensionDialogUiTest = ExtensionBrowserTest;
16 IN_PROC_BROWSER_TEST_F(ExtensionDialogUiTest, TabFocusLoop) {
17 ExtensionTestMessageListener init_listener("ready", false /* will_reply */);
18 ExtensionTestMessageListener button1_focus_listener("button1-focused", false);
19 ExtensionTestMessageListener button2_focus_listener("button2-focused", false);
20 ExtensionTestMessageListener button3_focus_listener("button3-focused", false);
22 // Load an extension for the test.
23 scoped_refptr<const extensions::Extension> extension =
24 LoadExtension(test_data_dir_.AppendASCII("uitest/tab_traversal"));
25 ASSERT_TRUE(extension.get());
27 // Open ExtensionDialog, whose initial page is the extension's main.html.
28 // The main.html contains three buttons.
29 ExtensionDialog* dialog = ExtensionDialog::Show(
30 extension->url().Resolve("main.html"),
31 browser()->window()->GetNativeWindow(), browser()->profile(),
32 NULL, 300, 300, 300, 300, base::string16(), NULL);
33 ASSERT_TRUE(dialog);
34 ASSERT_TRUE(init_listener.WaitUntilSatisfied());
36 // Focus the second button.
37 ASSERT_TRUE(content::ExecuteScript(
38 dialog->host()->render_view_host(),
39 "document.querySelector('#button2').focus()"));
40 ASSERT_TRUE(button2_focus_listener.WaitUntilSatisfied());
42 // Pressing TAB should focus the third(last) button.
43 ASSERT_TRUE(ui_controls::SendKeyPress(
44 browser()->window()->GetNativeWindow(),
45 ui::VKEY_TAB, false, false, false, false));
46 ASSERT_TRUE(button3_focus_listener.WaitUntilSatisfied());
48 // Pressing TAB again should focus the first button.
49 ASSERT_TRUE(ui_controls::SendKeyPress(
50 browser()->window()->GetNativeWindow(),
51 ui::VKEY_TAB, false, false, false, false));
52 ASSERT_TRUE(button1_focus_listener.WaitUntilSatisfied());
54 // Pressing Shift+TAB on the first button should focus the last button.
55 ASSERT_TRUE(ui_controls::SendKeyPress(
56 browser()->window()->GetNativeWindow(),
57 ui::VKEY_TAB, false, true, false, false));
58 ASSERT_TRUE(button3_focus_listener.WaitUntilSatisfied());