Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Unified Diff: ppapi/tests/blink_test_plugin.cc

Issue 1480303002: Implement a basic PPAPI plugin for Blink layout tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Change tests to use startAfterLoadAndFinish helper when possible. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ppapi/ppapi_tests.gypi ('k') | third_party/WebKit/LayoutTests/TestExpectations » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ppapi/tests/blink_test_plugin.cc
diff --git a/ppapi/tests/blink_test_plugin.cc b/ppapi/tests/blink_test_plugin.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6a21afb6e5028eb456efe816e761a060d43acad6
--- /dev/null
+++ b/ppapi/tests/blink_test_plugin.cc
@@ -0,0 +1,156 @@
+// Copyright 2015 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.
+
+#include <sstream>
+#include <utility>
+
+#include "ppapi/cpp/completion_callback.h"
+#include "ppapi/cpp/graphics_2d.h"
+#include "ppapi/cpp/image_data.h"
+#include "ppapi/cpp/input_event.h"
+#include "ppapi/cpp/instance.h"
+#include "ppapi/cpp/module.h"
+
+static void DummyCompletionCallback(void*, int32_t) {}
esprehn 2015/12/02 03:46:53 should this code be in some kind of namespace? I d
dcheng 2015/12/02 22:35:32 Done. It doesn't matter here, but it's probably be
+
+// This is a simple C++ Pepper plugin for Blink layout tests.
+class BlinkTestInstance : public pp::Instance {
+ public:
+ explicit BlinkTestInstance(PP_Instance instance)
+ : pp::Instance(instance), first_paint_(true) {}
+ ~BlinkTestInstance() override {}
+
+ bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
+ return RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_MOUSE |
+ PP_INPUTEVENT_CLASS_KEYBOARD) == PP_OK;
+ }
+
+ void DidChangeView(const pp::View& view) override {
+ view_ = view;
+ device_context_ = pp::Graphics2D(this, view_.GetRect().size(), true);
+ if (!BindGraphics(device_context_))
+ return;
+
+ // Since we draw a static image, we only need to make a new frame when
+ // the device is initialized or the view size changes.
+ Paint();
+ }
+
+ void DidChangeFocus(bool has_focus) override {
+ LogMessage("DidChangeFocus(", has_focus, ")");
+ }
+
+ bool HandleInputEvent(const pp::InputEvent& event) override {
+ switch (event.GetType()) {
+ case PP_INPUTEVENT_TYPE_MOUSEDOWN:
+ LogMouseEvent("Down", event);
+ break;
+ case PP_INPUTEVENT_TYPE_MOUSEUP:
+ LogMouseEvent("Up", event);
+ break;
+ case PP_INPUTEVENT_TYPE_KEYDOWN:
+ LogKeyboardEvent("Down", event);
+ break;
+ case PP_INPUTEVENT_TYPE_KEYUP:
+ LogKeyboardEvent("Up", event);
+ break;
+ case PP_INPUTEVENT_TYPE_MOUSEMOVE:
+ case PP_INPUTEVENT_TYPE_MOUSEENTER:
+ case PP_INPUTEVENT_TYPE_MOUSELEAVE:
+ case PP_INPUTEVENT_TYPE_RAWKEYDOWN:
+ case PP_INPUTEVENT_TYPE_CHAR:
+ // Just swallow these events without any logging.
+ return true;
+ default:
+ LogMessage("Unexpected input event with type = ", event.GetType());
+ return false;
+ }
+ return true;
+ }
+
+ private:
+ void Paint() {
+ pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL,
+ view_.GetRect().size(), true);
+ if (image.is_null())
+ return;
+
+ // Draw blue and green checkerboard pattern to show "interesting" keyframe.
+ const int kSquareSizePixels = 8;
+ for (int y = 0; y < view_.GetRect().size().height(); ++y) {
+ for (int x = 0; x < view_.GetRect().size().width(); ++x) {
+ int x_square = x / kSquareSizePixels;
+ int y_square = y / kSquareSizePixels;
+ uint32_t color = ((x_square + y_square) % 2) ? 0xFF0000FF : 0xFF00FF00;
+ *image.GetAddr32(pp::Point(x, y)) = color;
+ }
+ }
+
+ device_context_.ReplaceContents(&image);
+ device_context_.Flush(
+ pp::CompletionCallback(&DummyCompletionCallback, nullptr));
+
+ if (first_paint_) {
+ first_paint_ = false;
+ PostMessage(pp::Var("loaded"));
+ } else {
+ PostMessage(pp::Var("painted"));
+ }
+ }
+
+ void LogMouseEvent(const std::string& type, const pp::InputEvent& event) {
+ pp::MouseInputEvent mouse_event(event);
+ pp::Point mouse_position = mouse_event.GetPosition();
+ LogMessage("Mouse", type, " at (", mouse_position.x(), ",",
+ mouse_position.y(), ")");
+ }
+
+ void LogKeyboardEvent(const std::string& type, const pp::InputEvent& event) {
+ pp::KeyboardInputEvent keyboard_event(event);
+ LogMessage("Key", type, " '", keyboard_event.GetCode().AsString(), "'");
+ }
+
+ // Template magic to cover the lack of base::StringPrintf.
+ template <typename... Args>
+ void LogMessage(Args&&... args) {
+ std::ostringstream ss;
+ ss << std::boolalpha;
+ LogMessageHelper(&ss, std::forward<Args>(args)...);
+ }
+
+ template <typename Arg, typename... Args>
+ void LogMessageHelper(std::ostringstream* os, Arg&& arg, Args&&... args) {
+ *os << arg;
+ LogMessageHelper(os, std::forward<Args>(args)...);
+ }
+
+ template <typename Arg>
+ void LogMessageHelper(std::ostringstream* os, Arg&& arg) {
+ *os << arg;
+ LogToConsoleWithSource(PP_LOGLEVEL_LOG, pp::Var("Blink Test Plugin"),
+ pp::Var(os->str()));
+ }
+
+ bool first_paint_;
+ pp::View view_;
+ pp::Graphics2D device_context_;
+};
+
+class BlinkTestModule : public pp::Module {
+ public:
+ BlinkTestModule() {}
+ virtual ~BlinkTestModule() {}
+
+ virtual pp::Instance* CreateInstance(PP_Instance instance) {
+ return new BlinkTestInstance(instance);
+ }
+};
+
+namespace pp {
+
+Module* CreateModule() {
+ return new BlinkTestModule();
+}
+
+} // namespace pp
« no previous file with comments | « ppapi/ppapi_tests.gypi ('k') | third_party/WebKit/LayoutTests/TestExpectations » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698