summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/bookmarks/bookmark_bar_toolbar_view_unittest.mm
blob: aecdf84df5dda46fb63ca435b0e9cb208da879c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Cocoa/Cocoa.h>

#include "app/theme_provider.h"
#include "base/scoped_nsobject.h"
#import "chrome/browser/cocoa/bookmarks/bookmark_bar_controller.h"
#import "chrome/browser/cocoa/bookmarks/bookmark_bar_toolbar_view.h"
#import "chrome/browser/cocoa/cocoa_test_helper.h"
#include "chrome/browser/themes/browser_theme_provider.h"
#include "grit/theme_resources.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"

using ::testing::_;
using ::testing::DoAll;
using ::testing::Return;
using ::testing::SetArgumentPointee;

// When testing the floating drawing, we need to have a source of theme data.
class MockThemeProvider : public ThemeProvider {
 public:
  // Cross platform methods
  MOCK_METHOD1(Init, void(Profile*));
  MOCK_CONST_METHOD1(GetBitmapNamed, SkBitmap*(int));
  MOCK_CONST_METHOD1(GetColor, SkColor(int));
  MOCK_CONST_METHOD2(GetDisplayProperty, bool(int, int*));
  MOCK_CONST_METHOD0(ShouldUseNativeFrame, bool());
  MOCK_CONST_METHOD1(HasCustomImage, bool(int));
  MOCK_CONST_METHOD1(GetRawData, RefCountedMemory*(int));

  // OSX stuff
  MOCK_CONST_METHOD2(GetNSImageNamed, NSImage*(int, bool));
  MOCK_CONST_METHOD2(GetNSImageColorNamed, NSColor*(int, bool));
  MOCK_CONST_METHOD2(GetNSColor, NSColor*(int, bool));
  MOCK_CONST_METHOD2(GetNSColorTint, NSColor*(int, bool));
  MOCK_CONST_METHOD1(GetNSGradient, NSGradient*(int));
};

// Allows us to inject our fake controller below.
@interface BookmarkBarToolbarView (TestingAPI)
-(void)setController:(id<BookmarkBarToolbarViewController>)controller;
@end

@implementation BookmarkBarToolbarView (TestingAPI)
-(void)setController:(id<BookmarkBarToolbarViewController>)controller {
  controller_ = controller;
}
@end

// Allows us to control which way the view is rendered.
@interface DrawDetachedBarFakeController :
    NSObject<BookmarkBarState, BookmarkBarToolbarViewController> {
 @private
  int currentTabContentsHeight_;
  ThemeProvider* themeProvider_;
  bookmarks::VisualState visualState_;
}
@property (nonatomic, assign) int currentTabContentsHeight;
@property (nonatomic, assign) ThemeProvider* themeProvider;
@property (nonatomic, assign) bookmarks::VisualState visualState;

// |BookmarkBarState| protocol:
- (BOOL)isVisible;
- (BOOL)isAnimationRunning;
- (BOOL)isInState:(bookmarks::VisualState)state;
- (BOOL)isAnimatingToState:(bookmarks::VisualState)state;
- (BOOL)isAnimatingFromState:(bookmarks::VisualState)state;
- (BOOL)isAnimatingFromState:(bookmarks::VisualState)fromState
                     toState:(bookmarks::VisualState)toState;
- (BOOL)isAnimatingBetweenState:(bookmarks::VisualState)fromState
                       andState:(bookmarks::VisualState)toState;
- (CGFloat)detachedMorphProgress;
@end

@implementation DrawDetachedBarFakeController
@synthesize currentTabContentsHeight = currentTabContentsHeight_;
@synthesize themeProvider = themeProvider_;
@synthesize visualState = visualState_;

- (id)init {
  if ((self = [super init])) {
    [self setVisualState:bookmarks::kHiddenState];
  }
  return self;
}

- (BOOL)isVisible { return YES; }
- (BOOL)isAnimationRunning { return NO; }
- (BOOL)isInState:(bookmarks::VisualState)state
    { return ([self visualState] == state) ? YES : NO; }
- (BOOL)isAnimatingToState:(bookmarks::VisualState)state { return NO; }
- (BOOL)isAnimatingFromState:(bookmarks::VisualState)state { return NO; }
- (BOOL)isAnimatingFromState:(bookmarks::VisualState)fromState
                     toState:(bookmarks::VisualState)toState { return NO; }
