OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #import "ui/gfx/mac/coordinate_conversion.h" | |
6 | |
7 #import <Cocoa/Cocoa.h> | |
8 | |
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" | |
14 | |
15 const int kTestWidth = 320; | |
16 const int kTestHeight = 200; | |
17 | |
18 // Class to donate an implementation of -[NSScreen frame] that provides a known | |
19 // value for robust tests. | |
20 @interface MacCoordinateConversionTestScreenDonor : NSObject | |
21 - (NSRect)frame; | |
22 @end | |
23 | |
24 @implementation MacCoordinateConversionTestScreenDonor | |
25 - (NSRect)frame { | |
26 return NSMakeRect(0, 0, kTestWidth, kTestHeight); | |
27 } | |
28 @end | |
29 | |
30 namespace gfx { | |
31 namespace { | |
32 | |
33 class MacCoordinateConversionTest : public PlatformTest { | |
34 public: | |
35 MacCoordinateConversionTest() {} | |
36 | |
37 // Overridden from testing::Test: | |
38 virtual void SetUp() override; | |
39 virtual void TearDown() override; | |
40 | |
41 private: | |
42 scoped_ptr<base::mac::ScopedObjCClassSwizzler> swizzle_frame_; | |
43 | |
44 DISALLOW_COPY_AND_ASSIGN(MacCoordinateConversionTest); | |
45 }; | |
46 | |
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); | |
53 | |
54 swizzle_frame_.reset(new base::mac::ScopedObjCClassSwizzler( | |
55 [NSScreen class], | |
56 [MacCoordinateConversionTestScreenDonor class], | |
57 @selector(frame))); | |
58 | |
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); | |
62 } | |
63 | |
64 void MacCoordinateConversionTest::TearDown() { | |
65 swizzle_frame_.reset(); | |
66 } | |
67 | |
68 } // namespace | |
69 | |
70 // Tests for coordinate conversion on Mac. Start with the following setup: | |
71 // AppKit ....... gfx | |
72 // 199 0 | |
73 // 189 10 Window of height 40 fills in pixel | |
74 // 179 --------- 20 at index 20 | |
75 // 169 | | 30 through | |
76 // ... : : .. to | |
77 // 150 | | 49 pixel | |
78 // 140 --------- 59 at index 59 | |
79 // 130 69 (inclusive). | |
80 // .. .. | |
81 // 0 199 | |
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()); | |
87 | |
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()); | |
93 | |
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()); | |
99 | |
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()); | |
105 } | |
106 | |
107 } // namespace gfx | |
OLD | NEW |