Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 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 "content/browser/pointer_lock_browsertest.h" |
| 6 | |
Kartar Singh | b1bfa1a | 2024-06-24 13:14:57 | [diff] [blame] | 7 | #include <string> |
| 8 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 9 | #include "base/memory/raw_ptr.h" |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 10 | #include "base/test/scoped_feature_list.h" |
Scott Little | f0fd984 | 2019-08-09 19:56:51 | [diff] [blame] | 11 | #include "build/build_config.h" |
Kartar Singh | b1bfa1a | 2024-06-24 13:14:57 | [diff] [blame] | 12 | #include "components/input/render_widget_host_input_event_router.h" |
danakj | 10f3237 | 2020-09-15 22:25:16 | [diff] [blame] | 13 | #include "content/browser/renderer_host/frame_tree.h" |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 14 | #include "content/browser/renderer_host/render_widget_host_impl.h" |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 15 | #include "content/browser/web_contents/web_contents_impl.h" |
Arthur Sonzogni | bdeca8e | 2023-09-11 08:32:12 | [diff] [blame] | 16 | #include "content/common/features.h" |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 17 | #include "content/public/browser/web_contents_delegate.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 18 | #include "content/public/test/browser_test.h" |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 19 | #include "content/public/test/browser_test_utils.h" |
| 20 | #include "content/public/test/content_browser_test.h" |
| 21 | #include "content/public/test/content_browser_test_utils.h" |
jonross | 09d21de | 2018-06-13 12:31:36 | [diff] [blame] | 22 | #include "content/public/test/hit_test_region_observer.h" |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 23 | #include "content/public/test/test_utils.h" |
| 24 | #include "content/shell/browser/shell.h" |
| 25 | #include "content/test/content_browser_test_utils_internal.h" |
| 26 | #include "net/dns/mock_host_resolver.h" |
| 27 | #include "net/test/embedded_test_server/embedded_test_server.h" |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 28 | #include "ui/base/ui_base_features.h" |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 29 | |
| 30 | #ifdef USE_AURA |
| 31 | #include "content/browser/renderer_host/render_widget_host_view_aura.h" |
| 32 | #include "content/browser/web_contents/web_contents_view_aura.h" |
| 33 | #endif // USE_AURA |
Abdelrahman Eed | 180a762 | 2025-06-13 11:39:54 | [diff] [blame] | 34 | #if BUILDFLAG(IS_ANDROID) |
| 35 | #include "content/browser/renderer_host/render_widget_host_view_android.h" |
| 36 | #include "content/browser/web_contents/web_contents_view_android.h" |
| 37 | #endif // BUILDFLAG(IS_ANDROID) |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 38 | |
| 39 | namespace content { |
| 40 | |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 41 | class MockPointerLockWebContentsDelegate : public WebContentsDelegate { |
| 42 | public: |
| 43 | MockPointerLockWebContentsDelegate() {} |
| 44 | ~MockPointerLockWebContentsDelegate() override {} |
| 45 | |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 46 | void RequestPointerLock(WebContents* web_contents, |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 47 | bool user_gesture, |
| 48 | bool last_unlocked_by_target) override { |
James Hollyer | d5c9de46 | 2020-03-10 19:02:45 | [diff] [blame] | 49 | if (user_gesture) |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 50 | web_contents->GotResponseToPointerLockRequest( |
James Hollyer | d5c9de46 | 2020-03-10 19:02:45 | [diff] [blame] | 51 | blink::mojom::PointerLockResult::kSuccess); |
| 52 | else |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 53 | web_contents->GotResponseToPointerLockRequest( |
James Hollyer | d5c9de46 | 2020-03-10 19:02:45 | [diff] [blame] | 54 | blink::mojom::PointerLockResult::kRequiresUserGesture); |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 55 | } |
| 56 | |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 57 | void LostPointerLock() override {} |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | #ifdef USE_AURA |
Ella Ge | dd3c80b4 | 2019-09-25 01:17:57 | [diff] [blame] | 61 | class ScopedEnableUnadjustedMouseEventsForTesting |
| 62 | : public aura::ScopedEnableUnadjustedMouseEvents { |
| 63 | public: |
| 64 | explicit ScopedEnableUnadjustedMouseEventsForTesting() {} |
| 65 | ~ScopedEnableUnadjustedMouseEventsForTesting() override {} |
| 66 | }; |
| 67 | |
Ella Ge | b15a757 | 2018-06-25 17:03:50 | [diff] [blame] | 68 | class MockPointerLockRenderWidgetHostView : public RenderWidgetHostViewAura { |
| 69 | public: |
W. James MacLean | 13d834d | 2019-12-04 16:06:45 | [diff] [blame] | 70 | MockPointerLockRenderWidgetHostView(RenderWidgetHost* host) |
| 71 | : RenderWidgetHostViewAura(host), |
Ella Ge | b15a757 | 2018-06-25 17:03:50 | [diff] [blame] | 72 | host_(RenderWidgetHostImpl::From(host)) {} |
| 73 | ~MockPointerLockRenderWidgetHostView() override { |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 74 | if (IsPointerLocked()) { |
| 75 | UnlockPointer(); |
| 76 | } |
Ella Ge | b15a757 | 2018-06-25 17:03:50 | [diff] [blame] | 77 | } |
| 78 | |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 79 | blink::mojom::PointerLockResult LockPointer( |
James Hollyer | d5c9de46 | 2020-03-10 19:02:45 | [diff] [blame] | 80 | bool request_unadjusted_movement) override { |
Ella Ge | b15a757 | 2018-06-25 17:03:50 | [diff] [blame] | 81 | event_handler()->mouse_locked_ = true; |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 82 | event_handler()->mouse_locked_unadjusted_movement_ = |
Ella Ge | dd3c80b4 | 2019-09-25 01:17:57 | [diff] [blame] | 83 | request_unadjusted_movement |
| 84 | ? std::make_unique<ScopedEnableUnadjustedMouseEventsForTesting>() |
| 85 | : nullptr; |
James Hollyer | d5c9de46 | 2020-03-10 19:02:45 | [diff] [blame] | 86 | return blink::mojom::PointerLockResult::kSuccess; |
Ella Ge | b15a757 | 2018-06-25 17:03:50 | [diff] [blame] | 87 | } |
| 88 | |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 89 | void UnlockPointer() override { |
| 90 | host_->LostPointerLock(); |
Ella Ge | b15a757 | 2018-06-25 17:03:50 | [diff] [blame] | 91 | event_handler()->mouse_locked_ = false; |
Ella Ge | dd3c80b4 | 2019-09-25 01:17:57 | [diff] [blame] | 92 | event_handler()->mouse_locked_unadjusted_movement_.reset(); |
Ella Ge | b15a757 | 2018-06-25 17:03:50 | [diff] [blame] | 93 | } |
| 94 | |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 95 | bool GetIsPointerLockedUnadjustedMovementForTesting() override { |
| 96 | return IsPointerLocked() && |
jameshollyer | 48a702f | 2020-04-11 04:21:40 | [diff] [blame] | 97 | event_handler()->mouse_locked_unadjusted_movement_; |
| 98 | } |
Ella Ge | b15a757 | 2018-06-25 17:03:50 | [diff] [blame] | 99 | |
| 100 | void OnWindowFocused(aura::Window* gained_focus, |
| 101 | aura::Window* lost_focus) override { |
| 102 | // Ignore window focus events. |
| 103 | } |
| 104 | |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 105 | bool IsPointerLocked() override { return event_handler()->mouse_locked(); } |
jameshollyer | 48a702f | 2020-04-11 04:21:40 | [diff] [blame] | 106 | |
| 107 | bool HasFocus() override { return has_focus_; } |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 108 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 109 | raw_ptr<RenderWidgetHostImpl> host_; |
jameshollyer | 48a702f | 2020-04-11 04:21:40 | [diff] [blame] | 110 | bool has_focus_ = true; |
Ella Ge | b15a757 | 2018-06-25 17:03:50 | [diff] [blame] | 111 | }; |
| 112 | |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 113 | void InstallCreateHooksForPointerLockBrowserTests() { |
| 114 | WebContentsViewAura::InstallCreateHookForTests( |
W. James MacLean | 13d834d | 2019-12-04 16:06:45 | [diff] [blame] | 115 | [](RenderWidgetHost* host) -> RenderWidgetHostViewAura* { |
| 116 | return new MockPointerLockRenderWidgetHostView(host); |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 117 | }); |
| 118 | } |
| 119 | #endif // USE_AURA |
Abdelrahman Eed | 180a762 | 2025-06-13 11:39:54 | [diff] [blame] | 120 | #if BUILDFLAG(IS_ANDROID) |
| 121 | class MockPointerLockRenderWidgetHostView : public RenderWidgetHostViewAndroid { |
| 122 | public: |
| 123 | MockPointerLockRenderWidgetHostView(RenderWidgetHostImpl* rwhi, |
| 124 | gfx::NativeView parent_native_view, |
| 125 | cc::slim::Layer* parent_layer) |
| 126 | : RenderWidgetHostViewAndroid(rwhi, parent_native_view, parent_layer) {} |
| 127 | ~MockPointerLockRenderWidgetHostView() override { |
| 128 | if (pointer_locked_) { |
| 129 | UnlockPointer(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | blink::mojom::PointerLockResult LockPointer( |
| 134 | bool request_unadjusted_movement) override { |
| 135 | pointer_locked_ = true; |
| 136 | |
| 137 | return blink::mojom::PointerLockResult::kSuccess; |
| 138 | } |
| 139 | |
| 140 | blink::mojom::PointerLockResult ChangePointerLock( |
| 141 | bool request_unadjusted_movement) override { |
| 142 | return blink::mojom::PointerLockResult::kSuccess; |
| 143 | } |
| 144 | |
| 145 | void UnlockPointer() override { |
| 146 | if (RenderWidgetHostImpl* host = |
| 147 | RenderWidgetHostImpl::From(GetRenderWidgetHost())) { |
| 148 | host->LostPointerLock(); |
| 149 | } |
| 150 | pointer_locked_ = false; |
| 151 | } |
| 152 | |
| 153 | bool IsPointerLocked() override { return pointer_locked_; } |
| 154 | |
| 155 | bool HasFocus() override { return true; } |
| 156 | bool CanBePointerLocked() override { return true; } |
| 157 | |
| 158 | private: |
| 159 | bool pointer_locked_ = false; |
| 160 | }; |
| 161 | |
| 162 | void InstallCreateHooksForPointerLockBrowserTests() { |
| 163 | WebContentsViewAndroid::InstallCreateHookForTests( |
| 164 | [](RenderWidgetHostImpl* rwhi, gfx::NativeView parent_native_view, |
| 165 | cc::slim::Layer* parent_layer) -> RenderWidgetHostViewAndroid* { |
| 166 | return new MockPointerLockRenderWidgetHostView(rwhi, parent_native_view, |
| 167 | parent_layer); |
| 168 | }); |
| 169 | } |
| 170 | #endif // BUILDFLAG(IS_ANDROID) |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 171 | |
| 172 | class PointerLockBrowserTest : public ContentBrowserTest { |
| 173 | public: |
| 174 | PointerLockBrowserTest() {} |
| 175 | |
| 176 | protected: |
| 177 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 178 | IsolateAllSitesForTesting(command_line); |
| 179 | } |
| 180 | |
| 181 | void SetUp() override { |
| 182 | InstallCreateHooksForPointerLockBrowserTests(); |
| 183 | ContentBrowserTest::SetUp(); |
| 184 | } |
| 185 | |
| 186 | void SetUpOnMainThread() override { |
| 187 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 188 | SetupCrossSiteRedirector(embedded_test_server()); |
| 189 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 190 | |
| 191 | web_contents()->SetDelegate(&web_contents_delegate_); |
| 192 | } |
| 193 | |
| 194 | WebContentsImpl* web_contents() const { |
| 195 | return static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 196 | } |
| 197 | |
Nick Carter | d73635b | 2018-03-13 18:31:41 | [diff] [blame] | 198 | protected: |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 199 | MockPointerLockWebContentsDelegate web_contents_delegate_; |
| 200 | }; |
| 201 | |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 202 | namespace { |
| 203 | class PointerLockHelper { |
| 204 | public: |
| 205 | // requestPointerLock is an asynchronous operation. This method returns when |
| 206 | // document.body.requestPointerLock() either succeeds or fails. |
| 207 | // Returns true if Pointer Lock on body was successful. |
| 208 | static EvalJsResult RequestPointerLockOnBody( |
| 209 | const ToRenderFrameHost& execution_target, |
| 210 | const int options = EXECUTE_SCRIPT_DEFAULT_OPTIONS) { |
| 211 | return EvalJs(execution_target, |
| 212 | set_pointer_lock_promise_ + |
| 213 | "document.body.requestPointerLock();" + |
| 214 | wait_for_pointer_lock_promise_, |
| 215 | options); |
| 216 | } |
| 217 | static EvalJsResult RequestPointerLockWithUnadjustedMovementOnBody( |
| 218 | const ToRenderFrameHost& execution_target, |
| 219 | const int options = EXECUTE_SCRIPT_DEFAULT_OPTIONS) { |
| 220 | return EvalJs( |
| 221 | execution_target, |
| 222 | set_pointer_lock_promise_ + |
| 223 | "document.body.requestPointerLock({unadjustedMovement:true});" + |
| 224 | wait_for_pointer_lock_promise_, |
| 225 | options); |
| 226 | } |
| 227 | // exitPointerLock is an asynchronous operation. This method returns when |
| 228 | // document.exitPointerLock() either succeeds or fails. |
| 229 | // Returns true if Exit Pointer Lock was successful |
| 230 | static EvalJsResult ExitPointerLock( |
| 231 | const ToRenderFrameHost& execution_target, |
| 232 | const int options = EXECUTE_SCRIPT_DEFAULT_OPTIONS) { |
| 233 | return EvalJs(execution_target, |
| 234 | set_pointer_lock_promise_ + "document.exitPointerLock();" + |
| 235 | wait_for_pointer_lock_promise_, |
| 236 | options); |
| 237 | } |
| 238 | static EvalJsResult IsPointerLockOnBody( |
| 239 | const ToRenderFrameHost& execution_target, |
| 240 | const int options = EXECUTE_SCRIPT_DEFAULT_OPTIONS) { |
| 241 | return EvalJs(execution_target, |
| 242 | "document.pointerLockElement === document.body", options); |
| 243 | } |
| 244 | |
| 245 | private: |
| 246 | static const std::string set_pointer_lock_promise_; |
| 247 | static const std::string wait_for_pointer_lock_promise_; |
| 248 | }; |
| 249 | |
| 250 | // static |
| 251 | const std::string PointerLockHelper::set_pointer_lock_promise_ = |
| 252 | R"code(pointerLockPromise=new Promise(function (resolve, reject){ |
| 253 | document.addEventListener('pointerlockchange', resolve); |
| 254 | document.addEventListener('pointerlockerror', reject); |
| 255 | });)code"; |
| 256 | // static |
| 257 | const std::string PointerLockHelper::wait_for_pointer_lock_promise_ = |
| 258 | "(async()=> {return await pointerLockPromise.then(()=>true, " |
| 259 | "()=>false);})()"; |
| 260 | } // namespace |
| 261 | |
Liviu Tinta | cd230a5 | 2020-05-27 14:18:33 | [diff] [blame] | 262 | IN_PROC_BROWSER_TEST_F(PointerLockBrowserTest, PointerLockBasic) { |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 263 | GURL main_url(embedded_test_server()->GetURL( |
| 264 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 265 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 266 | |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 267 | FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root(); |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 268 | FrameTreeNode* child = root->child_at(0); |
| 269 | |
| 270 | // Request a pointer lock on the root frame's body. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 271 | EXPECT_EQ(true, PointerLockHelper::RequestPointerLockOnBody(root)); |
| 272 | EXPECT_EQ(true, PointerLockHelper::IsPointerLockOnBody(root)); |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 273 | |
| 274 | // Request a pointer lock on the child frame's body. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 275 | EXPECT_EQ(false, PointerLockHelper::RequestPointerLockOnBody(child)); |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 276 | // Child frame should not be granted pointer lock since the root frame has it. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 277 | EXPECT_EQ(false, PointerLockHelper::IsPointerLockOnBody(child)); |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 278 | |
| 279 | // Release pointer lock on root frame. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 280 | EXPECT_EQ(true, PointerLockHelper::ExitPointerLock(root)); |
James Hollyer | 5b0230f | 2020-03-10 03:52:26 | [diff] [blame] | 281 | |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 282 | // Request a pointer lock on the child frame's body. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 283 | EXPECT_EQ(true, PointerLockHelper::RequestPointerLockOnBody(child)); |
James Hollyer | 5b0230f | 2020-03-10 03:52:26 | [diff] [blame] | 284 | // ensure request finishes before moving on. |
James Hollyer | 5b0230f | 2020-03-10 03:52:26 | [diff] [blame] | 285 | |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 286 | // Child frame should have been granted pointer lock. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 287 | EXPECT_EQ(true, PointerLockHelper::IsPointerLockOnBody(child)); |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 288 | } |
| 289 | |
Liviu Tinta | 2775eb8 | 2020-05-28 13:33:36 | [diff] [blame] | 290 | IN_PROC_BROWSER_TEST_F(PointerLockBrowserTest, PointerLockAndUserActivation) { |
Mustaq Ahmed | 32a0b15 | 2019-08-06 16:01:26 | [diff] [blame] | 291 | GURL main_url(embedded_test_server()->GetURL( |
| 292 | "a.com", "/cross_site_iframe_factory.html?a(b(b))")); |
| 293 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 294 | |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 295 | FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root(); |
Mustaq Ahmed | 32a0b15 | 2019-08-06 16:01:26 | [diff] [blame] | 296 | FrameTreeNode* child = root->child_at(0); |
| 297 | FrameTreeNode* grand_child = child->child_at(0); |
| 298 | |
| 299 | // Without user activation, pointer lock request from any (child or |
| 300 | // grand_child) frame fails. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 301 | EXPECT_EQ(false, PointerLockHelper::RequestPointerLockOnBody( |
| 302 | child, EXECUTE_SCRIPT_NO_USER_GESTURE)); |
| 303 | EXPECT_EQ(false, PointerLockHelper::IsPointerLockOnBody( |
| 304 | child, EXECUTE_SCRIPT_NO_USER_GESTURE)); |
| 305 | |
| 306 | EXPECT_EQ(false, PointerLockHelper::RequestPointerLockOnBody( |
| 307 | grand_child, EXECUTE_SCRIPT_NO_USER_GESTURE)); |
| 308 | EXPECT_EQ(false, PointerLockHelper::IsPointerLockOnBody( |
| 309 | grand_child, EXECUTE_SCRIPT_NO_USER_GESTURE)); |
Mustaq Ahmed | 32a0b15 | 2019-08-06 16:01:26 | [diff] [blame] | 310 | |
| 311 | // Execute a empty (dummy) JS to activate the child frame. |
| 312 | EXPECT_TRUE(ExecJs(child, "")); |
| 313 | |
| 314 | // With user activation in the child frame, pointer lock from the same frame |
| 315 | // succeeds. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 316 | EXPECT_EQ(true, PointerLockHelper::RequestPointerLockOnBody( |
| 317 | child, EXECUTE_SCRIPT_NO_USER_GESTURE)); |
| 318 | EXPECT_EQ(true, PointerLockHelper::IsPointerLockOnBody( |
| 319 | child, EXECUTE_SCRIPT_NO_USER_GESTURE)); |
Mustaq Ahmed | 32a0b15 | 2019-08-06 16:01:26 | [diff] [blame] | 320 | |
| 321 | // But with user activation in the child frame, pointer lock from the |
| 322 | // grand_child frame fails. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 323 | EXPECT_EQ(false, PointerLockHelper::RequestPointerLockOnBody( |
| 324 | grand_child, EXECUTE_SCRIPT_NO_USER_GESTURE)); |
| 325 | EXPECT_EQ(false, PointerLockHelper::IsPointerLockOnBody( |
| 326 | grand_child, EXECUTE_SCRIPT_NO_USER_GESTURE)); |
Mustaq Ahmed | 32a0b15 | 2019-08-06 16:01:26 | [diff] [blame] | 327 | } |
| 328 | |
Xida Chen | 365e7ca | 2021-05-25 17:39:01 | [diff] [blame] | 329 | // crbug.com/1210940: flaky on Linux |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 330 | #if BUILDFLAG(IS_LINUX) |
Xida Chen | 365e7ca | 2021-05-25 17:39:01 | [diff] [blame] | 331 | #define MAYBE_PointerLockEventRouting DISABLED_PointerLockEventRouting |
| 332 | #else |
| 333 | #define MAYBE_PointerLockEventRouting PointerLockEventRouting |
| 334 | #endif |
| 335 | IN_PROC_BROWSER_TEST_F(PointerLockBrowserTest, MAYBE_PointerLockEventRouting) { |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 336 | GURL main_url(embedded_test_server()->GetURL( |
| 337 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 338 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 339 | |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 340 | FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root(); |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 341 | FrameTreeNode* child = root->child_at(0); |
Kartar Singh | b1bfa1a | 2024-06-24 13:14:57 | [diff] [blame] | 342 | input::RenderWidgetHostInputEventRouter* router = |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 343 | web_contents()->GetInputEventRouter(); |
| 344 | RenderWidgetHostViewBase* root_view = static_cast<RenderWidgetHostViewBase*>( |
| 345 | root->current_frame_host()->GetView()); |
| 346 | RenderWidgetHostViewBase* child_view = static_cast<RenderWidgetHostViewBase*>( |
| 347 | child->current_frame_host()->GetView()); |
| 348 | |
kylechar | a7c549b | 2019-07-29 17:47:28 | [diff] [blame] | 349 | WaitForHitTestData(child->current_frame_host()); |
Abdelrahman Eed | 180a762 | 2025-06-13 11:39:54 | [diff] [blame] | 350 | // Make sure that page_scale is 1, on Android page scale is not guaranteed to |
| 351 | // be 1 |
| 352 | web_contents()->SetPageScale(1.0); |
Ken Buchanan | 8a319fb | 2017-11-15 18:37:12 | [diff] [blame] | 353 | |
arthursonzogni | 3d3ec9e | 2021-03-04 11:00:50 | [diff] [blame] | 354 | std::string set_mouse_move_event_listener = R"( |
| 355 | mouseMoveExecuted = new Promise(function (resolve, reject) { |
| 356 | mousemoveHandler = function(e) { |
| 357 | x = e.x; |
| 358 | y = e.y; |
| 359 | mX = e.movementX; |
| 360 | mY = e.movementY; |
| 361 | resolve(); |
| 362 | }; |
| 363 | document.addEventListener('mousemove', mousemoveHandler, {once: true}); |
| 364 | }); |
| 365 | true; // A promise is defined above, but do not wait. |
| 366 | )"; |
| 367 | std::string define_variables = R"( |
| 368 | var x; |
| 369 | var y; |
| 370 | var mX; |
| 371 | var mY; |
| 372 | var mouseMoveExecuted; |
| 373 | var mousemoveHandler; |
| 374 | )"; |
Liviu Tinta | cb1d46d | 2020-05-04 16:26:05 | [diff] [blame] | 375 | // Add a mouse move event listener to the root frame. |
| 376 | EXPECT_TRUE(ExecJs(root, define_variables)); |
Liviu Tinta | d59e507 | 2020-04-23 14:31:06 | [diff] [blame] | 377 | EXPECT_TRUE(ExecJs(root, set_mouse_move_event_listener)); |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 378 | |
Ella Ge | 622de9b0 | 2019-03-20 21:42:43 | [diff] [blame] | 379 | // Send a mouse move to root frame before lock to set last mouse position. |
Daniel Cheng | 93c80a9 | 2018-02-14 19:02:43 | [diff] [blame] | 380 | blink::WebMouseEvent mouse_event( |
Dave Tapuska | 347d60a | 2020-04-21 23:55:47 | [diff] [blame] | 381 | blink::WebInputEvent::Type::kMouseMove, |
| 382 | blink::WebInputEvent::kNoModifiers, |
Daniel Cheng | 93c80a9 | 2018-02-14 19:02:43 | [diff] [blame] | 383 | blink::WebInputEvent::GetStaticTimeStampForTests()); |
Ella Ge | 2ebcc1f6 | 2019-07-02 21:50:15 | [diff] [blame] | 384 | mouse_event.pointer_type = blink::WebPointerProperties::PointerType::kMouse; |
Ella Ge | 622de9b0 | 2019-03-20 21:42:43 | [diff] [blame] | 385 | mouse_event.SetPositionInWidget(6, 7); |
Ella Ge | 2ebcc1f6 | 2019-07-02 21:50:15 | [diff] [blame] | 386 | mouse_event.SetPositionInScreen(6, 7); |
Ella Ge | 622de9b0 | 2019-03-20 21:42:43 | [diff] [blame] | 387 | mouse_event.movement_x = 8; |
| 388 | mouse_event.movement_y = 9; |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 389 | router->RouteMouseEvent(root_view, &mouse_event, ui::LatencyInfo()); |
| 390 | |
Liviu Tinta | d59e507 | 2020-04-23 14:31:06 | [diff] [blame] | 391 | // wait for mouse move to fire mouse move event |
| 392 | EXPECT_EQ(true, EvalJs(root, |
| 393 | "(async ()=> {return await " |
Liviu Tinta | cb1d46d | 2020-05-04 16:26:05 | [diff] [blame] | 394 | "mouseMoveExecuted.then(()=>true);})();")); |
Dave Tapuska | 9425f10 | 2024-04-19 18:18:31 | [diff] [blame] | 395 | EXPECT_EQ("[6,7,0,0]", EvalJs(root, "JSON.stringify([x,y,mX,mY])")); |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 396 | EXPECT_EQ(true, PointerLockHelper::RequestPointerLockOnBody(root)); |
Ella Ge | 622de9b0 | 2019-03-20 21:42:43 | [diff] [blame] | 397 | // Root frame should have been granted pointer lock. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 398 | EXPECT_EQ(true, PointerLockHelper::IsPointerLockOnBody(root)); |
Liviu Tinta | d59e507 | 2020-04-23 14:31:06 | [diff] [blame] | 399 | EXPECT_TRUE(ExecJs(root, set_mouse_move_event_listener)); |
Ella Ge | 622de9b0 | 2019-03-20 21:42:43 | [diff] [blame] | 400 | |
Ella Ge | 2ebcc1f6 | 2019-07-02 21:50:15 | [diff] [blame] | 401 | mouse_event.SetPositionInWidget(10, 12); |
| 402 | mouse_event.SetPositionInScreen(10, 12); |
Ella Ge | 622de9b0 | 2019-03-20 21:42:43 | [diff] [blame] | 403 | mouse_event.movement_x = 12; |
| 404 | mouse_event.movement_y = 13; |
| 405 | router->RouteMouseEvent(root_view, &mouse_event, ui::LatencyInfo()); |
| 406 | |
Liviu Tinta | d59e507 | 2020-04-23 14:31:06 | [diff] [blame] | 407 | EXPECT_EQ(true, EvalJs(root, |
| 408 | "(async ()=> {return await " |
Liviu Tinta | cb1d46d | 2020-05-04 16:26:05 | [diff] [blame] | 409 | "mouseMoveExecuted.then(()=>true);})();")); |
Ella Ge | 622de9b0 | 2019-03-20 21:42:43 | [diff] [blame] | 410 | // Locked event has same coordinates as before locked. |
Dave Tapuska | 9425f10 | 2024-04-19 18:18:31 | [diff] [blame] | 411 | EXPECT_EQ("[6,7,4,5]", EvalJs(root, "JSON.stringify([x,y,mX,mY])")); |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 412 | |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 413 | EXPECT_EQ(true, PointerLockHelper::ExitPointerLock(root)); |
| 414 | |
| 415 | EXPECT_EQ(true, PointerLockHelper::RequestPointerLockOnBody(child)); |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 416 | |
Liviu Tinta | cb1d46d | 2020-05-04 16:26:05 | [diff] [blame] | 417 | // define all all global variables on the child |
| 418 | EXPECT_TRUE(ExecJs(child, define_variables)); |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 419 | // Child frame should have been granted pointer lock. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 420 | EXPECT_EQ(true, PointerLockHelper::IsPointerLockOnBody(child)); |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 421 | |
| 422 | // Add a mouse move event listener to the child frame. |
Liviu Tinta | d59e507 | 2020-04-23 14:31:06 | [diff] [blame] | 423 | EXPECT_TRUE(ExecJs(child, set_mouse_move_event_listener)); |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 424 | |
Ella Ge | 43cccf17 | 2017-10-19 15:44:16 | [diff] [blame] | 425 | gfx::PointF transformed_point; |
| 426 | root_view->TransformPointToCoordSpaceForView(gfx::PointF(0, 0), child_view, |
Ria Jiang | bd22e55d | 2019-03-04 19:23:50 | [diff] [blame] | 427 | &transformed_point); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 428 | mouse_event.SetPositionInWidget(-transformed_point.x() + 14, |
mustaq | c51f3aab | 2017-04-05 15:43:11 | [diff] [blame] | 429 | -transformed_point.y() + 15); |
Ella Ge | 2ebcc1f6 | 2019-07-02 21:50:15 | [diff] [blame] | 430 | mouse_event.SetPositionInScreen(-transformed_point.x() + 14, |
| 431 | -transformed_point.y() + 15); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 432 | mouse_event.movement_x = 16; |
| 433 | mouse_event.movement_y = 17; |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 434 | // We use root_view intentionally as the RenderWidgetHostInputEventRouter is |
| 435 | // responsible for correctly routing the event to the child frame. |
| 436 | router->RouteMouseEvent(root_view, &mouse_event, ui::LatencyInfo()); |
| 437 | |
Liviu Tinta | d59e507 | 2020-04-23 14:31:06 | [diff] [blame] | 438 | EXPECT_EQ(true, EvalJs(child, |
| 439 | "(async ()=> {return await " |
| 440 | "mouseMoveExecuted.then(()=>true);})()")); |
Ella Ge | 622de9b0 | 2019-03-20 21:42:43 | [diff] [blame] | 441 | // This is the first event to child render, so the coordinates is (0, 0) |
Dave Tapuska | 9425f10 | 2024-04-19 18:18:31 | [diff] [blame] | 442 | EXPECT_EQ("[0,0,0,0]", EvalJs(child, "JSON.stringify([x,y,mX,mY])")); |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 443 | } |
| 444 | |
lfg | 7d4caad | 2017-03-22 09:01:45 | [diff] [blame] | 445 | // Tests that the browser will not unlock the pointer if a RenderWidgetHostView |
Liviu Tinta | 942fbf4 | 2020-05-28 17:04:16 | [diff] [blame] | 446 | // that doesn't hold the pointer lock is destroyed |
| 447 | IN_PROC_BROWSER_TEST_F(PointerLockBrowserTest, PointerLockChildFrameDetached) { |
lfg | 7d4caad | 2017-03-22 09:01:45 | [diff] [blame] | 448 | GURL main_url(embedded_test_server()->GetURL( |
| 449 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 450 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 451 | |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 452 | FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root(); |
lfg | 7d4caad | 2017-03-22 09:01:45 | [diff] [blame] | 453 | |
| 454 | // Request a pointer lock on the root frame's body. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 455 | EXPECT_EQ(true, PointerLockHelper::RequestPointerLockOnBody(root)); |
lfg | 7d4caad | 2017-03-22 09:01:45 | [diff] [blame] | 456 | // Root frame should have been granted pointer lock. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 457 | EXPECT_EQ(true, PointerLockHelper::IsPointerLockOnBody(root)); |
lfg | 7d4caad | 2017-03-22 09:01:45 | [diff] [blame] | 458 | |
| 459 | // Root (platform) RenderWidgetHostView should have the pointer locked. |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 460 | EXPECT_TRUE(root->current_frame_host()->GetView()->IsPointerLocked()); |
lfg | 7d4caad | 2017-03-22 09:01:45 | [diff] [blame] | 461 | EXPECT_EQ(root->current_frame_host()->GetRenderWidgetHost(), |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 462 | web_contents()->GetPointerLockWidget()); |
lfg | 7d4caad | 2017-03-22 09:01:45 | [diff] [blame] | 463 | |
| 464 | // Detach the child frame. |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 465 | EXPECT_TRUE(ExecJs(root, "document.querySelector('iframe').remove()")); |
lfg | 7d4caad | 2017-03-22 09:01:45 | [diff] [blame] | 466 | |
| 467 | // Root (platform) RenderWidgetHostView should still have the pointer locked. |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 468 | EXPECT_TRUE(root->current_frame_host()->GetView()->IsPointerLocked()); |
lfg | 7d4caad | 2017-03-22 09:01:45 | [diff] [blame] | 469 | EXPECT_EQ(root->current_frame_host()->GetRenderWidgetHost(), |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 470 | web_contents()->GetPointerLockWidget()); |
lfg | 7d4caad | 2017-03-22 09:01:45 | [diff] [blame] | 471 | } |
| 472 | |
Nick Carter | d73635b | 2018-03-13 18:31:41 | [diff] [blame] | 473 | // Tests that the browser will unlock the pointer if a RenderWidgetHostView that |
| 474 | // holds the pointer lock crashes. |
| 475 | IN_PROC_BROWSER_TEST_F(PointerLockBrowserTest, |
| 476 | PointerLockInnerContentsCrashes) { |
| 477 | GURL main_url(embedded_test_server()->GetURL( |
| 478 | "a.com", "/cross_site_iframe_factory.html?a(b(b))")); |
| 479 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 480 | |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 481 | FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root(); |
Nick Carter | d73635b | 2018-03-13 18:31:41 | [diff] [blame] | 482 | |
| 483 | // Attach an inner WebContents; it's owned by the FrameTree, so we obtain an |
| 484 | // observer to it. |
| 485 | WebContents* inner_contents = CreateAndAttachInnerContents( |
| 486 | root->child_at(0)->child_at(0)->current_frame_host()); |
| 487 | WebContentsDestroyedWatcher inner_death_observer(inner_contents); |
| 488 | |
| 489 | // Override the delegate so that we can stub out pointer lock events. |
| 490 | inner_contents->SetDelegate(&web_contents_delegate_); |
| 491 | |
| 492 | // Navigate the inner webcontents to a page. |
| 493 | EXPECT_TRUE(NavigateToURLFromRenderer( |
| 494 | inner_contents, embedded_test_server()->GetURL( |
| 495 | "c.com", "/cross_site_iframe_factory.html?c(d)"))); |
| 496 | |
| 497 | // Request a pointer lock to the inner WebContents's document.body. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 498 | EXPECT_EQ(true, PointerLockHelper::RequestPointerLockOnBody( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 499 | inner_contents->GetPrimaryMainFrame())); |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 500 | EXPECT_EQ(true, PointerLockHelper::IsPointerLockOnBody( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 501 | inner_contents->GetPrimaryMainFrame())); |
Nick Carter | d73635b | 2018-03-13 18:31:41 | [diff] [blame] | 502 | |
| 503 | // Root (platform) RenderWidgetHostView should have the pointer locked. |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 504 | EXPECT_TRUE(root->current_frame_host()->GetView()->IsPointerLocked()); |
Nick Carter | d73635b | 2018-03-13 18:31:41 | [diff] [blame] | 505 | |
| 506 | // The widget doing the lock is the one from the inner WebContents. A link |
| 507 | // to that RWH is saved into the outer webcontents. |
| 508 | RenderWidgetHost* expected_lock_widget = |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 509 | inner_contents->GetPrimaryMainFrame()->GetView()->GetRenderWidgetHost(); |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 510 | EXPECT_EQ(expected_lock_widget, web_contents()->GetPointerLockWidget()); |
| 511 | EXPECT_EQ(expected_lock_widget, web_contents()->pointer_lock_widget_); |
| 512 | EXPECT_EQ( |
| 513 | expected_lock_widget, |
| 514 | static_cast<WebContentsImpl*>(inner_contents)->pointer_lock_widget_); |
Nick Carter | d73635b | 2018-03-13 18:31:41 | [diff] [blame] | 515 | |
| 516 | // Crash the subframe process. |
| 517 | RenderProcessHost* crash_process = |
| 518 | root->child_at(0)->current_frame_host()->GetProcess(); |
| 519 | RenderProcessHostWatcher crash_observer( |
| 520 | crash_process, RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 521 | crash_process->Shutdown(0); |
| 522 | crash_observer.Wait(); |
| 523 | |
| 524 | // Wait for destruction of |inner_contents|. |
| 525 | inner_death_observer.Wait(); |
| 526 | inner_contents = nullptr; |
| 527 | |
| 528 | // This should cancel the pointer lock. |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 529 | EXPECT_EQ(nullptr, web_contents()->GetPointerLockWidget()); |
| 530 | EXPECT_EQ(nullptr, web_contents()->pointer_lock_widget_.get()); |
| 531 | EXPECT_FALSE(web_contents()->HasPointerLock( |
Nick Carter | d73635b | 2018-03-13 18:31:41 | [diff] [blame] | 532 | root->current_frame_host()->GetRenderWidgetHost())); |
| 533 | } |
| 534 | |
Lucas Gadani | a0ea017 | 2018-09-20 18:31:37 | [diff] [blame] | 535 | IN_PROC_BROWSER_TEST_F(PointerLockBrowserTest, PointerLockOopifCrashes) { |
| 536 | // This test runs three times, testing a crash at each level of the frametree. |
| 537 | for (int crash_depth = 0; crash_depth < 3; crash_depth++) { |
| 538 | GURL main_url(embedded_test_server()->GetURL( |
| 539 | "a.com", "/cross_site_iframe_factory.html?a(b(c))")); |
| 540 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 541 | |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 542 | FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root(); |
Lucas Gadani | a0ea017 | 2018-09-20 18:31:37 | [diff] [blame] | 543 | FrameTreeNode* lock_node = root->child_at(0)->child_at(0); |
| 544 | |
| 545 | // Pick which node to crash. |
| 546 | FrameTreeNode* crash_node = root; |
| 547 | for (int i = 0; i < crash_depth; i++) |
| 548 | crash_node = crash_node->child_at(0); |
| 549 | |
| 550 | // Request a pointer lock to |lock_node|'s document.body. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 551 | EXPECT_EQ(true, PointerLockHelper::RequestPointerLockOnBody(lock_node)); |
| 552 | EXPECT_EQ(true, PointerLockHelper::IsPointerLockOnBody(lock_node)); |
Lucas Gadani | a0ea017 | 2018-09-20 18:31:37 | [diff] [blame] | 553 | |
| 554 | // Root (platform) RenderWidgetHostView should have the pointer locked. |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 555 | EXPECT_TRUE(root->current_frame_host()->GetView()->IsPointerLocked()); |
Lucas Gadani | a0ea017 | 2018-09-20 18:31:37 | [diff] [blame] | 556 | EXPECT_EQ(lock_node->current_frame_host()->GetRenderWidgetHost(), |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 557 | web_contents()->GetPointerLockWidget()); |
Lucas Gadani | a0ea017 | 2018-09-20 18:31:37 | [diff] [blame] | 558 | |
| 559 | // Crash the process of |crash_node|. |
| 560 | RenderProcessHost* crash_process = |
| 561 | crash_node->current_frame_host()->GetProcess(); |
| 562 | RenderProcessHostWatcher crash_observer( |
| 563 | crash_process, RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 564 | crash_process->Shutdown(0); |
| 565 | crash_observer.Wait(); |
| 566 | |
| 567 | // This should cancel the pointer lock. |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 568 | EXPECT_EQ(nullptr, web_contents()->GetPointerLockWidget()); |
| 569 | EXPECT_EQ(nullptr, web_contents()->pointer_lock_widget_.get()); |
| 570 | EXPECT_FALSE(web_contents()->HasPointerLock( |
Lucas Gadani | a0ea017 | 2018-09-20 18:31:37 | [diff] [blame] | 571 | root->current_frame_host()->GetRenderWidgetHost())); |
| 572 | if (crash_depth != 0) |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 573 | EXPECT_FALSE(root->current_frame_host()->GetView()->IsPointerLocked()); |
Lucas Gadani | a0ea017 | 2018-09-20 18:31:37 | [diff] [blame] | 574 | else |
| 575 | EXPECT_EQ(nullptr, root->current_frame_host()->GetView()); |
| 576 | } |
| 577 | } |
| 578 | |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 579 | #if BUILDFLAG(IS_LINUX) |
Martin Šrámek | bcbe62a | 2021-05-07 11:54:27 | [diff] [blame] | 580 | #define MAYBE_PointerLockWheelEventRouting DISABLED_PointerLockWheelEventRouting |
| 581 | #else |
| 582 | #define MAYBE_PointerLockWheelEventRouting PointerLockWheelEventRouting |
| 583 | #endif |
| 584 | IN_PROC_BROWSER_TEST_F(PointerLockBrowserTest, |
| 585 | MAYBE_PointerLockWheelEventRouting) { |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 586 | GURL main_url(embedded_test_server()->GetURL( |
| 587 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 588 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 589 | |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 590 | FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root(); |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 591 | FrameTreeNode* child = root->child_at(0); |
Kartar Singh | b1bfa1a | 2024-06-24 13:14:57 | [diff] [blame] | 592 | input::RenderWidgetHostInputEventRouter* router = |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 593 | web_contents()->GetInputEventRouter(); |
| 594 | RenderWidgetHostViewBase* root_view = static_cast<RenderWidgetHostViewBase*>( |
| 595 | root->current_frame_host()->GetView()); |
| 596 | RenderWidgetHostViewBase* child_view = static_cast<RenderWidgetHostViewBase*>( |
| 597 | child->current_frame_host()->GetView()); |
| 598 | |
kylechar | a7c549b | 2019-07-29 17:47:28 | [diff] [blame] | 599 | WaitForHitTestData(child->current_frame_host()); |
Abdelrahman Eed | 180a762 | 2025-06-13 11:39:54 | [diff] [blame] | 600 | // Make sure that page_scale is 1, on Android page scale is not guaranteed to |
| 601 | // be 1 |
| 602 | web_contents()->SetPageScale(1.0); |
Ken Buchanan | 8a319fb | 2017-11-15 18:37:12 | [diff] [blame] | 603 | |
Ella Ge | 622de9b0 | 2019-03-20 21:42:43 | [diff] [blame] | 604 | // Add a mouse move event listener to the root frame. |
| 605 | EXPECT_TRUE(ExecJs( |
| 606 | root, |
| 607 | "var x; var y; var dX; var dY; document.addEventListener('mousemove', " |
| 608 | "function(e) {x = e.x; y = e.y; mX = e.movementX; mY = e.movementY;});")); |
| 609 | |
| 610 | // Send a mouse move to root frame before lock to set last mouse position. |
| 611 | blink::WebMouseEvent mouse_event( |
Dave Tapuska | 347d60a | 2020-04-21 23:55:47 | [diff] [blame] | 612 | blink::WebInputEvent::Type::kMouseMove, |
| 613 | blink::WebInputEvent::kNoModifiers, |
Ella Ge | 622de9b0 | 2019-03-20 21:42:43 | [diff] [blame] | 614 | blink::WebInputEvent::GetStaticTimeStampForTests()); |
Ella Ge | 2ebcc1f6 | 2019-07-02 21:50:15 | [diff] [blame] | 615 | mouse_event.pointer_type = blink::WebPointerProperties::PointerType::kMouse; |
Ella Ge | 622de9b0 | 2019-03-20 21:42:43 | [diff] [blame] | 616 | mouse_event.SetPositionInWidget(6, 7); |
Ella Ge | 2ebcc1f6 | 2019-07-02 21:50:15 | [diff] [blame] | 617 | mouse_event.SetPositionInScreen(6, 7); |
Ella Ge | 622de9b0 | 2019-03-20 21:42:43 | [diff] [blame] | 618 | mouse_event.movement_x = 8; |
| 619 | mouse_event.movement_y = 9; |
| 620 | router->RouteMouseEvent(root_view, &mouse_event, ui::LatencyInfo()); |
| 621 | |
| 622 | // Make sure that the renderer handled the input event. |
| 623 | MainThreadFrameObserver root_observer(root_view->GetRenderWidgetHost()); |
| 624 | root_observer.Wait(); |
| 625 | |
Dave Tapuska | 9425f10 | 2024-04-19 18:18:31 | [diff] [blame] | 626 | EXPECT_EQ("[6,7,0,0]", EvalJs(root, "JSON.stringify([x,y,mX,mY])")); |
Ella Ge | 622de9b0 | 2019-03-20 21:42:43 | [diff] [blame] | 627 | |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 628 | EXPECT_EQ(true, PointerLockHelper::RequestPointerLockOnBody(root)); |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 629 | |
| 630 | // Root frame should have been granted pointer lock. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 631 | EXPECT_EQ(true, PointerLockHelper::IsPointerLockOnBody(root)); |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 632 | |
| 633 | // Add a mouse move wheel event listener to the root frame. |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 634 | EXPECT_TRUE(ExecJs( |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 635 | root, |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 636 | "var x; var y; var dX; var dY; document.addEventListener('mousewheel', " |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 637 | "function(e) {x = e.x; y = e.y; dX = e.deltaX; dY = e.deltaY;});")); |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 638 | root_observer.Wait(); |
| 639 | |
| 640 | blink::WebMouseWheelEvent wheel_event( |
Dave Tapuska | 347d60a | 2020-04-21 23:55:47 | [diff] [blame] | 641 | blink::WebInputEvent::Type::kMouseWheel, |
| 642 | blink::WebInputEvent::kNoModifiers, |
Daniel Cheng | 93c80a9 | 2018-02-14 19:02:43 | [diff] [blame] | 643 | blink::WebInputEvent::GetStaticTimeStampForTests()); |
Ella Ge | 2ebcc1f6 | 2019-07-02 21:50:15 | [diff] [blame] | 644 | wheel_event.SetPositionInScreen(10, 11); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 645 | wheel_event.delta_x = -12; |
| 646 | wheel_event.delta_y = -13; |
sahel | 4194246 | 2017-07-06 14:14:37 | [diff] [blame] | 647 | wheel_event.phase = blink::WebMouseWheelEvent::kPhaseBegan; |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 648 | router->RouteMouseWheelEvent(root_view, &wheel_event, ui::LatencyInfo()); |
| 649 | |
| 650 | // Make sure that the renderer handled the input event. |
| 651 | root_observer.Wait(); |
| 652 | |
Sahel Sharify | e6d81f47 | 2018-07-11 20:40:26 | [diff] [blame] | 653 | // All wheel events during a scroll sequence will be sent to a single target. |
| 654 | // Send a wheel end event to the current target before sending wheel events to |
| 655 | // a new target. |
| 656 | wheel_event.delta_x = 0; |
| 657 | wheel_event.delta_y = 0; |
| 658 | wheel_event.phase = blink::WebMouseWheelEvent::kPhaseEnded; |
| 659 | router->RouteMouseWheelEvent(root_view, &wheel_event, ui::LatencyInfo()); |
sahel | 955c329 | 2017-08-17 14:56:44 | [diff] [blame] | 660 | |
Sahel Sharify | e6d81f47 | 2018-07-11 20:40:26 | [diff] [blame] | 661 | // Make sure that the renderer handled the input event. |
| 662 | root_observer.Wait(); |
sahel | 955c329 | 2017-08-17 14:56:44 | [diff] [blame] | 663 | |
Ella Ge | 622de9b0 | 2019-03-20 21:42:43 | [diff] [blame] | 664 | // Locked event has same coordinates as before locked. |
| 665 | EXPECT_EQ("[6,7,12,13]", EvalJs(root, "JSON.stringify([x, y, dX, dY])")); |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 666 | |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 667 | EXPECT_EQ(true, PointerLockHelper::ExitPointerLock(root)); |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 668 | |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 669 | EXPECT_EQ(true, PointerLockHelper::RequestPointerLockOnBody(child)); |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 670 | |
| 671 | // Child frame should have been granted pointer lock. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 672 | EXPECT_EQ(true, PointerLockHelper::IsPointerLockOnBody(child)); |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 673 | |
| 674 | // Add a mouse move event listener to the child frame. |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 675 | EXPECT_TRUE(ExecJs( |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 676 | child, |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 677 | "var x; var y; var dX; var dY; document.addEventListener('mousewheel', " |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 678 | "function(e) {x = e.x; y = e.y; dX = e.deltaX; dY = e.deltaY;});")); |
| 679 | MainThreadFrameObserver child_observer(child_view->GetRenderWidgetHost()); |
| 680 | child_observer.Wait(); |
| 681 | |
Ella Ge | 43cccf17 | 2017-10-19 15:44:16 | [diff] [blame] | 682 | gfx::PointF transformed_point; |
| 683 | root_view->TransformPointToCoordSpaceForView(gfx::PointF(0, 0), child_view, |
Ria Jiang | bd22e55d | 2019-03-04 19:23:50 | [diff] [blame] | 684 | &transformed_point); |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 685 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 686 | wheel_event.SetPositionInWidget(-transformed_point.x() + 14, |
mustaq | c51f3aab | 2017-04-05 15:43:11 | [diff] [blame] | 687 | -transformed_point.y() + 15); |
Ella Ge | 2ebcc1f6 | 2019-07-02 21:50:15 | [diff] [blame] | 688 | wheel_event.SetPositionInScreen(-transformed_point.x() + 14, |
| 689 | -transformed_point.y() + 15); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 690 | wheel_event.delta_x = -16; |
| 691 | wheel_event.delta_y = -17; |
Sahel Sharify | e6d81f47 | 2018-07-11 20:40:26 | [diff] [blame] | 692 | wheel_event.phase = blink::WebMouseWheelEvent::kPhaseBegan; |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 693 | // We use root_view intentionally as the RenderWidgetHostInputEventRouter is |
| 694 | // responsible for correctly routing the event to the child frame. |
| 695 | router->RouteMouseWheelEvent(root_view, &wheel_event, ui::LatencyInfo()); |
| 696 | |
| 697 | // Make sure that the renderer handled the input event. |
| 698 | child_observer.Wait(); |
| 699 | |
Ella Ge | 622de9b0 | 2019-03-20 21:42:43 | [diff] [blame] | 700 | // This is the first event to child render, so the coordinates is (0, 0) |
| 701 | EXPECT_EQ("[0,0,16,17]", EvalJs(child, "JSON.stringify([x, y, dX, dY])")); |
lfg | 9a467e1 | 2017-03-23 21:14:36 | [diff] [blame] | 702 | } |
| 703 | |
Lucas Furukawa Gadani | dd328daaa | 2018-03-19 22:38:59 | [diff] [blame] | 704 | IN_PROC_BROWSER_TEST_F(PointerLockBrowserTest, PointerLockWidgetHidden) { |
| 705 | GURL main_url(embedded_test_server()->GetURL( |
| 706 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 707 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 708 | |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 709 | FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root(); |
Lucas Furukawa Gadani | dd328daaa | 2018-03-19 22:38:59 | [diff] [blame] | 710 | FrameTreeNode* child = root->child_at(0); |
| 711 | RenderWidgetHostViewBase* child_view = static_cast<RenderWidgetHostViewBase*>( |
| 712 | child->current_frame_host()->GetView()); |
| 713 | |
kylechar | a7c549b | 2019-07-29 17:47:28 | [diff] [blame] | 714 | WaitForHitTestData(child->current_frame_host()); |
Lucas Furukawa Gadani | dd328daaa | 2018-03-19 22:38:59 | [diff] [blame] | 715 | |
| 716 | // Request a pointer lock on the child frame's body. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 717 | EXPECT_EQ(true, PointerLockHelper::RequestPointerLockOnBody(child)); |
Lucas Furukawa Gadani | dd328daaa | 2018-03-19 22:38:59 | [diff] [blame] | 718 | // Child frame should have been granted pointer lock. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 719 | EXPECT_EQ(true, PointerLockHelper::IsPointerLockOnBody(child)); |
| 720 | |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 721 | EXPECT_TRUE(child_view->IsPointerLocked()); |
| 722 | EXPECT_EQ(child_view->host(), web_contents()->GetPointerLockWidget()); |
Lucas Furukawa Gadani | dd328daaa | 2018-03-19 22:38:59 | [diff] [blame] | 723 | |
| 724 | child_view->Hide(); |
| 725 | |
| 726 | // Child frame should've released the mouse lock when hidden. |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 727 | EXPECT_FALSE(child_view->IsPointerLocked()); |
| 728 | EXPECT_EQ(nullptr, web_contents()->GetPointerLockWidget()); |
Lucas Furukawa Gadani | dd328daaa | 2018-03-19 22:38:59 | [diff] [blame] | 729 | } |
| 730 | |
jameshollyer | 48a702f | 2020-04-11 04:21:40 | [diff] [blame] | 731 | #ifdef USE_AURA |
| 732 | IN_PROC_BROWSER_TEST_F(PointerLockBrowserTest, PointerLockOutOfFocus) { |
| 733 | GURL main_url(embedded_test_server()->GetURL( |
| 734 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 735 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 736 | |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 737 | FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root(); |
jameshollyer | 48a702f | 2020-04-11 04:21:40 | [diff] [blame] | 738 | MockPointerLockRenderWidgetHostView* root_view = |
| 739 | static_cast<MockPointerLockRenderWidgetHostView*>( |
| 740 | root->current_frame_host()->GetView()); |
| 741 | |
| 742 | root_view->has_focus_ = false; |
| 743 | // Request a pointer lock on the root frame's body. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 744 | EXPECT_EQ(false, PointerLockHelper::RequestPointerLockOnBody(root)); |
jameshollyer | 48a702f | 2020-04-11 04:21:40 | [diff] [blame] | 745 | // Root frame should not have been granted pointer lock. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 746 | EXPECT_EQ(false, PointerLockHelper::IsPointerLockOnBody(root)); |
jameshollyer | 48a702f | 2020-04-11 04:21:40 | [diff] [blame] | 747 | } |
| 748 | #endif |
| 749 | |
Mustaq Ahmed | 8f49a8b2f | 2021-06-25 22:42:28 | [diff] [blame] | 750 | IN_PROC_BROWSER_TEST_F(PointerLockBrowserTest, PointerLockOnDroppedElem) { |
| 751 | GURL url = embedded_test_server()->GetURL( |
| 752 | "a.com", "/pointerlock_on_dropped_elem.html"); |
| 753 | EXPECT_TRUE(NavigateToURL(shell(), url)); |
| 754 | |
| 755 | EXPECT_TRUE(ExecJs(shell(), "document.body.click();")); |
| 756 | |
| 757 | // The second ExecJS() call here delays test termination so that the first |
| 758 | // call's async tasks get a chance to run. |
| 759 | EXPECT_TRUE(ExecJs(shell(), "", EXECUTE_SCRIPT_NO_USER_GESTURE)); |
| 760 | } |
| 761 | |
Abdelrahman Eed | 180a762 | 2025-06-13 11:39:54 | [diff] [blame] | 762 | // unajustedMovement flag is not supported on Android |
| 763 | #if BUILDFLAG(IS_ANDROID) |
| 764 | #define MAYBE_PointerLockRequestUnadjustedMovement \ |
| 765 | DISABLED_PointerLockRequestUnadjustedMovement |
| 766 | #else |
| 767 | #define MAYBE_PointerLockRequestUnadjustedMovement \ |
| 768 | PointerLockRequestUnadjustedMovement |
| 769 | #endif |
Ella Ge | 04708ae | 2023-03-24 19:55:18 | [diff] [blame] | 770 | IN_PROC_BROWSER_TEST_F(PointerLockBrowserTest, |
Abdelrahman Eed | 180a762 | 2025-06-13 11:39:54 | [diff] [blame] | 771 | MAYBE_PointerLockRequestUnadjustedMovement) { |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 772 | GURL main_url(embedded_test_server()->GetURL( |
| 773 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 774 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 775 | |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 776 | FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root(); |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 777 | |
Liviu Tinta | bf520b3c5 | 2020-04-23 14:33:15 | [diff] [blame] | 778 | EXPECT_TRUE(ExecJs(root, "var pointerLockPromise;")); |
| 779 | std::string wait_for_pointer_lock_promise = |
| 780 | "(async ()=> {return await pointerLockPromise.then(()=>true, " |
| 781 | "()=>false);})()"; |
| 782 | std::string set_pointer_lock_promise = |
| 783 | R"code(pointerLockPromise = new Promise( function(resolve, reject){ |
| 784 | document.addEventListener('pointerlockchange', resolve); |
| 785 | document.addEventListener('pointerlockerror', reject) |
| 786 | });)code"; |
| 787 | |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 788 | // Request a pointer lock. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 789 | EXPECT_EQ(true, PointerLockHelper::RequestPointerLockOnBody(root)); |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 790 | // Root frame should have been granted pointer lock. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 791 | EXPECT_EQ(true, PointerLockHelper::IsPointerLockOnBody(root)); |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 792 | // Mouse is locked and unadjusted_movement is not set. |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 793 | EXPECT_TRUE(root->current_frame_host()->GetView()->IsPointerLocked()); |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 794 | |
Liviu Tinta | bf520b3c5 | 2020-04-23 14:33:15 | [diff] [blame] | 795 | // Release pointer lock. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 796 | EXPECT_EQ(true, PointerLockHelper::ExitPointerLock(root)); |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 797 | |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 798 | #if defined(USE_AURA) || BUILDFLAG(IS_MAC) |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 799 | // Request a pointer lock with unadjustedMovement. |
| 800 | EXPECT_EQ( |
| 801 | true, |
| 802 | PointerLockHelper::RequestPointerLockWithUnadjustedMovementOnBody(root)); |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 803 | // Root frame should have been granted pointer lock. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 804 | EXPECT_EQ(true, PointerLockHelper::IsPointerLockOnBody(root)); |
| 805 | |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 806 | // Mouse is locked and unadjusted_movement is set. |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 807 | EXPECT_TRUE(root->current_frame_host()->GetView()->IsPointerLocked()); |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 808 | EXPECT_TRUE(root->current_frame_host() |
| 809 | ->GetView() |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 810 | ->GetIsPointerLockedUnadjustedMovementForTesting()); |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 811 | |
| 812 | // Release pointer lock, unadjusted_movement bit is reset. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 813 | EXPECT_EQ(true, PointerLockHelper::ExitPointerLock(root)); |
| 814 | |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 815 | EXPECT_FALSE(root->current_frame_host() |
| 816 | ->GetView() |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 817 | ->GetIsPointerLockedUnadjustedMovementForTesting()); |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 818 | #else |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 819 | // Request a pointer lock with unadjustedMovement. |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 820 | // On platform that does not support unadjusted movement yet, do not lock and |
| 821 | // a pointerlockerror event is dispatched. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 822 | EXPECT_EQ( |
| 823 | false, |
| 824 | PointerLockHelper::RequestPointerLockWithUnadjustedMovementOnBody(root)); |
| 825 | EXPECT_EQ(false, PointerLockHelper::IsPointerLockOnBody(root)); |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 826 | EXPECT_FALSE(root->current_frame_host()->GetView()->IsPointerLocked()); |
Ella Ge | 3fa5c3f | 2019-09-05 19:00:06 | [diff] [blame] | 827 | #endif |
| 828 | } |
Ella Ge | 2f721129 | 2019-09-05 23:52:19 | [diff] [blame] | 829 | |
| 830 | #if defined(USE_AURA) |
Etienne Pierre-doray | 544a9a1 | 2021-04-14 22:48:13 | [diff] [blame] | 831 | // Flaky on all platforms http://crbug.com/1198612. |
Ella Ge | 04708ae | 2023-03-24 19:55:18 | [diff] [blame] | 832 | IN_PROC_BROWSER_TEST_F(PointerLockBrowserTest, DISABLED_UnadjustedMovement) { |
Ella Ge | 2f721129 | 2019-09-05 23:52:19 | [diff] [blame] | 833 | GURL main_url(embedded_test_server()->GetURL( |
| 834 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 835 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 836 | |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 837 | FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root(); |
Kartar Singh | b1bfa1a | 2024-06-24 13:14:57 | [diff] [blame] | 838 | input::RenderWidgetHostInputEventRouter* router = |
Ella Ge | 2f721129 | 2019-09-05 23:52:19 | [diff] [blame] | 839 | web_contents()->GetInputEventRouter(); |
| 840 | RenderWidgetHostViewBase* root_view = static_cast<RenderWidgetHostViewBase*>( |
| 841 | root->current_frame_host()->GetView()); |
| 842 | |
| 843 | // Add a mouse move event listener to the root frame. |
| 844 | EXPECT_TRUE(ExecJs( |
| 845 | root, |
| 846 | "var x; var y; var mX; var mY; document.addEventListener('mousemove', " |
| 847 | "function(e) {x = e.x; y = e.y; mX = e.movementX; mY = e.movementY;});")); |
| 848 | |
| 849 | // Send a mouse move to root frame before lock. |
| 850 | blink::WebMouseEvent mouse_event( |
Dave Tapuska | 347d60a | 2020-04-21 23:55:47 | [diff] [blame] | 851 | blink::WebInputEvent::Type::kMouseMove, |
| 852 | blink::WebInputEvent::kNoModifiers, |
Ella Ge | 2f721129 | 2019-09-05 23:52:19 | [diff] [blame] | 853 | blink::WebInputEvent::GetStaticTimeStampForTests()); |
| 854 | mouse_event.pointer_type = blink::WebPointerProperties::PointerType::kMouse; |
| 855 | mouse_event.SetPositionInWidget(6, 7); |
| 856 | mouse_event.SetPositionInScreen(6, 7); |
| 857 | mouse_event.movement_x = 8; |
| 858 | mouse_event.movement_y = 9; |
| 859 | router->RouteMouseEvent(root_view, &mouse_event, ui::LatencyInfo()); |
| 860 | |
| 861 | // Make sure that the renderer handled the input event. |
| 862 | MainThreadFrameObserver root_observer(root_view->GetRenderWidgetHost()); |
| 863 | root_observer.Wait(); |
| 864 | |
| 865 | EXPECT_EQ("[6,7,0,0]", EvalJs(root, "JSON.stringify([x,y,mX,mY])")); |
| 866 | |
| 867 | // Request a pointer lock with unadjustedMovement. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 868 | EXPECT_EQ( |
| 869 | true, |
| 870 | PointerLockHelper::RequestPointerLockWithUnadjustedMovementOnBody(root)); |
| 871 | |
Ella Ge | 2f721129 | 2019-09-05 23:52:19 | [diff] [blame] | 872 | // Root frame should have been granted pointer lock. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 873 | EXPECT_EQ(true, PointerLockHelper::IsPointerLockOnBody(root)); |
| 874 | |
Ella Ge | 2f721129 | 2019-09-05 23:52:19 | [diff] [blame] | 875 | // Mouse is locked and unadjusted_movement is not set. |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 876 | EXPECT_TRUE(root->current_frame_host()->GetView()->IsPointerLocked()); |
Ella Ge | 2f721129 | 2019-09-05 23:52:19 | [diff] [blame] | 877 | |
| 878 | mouse_event.SetPositionInWidget(10, 10); |
| 879 | mouse_event.SetPositionInScreen(10, 10); |
| 880 | mouse_event.movement_x = 12; |
| 881 | mouse_event.movement_y = 9; |
| 882 | mouse_event.is_raw_movement_event = true; |
| 883 | router->RouteMouseEvent(root_view, &mouse_event, ui::LatencyInfo()); |
| 884 | root_observer.Wait(); |
| 885 | |
| 886 | // Raw movement events movement value from WebMouseEvent.movement_x/y. |
| 887 | EXPECT_EQ("[6,7,12,9]", EvalJs(root, "JSON.stringify([x,y,mX,mY])")); |
| 888 | |
| 889 | mouse_event.SetPositionInWidget(20, 21); |
| 890 | mouse_event.SetPositionInScreen(20, 21); |
| 891 | mouse_event.movement_x = 1; |
| 892 | mouse_event.movement_y = 2; |
| 893 | mouse_event.is_raw_movement_event = false; |
| 894 | router->RouteMouseEvent(root_view, &mouse_event, ui::LatencyInfo()); |
| 895 | root_observer.Wait(); |
| 896 | |
| 897 | // Non-raw movement events movement value from screen pos - last screen pos. |
| 898 | EXPECT_EQ("[6,7,10,11]", EvalJs(root, "JSON.stringify([x,y,mX,mY])")); |
| 899 | } |
| 900 | #endif |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 901 | |
| 902 | #if defined(USE_AURA) |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 903 | // TODO(crbug.com/40635377): Remove failure test when fully implemented |
Georg Neis | 35ff854b | 2024-12-17 02:02:08 | [diff] [blame] | 904 | #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_CHROMEOS) |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 905 | #define MAYBE_ChangeUnadjustedMovementFailure \ |
| 906 | DISABLED_ChangeUnadjustedMovementFailure |
| 907 | #else |
| 908 | #define MAYBE_ChangeUnadjustedMovementFailure ChangeUnadjustedMovementFailure |
| 909 | #endif |
| 910 | // Tests that a subsequent request to RequestPointerLock with different |
| 911 | // options inside a Child view gets piped to the proper places and gives |
| 912 | // the proper unsupported error(this option is only supported on Windows |
| 913 | // This was prompted by this bug: https://crbug.com/1062702 |
Ella Ge | 04708ae | 2023-03-24 19:55:18 | [diff] [blame] | 914 | IN_PROC_BROWSER_TEST_F(PointerLockBrowserTest, |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 915 | MAYBE_ChangeUnadjustedMovementFailure) { |
| 916 | GURL main_url(embedded_test_server()->GetURL( |
| 917 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 918 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 919 | |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 920 | FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root(); |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 921 | FrameTreeNode* child = root->child_at(0); |
| 922 | RenderWidgetHostViewBase* child_view = static_cast<RenderWidgetHostViewBase*>( |
| 923 | child->current_frame_host()->GetView()); |
| 924 | |
| 925 | WaitForHitTestData(child->current_frame_host()); |
| 926 | |
| 927 | // Request a pointer lock on the child frame's body and wait for the promise |
| 928 | // to resolve. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 929 | EXPECT_EQ(true, PointerLockHelper::RequestPointerLockOnBody(child)); |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 930 | // Child frame should have been granted pointer lock. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 931 | EXPECT_EQ(true, PointerLockHelper::IsPointerLockOnBody(child)); |
| 932 | |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 933 | EXPECT_TRUE(child_view->IsPointerLocked()); |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 934 | EXPECT_FALSE(root->current_frame_host() |
| 935 | ->GetView() |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 936 | ->GetIsPointerLockedUnadjustedMovementForTesting()); |
| 937 | EXPECT_EQ(child_view->host(), web_contents()->GetPointerLockWidget()); |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 938 | |
| 939 | // Request to change pointer lock options and wait for return. |
| 940 | EXPECT_EQ( |
| 941 | "a JavaScript error: \"NotSupportedError: The options asked for in this " |
| 942 | "request are not supported on this platform.\"\n", |
| 943 | EvalJs(child, |
| 944 | "document.body.requestPointerLock({unadjustedMovement:true})") |
Chris Fredrickson | c2831792 | 2025-07-27 21:18:43 | [diff] [blame] | 945 | .ExtractError()); |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 946 | |
| 947 | // The change errored out but the original lock should still be in place. |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 948 | EXPECT_TRUE(child_view->IsPointerLocked()); |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 949 | EXPECT_FALSE(root->current_frame_host() |
| 950 | ->GetView() |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 951 | ->GetIsPointerLockedUnadjustedMovementForTesting()); |
| 952 | EXPECT_EQ(child_view->host(), web_contents()->GetPointerLockWidget()); |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 953 | } |
| 954 | #endif |
| 955 | |
| 956 | #if defined(USE_AURA) |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 957 | #if BUILDFLAG(IS_WIN) |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 958 | // Tests that a subsequent request to RequestPointerLock with different |
| 959 | // options inside a Child view gets piped to the proper places and updates |
| 960 | // the option(this option is only supported on Windows). |
| 961 | // This was prompted by this bug: https://crbug.com/1062702 |
Ella Ge | 04708ae | 2023-03-24 19:55:18 | [diff] [blame] | 962 | IN_PROC_BROWSER_TEST_F(PointerLockBrowserTest, |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 963 | ChangeUnadjustedMovementSuccess) { |
| 964 | GURL main_url(embedded_test_server()->GetURL( |
| 965 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 966 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 967 | |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 968 | FrameTreeNode* root = web_contents()->GetPrimaryFrameTree().root(); |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 969 | FrameTreeNode* child = root->child_at(0); |
| 970 | RenderWidgetHostViewBase* child_view = static_cast<RenderWidgetHostViewBase*>( |
| 971 | child->current_frame_host()->GetView()); |
| 972 | |
| 973 | WaitForHitTestData(child->current_frame_host()); |
| 974 | |
| 975 | // Request a pointer lock on the child frame's body and wait for the promise |
| 976 | // to resolve. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 977 | EXPECT_EQ(true, PointerLockHelper::RequestPointerLockOnBody(child)); |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 978 | // Child frame should have been granted pointer lock. |
Liviu Tinta | 7f8bdfb | 2020-05-05 23:35:26 | [diff] [blame] | 979 | EXPECT_EQ(true, PointerLockHelper::IsPointerLockOnBody(child)); |
| 980 | |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 981 | EXPECT_TRUE(child_view->IsPointerLocked()); |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 982 | EXPECT_FALSE(root->current_frame_host() |
| 983 | ->GetView() |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 984 | ->GetIsPointerLockedUnadjustedMovementForTesting()); |
| 985 | EXPECT_EQ(child_view->host(), web_contents()->GetPointerLockWidget()); |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 986 | |
| 987 | // Request to change pointer lock options and wait for return. |
| 988 | EXPECT_EQ( |
Chris Fredrickson | d72b189 | 2025-07-10 22:04:42 | [diff] [blame] | 989 | base::Value(), |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 990 | EvalJs(child, |
| 991 | "document.body.requestPointerLock({unadjustedMovement:true})")); |
| 992 | |
| 993 | // The new changed lock should now be in place. |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 994 | EXPECT_TRUE(child_view->IsPointerLocked()); |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 995 | EXPECT_TRUE(root->current_frame_host() |
| 996 | ->GetView() |
Takumi Fujimoto | 4661871d | 2024-01-25 02:04:18 | [diff] [blame] | 997 | ->GetIsPointerLockedUnadjustedMovementForTesting()); |
| 998 | EXPECT_EQ(child_view->host(), web_contents()->GetPointerLockWidget()); |
James Hollyer | 0a8c7ee1 | 2020-04-04 07:45:16 | [diff] [blame] | 999 | } |
| 1000 | #endif // WIN_OS |
| 1001 | #endif // USE_AURA |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 1002 | } // namespace content |