| OLD | NEW |
| 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 #import "chrome/browser/cocoa/cocoa_utils.h" | 5 #import "chrome/browser/cocoa/cocoa_utils.h" |
| 6 #import "chrome/browser/cocoa/cocoa_test_helper.h" | 6 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 8 |
| 9 namespace { | 9 namespace { |
| 10 | 10 |
| 11 class CocoaUtilsTest : public testing::Test { | 11 class CocoaUtilsTest : public testing::Test { |
| 12 public: | 12 public: |
| 13 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... | 13 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... |
| 14 | 14 |
| 15 // If not red, is blue. | 15 // If not red, is blue. |
| 16 // If not tfbit (twenty-four-bit), is 444. | 16 // If not tfbit (twenty-four-bit), is 444. |
| 17 // If swap, swap R and B before testing (see TODOs in cocoa_utils.mm) | 17 void ShapeHelper(int width, int height, bool isred, bool tfbit); |
| 18 void ShapeHelper(int width, int height, bool isred, bool tfbit, bool swap); | |
| 19 }; | 18 }; |
| 20 | 19 |
| 21 void CocoaUtilsTest::ShapeHelper(int width, int height, | 20 void CocoaUtilsTest::ShapeHelper(int width, int height, |
| 22 bool isred, bool tfbit, bool swap) { | 21 bool isred, bool tfbit) { |
| 23 SkBitmap thing; | 22 SkBitmap thing; |
| 24 | 23 |
| 25 if (tfbit) | 24 if (tfbit) |
| 26 thing.setConfig(SkBitmap::kARGB_8888_Config, width, height); | 25 thing.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 27 else | 26 else |
| 28 thing.setConfig(SkBitmap::kARGB_4444_Config, width, height); | 27 thing.setConfig(SkBitmap::kARGB_4444_Config, width, height); |
| 29 thing.allocPixels(); | 28 thing.allocPixels(); |
| 30 | 29 |
| 31 if (isred) | 30 if (isred) |
| 32 thing.eraseRGB(0xff, 0, 0); | 31 thing.eraseRGB(0xff, 0, 0); |
| 33 else | 32 else |
| 34 thing.eraseRGB(0, 0, 0xff); | 33 thing.eraseRGB(0, 0, 0xff); |
| 35 | 34 |
| 36 // Confirm size | 35 // Confirm size |
| 37 NSImage* image = CocoaUtils::SkBitmapToNSImage(thing); | 36 NSImage* image = CocoaUtils::SkBitmapToNSImage(thing); |
| 38 EXPECT_DOUBLE_EQ([image size].width, (double)width); | 37 EXPECT_DOUBLE_EQ([image size].width, (double)width); |
| 39 EXPECT_DOUBLE_EQ([image size].height, (double)height); | 38 EXPECT_DOUBLE_EQ([image size].height, (double)height); |
| 40 | 39 |
| 41 // Get the color of a pixel and make sure it looks fine | 40 // Get the color of a pixel and make sure it looks fine |
| 42 [image lockFocus]; | 41 [image lockFocus]; |
| 43 | 42 |
| 44 int x = width > 17 ? 17 : 0; | 43 int x = width > 17 ? 17 : 0; |
| 45 int y = height > 17 ? 17 : 0; | 44 int y = height > 17 ? 17 : 0; |
| 46 NSColor* color = NSReadPixel(NSMakePoint(x, y)); | 45 NSColor* color = NSReadPixel(NSMakePoint(x, y)); |
| 47 CGFloat red = 0, green = 0, blue = 0, alpha = 0; | 46 CGFloat red = 0, green = 0, blue = 0, alpha = 0; |
| 48 [image unlockFocus]; | 47 [image unlockFocus]; |
| 49 [color getRed:&red green:&green blue:&blue alpha:&alpha]; | 48 [color getRed:&red green:&green blue:&blue alpha:&alpha]; |
| 50 | 49 |
| 51 if (swap) { | |
| 52 CGFloat tmp = red; | |
| 53 red = blue; | |
| 54 blue = tmp; | |
| 55 } | |
| 56 | |
| 57 // Be tolerant of floating point rounding, gamma, etc. | 50 // Be tolerant of floating point rounding, gamma, etc. |
| 58 if (isred) { | 51 if (isred) { |
| 59 EXPECT_GT(red, 0.8); | 52 EXPECT_GT(red, 0.8); |
| 60 EXPECT_LT(blue, 0.2); | 53 EXPECT_LT(blue, 0.2); |
| 61 } else { | 54 } else { |
| 62 EXPECT_LT(red, 0.2); | 55 EXPECT_LT(red, 0.2); |
| 63 EXPECT_GT(blue, 0.8); | 56 EXPECT_GT(blue, 0.8); |
| 64 } | 57 } |
| 65 EXPECT_LT(green, 0.2); | 58 EXPECT_LT(green, 0.2); |
| 66 EXPECT_GT(alpha, 0.9); | 59 EXPECT_GT(alpha, 0.9); |
| 67 } | 60 } |
| 68 | 61 |
| 69 | 62 |
| 70 TEST_F(CocoaUtilsTest, BitmapToNSImage_RedSquare64x64) { | 63 TEST_F(CocoaUtilsTest, BitmapToNSImage_RedSquare64x64) { |
| 71 ShapeHelper(64, 64, true, true, true); | 64 ShapeHelper(64, 64, true, true); |
| 72 } | 65 } |
| 73 | 66 |
| 74 TEST_F(CocoaUtilsTest, BitmapToNSImage_BlueRectangle199x19) { | 67 TEST_F(CocoaUtilsTest, BitmapToNSImage_BlueRectangle199x19) { |
| 75 ShapeHelper(199, 19, false, true, true); | 68 ShapeHelper(199, 19, false, true); |
| 76 } | 69 } |
| 77 | 70 |
| 78 TEST_F(CocoaUtilsTest, BitmapToNSImage_BlueRectangle444) { | 71 TEST_F(CocoaUtilsTest, BitmapToNSImage_BlueRectangle444) { |
| 79 ShapeHelper(200, 200, false, false, false); | 72 ShapeHelper(200, 200, false, false); |
| 80 } | 73 } |
| 81 | 74 |
| 82 | 75 |
| 83 } // namespace | 76 } // namespace |
| OLD | NEW |