1 // Copyright (c) 2011 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 <Cocoa/Cocoa.h> 6 7 #include "base/mac/scoped_nsobject.h" 8 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/platform_test.h" 10 #include "ui/base/cocoa/base_view.h" 11 #import "ui/gfx/test/ui_cocoa_test_helper.h" 12 13 namespace { 14 15 class BaseViewTest : public ui::CocoaTest { 16 public: 17 BaseViewTest() { 18 NSRect frame = NSMakeRect(0, 0, 100, 100); 19 base::scoped_nsobject<BaseView> view( 20 [[BaseView alloc] initWithFrame:frame]); 21 view_ = view.get(); 22 [[test_window() contentView] addSubview:view_]; 23 } 24 25 BaseView* view_; // weak 26 }; 27 28 TEST_VIEW(BaseViewTest, view_) 29 30 // Convert a rect in |view_|'s Cocoa coordinate system to gfx::Rect's top-left 31 // coordinate system. Repeat the process in reverse and make sure we come out 32 // with the original rect. 33 TEST_F(BaseViewTest, flipNSRectToRect) { 34 NSRect convert = NSMakeRect(10, 10, 50, 50); 35 gfx::Rect converted = [view_ flipNSRectToRect:convert]; 36 EXPECT_EQ(converted.x(), 10); 37 EXPECT_EQ(converted.y(), 40); // Due to view being 100px tall. 38 EXPECT_EQ(converted.width(), NSWidth(convert)); 39 EXPECT_EQ(converted.height(), NSHeight(convert)); 40 41 // Go back the other way. 42 NSRect back_again = [view_ flipRectToNSRect:converted]; 43 EXPECT_EQ(NSMinX(back_again), NSMinX(convert)); 44 EXPECT_EQ(NSMinY(back_again), NSMinY(convert)); 45 EXPECT_EQ(NSWidth(back_again), NSWidth(convert)); 46 EXPECT_EQ(NSHeight(back_again), NSHeight(convert)); 47 } 48 49 } // namespace 50