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

Side by Side Diff: skia/ext/skia_utils_mac_unittest.mm

Issue 7031006: Add utility for converting SkCanvas to CGContext (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years, 7 months 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « skia/ext/skia_utils_mac.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "skia/ext/skia_utils_mac.mm" 5 #include "skia/ext/skia_utils_mac.mm"
6 #include "testing/gtest/include/gtest/gtest.h" 6 #include "testing/gtest/include/gtest/gtest.h"
7 7
8 namespace { 8 namespace {
9 9
10 class SkiaUtilsMacTest : public testing::Test { 10 class SkiaUtilsMacTest : public testing::Test {
11 public: 11 public:
12 // Creates a red or blue bitmap. 12 // Creates a red or blue bitmap.
13 SkBitmap CreateSkBitmap(int width, int height, bool isred, bool tfbit); 13 SkBitmap CreateSkBitmap(int width, int height, bool isred, bool tfbit);
14 14
15 // Creates a red or blue image. 15 // Creates a red or blue image.
16 NSImage* CreateNSImage(int width, int height, bool isred); 16 NSImage* CreateNSImage(int width, int height, bool isred);
17 17
18 // Checks that the given bitmap rep is actually red or blue. 18 // Checks that the given bitmap rep is actually red or blue.
19 void TestImageRep(NSBitmapImageRep* imageRep, bool isred); 19 void TestImageRep(NSBitmapImageRep* imageRep, bool isred);
20 20
21 // Checks that the given bitmap is actually red or blue. 21 // Checks that the given bitmap is actually red or blue.
22 void TestSkBitmap(const SkBitmap& bitmap, bool isred); 22 void TestSkBitmap(const SkBitmap& bitmap, bool isred);
23 23
24 enum BitLockerTest {
25 TestIdentity = 0,
26 TestTranslate = 1,
27 TestClip = 2,
28 TestXClip = TestTranslate | TestClip
29 };
30 void RunBitLockerTest(BitLockerTest test);
31
24 // If not red, is blue. 32 // If not red, is blue.
25 // If not tfbit (twenty-four-bit), is 444. 33 // If not tfbit (twenty-four-bit), is 444.
26 void ShapeHelper(int width, int height, bool isred, bool tfbit); 34 void ShapeHelper(int width, int height, bool isred, bool tfbit);
27 }; 35 };
28 36
29 SkBitmap SkiaUtilsMacTest::CreateSkBitmap(int width, int height, 37 SkBitmap SkiaUtilsMacTest::CreateSkBitmap(int width, int height,
30 bool isred, bool tfbit) { 38 bool isred, bool tfbit) {
31 SkBitmap bitmap; 39 SkBitmap bitmap;
32 40
33 if (tfbit) 41 if (tfbit)
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 EXPECT_GT(SkColorGetR(color), 245u); 102 EXPECT_GT(SkColorGetR(color), 245u);
95 EXPECT_LT(SkColorGetB(color), 10u); 103 EXPECT_LT(SkColorGetB(color), 10u);
96 } else { 104 } else {
97 EXPECT_LT(SkColorGetR(color), 10u); 105 EXPECT_LT(SkColorGetR(color), 10u);
98 EXPECT_GT(SkColorGetB(color), 245u); 106 EXPECT_GT(SkColorGetB(color), 245u);
99 } 107 }
100 EXPECT_LT(SkColorGetG(color), 10u); 108 EXPECT_LT(SkColorGetG(color), 10u);
101 EXPECT_GT(SkColorGetA(color), 245u); 109 EXPECT_GT(SkColorGetA(color), 245u);
102 } 110 }
103 111
112 void SkiaUtilsMacTest::RunBitLockerTest(BitLockerTest test) {
113 const unsigned width = 2;
114 const unsigned height = 2;
115 const unsigned storageSize = width * height;
116 const unsigned original[] = {0xFF333333, 0xFF666666, 0xFF999999, 0xFFCCCCCC};
117 EXPECT_EQ(storageSize, sizeof(original) / sizeof(original[0]));
118 unsigned bits[storageSize];
119 memcpy(bits, original, sizeof(original));
120 SkBitmap bitmap;
121 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
122 bitmap.setPixels(bits);
123 SkCanvas canvas;
124 canvas.setBitmapDevice(bitmap);
125 if (test & TestTranslate)
126 canvas.translate(width / 2, 0);
127 if (test & TestClip) {
128 SkRect clipRect = {0, height / 2, width, height};
129 canvas.clipRect(clipRect);
130 }
131 gfx::SkiaBitLocker bitLocker(&canvas);
132 CGContextRef cgContext = bitLocker.cgContext();
133 CGColorRef testColor = CGColorGetConstantColor(kCGColorWhite);
134 CGContextSetFillColorWithColor(cgContext, testColor);
135 CGRect cgRect = {{0, 0}, {width, height}};
136 CGContextFillRect(cgContext, cgRect);
137 const unsigned results[][storageSize] = {
138 {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}, // identity
139 {0xFF333333, 0xFFFFFFFF, 0xFF999999, 0xFFFFFFFF}, // translate
140 {0xFF333333, 0xFF666666, 0xFFFFFFFF, 0xFFFFFFFF}, // clip
141 {0xFF333333, 0xFF666666, 0xFF999999, 0xFFFFFFFF} // translate | clip
142 };
143 for (unsigned index = 0; index < storageSize; index++)
144 EXPECT_EQ(results[test][index], bits[index]);
145 }
146
104 void SkiaUtilsMacTest::ShapeHelper(int width, int height, 147 void SkiaUtilsMacTest::ShapeHelper(int width, int height,
105 bool isred, bool tfbit) { 148 bool isred, bool tfbit) {
106 SkBitmap thing(CreateSkBitmap(width, height, isred, tfbit)); 149 SkBitmap thing(CreateSkBitmap(width, height, isred, tfbit));
107 150
108 // Confirm size 151 // Confirm size
109 NSImage* image = gfx::SkBitmapToNSImage(thing); 152 NSImage* image = gfx::SkBitmapToNSImage(thing);
110 EXPECT_DOUBLE_EQ([image size].width, (double)width); 153 EXPECT_DOUBLE_EQ([image size].width, (double)width);
111 EXPECT_DOUBLE_EQ([image size].height, (double)height); 154 EXPECT_DOUBLE_EQ([image size].height, (double)height);
112 155
113 EXPECT_TRUE([[image representations] count] == 1); 156 EXPECT_TRUE([[image representations] count] == 1);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 int height = 15; 208 int height = 15;
166 bool isred = true; 209 bool isred = true;
167 210
168 NSImage* image = CreateNSImage(width, height, isred); 211 NSImage* image = CreateNSImage(width, height, isred);
169 EXPECT_EQ(1u, [[image representations] count]); 212 EXPECT_EQ(1u, [[image representations] count]);
170 NSBitmapImageRep* imageRep = [[image representations] lastObject]; 213 NSBitmapImageRep* imageRep = [[image representations] lastObject];
171 SkBitmap bitmap(gfx::NSImageRepToSkBitmap(imageRep, [image size], false)); 214 SkBitmap bitmap(gfx::NSImageRepToSkBitmap(imageRep, [image size], false));
172 TestSkBitmap(bitmap, isred); 215 TestSkBitmap(bitmap, isred);
173 } 216 }
174 217
218 TEST_F(SkiaUtilsMacTest, BitLocker_Identity) {
219 RunBitLockerTest(SkiaUtilsMacTest::TestIdentity);
220 }
221
222 TEST_F(SkiaUtilsMacTest, BitLocker_Translate) {
223 RunBitLockerTest(SkiaUtilsMacTest::TestTranslate);
224 }
225
226 TEST_F(SkiaUtilsMacTest, BitLocker_Clip) {
227 RunBitLockerTest(SkiaUtilsMacTest::TestClip);
228 }
229
230 TEST_F(SkiaUtilsMacTest, BitLocker_XClip) {
231 RunBitLockerTest(SkiaUtilsMacTest::TestXClip);
232 }
233
175 } // namespace 234 } // namespace
235
OLDNEW
« no previous file with comments | « skia/ext/skia_utils_mac.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698