1 // Copyright (c) 2012 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 <Cocoa/Cocoa.h>
7 #include "base/mac/scoped_nsobject.h"
8 #include "chrome/browser/themes/theme_properties.h"
9 #include "chrome/browser/themes/theme_service.h"
10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h"
11 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_toolbar_view.h"
12 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
13 #include "grit/theme_resources.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/platform_test.h"
17 #include "third_party/skia/include/core/SkBitmap.h"
18 #include "third_party/skia/include/core/SkColor.h"
19 #include "ui/base/theme_provider.h"
20 #include "ui/gfx/image/image_skia.h"
23 using ::testing::DoAll;
24 using ::testing::NiceMock;
25 using ::testing::Return;
26 using ::testing::SetArgumentPointee;
28 // Allows us to inject our fake controller below.
29 @interface BookmarkBarToolbarView (TestingAPI)
30 -(void)setController:(id<BookmarkBarToolbarViewController>)controller;
33 @implementation BookmarkBarToolbarView (TestingAPI)
34 -(void)setController:(id<BookmarkBarToolbarViewController>)controller {
35 controller_ = controller;
39 // Allows us to control which way the view is rendered.
40 @interface DrawDetachedBarFakeController :
41 NSObject<BookmarkBarState, BookmarkBarToolbarViewController> {
43 int currentTabContentsHeight_;
44 ThemeService* themeService_;
45 BookmarkBar::State state_;
48 @property (nonatomic, assign) int currentTabContentsHeight;
49 @property (nonatomic, assign) ThemeService* themeService;
50 @property (nonatomic, assign) BookmarkBar::State state;
51 @property (nonatomic, assign) BOOL isEmpty;
53 // |BookmarkBarState| protocol:
55 - (BOOL)isAnimationRunning;
56 - (BOOL)isInState:(BookmarkBar::State)state;
57 - (BOOL)isAnimatingToState:(BookmarkBar::State)state;
58 - (BOOL)isAnimatingFromState:(BookmarkBar::State)state;
59 - (BOOL)isAnimatingFromState:(BookmarkBar::State)fromState
60 toState:(BookmarkBar::State)toState;
61 - (BOOL)isAnimatingBetweenState:(BookmarkBar::State)fromState
62 andState:(BookmarkBar::State)toState;
63 - (CGFloat)detachedMorphProgress;
66 @implementation DrawDetachedBarFakeController
67 @synthesize currentTabContentsHeight = currentTabContentsHeight_;
68 @synthesize themeService = themeService_;
69 @synthesize state = state_;
70 @synthesize isEmpty = isEmpty_;
73 if ((self = [super init])) {
74 [self setState:BookmarkBar::HIDDEN];
79 - (BOOL)isVisible { return YES; }
80 - (BOOL)isAnimationRunning { return NO; }
81 - (BOOL)isInState:(BookmarkBar::State)state
82 { return ([self state] == state) ? YES : NO; }
83 - (BOOL)isAnimatingToState:(BookmarkBar::State)state { return NO; }
84 - (BOOL)isAnimatingFromState:(BookmarkBar::State)state { return NO; }
85 - (BOOL)isAnimatingFromState:(BookmarkBar::State)fromState
86 toState:(BookmarkBar::State)toState { return NO; }
87 - (BOOL)isAnimatingBetweenState:(BookmarkBar::State)fromState
88 andState:(BookmarkBar::State)toState { return NO; }
89 - (CGFloat)detachedMorphProgress { return 1; }
92 class BookmarkBarToolbarViewTest : public CocoaTest {
94 BookmarkBarToolbarViewTest() {
95 controller_.reset([[DrawDetachedBarFakeController alloc] init]);
96 NSRect frame = NSMakeRect(0, 0, 400, 40);
97 base::scoped_nsobject<BookmarkBarToolbarView> view(
98 [[BookmarkBarToolbarView alloc] initWithFrame:frame]);
100 [[test_window() contentView] addSubview:view_];
101 [view_ setController:controller_.get()];
104 base::scoped_nsobject<DrawDetachedBarFakeController> controller_;
105 BookmarkBarToolbarView* view_;
108 TEST_VIEW(BookmarkBarToolbarViewTest, view_)
110 // Test drawing, mostly to ensure nothing leaks or crashes.
111 TEST_F(BookmarkBarToolbarViewTest, DisplayAsNormalBar) {
112 [controller_.get() setState:BookmarkBar::SHOW];
116 // Actions used in DisplayAsDetachedBarWithBgImage.
117 ACTION(SetBackgroundTiling) {
118 *arg1 = ThemeProperties::NO_REPEAT;
122 ACTION(SetAlignLeft) {
123 *arg1 = ThemeProperties::ALIGN_LEFT;
127 // TODO(viettrungluu): write more unit tests, especially after my refactoring.