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