Refactor management of overview window copy lifetime into a separate class.
[chromium-blink-merge.git] / content / renderer / pepper / pepper_graphics_2d_host_unittest.cc
blob78a8fe01ae1dd0878a576dbee75e2318ca03d483
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 "testing/gtest/include/gtest/gtest.h"
9 #include "ui/gfx/point.h"
10 #include "ui/gfx/rect.h"
12 namespace content {
14 class PepperGraphics2DHostTest : public testing::Test {
15 public:
16 static bool ConvertToLogicalPixels(float scale,
17 gfx::Rect* op_rect,
18 gfx::Point* delta) {
19 return PepperGraphics2DHost::ConvertToLogicalPixels(scale, op_rect, delta);
23 TEST_F(PepperGraphics2DHostTest, ConvertToLogicalPixels) {
24 static const struct {
25 int x1;
26 int y1;
27 int w1;
28 int h1;
29 int x2;
30 int y2;
31 int w2;
32 int h2;
33 int dx1;
34 int dy1;
35 int dx2;
36 int dy2;
37 float scale;
38 bool result;
39 } tests[] = {
40 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.0, true },
41 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.0, true },
42 { 0, 0, 4, 4, 0, 0, 2, 2, 0, 0, 0, 0, 0.5, true },
43 { 1, 1, 4, 4, 0, 0, 3, 3, 0, 0, 0, 0, 0.5, false },
44 { 53, 75, 100, 100, 53, 75, 100, 100, 0, 0, 0, 0, 1.0, true },
45 { 53, 75, 100, 100, 106, 150, 200, 200, 0, 0, 0, 0, 2.0, true },
46 { 53, 75, 100, 100, 26, 37, 51, 51, 0, 0, 0, 0, 0.5, false },
47 { 53, 74, 100, 100, 26, 37, 51, 50, 0, 0, 0, 0, 0.5, false },
48 { -1, -1, 100, 100, -1, -1, 51, 51, 0, 0, 0, 0, 0.5, false },
49 { -2, -2, 100, 100, -1, -1, 50, 50, 4, -4, 2, -2, 0.5, true },
50 { -101, -100, 50, 50, -51, -50, 26, 25, 0, 0, 0, 0, 0.5, false },
51 { 10, 10, 20, 20, 5, 5, 10, 10, 0, 0, 0, 0, 0.5, true },
52 // Cannot scroll due to partial coverage on sides
53 { 11, 10, 20, 20, 5, 5, 11, 10, 0, 0, 0, 0, 0.5, false },
54 // Can scroll since backing store is actually smaller/scaling up
55 { 11, 20, 100, 100, 22, 40, 200, 200, 7, 3, 14, 6, 2.0, true },
56 // Can scroll due to delta and bounds being aligned
57 { 10, 10, 20, 20, 5, 5, 10, 10, 6, 4, 3, 2, 0.5, true },
58 // Cannot scroll due to dx
59 { 10, 10, 20, 20, 5, 5, 10, 10, 5, 4, 2, 2, 0.5, false },
60 // Cannot scroll due to dy
61 { 10, 10, 20, 20, 5, 5, 10, 10, 6, 3, 3, 1, 0.5, false },
62 // Cannot scroll due to top
63 { 10, 11, 20, 20, 5, 5, 10, 11, 6, 4, 3, 2, 0.5, false },
64 // Cannot scroll due to left
65 { 7, 10, 20, 20, 3, 5, 11, 10, 6, 4, 3, 2, 0.5, false },
66 // Cannot scroll due to width
67 { 10, 10, 21, 20, 5, 5, 11, 10, 6, 4, 3, 2, 0.5, false },
68 // Cannot scroll due to height
69 { 10, 10, 20, 51, 5, 5, 10, 26, 6, 4, 3, 2, 0.5, false },
70 // Check negative scroll deltas
71 { 10, 10, 20, 20, 5, 5, 10, 10, -6, -4, -3, -2, 0.5, true },
72 { 10, 10, 20, 20, 5, 5, 10, 10, -6, -3, -3, -1, 0.5, false },
74 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
75 gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
76 gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
77 gfx::Rect orig = r1;
78 gfx::Point delta(tests[i].dx1, tests[i].dy1);
79 bool res = ConvertToLogicalPixels(tests[i].scale, &r1, &delta);
80 EXPECT_EQ(r2.ToString(), r1.ToString());
81 EXPECT_EQ(res, tests[i].result);
82 if (res) {
83 EXPECT_EQ(delta, gfx::Point(tests[i].dx2, tests[i].dy2));
85 // Reverse the scale and ensure all the original pixels are still inside
86 // the result.
87 ConvertToLogicalPixels(1.0f / tests[i].scale, &r1, NULL);
88 EXPECT_TRUE(r1.Contains(orig));
92 } // namespace content