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 "ui/gfx/mac/coordinate_conversion.h"
7 #import <Cocoa/Cocoa.h>
9 #import "base/mac/scoped_objc_class_swizzler.h"
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #import "testing/platform_test.h"
13 #include "ui/gfx/geometry/rect.h"
15 const int kTestWidth = 320;
16 const int kTestHeight = 200;
18 // Class to donate an implementation of -[NSScreen frame] that provides a known
19 // value for robust tests.
20 @interface MacCoordinateConversionTestScreenDonor : NSObject
24 @implementation MacCoordinateConversionTestScreenDonor
26 return NSMakeRect(0, 0, kTestWidth, kTestHeight);
33 class MacCoordinateConversionTest : public PlatformTest {
35 MacCoordinateConversionTest() {}
37 // Overridden from testing::Test:
38 void SetUp() override;
39 void TearDown() override;
42 scoped_ptr<base::mac::ScopedObjCClassSwizzler> swizzle_frame_;
44 DISALLOW_COPY_AND_ASSIGN(MacCoordinateConversionTest);
47 void MacCoordinateConversionTest::SetUp() {
48 // Before swizzling, do a sanity check that the primary screen's origin is
49 // (0, 0). This should always be true.
50 NSRect primary_screen_frame = [[[NSScreen screens] objectAtIndex:0] frame];
51 EXPECT_EQ(0, primary_screen_frame.origin.x);
52 EXPECT_EQ(0, primary_screen_frame.origin.y);
54 swizzle_frame_.reset(new base::mac::ScopedObjCClassSwizzler(
56 [MacCoordinateConversionTestScreenDonor class],
59 primary_screen_frame = [[[NSScreen screens] objectAtIndex:0] frame];
60 EXPECT_EQ(kTestWidth, primary_screen_frame.size.width);
61 EXPECT_EQ(kTestHeight, primary_screen_frame.size.height);
64 void MacCoordinateConversionTest::TearDown() {
65 swizzle_frame_.reset();
70 // Tests for coordinate conversion on Mac. Start with the following setup:
73 // 189 10 Window of height 40 fills in pixel
74 // 179 --------- 20 at index 20
78 // 140 --------- 59 at index 59
79 // 130 69 (inclusive).
82 TEST_F(MacCoordinateConversionTest, ScreenRectToFromNSRect) {
83 Rect gfx_rect = Rect(10, 20, 30, 40);
84 NSRect ns_rect = ScreenRectToNSRect(gfx_rect);
85 EXPECT_TRUE(NSEqualRects(NSMakeRect(10, 140, 30, 40), ns_rect));
86 EXPECT_EQ(gfx_rect.ToString(), ScreenRectFromNSRect(ns_rect).ToString());
88 // Window in a screen to the left of the primary screen.
89 gfx_rect = Rect(-40, 20, 30, 40);
90 ns_rect = ScreenRectToNSRect(gfx_rect);
91 EXPECT_TRUE(NSEqualRects(NSMakeRect(-40, 140, 30, 40), ns_rect));
92 EXPECT_EQ(gfx_rect.ToString(), ScreenRectFromNSRect(ns_rect).ToString());
94 // Window in a screen below the primary screen.
95 gfx_rect = Rect(10, 220, 30, 40);
96 ns_rect = ScreenRectToNSRect(gfx_rect);
97 EXPECT_TRUE(NSEqualRects(NSMakeRect(10, -60, 30, 40), ns_rect));
98 EXPECT_EQ(gfx_rect.ToString(), ScreenRectFromNSRect(ns_rect).ToString());
100 // Window in a screen below and to the left primary screen.
101 gfx_rect = Rect(-40, 220, 30, 40);
102 ns_rect = ScreenRectToNSRect(gfx_rect);
103 EXPECT_TRUE(NSEqualRects(NSMakeRect(-40, -60, 30, 40), ns_rect));
104 EXPECT_EQ(gfx_rect.ToString(), ScreenRectFromNSRect(ns_rect).ToString());