[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / ui / gfx / mac / coordinate_conversion_unittest.mm
blobac882fa65dff57e2125b90d6f8d34fee0680d060
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/gtest_mac.h"
13 #import "testing/platform_test.h"
14 #include "ui/gfx/geometry/rect.h"
16 const int kTestWidth = 320;
17 const int kTestHeight = 200;
19 // Class to donate an implementation of -[NSScreen frame] that provides a known
20 // value for robust tests.
21 @interface MacCoordinateConversionTestScreenDonor : NSObject
22 - (NSRect)frame;
23 @end
25 @implementation MacCoordinateConversionTestScreenDonor
26 - (NSRect)frame {
27   return NSMakeRect(0, 0, kTestWidth, kTestHeight);
29 @end
31 namespace gfx {
32 namespace {
34 class MacCoordinateConversionTest : public PlatformTest {
35  public:
36   MacCoordinateConversionTest() {}
38   // Overridden from testing::Test:
39   void SetUp() override;
40   void TearDown() override;
42  private:
43   scoped_ptr<base::mac::ScopedObjCClassSwizzler> swizzle_frame_;
45   DISALLOW_COPY_AND_ASSIGN(MacCoordinateConversionTest);
48 void MacCoordinateConversionTest::SetUp() {
49   // Before swizzling, do a sanity check that the primary screen's origin is
50   // (0, 0). This should always be true.
51   NSRect primary_screen_frame = [[[NSScreen screens] objectAtIndex:0] frame];
52   EXPECT_EQ(0, primary_screen_frame.origin.x);
53   EXPECT_EQ(0, primary_screen_frame.origin.y);
55   swizzle_frame_.reset(new base::mac::ScopedObjCClassSwizzler(
56       [NSScreen class],
57       [MacCoordinateConversionTestScreenDonor class],
58       @selector(frame)));
60   primary_screen_frame = [[[NSScreen screens] objectAtIndex:0] frame];
61   EXPECT_EQ(kTestWidth, primary_screen_frame.size.width);
62   EXPECT_EQ(kTestHeight, primary_screen_frame.size.height);
65 void MacCoordinateConversionTest::TearDown() {
66   swizzle_frame_.reset();
69 }  // namespace
71 // Tests for coordinate conversion on Mac. Start with the following setup:
72 // AppKit ....... gfx
73 // 199              0
74 // 189             10   Window of height 40 fills in pixel
75 // 179  ---------  20   at index 20
76 // 169  |       |  30   through
77 // ...  :       :  ..   to
78 // 150  |       |  49   pixel
79 // 140  ---------  59   at index 59
80 // 130             69   (inclusive).
81 //  ..             ..
82 //   0            199
83 TEST_F(MacCoordinateConversionTest, ScreenRectToFromNSRect) {
84   // Window on the primary screen.
85   Rect gfx_rect = Rect(10, 20, 30, 40);
86   NSRect ns_rect = ScreenRectToNSRect(gfx_rect);
87   EXPECT_NSEQ(NSMakeRect(10, 140, 30, 40), ns_rect);
88   EXPECT_EQ(gfx_rect, ScreenRectFromNSRect(ns_rect));
90   // Window in a screen to the left of the primary screen.
91   gfx_rect = Rect(-40, 20, 30, 40);
92   ns_rect = ScreenRectToNSRect(gfx_rect);
93   EXPECT_NSEQ(NSMakeRect(-40, 140, 30, 40), ns_rect);
94   EXPECT_EQ(gfx_rect, ScreenRectFromNSRect(ns_rect));
96   // Window in a screen below the primary screen.
97   gfx_rect = Rect(10, 220, 30, 40);
98   ns_rect = ScreenRectToNSRect(gfx_rect);
99   EXPECT_NSEQ(NSMakeRect(10, -60, 30, 40), ns_rect);
100   EXPECT_EQ(gfx_rect, ScreenRectFromNSRect(ns_rect));
102   // Window in a screen below and to the left primary screen.
103   gfx_rect = Rect(-40, 220, 30, 40);
104   ns_rect = ScreenRectToNSRect(gfx_rect);
105   EXPECT_NSEQ(NSMakeRect(-40, -60, 30, 40), ns_rect);
106   EXPECT_EQ(gfx_rect, ScreenRectFromNSRect(ns_rect));
109 // Test point conversions using the same setup as ScreenRectToFromNSRect, but
110 // using only the origin.
111 TEST_F(MacCoordinateConversionTest, ScreenPointToFromNSPoint) {
112   // Point on the primary screen.
113   Point gfx_point = Point(10, 20);
114   NSPoint ns_point = ScreenPointToNSPoint(gfx_point);
115   EXPECT_NSEQ(NSMakePoint(10, 180), ns_point);
116   EXPECT_EQ(gfx_point, ScreenPointFromNSPoint(ns_point));
118   // Point in a screen to the left of the primary screen.
119   gfx_point = Point(-40, 20);
120   ns_point = ScreenPointToNSPoint(gfx_point);
121   EXPECT_NSEQ(NSMakePoint(-40, 180), ns_point);
122   EXPECT_EQ(gfx_point, ScreenPointFromNSPoint(ns_point));
124   // Point in a screen below the primary screen.
125   gfx_point = Point(10, 220);
126   ns_point = ScreenPointToNSPoint(gfx_point);
127   EXPECT_NSEQ(NSMakePoint(10, -20), ns_point);
128   EXPECT_EQ(gfx_point, ScreenPointFromNSPoint(ns_point));
130   // Point in a screen below and to the left primary screen.
131   gfx_point = Point(-40, 220);
132   ns_point = ScreenPointToNSPoint(gfx_point);
133   EXPECT_NSEQ(NSMakePoint(-40, -20), ns_point);
134   EXPECT_EQ(gfx_point, ScreenPointFromNSPoint(ns_point));
137 }  // namespace gfx