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 "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/platform_test.h"
16 #include "third_party/skia/include/core/SkBitmap.h"
17 #include "third_party/skia/include/core/SkColor.h"
18 #include "ui/base/theme_provider.h"
19 #include "ui/gfx/image/image_skia.h"
22 using ::testing::DoAll;
23 using ::testing::NiceMock;
24 using ::testing::Return;
25 using ::testing::SetArgumentPointee;
27 // Allows us to inject our fake controller below.
28 @interface BookmarkBarToolbarView (TestingAPI)
29 -(void)setController:(id<BookmarkBarToolbarViewController>)controller;
32 @implementation BookmarkBarToolbarView (TestingAPI)
33 -(void)setController:(id<BookmarkBarToolbarViewController>)controller {
34 controller_ = controller;
38 // Allows us to control which way the view is rendered.
39 @interface DrawDetachedBarFakeController :
40 NSObject<BookmarkBarState, BookmarkBarToolbarViewController> {
42 int currentTabContentsHeight_;
43 ThemeService* themeService_;
44 BookmarkBar::State state_;
47 @property (nonatomic, assign) int currentTabContentsHeight;
48 @property (nonatomic, assign) ThemeService* themeService;
49 @property (nonatomic, assign) BookmarkBar::State state;
50 @property (nonatomic, assign) BOOL isEmpty;
52 // |BookmarkBarState| protocol:
54 - (BOOL)isAnimationRunning;
55 - (BOOL)isInState:(BookmarkBar::State)state;
56 - (BOOL)isAnimatingToState:(BookmarkBar::State)state;
57 - (BOOL)isAnimatingFromState:(BookmarkBar::State)state;
58 - (BOOL)isAnimatingFromState:(BookmarkBar::State)fromState
59 toState:(BookmarkBar::State)toState;
60 - (BOOL)isAnimatingBetweenState:(BookmarkBar::State)fromState
61 andState:(BookmarkBar::State)toState;
62 - (CGFloat)detachedMorphProgress;
65 @implementation DrawDetachedBarFakeController
66 @synthesize currentTabContentsHeight = currentTabContentsHeight_;
67 @synthesize themeService = themeService_;
68 @synthesize state = state_;
69 @synthesize isEmpty = isEmpty_;
72 if ((self = [super init])) {
73 [self setState:BookmarkBar::HIDDEN];
78 - (BOOL)isVisible { return YES; }
79 - (BOOL)isAnimationRunning { return NO; }
80 - (BOOL)isInState:(BookmarkBar::State)state
81 { return ([self state] == state) ? YES : NO; }
82 - (BOOL)isAnimatingToState:(BookmarkBar::State)state { return NO; }
83 - (BOOL)isAnimatingFromState:(BookmarkBar::State)state { return NO; }
84 - (BOOL)isAnimatingFromState:(BookmarkBar::State)fromState
85 toState:(BookmarkBar::State)toState { return NO; }
86 - (BOOL)isAnimatingBetweenState:(BookmarkBar::State)fromState
87 andState:(BookmarkBar::State)toState { return NO; }
88 - (CGFloat)detachedMorphProgress { return 1; }
91 class BookmarkBarToolbarViewTest : public CocoaTest {
93 BookmarkBarToolbarViewTest() {
94 controller_.reset([[DrawDetachedBarFakeController alloc] init]);
95 NSRect frame = NSMakeRect(0, 0, 400, 40);
96 base::scoped_nsobject<BookmarkBarToolbarView> view(
97 [[BookmarkBarToolbarView alloc] initWithFrame:frame]);
99 [[test_window() contentView] addSubview:view_];
100 [view_ setController:controller_.get()];
103 base::scoped_nsobject<DrawDetachedBarFakeController> controller_;
104 BookmarkBarToolbarView* view_;
107 TEST_VIEW(BookmarkBarToolbarViewTest, view_)
109 // Test drawing, mostly to ensure nothing leaks or crashes.
110 TEST_F(BookmarkBarToolbarViewTest, DisplayAsNormalBar) {
111 [controller_.get() setState:BookmarkBar::SHOW];
115 // Actions used in DisplayAsDetachedBarWithBgImage.
116 ACTION(SetBackgroundTiling) {
117 *arg1 = ThemeProperties::NO_REPEAT;
121 ACTION(SetAlignLeft) {
122 *arg1 = ThemeProperties::ALIGN_LEFT;
126 // TODO(viettrungluu): write more unit tests, especially after my refactoring.