coordinate_conversion_unittest.mm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #import "ui/gfx/mac/coordinate_conversion.h"
  5. #import <Cocoa/Cocoa.h>
  6. #include <memory>
  7. #import "base/mac/scoped_objc_class_swizzler.h"
  8. #import "testing/gtest_mac.h"
  9. #import "testing/platform_test.h"
  10. #include "ui/gfx/geometry/rect.h"
  11. const int kTestWidth = 320;
  12. const int kTestHeight = 200;
  13. // Class to donate an implementation of -[NSScreen frame] that provides a known
  14. // value for robust tests.
  15. @interface MacCoordinateConversionTestScreenDonor : NSObject
  16. - (NSRect)frame;
  17. @end
  18. @implementation MacCoordinateConversionTestScreenDonor
  19. - (NSRect)frame {
  20. return NSMakeRect(0, 0, kTestWidth, kTestHeight);
  21. }
  22. @end
  23. namespace gfx {
  24. namespace {
  25. class MacCoordinateConversionTest : public PlatformTest {
  26. public:
  27. MacCoordinateConversionTest() {}
  28. MacCoordinateConversionTest(const MacCoordinateConversionTest&) = delete;
  29. MacCoordinateConversionTest& operator=(const MacCoordinateConversionTest&) =
  30. delete;
  31. // Overridden from testing::Test:
  32. void SetUp() override;
  33. void TearDown() override;
  34. private:
  35. std::unique_ptr<base::mac::ScopedObjCClassSwizzler> swizzle_frame_;
  36. };
  37. void MacCoordinateConversionTest::SetUp() {
  38. // Before swizzling, do a sanity check that the primary screen's origin is
  39. // (0, 0). This should always be true.
  40. NSRect primary_screen_frame = [[[NSScreen screens] firstObject] frame];
  41. EXPECT_EQ(0, primary_screen_frame.origin.x);
  42. EXPECT_EQ(0, primary_screen_frame.origin.y);
  43. swizzle_frame_ = std::make_unique<base::mac::ScopedObjCClassSwizzler>(
  44. [NSScreen class], [MacCoordinateConversionTestScreenDonor class],
  45. @selector(frame));
  46. primary_screen_frame = [[[NSScreen screens] firstObject] frame];
  47. EXPECT_EQ(kTestWidth, primary_screen_frame.size.width);
  48. EXPECT_EQ(kTestHeight, primary_screen_frame.size.height);
  49. }
  50. void MacCoordinateConversionTest::TearDown() {
  51. swizzle_frame_.reset();
  52. }
  53. } // namespace
  54. // Tests for coordinate conversion on Mac. Start with the following setup:
  55. // AppKit ....... gfx
  56. // 199 0
  57. // 189 10 Window of height 40 fills in pixel
  58. // 179 --------- 20 at index 20
  59. // 169 | | 30 through
  60. // ... : : .. to
  61. // 150 | | 49 pixel
  62. // 140 --------- 59 at index 59
  63. // 130 69 (inclusive).
  64. // .. ..
  65. // 0 199
  66. TEST_F(MacCoordinateConversionTest, ScreenRectToFromNSRect) {
  67. // Window on the primary screen.
  68. Rect gfx_rect = Rect(10, 20, 30, 40);
  69. NSRect ns_rect = ScreenRectToNSRect(gfx_rect);
  70. EXPECT_NSEQ(NSMakeRect(10, 140, 30, 40), ns_rect);
  71. EXPECT_EQ(gfx_rect, ScreenRectFromNSRect(ns_rect));
  72. // Window in a screen to the left of the primary screen.
  73. gfx_rect = Rect(-40, 20, 30, 40);
  74. ns_rect = ScreenRectToNSRect(gfx_rect);
  75. EXPECT_NSEQ(NSMakeRect(-40, 140, 30, 40), ns_rect);
  76. EXPECT_EQ(gfx_rect, ScreenRectFromNSRect(ns_rect));
  77. // Window in a screen below the primary screen.
  78. gfx_rect = Rect(10, 220, 30, 40);
  79. ns_rect = ScreenRectToNSRect(gfx_rect);
  80. EXPECT_NSEQ(NSMakeRect(10, -60, 30, 40), ns_rect);
  81. EXPECT_EQ(gfx_rect, ScreenRectFromNSRect(ns_rect));
  82. // Window in a screen below and to the left primary screen.
  83. gfx_rect = Rect(-40, 220, 30, 40);
  84. ns_rect = ScreenRectToNSRect(gfx_rect);
  85. EXPECT_NSEQ(NSMakeRect(-40, -60, 30, 40), ns_rect);
  86. EXPECT_EQ(gfx_rect, ScreenRectFromNSRect(ns_rect));
  87. }
  88. // Test point conversions using the same setup as ScreenRectToFromNSRect, but
  89. // using only the origin.
  90. TEST_F(MacCoordinateConversionTest, ScreenPointToFromNSPoint) {
  91. // Point on the primary screen.
  92. Point gfx_point = Point(10, 20);
  93. NSPoint ns_point = ScreenPointToNSPoint(gfx_point);
  94. EXPECT_NSEQ(NSMakePoint(10, 180), ns_point);
  95. EXPECT_EQ(gfx_point, ScreenPointFromNSPoint(ns_point));
  96. // Point in a screen to the left of the primary screen.
  97. gfx_point = Point(-40, 20);
  98. ns_point = ScreenPointToNSPoint(gfx_point);
  99. EXPECT_NSEQ(NSMakePoint(-40, 180), ns_point);
  100. EXPECT_EQ(gfx_point, ScreenPointFromNSPoint(ns_point));
  101. // Point in a screen below the primary screen.
  102. gfx_point = Point(10, 220);
  103. ns_point = ScreenPointToNSPoint(gfx_point);
  104. EXPECT_NSEQ(NSMakePoint(10, -20), ns_point);
  105. EXPECT_EQ(gfx_point, ScreenPointFromNSPoint(ns_point));
  106. // Point in a screen below and to the left primary screen.
  107. gfx_point = Point(-40, 220);
  108. ns_point = ScreenPointToNSPoint(gfx_point);
  109. EXPECT_NSEQ(NSMakePoint(-40, -20), ns_point);
  110. EXPECT_EQ(gfx_point, ScreenPointFromNSPoint(ns_point));
  111. }
  112. } // namespace gfx