ui_gfx_image_unittest.mm revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 2012 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 <AppKit/AppKit.h>
6
7#import "base/mac/mac_util.h"
8#include "base/memory/scoped_nsobject.h"
9#import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
10#include "testing/gtest/include/gtest/gtest.h"
11#include "third_party/skia/include/core/SkBitmap.h"
12#include "ui/gfx/image/image.h"
13#include "ui/gfx/image/image_unittest_util.h"
14
15namespace {
16
17class UiGfxImageTest : public CocoaTest {
18};
19
20TEST_F(UiGfxImageTest, CheckColor) {
21  // TODO(kbr): re-enable: http://crbug.com/222296
22  if (base::mac::IsOSMountainLionOrLater())
23    return;
24
25  gfx::Image image = gfx::Image::CreateFrom1xBitmap(
26      gfx::test::CreateBitmap(25, 25));
27  NSImage* ns_image = image.ToNSImage();
28  [ns_image lockFocus];
29  NSColor* color = NSReadPixel(NSMakePoint(10, 10));
30  [ns_image unlockFocus];
31
32  // SkBitmapToNSImage returns a bitmap in the calibrated color space (sRGB),
33  // while NSReadPixel returns a color in the device color space. Convert back
34  // to the calibrated color space before testing.
35  color = [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
36
37  CGFloat components[4] = { 0 };
38  [color getComponents:components];
39
40  EXPECT_LT(components[0], 0.05);
41  EXPECT_GT(components[1], 0.95);
42  EXPECT_LT(components[2], 0.05);
43  EXPECT_GT(components[3], 0.95);
44}
45
46TEST_F(UiGfxImageTest, ImageView) {
47  scoped_nsobject<NSImageView> image_view(
48      [[NSImageView alloc] initWithFrame:NSMakeRect(10, 10, 25, 25)]);
49  [[test_window() contentView] addSubview:image_view];
50  [test_window() orderFront:nil];
51
52  gfx::Image image = gfx::Image::CreateFrom1xBitmap(
53      gfx::test::CreateBitmap(25, 25));
54  [image_view setImage:image.ToNSImage()];
55}
56
57}  // namespace
58