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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
// Copyright 2013 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>
#import "base/memory/scoped_nsobject.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/download/download_shelf.h"
#include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
#import "chrome/browser/ui/cocoa/download/download_item_controller.h"
#import "chrome/browser/ui/cocoa/download/download_shelf_controller.h"
#import "chrome/browser/ui/cocoa/view_resizer_pong.h"
#include "content/public/test/mock_download_item.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#import "third_party/ocmock/gtest_support.h"
using ::testing::Return;
using ::testing::AnyNumber;
// Wraps a content::MockDownloadItem so it can be retained by the mock
// DownloadItemController.
@interface WrappedMockDownloadItem : NSObject {
@private
scoped_ptr<content::MockDownloadItem> download_;
}
- (id)initWithMockDownload:(scoped_ptr<content::MockDownloadItem>)download;
- (content::DownloadItem*)download;
- (content::MockDownloadItem*)mockDownload;
@end
@implementation WrappedMockDownloadItem
- (id)initWithMockDownload:(scoped_ptr<content::MockDownloadItem>)download {
if ((self = [super init])) {
download_ = download.Pass();
}
return self;
}
- (content::DownloadItem*)download {
return download_.get();
}
- (content::MockDownloadItem*)mockDownload {
return download_.get();
}
@end
// Test method for accessing the wrapped MockDownloadItem.
@interface DownloadItemController (DownloadShelfControllerTest) {
}
- (WrappedMockDownloadItem*)wrappedMockDownload;
@end
@implementation DownloadItemController (DownloadShelfControllerTest)
- (WrappedMockDownloadItem*)wrappedMockDownload {
return nil;
}
@end
// Subclass of the DownloadShelfController to override scheduleAutoClose and
// cancelAutoClose. During regular operation, a scheduled autoClose waits for 5
// seconds before closing the shelf (unless it is cancelled during this
// time). For testing purposes, we count the number of invocations of
// {schedule,cancel}AutoClose instead of actually scheduling and cancelling.
@interface CountingDownloadShelfController : DownloadShelfController {
@public
int scheduleAutoCloseCount_;
int cancelAutoCloseCount_;
}
@end
@implementation CountingDownloadShelfController
-(void)scheduleAutoClose {
++scheduleAutoCloseCount_;
}
-(void)cancelAutoClose {
++cancelAutoCloseCount_;
}
@end
namespace {
class DownloadShelfControllerTest : public CocoaProfileTest {
public:
virtual void SetUp() OVERRIDE {
CocoaProfileTest::SetUp();
ASSERT_TRUE(browser());
resize_delegate_.reset([[ViewResizerPong alloc] init]);
shelf_.reset([[CountingDownloadShelfController alloc]
initWithBrowser:browser()
resizeDelegate:resize_delegate_.get()]);
EXPECT_TRUE([shelf_ view]);
[[test_window() contentView] addSubview:[shelf_ view]];
}
virtual void TearDown() OVERRIDE {
if (shelf_.get()) {
[shelf_ exiting];
shelf_.reset();
}
CocoaProfileTest::TearDown();
}
protected:
id CreateItemController();
scoped_nsobject<CountingDownloadShelfController> shelf_;
scoped_nsobject<ViewResizerPong> resize_delegate_;
};
id DownloadShelfControllerTest::CreateItemController() {
scoped_ptr<content::MockDownloadItem> download(
new ::testing::NiceMock<content::MockDownloadItem>);
ON_CALL(*download.get(), GetOpened())
.WillByDefault(Return(false));
ON_CALL(*download.get(), IsInProgress())
.WillByDefault(Return(true));
scoped_nsobject<WrappedMockDownloadItem> wrappedMockDownload(
[[WrappedMockDownloadItem alloc] initWithMockDownload:download.Pass()]);
id item_controller =
[OCMockObject mockForClass:[DownloadItemController class]];
scoped_nsobject<NSView> view([[NSView alloc] initWithFrame:NSZeroRect]);
[[[item_controller stub] andCall:@selector(download)
onObject:wrappedMockDownload.get()] download];
[[item_controller stub] updateVisibility:[OCMArg any]];
[[[item_controller stub]
andReturnValue:[NSValue valueWithSize:NSMakeSize(10,10)]] preferredSize];
[[[item_controller stub] andReturn:view.get()] view];
[[[item_controller stub]
andReturn:wrappedMockDownload.get()] wrappedMockDownload];
return [item_controller retain];
}
TEST_VIEW(DownloadShelfControllerTest, [shelf_ view]);
// Removing the last download from the shelf should cause it to close
// immediately.
TEST_F(DownloadShelfControllerTest, AddAndRemoveDownload) {
scoped_nsobject<DownloadItemController> item(CreateItemController());
[shelf_ showDownloadShelf:YES
isUserAction:NO];
EXPECT_TRUE([shelf_ isVisible]);
EXPECT_TRUE([shelf_ bridge]->IsShowing());
[shelf_ add:item];
[shelf_ remove:item];
EXPECT_FALSE([shelf_ isVisible]);
EXPECT_FALSE([shelf_ bridge]->IsShowing());
// The shelf should be closed without scheduling an autoClose.
EXPECT_EQ(0, shelf_.get()->scheduleAutoCloseCount_);
}
// Test that the shelf doesn't close automatically after a removal if there are
// active download items still on the shelf.
TEST_F(DownloadShelfControllerTest, AddAndRemoveWithActiveItem) {
scoped_nsobject<DownloadItemController> item1(CreateItemController());
scoped_nsobject<DownloadItemController> item2(CreateItemController());
[shelf_ showDownloadShelf:YES
isUserAction:NO];
EXPECT_TRUE([shelf_ isVisible]);
[shelf_ add:item1.get()];
[shelf_ add:item2.get()];
[shelf_ remove:item1.get()];
EXPECT_TRUE([shelf_ isVisible]);
[shelf_ remove:item2.get()];
EXPECT_FALSE([shelf_ isVisible]);
EXPECT_EQ(0, shelf_.get()->scheduleAutoCloseCount_);
}
// DownloadShelf::Unhide() should cause the shelf to be displayed if there are
// active downloads on it.
TEST_F(DownloadShelfControllerTest, HideAndUnhide) {
scoped_nsobject<DownloadItemController> item(CreateItemController());
[shelf_ showDownloadShelf:YES
isUserAction:NO];
EXPECT_TRUE([shelf_ isVisible]);
[shelf_ add:item.get()];
[shelf_ bridge]->Hide();
EXPECT_FALSE([shelf_ isVisible]);
[shelf_ bridge]->Unhide();
EXPECT_TRUE([shelf_ isVisible]);
[shelf_ remove:item.get()];
EXPECT_FALSE([shelf_ isVisible]);
}
// DownloadShelf::Unhide() shouldn't cause the shelf to be displayed if all
// active downloads are removed from the shelf while the shelf was hidden.
TEST_F(DownloadShelfControllerTest, HideAutocloseUnhide) {
scoped_nsobject<DownloadItemController> item(CreateItemController());
[shelf_ showDownloadShelf:YES
isUserAction:NO];
EXPECT_TRUE([shelf_ isVisible]);
[shelf_ add:item.get()];
[shelf_ bridge]->Hide();
EXPECT_FALSE([shelf_ isVisible]);
[shelf_ remove:item.get()];
EXPECT_FALSE([shelf_ isVisible]);
[shelf_ bridge]->Unhide();
EXPECT_FALSE([shelf_ isVisible]);
}
// Test of autoclosing behavior after opening a download item. The mouse is on
// the download shelf at the time the autoclose is scheduled.
TEST_F(DownloadShelfControllerTest, AutoCloseAfterOpenWithMouseInShelf) {
scoped_nsobject<DownloadItemController> item(CreateItemController());
[shelf_ showDownloadShelf:YES
isUserAction:NO];
EXPECT_TRUE([shelf_ isVisible]);
[shelf_ add:item.get()];
// Expect 2 cancelAutoClose calls: From the showDownloadShelf: call and the
// add: call.
EXPECT_EQ(2, shelf_.get()->cancelAutoCloseCount_);
shelf_.get()->cancelAutoCloseCount_ = 0;
// The mouse enters the shelf.
[shelf_ mouseEntered:nil];
EXPECT_EQ(0, shelf_.get()->scheduleAutoCloseCount_);
// The download opens.
EXPECT_CALL(*[[item wrappedMockDownload] mockDownload], GetOpened())
.WillRepeatedly(Return(true));
[shelf_ downloadWasOpened:item.get()];
// The shelf should now be waiting for the mouse to exit.
EXPECT_TRUE([shelf_ isVisible]);
EXPECT_EQ(0, shelf_.get()->scheduleAutoCloseCount_);
EXPECT_EQ(0, shelf_.get()->cancelAutoCloseCount_);
// The mouse exits the shelf. autoClose should be scheduled now.
[shelf_ mouseExited:nil];
EXPECT_EQ(1, shelf_.get()->scheduleAutoCloseCount_);
EXPECT_EQ(0, shelf_.get()->cancelAutoCloseCount_);
// The mouse enters the shelf again. The autoClose should be cancelled.
[shelf_ mouseEntered:nil];
EXPECT_EQ(1, shelf_.get()->scheduleAutoCloseCount_);
EXPECT_EQ(1, shelf_.get()->cancelAutoCloseCount_);
}
// Test of autoclosing behavior after opening a download item.
TEST_F(DownloadShelfControllerTest, AutoCloseAfterOpenWithMouseOffShelf) {
scoped_nsobject<DownloadItemController> item(CreateItemController());
[shelf_ showDownloadShelf:YES
isUserAction:NO];
EXPECT_TRUE([shelf_ isVisible]);
[shelf_ add:item.get()];
// The download is opened.
EXPECT_CALL(*[[item wrappedMockDownload] mockDownload], GetOpened())
.WillRepeatedly(Return(true));
[shelf_ downloadWasOpened:item.get()];
// The shelf should be closed immediately since the mouse is not over the
// shelf.
EXPECT_FALSE([shelf_ isVisible]);
}
// Test that if the shelf is closed while an autoClose is pending, the pending
// autoClose is cancelled.
TEST_F(DownloadShelfControllerTest, CloseWithPendingAutoClose) {
scoped_nsobject<DownloadItemController> item(CreateItemController());
[shelf_ showDownloadShelf:YES
isUserAction:NO];
EXPECT_TRUE([shelf_ isVisible]);
[shelf_ add:item.get()];
// Expect 2 cancelAutoClose calls: From the showDownloadShelf: call and the
// add: call.
EXPECT_EQ(2, shelf_.get()->cancelAutoCloseCount_);
shelf_.get()->cancelAutoCloseCount_ = 0;
// The mouse enters the shelf.
[shelf_ mouseEntered:nil];
EXPECT_EQ(0, shelf_.get()->scheduleAutoCloseCount_);
// The download opens.
EXPECT_CALL(*[[item wrappedMockDownload] mockDownload], GetOpened())
.WillRepeatedly(Return(true));
[shelf_ downloadWasOpened:item.get()];
// The shelf should now be waiting for the mouse to exit. autoClose should not
// be scheduled yet.
EXPECT_TRUE([shelf_ isVisible]);
EXPECT_EQ(0, shelf_.get()->scheduleAutoCloseCount_);
EXPECT_EQ(0, shelf_.get()->cancelAutoCloseCount_);
// The mouse exits the shelf. autoClose should be scheduled now.
[shelf_ mouseExited:nil];
EXPECT_EQ(1, shelf_.get()->scheduleAutoCloseCount_);
EXPECT_EQ(0, shelf_.get()->cancelAutoCloseCount_);
// Remove the download item. This should cause the download shelf to be hidden
// immediately. The pending autoClose should be cancelled.
[shelf_ remove:item];
EXPECT_EQ(1, shelf_.get()->scheduleAutoCloseCount_);
EXPECT_EQ(1, shelf_.get()->cancelAutoCloseCount_);
EXPECT_FALSE([shelf_ isVisible]);
}
// That that the shelf cancels a pending autoClose if a new download item is
// added to it.
TEST_F(DownloadShelfControllerTest, AddItemWithPendingAutoClose) {
scoped_nsobject<DownloadItemController> item(CreateItemController());
[shelf_ showDownloadShelf:YES
isUserAction:NO];
EXPECT_TRUE([shelf_ isVisible]);
[shelf_ add:item.get()];
// Expect 2 cancelAutoClose calls: From the showDownloadShelf: call and the
// add: call.
EXPECT_EQ(2, shelf_.get()->cancelAutoCloseCount_);
shelf_.get()->cancelAutoCloseCount_ = 0;
// The mouse enters the shelf.
[shelf_ mouseEntered:nil];
EXPECT_EQ(0, shelf_.get()->scheduleAutoCloseCount_);
// The download opens.
EXPECT_CALL(*[[item wrappedMockDownload] mockDownload], GetOpened())
.WillRepeatedly(Return(true));
[shelf_ downloadWasOpened:item.get()];
// The shelf should now be waiting for the mouse to exit. autoClose should not
// be scheduled yet.
EXPECT_TRUE([shelf_ isVisible]);
EXPECT_EQ(0, shelf_.get()->scheduleAutoCloseCount_);
EXPECT_EQ(0, shelf_.get()->cancelAutoCloseCount_);
// The mouse exits the shelf. autoClose should be scheduled now.
[shelf_ mouseExited:nil];
EXPECT_EQ(1, shelf_.get()->scheduleAutoCloseCount_);
EXPECT_EQ(0, shelf_.get()->cancelAutoCloseCount_);
// Add a new download item. The pending autoClose should be cancelled.
scoped_nsobject<DownloadItemController> item2(CreateItemController());
[shelf_ add:item.get()];
EXPECT_EQ(1, shelf_.get()->scheduleAutoCloseCount_);
EXPECT_EQ(1, shelf_.get()->cancelAutoCloseCount_);
EXPECT_TRUE([shelf_ isVisible]);
}
// Test that pending autoClose calls are cancelled when exiting.
TEST_F(DownloadShelfControllerTest, CancelAutoCloseOnExit) {
scoped_nsobject<DownloadItemController> item(CreateItemController());
[shelf_ showDownloadShelf:YES
isUserAction:NO];
EXPECT_TRUE([shelf_ isVisible]);
[shelf_ add:item.get()];
EXPECT_EQ(0, shelf_.get()->scheduleAutoCloseCount_);
EXPECT_EQ(2, shelf_.get()->cancelAutoCloseCount_);
[shelf_ exiting];
EXPECT_EQ(0, shelf_.get()->scheduleAutoCloseCount_);
EXPECT_EQ(3, shelf_.get()->cancelAutoCloseCount_);
shelf_.reset();
}
} // namespace
|