Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / ui / gfx / color_profile_mac_unittest.mm
blob26dd29072da5a141abfdcad85f8a0d1cf8b4c862
1 // Copyright 2014 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.
5 #import <Cocoa/Cocoa.h>
7 #include "base/mac/scoped_nsobject.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/gfx/color_profile.h"
10 #include "ui/gfx/mac/coordinate_conversion.h"
11 #import "ui/gfx/test/ui_cocoa_test_helper.h"
13 namespace {
15 class ColorProfileTest : public ui::CocoaTest {
16  public:
17   void SetUp() override {
18     ui::CocoaTest::SetUp();
20     [test_window() setUseDefaultConstraints:NO];
22     // Verify the primary screen origin.
23     NSRect primary_screen_frame = PrimaryScreenFrame();
24     EXPECT_EQ(0, primary_screen_frame.origin.x);
25     EXPECT_EQ(0, primary_screen_frame.origin.y);
27     // Move the test window onto the screen.
28     MoveTestWindowTo(gfx::Rect(0, 0, 200, 200));
30     // Verify it is contained by the screen.
31     BOOL screen_contains_test_window = NSContainsRect(
32         primary_screen_frame, [test_window() frame]);
33     EXPECT_TRUE(screen_contains_test_window);
34   }
36   void MoveTestWindowTo(gfx::Rect bounds) {
37     [test_window() setFrame:gfx::ScreenRectToNSRect(bounds) display:NO];
38     EXPECT_EQ(bounds.ToString(), TestWindowBounds().ToString());
39   }
41   gfx::Rect TestWindowBounds() {
42     return gfx::ScreenRectFromNSRect([test_window() frame]);
43   }
45   BOOL TestWindowOnScreen() {
46     return NSIntersectsRect(PrimaryScreenFrame(), [test_window() frame]);
47   }
49   BOOL TestWindowContainedOnScreen() {
50     return NSContainsRect(PrimaryScreenFrame(), [test_window() frame]);
51   }
53   NSRect PrimaryScreenFrame() {
54     return [[[NSScreen screens] objectAtIndex:0] frame];
55   }
58 bool TestColorProfileForBounds(const gfx::Rect& bounds) {
59   std::vector<char> color_profile;
60   return gfx::GetDisplayColorProfile(bounds, &color_profile);
63 TEST_F(ColorProfileTest, GetDisplayColorProfileForOnScreenBounds) {
64   MoveTestWindowTo(gfx::Rect(10, 10, 100, 100));
65   EXPECT_FALSE(TestWindowBounds().IsEmpty());
66   EXPECT_TRUE(TestWindowContainedOnScreen());
67   EXPECT_TRUE(TestColorProfileForBounds(TestWindowBounds()));
70 TEST_F(ColorProfileTest, GetDisplayColorProfileForPartiallyOnScreenBounds) {
71   MoveTestWindowTo(gfx::Rect(-50, -50, 80, 80));
72   EXPECT_FALSE(TestWindowBounds().IsEmpty());
73   EXPECT_TRUE(TestWindowOnScreen());
74   EXPECT_TRUE(TestColorProfileForBounds(TestWindowBounds()));
77 TEST_F(ColorProfileTest, GetDisplayColorProfileForOffScreenBounds) {
78   MoveTestWindowTo(gfx::Rect(-100, -100, 10, 10));
79   EXPECT_FALSE(TestWindowBounds().IsEmpty());
80   EXPECT_FALSE(TestWindowOnScreen());
81   EXPECT_FALSE(TestColorProfileForBounds(TestWindowBounds()));
84 TEST_F(ColorProfileTest, GetDisplayColorProfileForEmptyOnScreenBounds) {
85   MoveTestWindowTo(gfx::Rect(10, 10, 0, 0));
86   EXPECT_TRUE(TestWindowBounds().IsEmpty());
87   EXPECT_FALSE(TestWindowOnScreen());
88   EXPECT_FALSE(TestColorProfileForBounds(TestWindowBounds()));
91 TEST_F(ColorProfileTest, GetDisplayColorProfileForEmptyOffScreenBounds) {
92   MoveTestWindowTo(gfx::Rect(-100, -100, 0, 0));
93   EXPECT_TRUE(TestWindowBounds().IsEmpty());
94   EXPECT_FALSE(TestWindowOnScreen());
95   EXPECT_FALSE(TestColorProfileForBounds(TestWindowBounds()));
98 bool TestColorProfileForWindow(NSWindow* window) {
99   std::vector<char> color_profile;
100   return gfx::GetDisplayColorProfile(window, &color_profile);
103 TEST_F(ColorProfileTest, GetDisplayColorProfileForOnScreenWindow) {
104   MoveTestWindowTo(gfx::Rect(10, 10, 100, 100));
105   EXPECT_FALSE(TestWindowBounds().IsEmpty());
106   EXPECT_TRUE(TestWindowContainedOnScreen());
107   EXPECT_TRUE(TestColorProfileForWindow(test_window()));
110 TEST_F(ColorProfileTest, GetDisplayColorProfileForPartiallyOnScreenWindow) {
111   MoveTestWindowTo(gfx::Rect(-50, -50, 80, 80));
112   EXPECT_FALSE(TestWindowBounds().IsEmpty());
113   EXPECT_TRUE(TestWindowOnScreen());
114   EXPECT_TRUE(TestColorProfileForWindow(test_window()));
117 TEST_F(ColorProfileTest, GetDisplayColorProfileForOffScreenWindow) {
118   MoveTestWindowTo(gfx::Rect(-100, -100, 10, 10));
119   EXPECT_FALSE(TestWindowBounds().IsEmpty());
120   EXPECT_FALSE(TestWindowOnScreen());
121   EXPECT_TRUE(TestColorProfileForWindow(test_window()));
124 TEST_F(ColorProfileTest, GetDisplayColorProfileForEmptyOnScreenWindow) {
125   MoveTestWindowTo(gfx::Rect(10, 10, 0, 0));
126   EXPECT_TRUE(TestWindowBounds().IsEmpty());
127   EXPECT_FALSE(TestWindowOnScreen());
128   EXPECT_FALSE(TestColorProfileForWindow(test_window()));
131 TEST_F(ColorProfileTest, GetDisplayColorProfileForEmptyOffScreenWindow) {
132   MoveTestWindowTo(gfx::Rect(-100, -100, 0, 0));
133   EXPECT_TRUE(TestWindowBounds().IsEmpty());
134   EXPECT_FALSE(TestWindowOnScreen());
135   EXPECT_FALSE(TestColorProfileForWindow(test_window()));
138 TEST_F(ColorProfileTest, GetDisplayColorProfileForNullWindow) {
139   EXPECT_FALSE(TestColorProfileForWindow(nullptr));
140   EXPECT_FALSE(TestColorProfileForWindow(nil));
143 }  // namespace