- (BOOL)isAnimatingBetweenState:(bookmarks::VisualState)fromState
                       andState:(bookmarks::VisualState)toState { return NO; }
- (CGFloat)detachedMorphProgress { return 1; }
@end

class BookmarkBarToolbarViewTest : public CocoaTest {
 public:
  BookmarkBarToolbarViewTest() {
    controller_.reset([[DrawDetachedBarFakeController alloc] init]);
    NSRect frame = NSMakeRect(0, 0, 400, 40);
    scoped_nsobject<BookmarkBarToolbarView> view(
        [[BookmarkBarToolbarView alloc] initWithFrame:frame]);
    view_ = view.get();
    [[test_window() contentView] addSubview:view_];
    [view_ setController:controller_.get()];
  }

  scoped_nsobject<DrawDetachedBarFakeController> controller_;
  BookmarkBarToolbarView* view_;
};

TEST_VIEW(BookmarkBarToolbarViewTest, view_)

// Test drawing (part 1), mostly to ensure nothing leaks or crashes.
TEST_F(BookmarkBarToolbarViewTest, DisplayAsNormalBar) {
  [controller_.get() setVisualState:bookmarks::kShowingState];
  [view_ display];
}

// Test drawing (part 2), mostly to ensure nothing leaks or crashes.
TEST_F(BookmarkBarToolbarViewTest, DisplayAsDetachedBarWithNoImage) {
  [controller_.get() setVisualState:bookmarks::kDetachedState];

  // Tests where we don't have a background image, only a color.
  MockThemeProvider provider;
  EXPECT_CALL(provider, GetColor(BrowserThemeProvider::COLOR_NTP_BACKGROUND))
      .WillRepeatedly(Return(SK_ColorWHITE));
  EXPECT_CALL(provider, HasCustomImage(IDR_THEME_NTP_BACKGROUND))
      .WillRepeatedly(Return(false));
  [controller_.get() setThemeProvider:&provider];

  [view_ display];
}

// Actions used in DisplayAsDetachedBarWithBgImage.
ACTION(SetBackgroundTiling) {
  *arg1 = BrowserThemeProvider::NO_REPEAT;
  return true;
}

ACTION(SetAlignLeft) {
  *arg1 = BrowserThemeProvider::ALIGN_LEFT;
  return true;
}

// Test drawing (part 3), mostly to ensure nothing leaks or crashes.
TEST_F(BookmarkBarToolbarViewTest, DisplayAsDetachedBarWithBgImage) {
  [controller_.get() setVisualState:bookmarks::kDetachedState];

  // Tests where we have a background image, with positioning information.
  MockThemeProvider provider;

  // Advertise having an image.
  EXPECT_CALL(provider, GetColor(BrowserThemeProvider::COLOR_NTP_BACKGROUND))
      .WillRepeatedly(Return(SK_ColorRED));
  EXPECT_CALL(provider, HasCustomImage(IDR_THEME_NTP_BACKGROUND))
      .WillRepeatedly(Return(true));

  // Return the correct tiling/alignment information.
  EXPECT_CALL(provider,
      GetDisplayProperty(BrowserThemeProvider::NTP_BACKGROUND_TILING, _))
      .WillRepeatedly(SetBackgroundTiling());
  EXPECT_CALL(provider,
      GetDisplayProperty(BrowserThemeProvider::NTP_BACKGROUND_ALIGNMENT, _))
      .WillRepeatedly(SetAlignLeft());

  // Create a dummy bitmap full of not-red to blit with.
  SkBitmap fake_bg;
  fake_bg.setConfig(SkBitmap::kARGB_8888_Config, 800, 800);
  fake_bg.allocPixels();
  fake_bg.eraseColor(SK_ColorGREEN);
  EXPECT_CALL(provider, GetBitmapNamed(IDR_THEME_NTP_BACKGROUND))
      .WillRepeatedly(Return(&fake_bg));

  [controller_.get() setThemeProvider:&provider];
  [controller_.get() setCurrentTabContentsHeight:200];

  [view_ display];
}

// TODO(viettrungluu): write more unit tests, especially after my refactoring.