[Android WebViewShell] Make WebViewLayoutTest runnable with test_runner.py
[chromium-blink-merge.git] / ui / gfx / mac / coordinate_conversion_unittest.mm
blobae4931763c8a212db0fe07e6d1a9c2ca97e7eb58
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   Rect gfx_rect = Rect(10, 20, 30, 40);
85   NSRect ns_rect = ScreenRectToNSRect(gfx_rect);
86   EXPECT_NSEQ(NSMakeRect(10, 140, 30, 40), ns_rect);
87   EXPECT_EQ(gfx_rect.ToString(), ScreenRectFromNSRect(ns_rect).ToString());
89   // Window in a screen to the left of the primary screen.
90   gfx_rect = Rect(-40, 20, 30, 40);
91   ns_rect = ScreenRectToNSRect(gfx_rect);
92   EXPECT_NSEQ(NSMakeRect(-40, 140, 30, 40), ns_rect);
93   EXPECT_EQ(gfx_rect.ToString(), ScreenRectFromNSRect(ns_rect).ToString());
95   // Window in a screen below the primary screen.
96   gfx_rect = Rect(10, 220, 30, 40);
97   ns_rect = ScreenRectToNSRect(gfx_rect);
98   EXPECT_NSEQ(NSMakeRect(10, -60, 30, 40), ns_rect);
99   EXPECT_EQ(gfx_rect.ToString(), ScreenRectFromNSRect(ns_rect).ToString());
101   // Window in a screen below and to the left primary screen.
102   gfx_rect = Rect(-40, 220, 30, 40);
103   ns_rect = ScreenRectToNSRect(gfx_rect);
104   EXPECT_NSEQ(NSMakeRect(-40, -60, 30, 40), ns_rect);
105   EXPECT_EQ(gfx_rect.ToString(), ScreenRectFromNSRect(ns_rect).ToString());
108 }  // namespace gfx