OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import <Cocoa/Cocoa.h> |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #import "base/mac/scoped_nsobject.h" |
| 9 #include "chrome/browser/chrome_notification_types.h" |
| 10 #include "chrome/browser/permissions/permission_request_manager_test_api.h" |
| 11 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/browser_commands.h" |
| 13 #include "chrome/browser/ui/browser_window.h" |
| 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 #include "chrome/test/base/in_process_browser_test.h" |
| 16 #include "chrome/test/base/interactive_test_utils.h" |
| 17 #include "ui/base/test/ui_controls.h" |
| 18 #import "ui/base/test/windowed_nsnotification_observer.h" |
| 19 #include "ui/base/ui_base_switches.h" |
| 20 #import "ui/events/test/cocoa_test_event_utils.h" |
| 21 |
| 22 namespace { |
| 23 |
| 24 enum class UiMode { |
| 25 VIEWS, |
| 26 COCOA, |
| 27 }; |
| 28 |
| 29 std::string UiModeToString(const ::testing::TestParamInfo<UiMode>& info) { |
| 30 return info.param == UiMode::VIEWS ? "Views" : "Cocoa"; |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 35 class PermissionBubbleInteractiveUITest |
| 36 : public InProcessBrowserTest, |
| 37 public ::testing::WithParamInterface<UiMode> { |
| 38 public: |
| 39 PermissionBubbleInteractiveUITest() {} |
| 40 |
| 41 void EnsureWindowActive(NSWindow* window, const char* message) { |
| 42 SCOPED_TRACE(message); |
| 43 EXPECT_TRUE(window); |
| 44 |
| 45 // Activation is asynchronous on Mac. If the window didn't become active, |
| 46 // wait for it. |
| 47 if (![window isKeyWindow]) { |
| 48 base::scoped_nsobject<WindowedNSNotificationObserver> waiter( |
| 49 [[WindowedNSNotificationObserver alloc] |
| 50 initForNotification:NSWindowDidBecomeKeyNotification |
| 51 object:window]); |
| 52 [waiter wait]; |
| 53 } |
| 54 EXPECT_TRUE([window isKeyWindow]); |
| 55 } |
| 56 |
| 57 // Send Cmd+keycode in the key window to NSApp. |
| 58 void SendAccelerator(ui::KeyboardCode keycode, bool shift, bool alt) { |
| 59 bool control = false; |
| 60 bool command = true; |
| 61 // Note that although this takes an NSWindow, it's just used to create the |
| 62 // NSEvent* which will be dispatched to NSApp (i.e. not NSWindow). |
| 63 ui_controls::SendKeyPress( |
| 64 [NSApp keyWindow], keycode, control, shift, alt, command); |
| 65 } |
| 66 |
| 67 // InProcessBrowserTest: |
| 68 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 69 if (GetParam() == UiMode::VIEWS) |
| 70 command_line->AppendSwitch(switches::kExtendMdToSecondaryUi); |
| 71 } |
| 72 |
| 73 void SetUpOnMainThread() override { |
| 74 // Make the browser active (ensures the app can receive key events). |
| 75 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 76 |
| 77 test_api_ = |
| 78 base::MakeUnique<test::PermissionRequestManagerTestApi>(browser()); |
| 79 EXPECT_TRUE(test_api_->manager()); |
| 80 |
| 81 test_api_->AddSimpleRequest(browser()->profile(), |
| 82 content::PermissionType::GEOLOCATION); |
| 83 |
| 84 EXPECT_TRUE([browser()->window()->GetNativeWindow() isKeyWindow]); |
| 85 test_api_->manager()->DisplayPendingRequests(); |
| 86 |
| 87 // The bubble should steal key focus when shown. |
| 88 EnsureWindowActive(test_api_->GetPromptWindow(), "show permission bubble"); |
| 89 } |
| 90 |
| 91 protected: |
| 92 std::unique_ptr<test::PermissionRequestManagerTestApi> test_api_; |
| 93 |
| 94 private: |
| 95 DISALLOW_COPY_AND_ASSIGN(PermissionBubbleInteractiveUITest); |
| 96 }; |
| 97 |
| 98 // There is only one tab. Cmd+w will close it along with the browser window. |
| 99 IN_PROC_BROWSER_TEST_P(PermissionBubbleInteractiveUITest, CmdWClosesWindow) { |
| 100 base::scoped_nsobject<NSWindow> browser_window( |
| 101 browser()->window()->GetNativeWindow(), base::scoped_policy::RETAIN); |
| 102 EXPECT_TRUE([browser_window isVisible]); |
| 103 |
| 104 content::WindowedNotificationObserver observer( |
| 105 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(browser())); |
| 106 |
| 107 SendAccelerator(ui::VKEY_W, false, false); |
| 108 |
| 109 // The actual window close happens via a posted task. |
| 110 EXPECT_TRUE([browser_window isVisible]); |
| 111 observer.Wait(); |
| 112 EXPECT_FALSE([browser_window isVisible]); |
| 113 } |
| 114 |
| 115 // Add a tab, ensure we can switch away and back using Cmd+Alt+Left/Right and |
| 116 // curly braces. |
| 117 IN_PROC_BROWSER_TEST_P(PermissionBubbleInteractiveUITest, SwitchTabs) { |
| 118 NSWindow* browser_window = browser()->window()->GetNativeWindow(); |
| 119 |
| 120 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); |
| 121 EXPECT_TRUE(test_api_->GetPromptWindow()); |
| 122 |
| 123 // Add a blank tab in the foreground. |
| 124 AddBlankTabAndShow(browser()); |
| 125 EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); |
| 126 |
| 127 // The bubble should hide and give focus back to the browser. However, the |
| 128 // test environment can't guarantee that macOS decides that the Browser window |
| 129 // is actually the "best" window to activate upon closing the current key |
| 130 // window. So activate it manually. |
| 131 [browser_window makeKeyAndOrderFront:nil]; |
| 132 EnsureWindowActive(browser_window, "tab added"); |
| 133 |
| 134 // Prompt is hidden while its tab is not active. |
| 135 EXPECT_FALSE(test_api_->GetPromptWindow()); |
| 136 |
| 137 // Now a webcontents is active, it gets a first shot at processing the |
| 138 // accelerator before sending it back unhandled to the browser via IPC. That's |
| 139 // all a bit much to handle in a test, so activate the location bar. |
| 140 chrome::FocusLocationBar(browser()); |
| 141 SendAccelerator(ui::VKEY_LEFT, false, true); |
| 142 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); |
| 143 |
| 144 // Note we don't need to makeKeyAndOrderFront: the permission window will take |
| 145 // focus when it is shown again. |
| 146 EnsureWindowActive(test_api_->GetPromptWindow(), |
| 147 "switched to permission tab with arrow"); |
| 148 EXPECT_TRUE(test_api_->GetPromptWindow()); |
| 149 |
| 150 // Ensure we can switch away with the bubble active. |
| 151 SendAccelerator(ui::VKEY_RIGHT, false, true); |
| 152 EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); |
| 153 |
| 154 [browser_window makeKeyAndOrderFront:nil]; |
| 155 EnsureWindowActive(browser_window, "switch away with arrow"); |
| 156 EXPECT_FALSE(test_api_->GetPromptWindow()); |
| 157 |
| 158 // Also test switching tabs with curly braces. "VKEY_OEM_4" is |
| 159 // LeftBracket/Brace on a US keyboard, which ui::MacKeyCodeForWindowsKeyCode |
| 160 // will map to '{' when shift is passed. Also note there are only two tabs so |
| 161 // it doesn't matter which direction is taken (it wraps). |
| 162 chrome::FocusLocationBar(browser()); |
| 163 SendAccelerator(ui::VKEY_OEM_4, true, false); |
| 164 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); |
| 165 EnsureWindowActive(test_api_->GetPromptWindow(), |
| 166 "switch to permission tab with curly brace"); |
| 167 EXPECT_TRUE(test_api_->GetPromptWindow()); |
| 168 |
| 169 SendAccelerator(ui::VKEY_OEM_4, true, false); |
| 170 EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); |
| 171 [browser_window makeKeyAndOrderFront:nil]; |
| 172 EnsureWindowActive(browser_window, "switch away with curly brace"); |
| 173 EXPECT_FALSE(test_api_->GetPromptWindow()); |
| 174 } |
| 175 |
| 176 INSTANTIATE_TEST_CASE_P(, |
| 177 PermissionBubbleInteractiveUITest, |
| 178 ::testing::Values(UiMode::VIEWS, UiMode::COCOA), |
| 179 &UiModeToString); |
OLD | NEW |