1 // Copyright (c) 2012 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.
5 #include "content/renderer/pepper/pepper_graphics_2d_host.h"
7 #include "base/basictypes.h"
8 #include "base/message_loop/message_loop.h"
9 #include "content/renderer/pepper/gfx_conversion.h"
10 #include "content/renderer/pepper/mock_renderer_ppapi_host.h"
11 #include "content/renderer/pepper/ppb_image_data_impl.h"
12 #include "ppapi/shared_impl/ppb_view_shared.h"
13 #include "ppapi/shared_impl/proxy_lock.h"
14 #include "ppapi/shared_impl/test_globals.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/WebKit/public/platform/WebCanvas.h"
17 #include "third_party/skia/include/core/SkCanvas.h"
18 #include "ui/gfx/point.h"
19 #include "ui/gfx/rect.h"
21 using blink::WebCanvas;
25 class PepperGraphics2DHostTest : public testing::Test {
27 static bool ConvertToLogicalPixels(float scale,
30 return PepperGraphics2DHost::ConvertToLogicalPixels(scale, op_rect, delta);
33 PepperGraphics2DHostTest() : renderer_ppapi_host_(NULL, 12345) {}
35 virtual ~PepperGraphics2DHostTest() {
36 ppapi::ProxyAutoLock proxy_lock;
40 void Init(PP_Instance instance,
41 const PP_Size& backing_store_size,
42 const PP_Rect& plugin_rect) {
43 renderer_view_data_.rect = plugin_rect;
44 test_globals_.GetResourceTracker()->DidCreateInstance(instance);
45 scoped_refptr<PPB_ImageData_Impl> backing_store(
46 new PPB_ImageData_Impl(instance, PPB_ImageData_Impl::ForTest()));
47 host_.reset(PepperGraphics2DHost::Create(&renderer_ppapi_host_,
56 void PaintImageData(PPB_ImageData_Impl* image_data) {
57 ppapi::HostResource image_data_resource;
58 image_data_resource.SetHostResource(image_data->pp_instance(),
59 image_data->pp_resource());
60 host_->OnHostMsgPaintImageData(
61 NULL, image_data_resource, PP_Point(), false, PP_Rect());
65 ppapi::host::HostMessageContext context(
66 ppapi::proxy::ResourceMessageCallParams(host_->pp_resource(), 0));
67 host_->OnHostMsgFlush(&context);
68 host_->ViewFlushedPaint();
69 host_->SendOffscreenFlushAck();
72 void PaintToWebCanvas(SkBitmap* bitmap) {
73 scoped_ptr<WebCanvas> canvas(new WebCanvas(*bitmap));
74 gfx::Rect plugin_rect(PP_ToGfxRect(renderer_view_data_.rect));
75 host_->Paint(canvas.get(),
77 gfx::Rect(0, 0, plugin_rect.width(), plugin_rect.height()));
80 void ResetPageBitmap(SkBitmap* bitmap) {
81 PP_Rect plugin_rect = renderer_view_data_.rect;
82 int width = plugin_rect.point.x + plugin_rect.size.width;
83 int height = plugin_rect.point.y + plugin_rect.size.height;
84 if (bitmap->isNull() || bitmap->width() != width ||
85 bitmap->height() != height) {
86 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
87 bitmap->allocPixels();
89 bitmap->eraseColor(0);
93 ppapi::ViewData renderer_view_data_;
94 scoped_ptr<PepperGraphics2DHost> host_;
95 base::MessageLoop message_loop_;
96 MockRendererPpapiHost renderer_ppapi_host_;
97 ppapi::TestGlobals test_globals_;
100 TEST_F(PepperGraphics2DHostTest, ConvertToLogicalPixels) {
101 static const struct {
116 } tests[] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.0, true},
117 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.0, true},
118 {0, 0, 4, 4, 0, 0, 2, 2, 0, 0, 0, 0, 0.5, true},
119 {1, 1, 4, 4, 0, 0, 3, 3, 0, 0, 0, 0, 0.5, false},
120 {53, 75, 100, 100, 53, 75, 100, 100, 0, 0, 0, 0, 1.0, true},
121 {53, 75, 100, 100, 106, 150, 200, 200, 0, 0, 0, 0, 2.0, true},
122 {53, 75, 100, 100, 26, 37, 51, 51, 0, 0, 0, 0, 0.5, false},
123 {53, 74, 100, 100, 26, 37, 51, 50, 0, 0, 0, 0, 0.5, false},
124 {-1, -1, 100, 100, -1, -1, 51, 51, 0, 0, 0, 0, 0.5, false},
125 {-2, -2, 100, 100, -1, -1, 50, 50, 4, -4, 2, -2, 0.5, true},
126 {-101, -100, 50, 50, -51, -50, 26, 25, 0, 0, 0, 0, 0.5, false},
127 {10, 10, 20, 20, 5, 5, 10, 10, 0, 0, 0, 0, 0.5, true},
128 // Cannot scroll due to partial coverage on sides
129 {11, 10, 20, 20, 5, 5, 11, 10, 0, 0, 0, 0, 0.5, false},
130 // Can scroll since backing store is actually smaller/scaling up
131 {11, 20, 100, 100, 22, 40, 200, 200, 7, 3, 14, 6, 2.0, true},
132 // Can scroll due to delta and bounds being aligned
133 {10, 10, 20, 20, 5, 5, 10, 10, 6, 4, 3, 2, 0.5, true},
134 // Cannot scroll due to dx
135 {10, 10, 20, 20, 5, 5, 10, 10, 5, 4, 2, 2, 0.5, false},
136 // Cannot scroll due to dy
137 {10, 10, 20, 20, 5, 5, 10, 10, 6, 3, 3, 1, 0.5, false},
138 // Cannot scroll due to top
139 {10, 11, 20, 20, 5, 5, 10, 11, 6, 4, 3, 2, 0.5, false},
140 // Cannot scroll due to left
141 {7, 10, 20, 20, 3, 5, 11, 10, 6, 4, 3, 2, 0.5, false},
142 // Cannot scroll due to width
143 {10, 10, 21, 20, 5, 5, 11, 10, 6, 4, 3, 2, 0.5, false},
144 // Cannot scroll due to height
145 {10, 10, 20, 51, 5, 5, 10, 26, 6, 4, 3, 2, 0.5, false},
146 // Check negative scroll deltas
147 {10, 10, 20, 20, 5, 5, 10, 10, -6, -4, -3, -2, 0.5, true},
148 {10, 10, 20, 20, 5, 5, 10, 10, -6, -3, -3, -1, 0.5, false}, };
149 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
150 gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
151 gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
153 gfx::Point delta(tests[i].dx1, tests[i].dy1);
154 bool res = ConvertToLogicalPixels(tests[i].scale, &r1, &delta);
155 EXPECT_EQ(r2.ToString(), r1.ToString());
156 EXPECT_EQ(res, tests[i].result);
158 EXPECT_EQ(delta, gfx::Point(tests[i].dx2, tests[i].dy2));
160 // Reverse the scale and ensure all the original pixels are still inside
162 ConvertToLogicalPixels(1.0f / tests[i].scale, &r1, NULL);
163 EXPECT_TRUE(r1.Contains(orig));
167 } // namespace content