1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/vr/test/webxr_vr_browser_test.h"
#include <cstring>
#include "build/build_config.h"
#include "components/permissions/test/mock_permission_prompt_factory.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#if BUILDFLAG(ENABLE_VR)
#include "device/vr/public/cpp/features.h"
#endif
#if BUILDFLAG(ENABLE_OPENXR)
#include "content/public/common/content_switches.h"
#endif
using testing::_;
using testing::Invoke;
namespace vr {
WebXrVrBrowserTestBase::WebXrVrBrowserTestBase() {
enable_features_.push_back(features::kWebXr);
}
WebXrVrBrowserTestBase::~WebXrVrBrowserTestBase() = default;
void WebXrVrBrowserTestBase::EnterSessionWithUserGesture(
content::WebContents* web_contents) {
// ExecJs runs with a user gesture, so we can just directly call
// requestSession instead of having to do the hacky workaround the
// instrumentation tests use of actually sending a click event to the canvas.
RunJavaScriptOrFail("onRequestSession()", web_contents);
}
void WebXrVrBrowserTestBase::EnterSessionWithUserGestureOrFail(
content::WebContents* web_contents) {
EnterSessionWithUserGesture(web_contents);
PollJavaScriptBooleanOrFail(
"sessionInfos[sessionTypes.IMMERSIVE].currentSession != null",
kPollTimeoutLong, web_contents);
#if BUILDFLAG(IS_WIN)
// Creating a session may take foreground from us, and Windows may not return
// it when the session terminates. This means subsequent requests to enter an
// immersive session may fail. The fix for testing is to call
// SetForegroundWindow manually. In real code, we'll have foreground if there
// was a user gesture to enter VR.
SetForegroundWindow(hwnd_);
#endif
}
void WebXrVrBrowserTestBase::EndSession(content::WebContents* web_contents) {
RunJavaScriptOrFail(
"sessionInfos[sessionTypes.IMMERSIVE].currentSession.end()",
web_contents);
}
void WebXrVrBrowserTestBase::EndSessionOrFail(
content::WebContents* web_contents) {
EndSession(web_contents);
WaitForSessionEndOrFail(web_contents);
}
void WebXrVrBrowserTestBase::WaitForSessionEndOrFail(
content::WebContents* web_contents) {
PollJavaScriptBooleanOrFail(
"sessionInfos[sessionTypes.IMMERSIVE].currentSession == null",
kPollTimeoutLong, web_contents);
}
gfx::Vector3dF WebXrVrBrowserTestBase::GetControllerOffset() const {
return gfx::Vector3dF();
}
void WebXrVrBrowserTestBase::SetPermissionAutoResponse(
permissions::PermissionRequestManager::AutoResponseType
permission_auto_response) {
permission_auto_response_ = permission_auto_response;
for (auto& it : mock_permissions_map_) {
it.second->set_response_type(permission_auto_response_);
}
}
permissions::MockPermissionPromptFactory*
WebXrVrBrowserTestBase::GetPermissionPromptFactory() {
auto* web_contents = GetCurrentWebContents();
CHECK(web_contents);
if (!mock_permissions_map_.contains(web_contents)) {
return nullptr;
}
return mock_permissions_map_[web_contents].get();
}
void WebXrVrBrowserTestBase::OnBeforeLoadFile() {
auto* web_contents = GetCurrentWebContents();
CHECK(web_contents);
if (!mock_permissions_map_.contains(web_contents)) {
mock_permissions_map_.insert_or_assign(
web_contents,
std::make_unique<permissions::MockPermissionPromptFactory>(
permissions::PermissionRequestManager::FromWebContents(
GetCurrentWebContents())));
// Set the requested auto-response so that any session is appropriately
// granted or rejected (or the request ignored).
mock_permissions_map_[web_contents]->set_response_type(
permission_auto_response_);
}
}
WebXrVrRuntimelessBrowserTest::WebXrVrRuntimelessBrowserTest() {
#if BUILDFLAG(ENABLE_OPENXR)
disable_features_.push_back(device::features::kOpenXR);
#endif
}
WebXrVrRuntimelessBrowserTestSensorless::
WebXrVrRuntimelessBrowserTestSensorless() {
// WebXrOrientationSensorDevice is only defined when the enable_vr flag is
// set.
#if BUILDFLAG(ENABLE_VR)
disable_features_.push_back(device::features::kWebXrOrientationSensorDevice);
#endif // BUILDFLAG(ENABLE_VR)
}
#if BUILDFLAG(ENABLE_OPENXR)
WebXrVrOpenXrBrowserTestBase::WebXrVrOpenXrBrowserTestBase() {
enable_features_.push_back(device::features::kOpenXR);
enable_features_.push_back(device::features::kWebXrHandInput);
}
WebXrVrOpenXrBrowserTestBase::~WebXrVrOpenXrBrowserTestBase() = default;
XrBrowserTestBase::RuntimeType WebXrVrOpenXrBrowserTestBase::GetRuntimeType()
const {
return XrBrowserTestBase::RuntimeType::RUNTIME_OPENXR;
}
void WebXrVrOpenXrBrowserTestBase::SetUpCommandLine(
base::CommandLine* command_line) {
command_line->AppendSwitchASCII(switches::kWebXrForceRuntime,
switches::kWebXrRuntimeOpenXr);
}
WebXrVrOpenXrBrowserTest::WebXrVrOpenXrBrowserTest() {
#if BUILDFLAG(IS_WIN)
runtime_requirements_.push_back(XrTestRequirement::DIRECTX_11_1);
#endif
}
WebXrVrOpenXrBrowserTestWebXrDisabled::WebXrVrOpenXrBrowserTestWebXrDisabled() {
disable_features_.push_back(features::kWebXr);
}
#endif // BUIDFLAG(ENABLE_OPENXR)
} // namespace vr
|