Commit 078780b9 authored by b.kelemen@samsung.com's avatar b.kelemen@samsung.com

Gamepad: make page visibility behavior layout testable

This CL refactors testing infrastructure for gamepad so that we can
actually write a layout test that would fail if Blink doesn't honor
visibility state. Now Platform::setGamepadListener is now hooked to
GamepadController.

BUG=344556

Review URL: https://codereview.chromium.org/304403002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278723 0039d316-1c4b-4281-b951-d872f2087c98
parent 332d17d2
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_GAMEPAD_RENDERER_PROVIDER_H_
#define CONTENT_GAMEPAD_RENDERER_PROVIDER_H_
namespace blink {
class WebGamepadListener;
class WebGamepads;
}
namespace content {
// Provides gamepad data and events for blink.
class RendererGamepadProvider {
public:
// Provides latest snapshot of gamepads.
virtual void SampleGamepads(blink::WebGamepads& gamepads) = 0;
// Registers listener for be notified of events.
virtual void SetGamepadListener(blink::WebGamepadListener* listener) = 0;
protected:
virtual ~RendererGamepadProvider() {}
};
} // namespace content
#endif
......@@ -26,6 +26,7 @@ namespace content {
class PageState;
class RenderFrame;
class RendererGamepadProvider;
class RenderView;
class WebTestProxyBase;
......@@ -44,15 +45,8 @@ void EnableRendererLayoutTestMode();
void EnableWebTestProxyCreation(
const base::Callback<void(RenderView*, WebTestProxyBase*)>& callback);
// Sets the WebGamepads that should be returned by
// WebKitPlatformSupport::sampleGamepads().
void SetMockGamepads(const blink::WebGamepads& pads);
// Notifies blink about a new gamepad.
void MockGamepadConnected(int index, const blink::WebGamepad& pad);
// Notifies blink that a gamepad has been disconnected.
void MockGamepadDisconnected(int index, const blink::WebGamepad& pad);
// Sets gamepad provider to be used for layout tests.
void SetMockGamepadProvider(RendererGamepadProvider* provider);
// Sets WebDeviceMotionData that should be used when registering
// a listener through WebKitPlatformSupport::setDeviceMotionListener().
......
......@@ -6,19 +6,22 @@
#include "base/debug/trace_event.h"
#include "base/metrics/histogram.h"
#include "content/common/gamepad_hardware_buffer.h"
#include "content/common/gamepad_user_gesture.h"
#include "content/public/renderer/render_thread.h"
#include "content/common/gamepad_hardware_buffer.h"
#include "content/renderer/renderer_webkitplatformsupport_impl.h"
#include "ipc/ipc_sync_message_filter.h"
#include "third_party/WebKit/public/platform/WebGamepadListener.h"
namespace content {
GamepadSharedMemoryReader::GamepadSharedMemoryReader()
GamepadSharedMemoryReader::GamepadSharedMemoryReader(
RendererWebKitPlatformSupportImpl* webkit_platform_support)
: gamepad_hardware_buffer_(NULL),
gamepad_listener_(NULL),
is_polling_(false),
ever_interacted_with_(false) {
webkit_platform_support->set_gamepad_provider(this);
}
void GamepadSharedMemoryReader::StartPollingIfNecessary() {
......
......@@ -9,21 +9,27 @@
#include "base/memory/shared_memory.h"
#include "content/common/gamepad_messages.h"
#include "content/public/renderer/render_process_observer.h"
#include "content/public/renderer/renderer_gamepad_provider.h"
#include "third_party/WebKit/public/platform/WebGamepads.h"
namespace blink { class WebGamepadListener; }
namespace content {
struct GamepadHardwareBuffer;
class RendererWebKitPlatformSupportImpl;
class GamepadSharedMemoryReader : public RenderProcessObserver {
class GamepadSharedMemoryReader
: public RenderProcessObserver,
public RendererGamepadProvider {
public:
GamepadSharedMemoryReader();
GamepadSharedMemoryReader(
RendererWebKitPlatformSupportImpl* webkit_platform_support);
virtual ~GamepadSharedMemoryReader();
void SampleGamepads(blink::WebGamepads& gamepads);
void SetGamepadListener(blink::WebGamepadListener* listener);
// RendererGamepadProvider implementation.
virtual void SampleGamepads(
blink::WebGamepads& gamepads) OVERRIDE;
virtual void SetGamepadListener(
blink::WebGamepadListener* listener) OVERRIDE;
// RenderProcessObserver implementation.
virtual bool OnControlMessageReceived(const IPC::Message& message) OVERRIDE;
......
......@@ -405,9 +405,6 @@ void RenderThreadImpl::Init() {
AddFilter((new EmbeddedWorkerContextMessageFilter())->GetFilter());
gamepad_shared_memory_reader_.reset(new GamepadSharedMemoryReader());
AddObserver(gamepad_shared_memory_reader_.get());
GetContentClient()->renderer()->RenderThreadStarted();
InitSkiaEventTracer();
......@@ -792,6 +789,10 @@ void RenderThreadImpl::EnsureWebKitInitialized() {
CompositorOutputSurface::CreateFilter(output_surface_loop.get());
AddFilter(compositor_output_surface_filter_.get());
gamepad_shared_memory_reader_.reset(
new GamepadSharedMemoryReader(webkit_platform_support_.get()));
AddObserver(gamepad_shared_memory_reader_.get());
RenderThreadImpl::RegisterSchemes();
EnableBlinkPlatformLogChannels(
......
......@@ -192,6 +192,11 @@ class CONTENT_EXPORT RenderThreadImpl : public RenderThread,
layout_test_mode_ = layout_test_mode;
}
RendererWebKitPlatformSupportImpl* webkit_platform_support() const {
DCHECK(webkit_platform_support_);
return webkit_platform_support_.get();
}
IPC::ForwardingMessageFilter* compositor_output_surface_filter() const {
return compositor_output_surface_filter_.get();
}
......
......@@ -141,9 +141,6 @@ namespace content {
namespace {
static bool g_sandbox_enabled = true;
static blink::WebGamepadListener* web_gamepad_listener = NULL;
base::LazyInstance<WebGamepads>::Leaky g_test_gamepads =
LAZY_INSTANCE_INITIALIZER;
base::LazyInstance<blink::WebDeviceMotionData>::Leaky
g_test_device_motion_data = LAZY_INSTANCE_INITIALIZER;
base::LazyInstance<blink::WebDeviceOrientationData>::Leaky
......@@ -231,7 +228,8 @@ RendererWebKitPlatformSupportImpl::RendererWebKitPlatformSupportImpl()
sudden_termination_disables_(0),
plugin_refresh_allowed_(true),
child_thread_loop_(base::MessageLoopProxy::current()),
web_scrollbar_behavior_(new WebScrollbarBehaviorImpl) {
web_scrollbar_behavior_(new WebScrollbarBehaviorImpl),
gamepad_provider_(NULL) {
if (g_sandbox_enabled && sandboxEnabled()) {
sandbox_support_.reset(
new RendererWebKitPlatformSupportImpl::SandboxSupport);
......@@ -885,19 +883,14 @@ WebBlobRegistry* RendererWebKitPlatformSupportImpl::blobRegistry() {
//------------------------------------------------------------------------------
void RendererWebKitPlatformSupportImpl::sampleGamepads(WebGamepads& gamepads) {
if (g_test_gamepads == 0) {
RenderThreadImpl::current()->gamepad_shared_memory_reader()->
SampleGamepads(gamepads);
} else {
gamepads = g_test_gamepads.Get();
}
DCHECK(gamepad_provider_);
gamepad_provider_->SampleGamepads(gamepads);
}
void RendererWebKitPlatformSupportImpl::setGamepadListener(
blink::WebGamepadListener* listener) {
web_gamepad_listener = listener;
RenderThreadImpl::current()->gamepad_shared_memory_reader()->
SetGamepadListener(listener);
DCHECK(gamepad_provider_);
gamepad_provider_->SetGamepadListener(listener);
}
//------------------------------------------------------------------------------
......@@ -945,28 +938,6 @@ bool RendererWebKitPlatformSupportImpl::SetSandboxEnabledForTesting(
return was_enabled;
}
// static
void RendererWebKitPlatformSupportImpl::SetMockGamepadsForTesting(
const WebGamepads& pads) {
g_test_gamepads.Get() = pads;
}
// static
void RendererWebKitPlatformSupportImpl::MockGamepadConnected(
int index,
const WebGamepad& pad) {
if (web_gamepad_listener)
web_gamepad_listener->didConnectGamepad(index, pad);
}
// static
void RendererWebKitPlatformSupportImpl::MockGamepadDisconnected(
int index,
const WebGamepad& pad) {
if (web_gamepad_listener)
web_gamepad_listener->didDisconnectGamepad(index, pad);
}
//------------------------------------------------------------------------------
blink::WebSpeechSynthesizer*
......
......@@ -41,6 +41,7 @@ class DeviceOrientationEventPump;
class QuotaMessageFilter;
class RendererClipboardClient;
class RenderView;
class RendererGamepadProvider;
class ThreadSafeSender;
class WebClipboardImpl;
class WebDatabaseObserverImpl;
......@@ -148,6 +149,10 @@ class CONTENT_EXPORT RendererWebKitPlatformSupportImpl
virtual void setBatteryStatusListener(
blink::WebBatteryStatusListener* listener);
void set_gamepad_provider(RendererGamepadProvider* provider) {
gamepad_provider_ = provider;
}
// Disables the WebSandboxSupport implementation for testing.
// Tests that do not set up a full sandbox environment should call
// SetSandboxEnabledForTesting(false) _before_ creating any instances
......@@ -157,17 +162,6 @@ class CONTENT_EXPORT RendererWebKitPlatformSupportImpl
// Returns the previous |enable| value.
static bool SetSandboxEnabledForTesting(bool enable);
// Set WebGamepads to return when sampleGamepads() is invoked.
static void SetMockGamepadsForTesting(const blink::WebGamepads& pads);
// Notifies blink::WebGamepadListener about a new gamepad if a listener
// has been set via setGamepadListener.
static void MockGamepadConnected(int index, const blink::WebGamepad& pad);
// Notifies blink::WebGamepadListener that a gamepad has been disconnected if
// a listener has been set via setGamepadListener.
static void MockGamepadDisconnected(int index, const blink::WebGamepad& pad);
// Set WebDeviceMotionData to return when setDeviceMotionListener is invoked.
static void SetMockDeviceMotionDataForTesting(
const blink::WebDeviceMotionData& data);
......@@ -236,6 +230,8 @@ class CONTENT_EXPORT RendererWebKitPlatformSupportImpl
scoped_ptr<BatteryStatusDispatcher> battery_status_dispatcher_;
RendererGamepadProvider* gamepad_provider_;
DISALLOW_COPY_AND_ASSIGN(RendererWebKitPlatformSupportImpl);
};
......
......@@ -29,6 +29,7 @@ struct WebURLError;
namespace content {
class RendererGamepadProvider;
class WebTask;
class WebTestProxyBase;
struct TestPreferences;
......@@ -40,14 +41,8 @@ public:
virtual void clearEditCommand() = 0;
virtual void setEditCommand(const std::string& name, const std::string& value) = 0;
// Set the gamepads to return from Platform::sampleGamepads().
virtual void setGamepadData(const blink::WebGamepads&) = 0;
// Notifies blink about a new gamepad.
virtual void didConnectGamepad(int index, const blink::WebGamepad&) = 0;
// Notifies blink that a gamepad has been disconnected.
virtual void didDisconnectGamepad(int index, const blink::WebGamepad&) = 0;
// Sets gamepad provider to be used for tests.
virtual void setGamepadProvider(RendererGamepadProvider*) = 0;
// Set data to return when registering via Platform::setDeviceMotionListener().
virtual void setDeviceMotionData(const blink::WebDeviceMotionData&) = 0;
......
......@@ -10,6 +10,7 @@
#include "gin/handle.h"
#include "gin/object_template_builder.h"
#include "gin/wrappable.h"
#include "third_party/WebKit/public/platform/WebGamepadListener.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebKit.h"
#include "v8/include/v8.h"
......@@ -136,7 +137,9 @@ void GamepadControllerBindings::SetAxisData(int index, int axis, double data) {
controller_->SetAxisData(index, axis, data);
}
GamepadController::GamepadController() : delegate_(NULL), weak_factory_(this) {
GamepadController::GamepadController()
: listener_(NULL),
weak_factory_(this) {
Reset();
}
......@@ -151,7 +154,16 @@ void GamepadController::Install(WebFrame* frame) {
}
void GamepadController::SetDelegate(WebTestDelegate* delegate) {
delegate_ = delegate;
delegate->setGamepadProvider(this);
}
void GamepadController::SampleGamepads(blink::WebGamepads& gamepads) {
memcpy(&gamepads, &gamepads_, sizeof(blink::WebGamepads));
}
void GamepadController::SetGamepadListener(
blink::WebGamepadListener* listener) {
listener_ = listener;
}
void GamepadController::Connect(int index) {
......@@ -163,16 +175,15 @@ void GamepadController::Connect(int index) {
if (gamepads_.items[i].connected)
gamepads_.length = i + 1;
}
if (delegate_)
delegate_->setGamepadData(gamepads_);
}
void GamepadController::DispatchConnected(int index) {
if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap))
if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)
|| !gamepads_.items[index].connected)
return;
const WebGamepad& pad = gamepads_.items[index];
if (pad.connected && delegate_)
delegate_->didConnectGamepad(index, pad);
if (listener_)
listener_->didConnectGamepad(index, pad);
}
void GamepadController::Disconnect(int index) {
......@@ -185,10 +196,8 @@ void GamepadController::Disconnect(int index) {
if (gamepads_.items[i].connected)
gamepads_.length = i + 1;
}
if (delegate_) {
delegate_->setGamepadData(gamepads_);
delegate_->didDisconnectGamepad(index, pad);
}
if (listener_)
listener_->didDisconnectGamepad(index, pad);
}
void GamepadController::SetId(int index, const std::string& src) {
......@@ -198,8 +207,6 @@ void GamepadController::SetId(int index, const std::string& src) {
memset(gamepads_.items[index].id, 0, sizeof(gamepads_.items[index].id));
for (unsigned i = 0; *p && i < WebGamepad::idLengthCap - 1; ++i)
gamepads_.items[index].id[i] = *p++;
if (delegate_)
delegate_->setGamepadData(gamepads_);
}
void GamepadController::SetButtonCount(int index, int buttons) {
......@@ -208,8 +215,6 @@ void GamepadController::SetButtonCount(int index, int buttons) {
if (buttons < 0 || buttons >= static_cast<int>(WebGamepad::buttonsLengthCap))
return;
gamepads_.items[index].buttonsLength = buttons;
if (delegate_)
delegate_->setGamepadData(gamepads_);
}
void GamepadController::SetButtonData(int index, int button, double data) {
......@@ -219,8 +224,6 @@ void GamepadController::SetButtonData(int index, int button, double data) {
return;
gamepads_.items[index].buttons[button].value = data;
gamepads_.items[index].buttons[button].pressed = data > 0.1f;
if (delegate_)
delegate_->setGamepadData(gamepads_);
}
void GamepadController::SetAxisCount(int index, int axes) {
......@@ -229,8 +232,6 @@ void GamepadController::SetAxisCount(int index, int axes) {
if (axes < 0 || axes >= static_cast<int>(WebGamepad::axesLengthCap))
return;
gamepads_.items[index].axesLength = axes;
if (delegate_)
delegate_->setGamepadData(gamepads_);
}
void GamepadController::SetAxisData(int index, int axis, double data) {
......@@ -239,8 +240,6 @@ void GamepadController::SetAxisData(int index, int axis, double data) {
if (axis < 0 || axis >= static_cast<int>(WebGamepad::axesLengthCap))
return;
gamepads_.items[index].axes[axis] = data;
if (delegate_)
delegate_->setGamepadData(gamepads_);
}
} // namespace content
......@@ -5,26 +5,38 @@
#ifndef CONTENT_SHELL_RENDERER_TEST_RUNNER_GAMEPAD_CONTROLLER_H_
#define CONTENT_SHELL_RENDERER_TEST_RUNNER_GAMEPAD_CONTROLLER_H_
#include <map>
#include "base/memory/weak_ptr.h"
#include "content/public/renderer/renderer_gamepad_provider.h"
#include "third_party/WebKit/public/platform/WebGamepads.h"
namespace blink {
class WebFrame;
class WebGamepadListener;
}
namespace content {
class WebTestDelegate;
class GamepadController : public base::SupportsWeakPtr<GamepadController> {
class GamepadController
: public base::SupportsWeakPtr<GamepadController>,
public RendererGamepadProvider {
public:
GamepadController();
~GamepadController();
virtual ~GamepadController();
void Reset();
void Install(blink::WebFrame* frame);
void SetDelegate(WebTestDelegate* delegate);
// RendererGamepadProvider implementation.
virtual void SampleGamepads(
blink::WebGamepads& gamepads) OVERRIDE;
virtual void SetGamepadListener(
blink::WebGamepadListener* listener) OVERRIDE;
private:
friend class GamepadControllerBindings;
......@@ -46,7 +58,10 @@ class GamepadController : public base::SupportsWeakPtr<GamepadController> {
blink::WebGamepads gamepads_;
WebTestDelegate* delegate_;
blink::WebGamepadListener* listener_;
// Mapping from gamepad index to connection state.
std::map<int, bool> pending_changes_;
base::WeakPtrFactory<GamepadController> weak_factory_;
......
......@@ -201,20 +201,9 @@ void WebKitTestRunner::setEditCommand(const std::string& name,
render_view()->SetEditCommandForNextKeyEvent(name, value);
}
void WebKitTestRunner::setGamepadData(const WebGamepads& gamepads) {
SetMockGamepads(gamepads);
}
void WebKitTestRunner::didConnectGamepad(
int index,
const blink::WebGamepad& gamepad) {
MockGamepadConnected(index, gamepad);
}
void WebKitTestRunner::didDisconnectGamepad(
int index,
const blink::WebGamepad& gamepad) {
MockGamepadDisconnected(index, gamepad);
void WebKitTestRunner::setGamepadProvider(
RendererGamepadProvider* provider) {
SetMockGamepadProvider(provider);
}
void WebKitTestRunner::setDeviceMotionData(const WebDeviceMotionData& data) {
......
......@@ -55,11 +55,7 @@ class WebKitTestRunner : public RenderViewObserver,
virtual void clearEditCommand() OVERRIDE;
virtual void setEditCommand(const std::string& name,
const std::string& value) OVERRIDE;
virtual void setGamepadData(const blink::WebGamepads& gamepads) OVERRIDE;
virtual void didConnectGamepad(int index,
const blink::WebGamepad& gamepad) OVERRIDE;
virtual void didDisconnectGamepad(int index,
const blink::WebGamepad& gamepad) OVERRIDE;
virtual void setGamepadProvider(RendererGamepadProvider*) OVERRIDE;
virtual void setDeviceMotionData(
const blink::WebDeviceMotionData& data) OVERRIDE;
virtual void setDeviceOrientationData(
......
......@@ -81,16 +81,9 @@ void EnableWebTestProxyCreation(
RenderFrameImpl::InstallCreateHook(CreateWebFrameTestProxy);
}
void SetMockGamepads(const WebGamepads& pads) {
RendererWebKitPlatformSupportImpl::SetMockGamepadsForTesting(pads);
}
void MockGamepadConnected(int index, const WebGamepad& pad) {
RendererWebKitPlatformSupportImpl::MockGamepadConnected(index, pad);
}
void MockGamepadDisconnected(int index, const WebGamepad& pad) {
RendererWebKitPlatformSupportImpl::MockGamepadDisconnected(index, pad);
void SetMockGamepadProvider(RendererGamepadProvider* provider) {
RenderThreadImpl::current()->webkit_platform_support()->
set_gamepad_provider(provider);
}
void SetMockDeviceMotionData(const WebDeviceMotionData& data) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment