Chromium Code Reviews| 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 #include "base/macros.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #import "base/test/scoped_class_swizzler_mac.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 | |
|
Robert Sesek
2014/07/21 15:59:01
optional: Feel free to omit blank lines around met
tapted
2014/07/22 12:53:49
Done.
| |
| 22 - (NSRect)frame; | |
| 23 | |
| 24 @end | |
| 25 | |
| 26 @implementation MacCoordinateConversionTestScreenDonor | |
| 27 | |
| 28 - (NSRect)frame { | |
| 29 return NSMakeRect(0, 0, kTestWidth, kTestHeight); | |
| 30 } | |
| 31 | |
| 32 @end | |
| 33 | |
| 34 namespace gfx { | |
| 35 namespace { | |
| 36 | |
| 37 class MacCoordinateConversionTest : public PlatformTest { | |
| 38 public: | |
| 39 MacCoordinateConversionTest() {} | |
| 40 | |
| 41 // Overridden from testing::Test: | |
| 42 virtual void SetUp() OVERRIDE; | |
| 43 virtual void TearDown() OVERRIDE; | |
| 44 | |
| 45 private: | |
| 46 scoped_ptr<base::ScopedClassSwizzler> swizzle_frame_; | |
| 47 | |
| 48 DISALLOW_COPY_AND_ASSIGN(MacCoordinateConversionTest); | |
| 49 }; | |
| 50 | |
| 51 void MacCoordinateConversionTest::SetUp() { | |
| 52 // Before swizzling, do a sanity check that the primary screen's origin is | |
| 53 // (0, 0). This should always be true. | |
| 54 NSRect primary_screen_frame = [[[NSScreen screens] objectAtIndex:0] frame]; | |
| 55 EXPECT_EQ(0, primary_screen_frame.origin.x); | |
| 56 EXPECT_EQ(0, primary_screen_frame.origin.y); | |
| 57 | |
| 58 swizzle_frame_.reset(new base::ScopedClassSwizzler( | |
| 59 [NSScreen class], | |
| 60 [MacCoordinateConversionTestScreenDonor class], | |
| 61 @selector(frame))); | |
| 62 | |
| 63 primary_screen_frame = [[[NSScreen screens] objectAtIndex:0] frame]; | |
| 64 EXPECT_EQ(kTestWidth, primary_screen_frame.size.width); | |
| 65 EXPECT_EQ(kTestHeight, primary_screen_frame.size.height); | |
| 66 } | |
| 67 | |
| 68 void MacCoordinateConversionTest::TearDown() { | |
| 69 swizzle_frame_.reset(); | |
| 70 } | |
| 71 | |
| 72 } // namespace | |
| 73 | |
| 74 // Tests for coordinate conversion on Mac. Start with the following setup: | |
| 75 // AppKit ....... gfx | |
| 76 // 199 0 | |
| 77 // 189 10 Window of height 40 fills in pixel | |
| 78 // 179 --------- 20 at index 20 | |
| 79 // 169 | | 30 through | |
| 80 // ... : : .. to | |
| 81 // 150 | | 49 pixel | |
| 82 // 140 --------- 59 at index 59 | |
| 83 // 130 69 (inclusive). | |
| 84 // .. .. | |
| 85 // 0 199 | |
| 86 TEST_F(MacCoordinateConversionTest, ScreenRectToFromNSRect) { | |
| 87 Rect gfx_rect = Rect(10, 20, 30, 40); | |
| 88 NSRect ns_rect = ScreenRectToNSRect(gfx_rect); | |
| 89 EXPECT_TRUE(NSEqualRects(NSMakeRect(10, 140, 30, 40), ns_rect)); | |
| 90 EXPECT_EQ(gfx_rect.ToString(), ScreenRectFromNSRect(ns_rect).ToString()); | |
| 91 | |
| 92 // Window in a screen to the left of the primary screen. | |
| 93 gfx_rect = Rect(-40, 20, 30, 40); | |
| 94 ns_rect = ScreenRectToNSRect(gfx_rect); | |
| 95 EXPECT_TRUE(NSEqualRects(NSMakeRect(-40, 140, 30, 40), ns_rect)); | |
| 96 EXPECT_EQ(gfx_rect.ToString(), ScreenRectFromNSRect(ns_rect).ToString()); | |
| 97 | |
| 98 // Window in a screen below the primary screen. | |
| 99 gfx_rect = Rect(10, 220, 30, 40); | |
| 100 ns_rect = ScreenRectToNSRect(gfx_rect); | |
| 101 EXPECT_TRUE(NSEqualRects(NSMakeRect(10, -60, 30, 40), ns_rect)); | |
| 102 EXPECT_EQ(gfx_rect.ToString(), ScreenRectFromNSRect(ns_rect).ToString()); | |
| 103 | |
| 104 // Window in a screen below and to the left primary screen. | |
| 105 gfx_rect = Rect(-40, 220, 30, 40); | |
| 106 ns_rect = ScreenRectToNSRect(gfx_rect); | |
| 107 EXPECT_TRUE(NSEqualRects(NSMakeRect(-40, -60, 30, 40), ns_rect)); | |
| 108 EXPECT_EQ(gfx_rect.ToString(), ScreenRectFromNSRect(ns_rect).ToString()); | |
| 109 } | |
| 110 | |
| 111 } // namespace gfx | |
| OLD | NEW |