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
|
// Copyright 2014 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/mac/scoped_nsobject.h"
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.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"
#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::ReturnRefOfCopy;
@interface DownloadItemController (DownloadItemControllerTest)
- (void)verifyProgressViewIsVisible:(bool)visible;
- (void)verifyDangerousDownloadPromptIsVisible:(bool)visible;
@end
@implementation DownloadItemController (DownloadItemControllerTest)
- (void)verifyProgressViewIsVisible:(bool)visible {
EXPECT_EQ(visible, ![progressView_ isHidden]);
}
- (void)verifyDangerousDownloadPromptIsVisible:(bool)visible {
EXPECT_EQ(visible, ![dangerousDownloadView_ isHidden]);
}
@end
@interface DownloadItemControllerWithInitCallback : DownloadItemController {
@private
base::Closure initCallback_;
}
- (id)initWithDownload:(content::DownloadItem*)downloadItem
shelf:(DownloadShelfController*)shelf
initCallback:(const base::Closure&)callback;
- (void)awakeFromNib;
@end
@implementation DownloadItemControllerWithInitCallback
- (id)initWithDownload:(content::DownloadItem*)downloadItem
shelf:(DownloadShelfController*)shelf
initCallback:(const base::Closure&)callback {
if ((self = [super initWithDownload:downloadItem shelf:shelf navigator:NULL]))
initCallback_ = callback;
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
initCallback_.Run();
}
@end
namespace {
class DownloadItemControllerTest : public CocoaProfileTest {
public:
virtual void SetUp() OVERRIDE {
CocoaProfileTest::SetUp();
ASSERT_TRUE(browser());
id shelf_controller =
[OCMockObject mockForClass:[DownloadShelfController class]];
shelf_.reset([shelf_controller retain]);
download_item_.reset(new ::testing::NiceMock<content::MockDownloadItem>);
ON_CALL(*download_item_, GetState())
.WillByDefault(Return(content::DownloadItem::IN_PROGRESS));
ON_CALL(*download_item_, GetFileNameToReportUser())
.WillByDefault(Return(base::FilePath()));
ON_CALL(*download_item_, GetDangerType())
.WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
ON_CALL(*download_item_, GetTargetFilePath())
.WillByDefault(ReturnRefOfCopy(base::FilePath()));
ON_CALL(*download_item_, GetLastReason())
.WillByDefault(Return(content::DOWNLOAD_INTERRUPT_REASON_NONE));
ON_CALL(*download_item_, GetURL()).WillByDefault(ReturnRefOfCopy(GURL()));
ON_CALL(*download_item_, GetReferrerUrl())
.WillByDefault(ReturnRefOfCopy(GURL()));
ON_CALL(*download_item_, GetTargetDisposition()).WillByDefault(
Return(content::DownloadItem::TARGET_DISPOSITION_OVERWRITE));
}
virtual void TearDown() OVERRIDE {
download_item_.reset();
[(id)shelf_ verify];
CocoaProfileTest::TearDown();
}
DownloadItemController* CreateItemController() {
base::RunLoop run_loop;
base::scoped_nsobject<DownloadItemController> item(
[[DownloadItemControllerWithInitCallback alloc]
initWithDownload:download_item_.get()
shelf:shelf_.get()
initCallback:run_loop.QuitClosure()]);
[[test_window() contentView] addSubview:[item view]];
run_loop.Run();
return item.release();
}
protected:
scoped_ptr<content::MockDownloadItem> download_item_;
base::scoped_nsobject<DownloadShelfController> shelf_;
};
TEST_F(DownloadItemControllerTest, ShelfNotifiedOfOpenedDownload) {
base::scoped_nsobject<DownloadItemController> item(CreateItemController());
[[(id)shelf_ expect] downloadWasOpened:[OCMArg any]];
download_item_->NotifyObserversDownloadOpened();
}
TEST_F(DownloadItemControllerTest, RemovesSelfWhenDownloadIsDestroyed) {
base::scoped_nsobject<DownloadItemController> item(CreateItemController());
[[(id)shelf_ expect] remove:[OCMArg any]];
download_item_.reset();
}
TEST_F(DownloadItemControllerTest, NormalDownload) {
base::scoped_nsobject<DownloadItemController> item(CreateItemController());
[item verifyProgressViewIsVisible:true];
[item verifyDangerousDownloadPromptIsVisible:false];
}
TEST_F(DownloadItemControllerTest, DangerousDownload) {
[[(id)shelf_ expect] layoutItems];
ON_CALL(*download_item_, GetDangerType())
.WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(true));
base::scoped_nsobject<DownloadItemController> item(CreateItemController());
[item verifyProgressViewIsVisible:false];
[item verifyDangerousDownloadPromptIsVisible:true];
}
TEST_F(DownloadItemControllerTest, NormalDownloadBecomesDangerous) {
base::scoped_nsobject<DownloadItemController> item(CreateItemController());
[item verifyProgressViewIsVisible:true];
[item verifyDangerousDownloadPromptIsVisible:false];
// The download is now marked as dangerous.
[[(id)shelf_ expect] layoutItems];
ON_CALL(*download_item_, GetDangerType())
.WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(true));
download_item_->NotifyObserversDownloadUpdated();
[item verifyProgressViewIsVisible:false];
[item verifyDangerousDownloadPromptIsVisible:true];
[(id)shelf_ verify];
// And then marked as safe again.
[[(id)shelf_ expect] layoutItems];
ON_CALL(*download_item_, GetDangerType())
.WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(false));
download_item_->NotifyObserversDownloadUpdated();
[item verifyProgressViewIsVisible:true];
[item verifyDangerousDownloadPromptIsVisible:false];
[(id)shelf_ verify];
}
} // namespace
|