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