1 // 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 <cstring>
6 
7 #include "chrome/browser/vr/test/webxr_vr_browser_test.h"
8 #include "content/public/browser/web_contents.h"
9 #include "content/public/test/browser_test_utils.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 
12 using testing::_;
13 using testing::Invoke;
14 
15 namespace vr {
16 
WebXrVrBrowserTestBase()17 WebXrVrBrowserTestBase::WebXrVrBrowserTestBase() {
18   enable_features_.push_back(features::kWebXr);
19 }
20 
21 WebXrVrBrowserTestBase::~WebXrVrBrowserTestBase() = default;
22 
EnterSessionWithUserGesture(content::WebContents * web_contents)23 void WebXrVrBrowserTestBase::EnterSessionWithUserGesture(
24     content::WebContents* web_contents) {
25   // Before requesting the session, set the requested auto-response so that the
26   // session is appropriately granted or rejected (or the request ignored).
27   GetPermissionRequestManager()->set_auto_response_for_test(
28       permission_auto_response_);
29 
30   // ExecuteScript runs with a user gesture, so we can just directly call
31   // requestSession instead of having to do the hacky workaround the
32   // instrumentation tests use of actually sending a click event to the canvas.
33   RunJavaScriptOrFail("onRequestSession()", web_contents);
34 }
35 
EnterSessionWithUserGestureOrFail(content::WebContents * web_contents)36 void WebXrVrBrowserTestBase::EnterSessionWithUserGestureOrFail(
37     content::WebContents* web_contents) {
38   EnterSessionWithUserGesture(web_contents);
39   PollJavaScriptBooleanOrFail(
40       "sessionInfos[sessionTypes.IMMERSIVE].currentSession != null",
41       kPollTimeoutLong, web_contents);
42 
43 #if defined(OS_WIN)
44   // For WMR, creating a session may take foreground from us, and Windows may
45   // not return it when the session terminates. This means subsequent requests
46   // to enter an immersive session may fail. The fix for testing is to call
47   // SetForegroundWindow manually. In real code, we'll have foreground if there
48   // was a user gesture to enter VR.
49   SetForegroundWindow(hwnd_);
50 #endif
51 }
52 
EndSession(content::WebContents * web_contents)53 void WebXrVrBrowserTestBase::EndSession(content::WebContents* web_contents) {
54   RunJavaScriptOrFail(
55       "sessionInfos[sessionTypes.IMMERSIVE].currentSession.end()",
56       web_contents);
57 }
58 
EndSessionOrFail(content::WebContents * web_contents)59 void WebXrVrBrowserTestBase::EndSessionOrFail(
60     content::WebContents* web_contents) {
61   EndSession(web_contents);
62   PollJavaScriptBooleanOrFail(
63       "sessionInfos[sessionTypes.IMMERSIVE].currentSession == null",
64       kPollTimeoutLong, web_contents);
65 }
66 
GetControllerOffset() const67 gfx::Vector3dF WebXrVrBrowserTestBase::GetControllerOffset() const {
68   return gfx::Vector3dF();
69 }
70 
71 permissions::PermissionRequestManager*
GetPermissionRequestManager()72 WebXrVrBrowserTestBase::GetPermissionRequestManager() {
73   return GetPermissionRequestManager(GetCurrentWebContents());
74 }
75 
76 permissions::PermissionRequestManager*
GetPermissionRequestManager(content::WebContents * web_contents)77 WebXrVrBrowserTestBase::GetPermissionRequestManager(
78     content::WebContents* web_contents) {
79   return permissions::PermissionRequestManager::FromWebContents(web_contents);
80 }
81 
WebXrVrRuntimelessBrowserTest()82 WebXrVrRuntimelessBrowserTest::WebXrVrRuntimelessBrowserTest() {
83 #if BUILDFLAG(ENABLE_WINDOWS_MR)
84   disable_features_.push_back(device::features::kWindowsMixedReality);
85 #endif
86 #if BUILDFLAG(ENABLE_OPENXR)
87   disable_features_.push_back(device::features::kOpenXR);
88 #endif
89 }
90 
91 WebXrVrRuntimelessBrowserTestSensorless::
WebXrVrRuntimelessBrowserTestSensorless()92     WebXrVrRuntimelessBrowserTestSensorless() {
93   // WebXrOrientationSensorDevice is only defined when the enable_vr flag is
94   // set.
95 #if BUILDFLAG(ENABLE_VR)
96   disable_features_.push_back(device::kWebXrOrientationSensorDevice);
97 #endif  // BUILDFLAG(ENABLE_VR)
98 }
99 
100 #if defined(OS_WIN)
101 
WebXrVrWmrBrowserTestBase()102 WebXrVrWmrBrowserTestBase::WebXrVrWmrBrowserTestBase() {
103 #if BUILDFLAG(ENABLE_WINDOWS_MR)
104   enable_features_.push_back(device::features::kWindowsMixedReality);
105 #endif
106 #if BUILDFLAG(ENABLE_OPENXR)
107   disable_features_.push_back(device::features::kOpenXR);
108 #endif
109 }
110 
111 WebXrVrWmrBrowserTestBase::~WebXrVrWmrBrowserTestBase() = default;
112 
PreRunTestOnMainThread()113 void WebXrVrWmrBrowserTestBase::PreRunTestOnMainThread() {
114   dummy_hook_ = std::make_unique<MockXRDeviceHookBase>();
115   WebXrVrBrowserTestBase::PreRunTestOnMainThread();
116 }
117 
GetRuntimeType() const118 XrBrowserTestBase::RuntimeType WebXrVrWmrBrowserTestBase::GetRuntimeType()
119     const {
120   return XrBrowserTestBase::RuntimeType::RUNTIME_WMR;
121 }
122 
123 #if BUILDFLAG(ENABLE_OPENXR)
124 
WebXrVrOpenXrBrowserTestBase()125 WebXrVrOpenXrBrowserTestBase::WebXrVrOpenXrBrowserTestBase() {
126   enable_features_.push_back(device::features::kOpenXR);
127 #if BUILDFLAG(ENABLE_WINDOWS_MR)
128   disable_features_.push_back(device::features::kWindowsMixedReality);
129 #endif
130 }
131 
132 WebXrVrOpenXrBrowserTestBase::~WebXrVrOpenXrBrowserTestBase() = default;
133 
GetRuntimeType() const134 XrBrowserTestBase::RuntimeType WebXrVrOpenXrBrowserTestBase::GetRuntimeType()
135     const {
136   return XrBrowserTestBase::RuntimeType::RUNTIME_OPENXR;
137 }
138 #endif  // BUILDFLAG(ENABLE_OPENXR)
139 
WebXrVrWmrBrowserTest()140 WebXrVrWmrBrowserTest::WebXrVrWmrBrowserTest() {
141   runtime_requirements_.push_back(XrTestRequirement::DIRECTX_11_1);
142 }
143 
144 #if BUILDFLAG(ENABLE_OPENXR)
WebXrVrOpenXrBrowserTest()145 WebXrVrOpenXrBrowserTest::WebXrVrOpenXrBrowserTest() {
146   runtime_requirements_.push_back(XrTestRequirement::DIRECTX_11_1);
147 }
148 #endif  // BUILDFLAG(ENABLE_OPENXR)
149 
150 // Test classes with WebXR disabled.
WebXrVrWmrBrowserTestWebXrDisabled()151 WebXrVrWmrBrowserTestWebXrDisabled::WebXrVrWmrBrowserTestWebXrDisabled() {
152   disable_features_.push_back(features::kWebXr);
153 }
154 
155 #if BUILDFLAG(ENABLE_OPENXR)
WebXrVrOpenXrBrowserTestWebXrDisabled()156 WebXrVrOpenXrBrowserTestWebXrDisabled::WebXrVrOpenXrBrowserTestWebXrDisabled() {
157   disable_features_.push_back(features::kWebXr);
158 }
159 #endif  // BUIDFLAG(ENABLE_OPENXR)
160 
161 #endif  // OS_WIN
162 
163 }  // namespace vr
164