Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / pepper / pepper_graphics_2d_host_unittest.cc
blobd817bab41a98ac4352a0273b0b105ee08a676569
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/geometry/point.h"
19 #include "ui/gfx/geometry/rect.h"
21 using blink::WebCanvas;
23 namespace content {
25 class PepperGraphics2DHostTest : public testing::Test {
26 public:
27 static bool ConvertToLogicalPixels(float scale,
28 gfx::Rect* op_rect,
29 gfx::Point* delta) {
30 return PepperGraphics2DHost::ConvertToLogicalPixels(scale, op_rect, delta);
33 PepperGraphics2DHostTest() : renderer_ppapi_host_(NULL, 12345) {}
35 ~PepperGraphics2DHostTest() override {
36 ppapi::ProxyAutoLock proxy_lock;
37 host_.reset();
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_,
48 instance,
49 12345,
50 backing_store_size,
51 PP_FALSE,
52 backing_store));
53 DCHECK(host_.get());
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());
64 void Flush() {
65 ppapi::host::HostMessageContext context(
66 ppapi::proxy::ResourceMessageCallParams(host_->pp_resource(), 0));
67 host_->OnHostMsgFlush(&context);
68 host_->ViewInitiatedPaint();
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(),
76 plugin_rect,
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->allocN32Pixels(width, height);
88 bitmap->eraseColor(0);
91 private:
92 ppapi::ViewData renderer_view_data_;
93 scoped_ptr<PepperGraphics2DHost> host_;
94 base::MessageLoop message_loop_;
95 MockRendererPpapiHost renderer_ppapi_host_;
96 ppapi::TestGlobals test_globals_;
99 TEST_F(PepperGraphics2DHostTest, ConvertToLogicalPixels) {
100 static const struct {
101 int x1;
102 int y1;
103 int w1;
104 int h1;
105 int x2;
106 int y2;
107 int w2;
108 int h2;
109 int dx1;
110 int dy1;
111 int dx2;
112 int dy2;
113 float scale;
114 bool result;
115 } tests[] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.0, true},
116 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.0, true},
117 {0, 0, 4, 4, 0, 0, 2, 2, 0, 0, 0, 0, 0.5, true},
118 {1, 1, 4, 4, 0, 0, 3, 3, 0, 0, 0, 0, 0.5, false},
119 {53, 75, 100, 100, 53, 75, 100, 100, 0, 0, 0, 0, 1.0, true},
120 {53, 75, 100, 100, 106, 150, 200, 200, 0, 0, 0, 0, 2.0, true},
121 {53, 75, 100, 100, 26, 37, 51, 51, 0, 0, 0, 0, 0.5, false},
122 {53, 74, 100, 100, 26, 37, 51, 50, 0, 0, 0, 0, 0.5, false},
123 {-1, -1, 100, 100, -1, -1, 51, 51, 0, 0, 0, 0, 0.5, false},
124 {-2, -2, 100, 100, -1, -1, 50, 50, 4, -4, 2, -2, 0.5, true},
125 {-101, -100, 50, 50, -51, -50, 26, 25, 0, 0, 0, 0, 0.5, false},
126 {10, 10, 20, 20, 5, 5, 10, 10, 0, 0, 0, 0, 0.5, true},
127 // Cannot scroll due to partial coverage on sides
128 {11, 10, 20, 20, 5, 5, 11, 10, 0, 0, 0, 0, 0.5, false},
129 // Can scroll since backing store is actually smaller/scaling up
130 {11, 20, 100, 100, 22, 40, 200, 200, 7, 3, 14, 6, 2.0, true},
131 // Can scroll due to delta and bounds being aligned
132 {10, 10, 20, 20, 5, 5, 10, 10, 6, 4, 3, 2, 0.5, true},
133 // Cannot scroll due to dx
134 {10, 10, 20, 20, 5, 5, 10, 10, 5, 4, 2, 2, 0.5, false},
135 // Cannot scroll due to dy
136 {10, 10, 20, 20, 5, 5, 10, 10, 6, 3, 3, 1, 0.5, false},
137 // Cannot scroll due to top
138 {10, 11, 20, 20, 5, 5, 10, 11, 6, 4, 3, 2, 0.5, false},
139 // Cannot scroll due to left
140 {7, 10, 20, 20, 3, 5, 11, 10, 6, 4, 3, 2, 0.5, false},
141 // Cannot scroll due to width
142 {10, 10, 21, 20, 5, 5, 11, 10, 6, 4, 3, 2, 0.5, false},
143 // Cannot scroll due to height
144 {10, 10, 20, 51, 5, 5, 10, 26, 6, 4, 3, 2, 0.5, false},
145 // Check negative scroll deltas
146 {10, 10, 20, 20, 5, 5, 10, 10, -6, -4, -3, -2, 0.5, true},
147 {10, 10, 20, 20, 5, 5, 10, 10, -6, -3, -3, -1, 0.5, false}, };
148 for (size_t i = 0; i < arraysize(tests); ++i) {
149 gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
150 gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
151 gfx::Rect orig = r1;
152 gfx::Point delta(tests[i].dx1, tests[i].dy1);
153 bool res = ConvertToLogicalPixels(tests[i].scale, &r1, &delta);
154 EXPECT_EQ(r2.ToString(), r1.ToString());
155 EXPECT_EQ(res, tests[i].result);
156 if (res) {
157 EXPECT_EQ(delta, gfx::Point(tests[i].dx2, tests[i].dy2));
159 // Reverse the scale and ensure all the original pixels are still inside
160 // the result.
161 ConvertToLogicalPixels(1.0f / tests[i].scale, &r1, NULL);
162 EXPECT_TRUE(r1.Contains(orig));
166 } // namespace content