| 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 void ShapeHelper(int width, int height, bool isred, bool tfbit); | |
| 18 }; | |
| 19 | |
| 20 void CocoaUtilsTest::ShapeHelper(int width, int height, | |
| 21 bool isred, bool tfbit) { | |
| 22 SkBitmap thing; | |
| 23 | |
| 24 if (tfbit) | |
| 25 thing.setConfig(SkBitmap::kARGB_8888_Config, width, height); | |
| 26 else | |
| 27 thing.setConfig(SkBitmap::kARGB_4444_Config, width, height); | |
| 28 thing.allocPixels(); | |
| 29 | |
| 30 if (isred) | |
| 31 thing.eraseRGB(0xff, 0, 0); | |
| 32 else | |
| 33 thing.eraseRGB(0, 0, 0xff); | |
| 34 | |
| 35 // Confirm size | |
| 36 NSImage* image = CocoaUtils::SkBitmapToNSImage(thing); | |
| 37 EXPECT_DOUBLE_EQ([image size].width, (double)width); | |
| 38 EXPECT_DOUBLE_EQ([image size].height, (double)height); | |
| 39 | |
| 40 // Get the color of a pixel and make sure it looks fine | |
| 41 [image lockFocus]; | |
| 42 | |
| 43 int x = width > 17 ? 17 : 0; | |
| 44 int y = height > 17 ? 17 : 0; | |
| 45 NSColor* color = NSReadPixel(NSMakePoint(x, y)); | |
| 46 CGFloat red = 0, green = 0, blue = 0, alpha = 0; | |
| 47 [image unlockFocus]; | |
| 48 [color getRed:&red green:&green blue:&blue alpha:&alpha]; | |
| 49 | |
| 50 // Be tolerant of floating point rounding, gamma, etc. | |
| 51 if (isred) { | |
| 52 EXPECT_GT(red, 0.8); | |
| 53 EXPECT_LT(blue, 0.2); | |
| 54 } else { | |
| 55 EXPECT_LT(red, 0.2); | |
| 56 EXPECT_GT(blue, 0.8); | |
| 57 } | |
| 58 EXPECT_LT(green, 0.2); | |
| 59 EXPECT_GT(alpha, 0.9); | |
| 60 } | |
| 61 | |
| 62 | |
| 63 TEST_F(CocoaUtilsTest, BitmapToNSImage_RedSquare64x64) { | |
| 64 ShapeHelper(64, 64, true, true); | |
| 65 } | |
| 66 | |
| 67 TEST_F(CocoaUtilsTest, BitmapToNSImage_BlueRectangle199x19) { | |
| 68 ShapeHelper(199, 19, false, true); | |
| 69 } | |
| 70 | |
| 71 TEST_F(CocoaUtilsTest, BitmapToNSImage_BlueRectangle444) { | |
| 72 ShapeHelper(200, 200, false, false); | |
| 73 } | |
| 74 | |
| 75 | |
| 76 } // namespace | |
| OLD | NEW |