- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / bookmarks / bookmark_bar_toolbar_view_unittest.mm
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.
4
5 #import <Cocoa/Cocoa.h>
6
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"
21
22 using ::testing::_;
23 using ::testing::DoAll;
24 using ::testing::NiceMock;
25 using ::testing::Return;
26 using ::testing::SetArgumentPointee;
27
28 // Allows us to inject our fake controller below.
29 @interface BookmarkBarToolbarView (TestingAPI)
30 -(void)setController:(id<BookmarkBarToolbarViewController>)controller;
31 @end
32
33 @implementation BookmarkBarToolbarView (TestingAPI)
34 -(void)setController:(id<BookmarkBarToolbarViewController>)controller {
35   controller_ = controller;
36 }
37 @end
38
39 // Allows us to control which way the view is rendered.
40 @interface DrawDetachedBarFakeController :
41     NSObject<BookmarkBarState, BookmarkBarToolbarViewController> {
42  @private
43   int currentTabContentsHeight_;
44   ThemeService* themeService_;
45   BookmarkBar::State state_;
46   BOOL isEmpty_;
47 }
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;
52
53 // |BookmarkBarState| protocol:
54 - (BOOL)isVisible;
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;
64 @end
65
66 @implementation DrawDetachedBarFakeController
67 @synthesize currentTabContentsHeight = currentTabContentsHeight_;
68 @synthesize themeService = themeService_;
69 @synthesize state = state_;
70 @synthesize isEmpty = isEmpty_;
71
72 - (id)init {
73   if ((self = [super init])) {
74     [self setState:BookmarkBar::HIDDEN];
75   }
76   return self;
77 }
78
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; }
90 @end
91
92 class BookmarkBarToolbarViewTest : public CocoaTest {
93  public:
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]);
99     view_ = view.get();
100     [[test_window() contentView] addSubview:view_];
101     [view_ setController:controller_.get()];
102   }
103
104   base::scoped_nsobject<DrawDetachedBarFakeController> controller_;
105   BookmarkBarToolbarView* view_;
106 };
107
108 TEST_VIEW(BookmarkBarToolbarViewTest, view_)
109
110 // Test drawing, mostly to ensure nothing leaks or crashes.
111 TEST_F(BookmarkBarToolbarViewTest, DisplayAsNormalBar) {
112   [controller_.get() setState:BookmarkBar::SHOW];
113   [view_ display];
114 }
115
116 // Actions used in DisplayAsDetachedBarWithBgImage.
117 ACTION(SetBackgroundTiling) {
118   *arg1 = ThemeProperties::NO_REPEAT;
119   return true;
120 }
121
122 ACTION(SetAlignLeft) {
123   *arg1 = ThemeProperties::ALIGN_LEFT;
124   return true;
125 }
126
127 // TODO(viettrungluu): write more unit tests, especially after my refactoring.