| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
| 6 | 6 |
| 7 #include "app/theme_provider.h" | 7 #include "app/theme_provider.h" |
| 8 #include "base/scoped_nsobject.h" | 8 #include "base/scoped_nsobject.h" |
| 9 #include "chrome/browser/browser_theme_provider.h" | 9 #include "chrome/browser/browser_theme_provider.h" |
| 10 #import "chrome/browser/cocoa/bookmark_bar_controller.h" | 10 #import "chrome/browser/cocoa/bookmark_bar_controller.h" |
| 11 #import "chrome/browser/cocoa/bookmark_bar_toolbar_view.h" | 11 #import "chrome/browser/cocoa/bookmark_bar_toolbar_view.h" |
| 12 #import "chrome/browser/cocoa/cocoa_test_helper.h" | 12 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
| 13 #include "grit/theme_resources.h" | 13 #include "grit/theme_resources.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "testing/platform_test.h" | 16 #include "testing/platform_test.h" |
| 17 #include "third_party/skia/include/core/SkBitmap.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
| 18 #include "third_party/skia/include/core/SkColor.h" | 18 #include "third_party/skia/include/core/SkColor.h" |
| 19 | 19 |
| 20 using ::testing::_; | 20 using ::testing::_; |
| 21 using ::testing::DoAll; | 21 using ::testing::DoAll; |
| 22 using ::testing::Return; | 22 using ::testing::Return; |
| 23 using ::testing::SetArgumentPointee; | 23 using ::testing::SetArgumentPointee; |
| 24 | 24 |
| 25 // When testing the floating drawing, we need to have a source of | 25 // When testing the floating drawing, we need to have a source of theme data. |
| 26 // theme data. | |
| 27 class MockThemeProvider : public ThemeProvider { | 26 class MockThemeProvider : public ThemeProvider { |
| 28 public: | 27 public: |
| 29 // Cross platform methods | 28 // Cross platform methods |
| 30 MOCK_METHOD1(Init, void(Profile*)); | 29 MOCK_METHOD1(Init, void(Profile*)); |
| 31 MOCK_CONST_METHOD1(GetBitmapNamed, SkBitmap*(int)); | 30 MOCK_CONST_METHOD1(GetBitmapNamed, SkBitmap*(int)); |
| 32 MOCK_CONST_METHOD1(GetColor, SkColor(int)); | 31 MOCK_CONST_METHOD1(GetColor, SkColor(int)); |
| 33 MOCK_CONST_METHOD2(GetDisplayProperty, bool(int, int*)); | 32 MOCK_CONST_METHOD2(GetDisplayProperty, bool(int, int*)); |
| 34 MOCK_CONST_METHOD0(ShouldUseNativeFrame, bool()); | 33 MOCK_CONST_METHOD0(ShouldUseNativeFrame, bool()); |
| 35 MOCK_CONST_METHOD1(HasCustomImage, bool(int)); | 34 MOCK_CONST_METHOD1(HasCustomImage, bool(int)); |
| 36 MOCK_CONST_METHOD1(GetRawData, RefCountedMemory*(int)); | 35 MOCK_CONST_METHOD1(GetRawData, RefCountedMemory*(int)); |
| 37 | 36 |
| 38 // OSX stuff | 37 // OSX stuff |
| 39 MOCK_CONST_METHOD1(GetNSImageNamed, NSImage*(int)); | 38 MOCK_CONST_METHOD1(GetNSImageNamed, NSImage*(int)); |
| 40 MOCK_CONST_METHOD1(GetNSColor, NSColor*(int)); | 39 MOCK_CONST_METHOD1(GetNSColor, NSColor*(int)); |
| 41 MOCK_CONST_METHOD1(GetNSColorTint, NSColor*(int)); | 40 MOCK_CONST_METHOD1(GetNSColorTint, NSColor*(int)); |
| 42 }; | 41 }; |
| 43 | 42 |
| 44 // Allows us to inject our fake controller below. | 43 // Allows us to inject our fake controller below. |
| 45 @interface BookmarkBarToolbarView (TestingAPI) | 44 @interface BookmarkBarToolbarView (TestingAPI) |
| 46 -(void)setController:(id<BookmarkBarToolbarViewController>)controller; | 45 -(void)setController:(id<BookmarkBarToolbarViewController>)controller; |
| 47 @end | 46 @end |
| 48 | 47 |
| 49 @implementation BookmarkBarToolbarView (TestingAPI) | 48 @implementation BookmarkBarToolbarView (TestingAPI) |
| 50 -(void)setController:(id<BookmarkBarToolbarViewController>)controller { | 49 -(void)setController:(id<BookmarkBarToolbarViewController>)controller { |
| 51 controller_ = controller; | 50 controller_ = controller; |
| 52 } | 51 } |
| 53 @end | 52 @end |
| 54 | 53 |
| 55 // Allows us to control which way the view is rendered. | 54 // Allows us to control which way the view is rendered. |
| 56 @interface DrawFloatingFakeController : | 55 @interface DrawDetachedBarFakeController : |
| 57 NSObject<BookmarkBarToolbarViewController> { | 56 NSObject<BookmarkBarToolbarViewController> { |
| 58 int current_tab_contents_height_; | 57 int current_tab_contents_height_; |
| 59 ThemeProvider* theme_provider_; | 58 ThemeProvider* theme_provider_; |
| 60 BOOL drawAsFloating_; | 59 BOOL isShownAsDetachedBar_; |
| 61 } | 60 } |
| 62 @property(assign) int currentTabContentsHeight; | 61 @property(assign) int currentTabContentsHeight; |
| 63 @property(assign) ThemeProvider* themeProvider; | 62 @property(assign) ThemeProvider* themeProvider; |
| 64 @property(assign) BOOL drawAsFloatingBar; | 63 @property(assign) BOOL isShownAsDetachedBar; |
| 65 @end | 64 @end |
| 66 | 65 |
| 67 @implementation DrawFloatingFakeController | 66 @implementation DrawDetachedBarFakeController |
| 68 @synthesize currentTabContentsHeight = current_tab_contents_height_; | 67 @synthesize currentTabContentsHeight = current_tab_contents_height_; |
| 69 @synthesize themeProvider = theme_provider_; | 68 @synthesize themeProvider = theme_provider_; |
| 70 @synthesize drawAsFloatingBar = drawAsFloating_; | 69 @synthesize isShownAsDetachedBar = isShownAsDetachedBar_; |
| 71 @end | 70 @end |
| 72 | 71 |
| 73 class BookmarkBarToolbarViewTest : public PlatformTest { | 72 class BookmarkBarToolbarViewTest : public PlatformTest { |
| 74 public: | 73 public: |
| 75 BookmarkBarToolbarViewTest() { | 74 BookmarkBarToolbarViewTest() { |
| 76 controller_.reset([[DrawFloatingFakeController alloc] init]); | 75 controller_.reset([[DrawDetachedBarFakeController alloc] init]); |
| 77 NSRect frame = NSMakeRect(0, 0, 400, 40); | 76 NSRect frame = NSMakeRect(0, 0, 400, 40); |
| 78 view_.reset([[BookmarkBarToolbarView alloc] initWithFrame:frame]); | 77 view_.reset([[BookmarkBarToolbarView alloc] initWithFrame:frame]); |
| 79 [cocoa_helper_.contentView() addSubview:view_.get()]; | 78 [cocoa_helper_.contentView() addSubview:view_.get()]; |
| 80 [view_.get() setController:controller_.get()]; | 79 [view_.get() setController:controller_.get()]; |
| 81 } | 80 } |
| 82 | 81 |
| 83 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... | 82 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... |
| 84 scoped_nsobject<DrawFloatingFakeController> controller_; | 83 scoped_nsobject<DrawDetachedBarFakeController> controller_; |
| 85 scoped_nsobject<BookmarkBarToolbarView> view_; | 84 scoped_nsobject<BookmarkBarToolbarView> view_; |
| 86 }; | 85 }; |
| 87 | 86 |
| 88 // Test adding/removing from the view hierarchy, mostly to ensure nothing | 87 // Test adding/removing from the view hierarchy, mostly to ensure nothing |
| 89 // leaks or crashes. | 88 // leaks or crashes. |
| 90 TEST_F(BookmarkBarToolbarViewTest, AddRemove) { | 89 TEST_F(BookmarkBarToolbarViewTest, AddRemove) { |
| 91 EXPECT_EQ(cocoa_helper_.contentView(), [view_ superview]); | 90 EXPECT_EQ(cocoa_helper_.contentView(), [view_ superview]); |
| 92 [view_.get() removeFromSuperview]; | 91 [view_.get() removeFromSuperview]; |
| 93 EXPECT_FALSE([view_ superview]); | 92 EXPECT_FALSE([view_ superview]); |
| 94 } | 93 } |
| 95 | 94 |
| 96 // Test drawing (part 1), mostly to ensure nothing leaks or crashes. | 95 // Test drawing (part 1), mostly to ensure nothing leaks or crashes. |
| 97 TEST_F(BookmarkBarToolbarViewTest, DisplayAsNormalBar) { | 96 TEST_F(BookmarkBarToolbarViewTest, DisplayAsNormalBar) { |
| 98 [controller_.get() setDrawAsFloatingBar:NO]; | 97 [controller_.get() setIsShownAsDetachedBar:NO]; |
| 99 [view_ display]; | 98 [view_ display]; |
| 100 } | 99 } |
| 101 | 100 |
| 102 // Test drawing (part 2), mostly to ensure nothing leaks or crashes. | 101 // Test drawing (part 2), mostly to ensure nothing leaks or crashes. |
| 103 TEST_F(BookmarkBarToolbarViewTest, DisplayAsFloatingBarWithNoImage) { | 102 TEST_F(BookmarkBarToolbarViewTest, DisplayAsDetachedBarWithNoImage) { |
| 104 [controller_.get() setDrawAsFloatingBar:YES]; | 103 [controller_.get() setIsShownAsDetachedBar:YES]; |
| 105 | 104 |
| 106 // Tests where we don't have a background image, only a color. | 105 // Tests where we don't have a background image, only a color. |
| 107 MockThemeProvider provider; | 106 MockThemeProvider provider; |
| 108 EXPECT_CALL(provider, GetColor(BrowserThemeProvider::COLOR_NTP_BACKGROUND)) | 107 EXPECT_CALL(provider, GetColor(BrowserThemeProvider::COLOR_NTP_BACKGROUND)) |
| 109 .WillRepeatedly(Return(SK_ColorWHITE)); | 108 .WillRepeatedly(Return(SK_ColorWHITE)); |
| 110 EXPECT_CALL(provider, HasCustomImage(IDR_THEME_NTP_BACKGROUND)) | 109 EXPECT_CALL(provider, HasCustomImage(IDR_THEME_NTP_BACKGROUND)) |
| 111 .WillRepeatedly(Return(false)); | 110 .WillRepeatedly(Return(false)); |
| 112 [controller_.get() setThemeProvider:&provider]; | 111 [controller_.get() setThemeProvider:&provider]; |
| 113 | 112 |
| 114 [view_ display]; | 113 [view_ display]; |
| 115 } | 114 } |
| 116 | 115 |
| 117 // Actions used in DisplayAsFloatingBarWithBgImage. | 116 // Actions used in DisplayAsDetachedBarWithBgImage. |
| 118 ACTION(SetBackgroundTiling) { | 117 ACTION(SetBackgroundTiling) { |
| 119 *arg1 = BrowserThemeProvider::NO_REPEAT; | 118 *arg1 = BrowserThemeProvider::NO_REPEAT; |
| 120 return true; | 119 return true; |
| 121 } | 120 } |
| 122 | 121 |
| 123 ACTION(SetAlignLeft) { | 122 ACTION(SetAlignLeft) { |
| 124 *arg1 = BrowserThemeProvider::ALIGN_LEFT; | 123 *arg1 = BrowserThemeProvider::ALIGN_LEFT; |
| 125 return true; | 124 return true; |
| 126 } | 125 } |
| 127 | 126 |
| 128 // Test drawing (part 3), mostly to ensure nothing leaks or crashes. | 127 // Test drawing (part 3), mostly to ensure nothing leaks or crashes. |
| 129 TEST_F(BookmarkBarToolbarViewTest, DisplayAsFloatingBarWithBgImage) { | 128 TEST_F(BookmarkBarToolbarViewTest, DisplayAsDetachedBarWithBgImage) { |
| 130 [controller_.get() setDrawAsFloatingBar:YES]; | 129 [controller_.get() setIsShownAsDetachedBar:YES]; |
| 131 | 130 |
| 132 // Tests where we have a background image, with positioning information. | 131 // Tests where we have a background image, with positioning information. |
| 133 MockThemeProvider provider; | 132 MockThemeProvider provider; |
| 134 | 133 |
| 135 // Advertise having an image. | 134 // Advertise having an image. |
| 136 EXPECT_CALL(provider, GetColor(BrowserThemeProvider::COLOR_NTP_BACKGROUND)) | 135 EXPECT_CALL(provider, GetColor(BrowserThemeProvider::COLOR_NTP_BACKGROUND)) |
| 137 .WillRepeatedly(Return(SK_ColorRED)); | 136 .WillRepeatedly(Return(SK_ColorRED)); |
| 138 EXPECT_CALL(provider, HasCustomImage(IDR_THEME_NTP_BACKGROUND)) | 137 EXPECT_CALL(provider, HasCustomImage(IDR_THEME_NTP_BACKGROUND)) |
| 139 .WillRepeatedly(Return(true)); | 138 .WillRepeatedly(Return(true)); |
| 140 | 139 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 152 fake_bg.allocPixels(); | 151 fake_bg.allocPixels(); |
| 153 fake_bg.eraseColor(SK_ColorGREEN); | 152 fake_bg.eraseColor(SK_ColorGREEN); |
| 154 EXPECT_CALL(provider, GetBitmapNamed(IDR_THEME_NTP_BACKGROUND)) | 153 EXPECT_CALL(provider, GetBitmapNamed(IDR_THEME_NTP_BACKGROUND)) |
| 155 .WillRepeatedly(Return(&fake_bg)); | 154 .WillRepeatedly(Return(&fake_bg)); |
| 156 | 155 |
| 157 [controller_.get() setThemeProvider:&provider]; | 156 [controller_.get() setThemeProvider:&provider]; |
| 158 [controller_.get() setCurrentTabContentsHeight:200]; | 157 [controller_.get() setCurrentTabContentsHeight:200]; |
| 159 | 158 |
| 160 [view_ display]; | 159 [view_ display]; |
| 161 } | 160 } |
| OLD | NEW |