Disable flaky test ExtensionActionContextMenuTest.RunInspectPopup
[chromium-blink-merge.git] / ui / gfx / blit_unittest.cc
blobb8d3ab693bce4ecef40f1487d2282d97e9e8dd28
1 // Copyright (c) 2010 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 "base/basictypes.h"
6 #include "base/shared_memory.h"
7 #include "skia/ext/platform_canvas.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/gfx/blit.h"
10 #include "ui/gfx/point.h"
11 #include "ui/gfx/rect.h"
13 namespace {
15 // Fills the given canvas with the values by duplicating the values into each
16 // color channel for the corresponding pixel.
18 // Example values = {{0x0, 0x01}, {0x12, 0xFF}} would give a canvas with:
19 // 0x00000000 0x01010101
20 // 0x12121212 0xFFFFFFFF
21 template<int w, int h>
22 void SetToCanvas(skia::PlatformCanvas* canvas, uint8 values[h][w]) {
23 SkBitmap& bitmap = const_cast<SkBitmap&>(
24 skia::GetTopDevice(*canvas)->accessBitmap(true));
25 SkAutoLockPixels lock(bitmap);
26 ASSERT_EQ(w, bitmap.width());
27 ASSERT_EQ(h, bitmap.height());
29 for (int y = 0; y < h; y++) {
30 for (int x = 0; x < w; x++) {
31 uint8 value = values[y][x];
32 *bitmap.getAddr32(x, y) =
33 (value << 24) | (value << 16) | (value << 8) | value;
38 // Checks each pixel in the given canvas and see if it is made up of the given
39 // values, where each value has been duplicated into each channel of the given
40 // bitmap (see SetToCanvas above).
41 template<int w, int h>
42 void VerifyCanvasValues(skia::PlatformCanvas* canvas, uint8 values[h][w]) {
43 SkBitmap& bitmap = const_cast<SkBitmap&>(
44 skia::GetTopDevice(*canvas)->accessBitmap(true));
45 SkAutoLockPixels lock(bitmap);
46 ASSERT_EQ(w, bitmap.width());
47 ASSERT_EQ(h, bitmap.height());
49 for (int y = 0; y < h; y++) {
50 for (int x = 0; x < w; x++) {
51 uint8 value = values[y][x];
52 uint32 expected =
53 (value << 24) | (value << 16) | (value << 8) | value;
54 ASSERT_EQ(expected, *bitmap.getAddr32(x, y));
59 } // namespace
61 TEST(Blit, ScrollCanvas) {
62 static const int kCanvasWidth = 5;
63 static const int kCanvasHeight = 5;
64 skia::ScopedPlatformCanvas canvas(kCanvasWidth, kCanvasHeight, true);
65 uint8 initial_values[kCanvasHeight][kCanvasWidth] = {
66 { 0x00, 0x01, 0x02, 0x03, 0x04 },
67 { 0x10, 0x11, 0x12, 0x13, 0x14 },
68 { 0x20, 0x21, 0x22, 0x23, 0x24 },
69 { 0x30, 0x31, 0x32, 0x33, 0x34 },
70 { 0x40, 0x41, 0x42, 0x43, 0x44 }};
71 SetToCanvas<5, 5>(canvas.get(), initial_values);
73 // Sanity check on input.
74 VerifyCanvasValues<5, 5>(canvas.get(), initial_values);
76 // Scroll none and make sure it's a NOP.
77 gfx::ScrollCanvas(canvas.get(),
78 gfx::Rect(0, 0, kCanvasWidth, kCanvasHeight),
79 gfx::Vector2d(0, 0));
80 VerifyCanvasValues<5, 5>(canvas.get(), initial_values);
82 // Scroll the center 3 pixels up one.
83 gfx::Rect center_three(1, 1, 3, 3);
84 gfx::ScrollCanvas(canvas.get(), center_three, gfx::Vector2d(0, -1));
85 uint8 scroll_up_expected[kCanvasHeight][kCanvasWidth] = {
86 { 0x00, 0x01, 0x02, 0x03, 0x04 },
87 { 0x10, 0x21, 0x22, 0x23, 0x14 },
88 { 0x20, 0x31, 0x32, 0x33, 0x24 },
89 { 0x30, 0x31, 0x32, 0x33, 0x34 },
90 { 0x40, 0x41, 0x42, 0x43, 0x44 }};
91 VerifyCanvasValues<5, 5>(canvas.get(), scroll_up_expected);
93 // Reset and scroll the center 3 pixels down one.
94 SetToCanvas<5, 5>(canvas.get(), initial_values);
95 gfx::ScrollCanvas(canvas.get(), center_three, gfx::Vector2d(0, 1));
96 uint8 scroll_down_expected[kCanvasHeight][kCanvasWidth] = {
97 { 0x00, 0x01, 0x02, 0x03, 0x04 },
98 { 0x10, 0x11, 0x12, 0x13, 0x14 },
99 { 0x20, 0x11, 0x12, 0x13, 0x24 },
100 { 0x30, 0x21, 0x22, 0x23, 0x34 },
101 { 0x40, 0x41, 0x42, 0x43, 0x44 }};
102 VerifyCanvasValues<5, 5>(canvas.get(), scroll_down_expected);
104 // Reset and scroll the center 3 pixels right one.
105 SetToCanvas<5, 5>(canvas.get(), initial_values);
106 gfx::ScrollCanvas(canvas.get(), center_three, gfx::Vector2d(1, 0));
107 uint8 scroll_right_expected[kCanvasHeight][kCanvasWidth] = {
108 { 0x00, 0x01, 0x02, 0x03, 0x04 },
109 { 0x10, 0x11, 0x11, 0x12, 0x14 },
110 { 0x20, 0x21, 0x21, 0x22, 0x24 },
111 { 0x30, 0x31, 0x31, 0x32, 0x34 },
112 { 0x40, 0x41, 0x42, 0x43, 0x44 }};
113 VerifyCanvasValues<5, 5>(canvas.get(), scroll_right_expected);
115 // Reset and scroll the center 3 pixels left one.
116 SetToCanvas<5, 5>(canvas.get(), initial_values);
117 gfx::ScrollCanvas(canvas.get(), center_three, gfx::Vector2d(-1, 0));
118 uint8 scroll_left_expected[kCanvasHeight][kCanvasWidth] = {
119 { 0x00, 0x01, 0x02, 0x03, 0x04 },
120 { 0x10, 0x12, 0x13, 0x13, 0x14 },
121 { 0x20, 0x22, 0x23, 0x23, 0x24 },
122 { 0x30, 0x32, 0x33, 0x33, 0x34 },
123 { 0x40, 0x41, 0x42, 0x43, 0x44 }};
124 VerifyCanvasValues<5, 5>(canvas.get(), scroll_left_expected);
126 // Diagonal scroll.
127 SetToCanvas<5, 5>(canvas.get(), initial_values);
128 gfx::ScrollCanvas(canvas.get(), center_three, gfx::Vector2d(2, 2));
129 uint8 scroll_diagonal_expected[kCanvasHeight][kCanvasWidth] = {
130 { 0x00, 0x01, 0x02, 0x03, 0x04 },
131 { 0x10, 0x11, 0x12, 0x13, 0x14 },
132 { 0x20, 0x21, 0x22, 0x23, 0x24 },
133 { 0x30, 0x31, 0x32, 0x11, 0x34 },
134 { 0x40, 0x41, 0x42, 0x43, 0x44 }};
135 VerifyCanvasValues<5, 5>(canvas.get(), scroll_diagonal_expected);
138 #if defined(OS_WIN)
140 TEST(Blit, WithSharedMemory) {
141 const int kCanvasWidth = 5;
142 const int kCanvasHeight = 5;
143 base::SharedMemory shared_mem;
144 ASSERT_TRUE(shared_mem.CreateAnonymous(kCanvasWidth * kCanvasHeight));
145 base::SharedMemoryHandle section = shared_mem.handle();
146 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(
147 skia::CreatePlatformCanvas(kCanvasWidth, kCanvasHeight, true, section,
148 skia::RETURN_NULL_ON_FAILURE));
149 ASSERT_TRUE(canvas);
150 shared_mem.Close();
152 uint8 initial_values[kCanvasHeight][kCanvasWidth] = {
153 { 0x00, 0x01, 0x02, 0x03, 0x04 },
154 { 0x10, 0x11, 0x12, 0x13, 0x14 },
155 { 0x20, 0x21, 0x22, 0x23, 0x24 },
156 { 0x30, 0x31, 0x32, 0x33, 0x34 },
157 { 0x40, 0x41, 0x42, 0x43, 0x44 }};
158 SetToCanvas<5, 5>(canvas.get(), initial_values);
160 // Sanity check on input.
161 VerifyCanvasValues<5, 5>(canvas.get(), initial_values);
164 #endif