1 // Copyright 2014 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 #import "base/mac/scoped_nsobject.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/run_loop.h"
10 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
11 #import "chrome/browser/ui/cocoa/download/download_item_controller.h"
12 #import "chrome/browser/ui/cocoa/download/download_shelf_controller.h"
13 #include "content/public/test/mock_download_item.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/platform_test.h"
16 #import "third_party/ocmock/OCMock/OCMock.h"
17 #import "third_party/ocmock/gtest_support.h"
19 using ::testing::Return;
20 using ::testing::ReturnRefOfCopy;
22 @interface DownloadItemController (DownloadItemControllerTest)
23 - (void)verifyProgressViewIsVisible:(bool)visible;
24 - (void)verifyDangerousDownloadPromptIsVisible:(bool)visible;
27 @implementation DownloadItemController (DownloadItemControllerTest)
28 - (void)verifyProgressViewIsVisible:(bool)visible {
29 EXPECT_EQ(visible, ![progressView_ isHidden]);
32 - (void)verifyDangerousDownloadPromptIsVisible:(bool)visible {
33 EXPECT_EQ(visible, ![dangerousDownloadView_ isHidden]);
37 @interface DownloadItemControllerWithInitCallback : DownloadItemController {
39 base::Closure initCallback_;
41 - (id)initWithDownload:(content::DownloadItem*)downloadItem
42 shelf:(DownloadShelfController*)shelf
43 initCallback:(const base::Closure&)callback;
47 @implementation DownloadItemControllerWithInitCallback
49 - (id)initWithDownload:(content::DownloadItem*)downloadItem
50 shelf:(DownloadShelfController*)shelf
51 initCallback:(const base::Closure&)callback {
52 if ((self = [super initWithDownload:downloadItem shelf:shelf navigator:NULL]))
53 initCallback_ = callback;
57 - (void)awakeFromNib {
65 class DownloadItemControllerTest : public CocoaProfileTest {
67 virtual void SetUp() OVERRIDE {
68 CocoaProfileTest::SetUp();
69 ASSERT_TRUE(browser());
72 [OCMockObject mockForClass:[DownloadShelfController class]];
73 shelf_.reset([shelf_controller retain]);
75 download_item_.reset(new ::testing::NiceMock<content::MockDownloadItem>);
76 ON_CALL(*download_item_, GetState())
77 .WillByDefault(Return(content::DownloadItem::IN_PROGRESS));
78 ON_CALL(*download_item_, GetFileNameToReportUser())
79 .WillByDefault(Return(base::FilePath()));
80 ON_CALL(*download_item_, GetDangerType())
81 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
82 ON_CALL(*download_item_, GetTargetFilePath())
83 .WillByDefault(ReturnRefOfCopy(base::FilePath()));
84 ON_CALL(*download_item_, GetLastReason())
85 .WillByDefault(Return(content::DOWNLOAD_INTERRUPT_REASON_NONE));
86 ON_CALL(*download_item_, GetURL()).WillByDefault(ReturnRefOfCopy(GURL()));
87 ON_CALL(*download_item_, GetReferrerUrl())
88 .WillByDefault(ReturnRefOfCopy(GURL()));
89 ON_CALL(*download_item_, GetTargetDisposition()).WillByDefault(
90 Return(content::DownloadItem::TARGET_DISPOSITION_OVERWRITE));
93 virtual void TearDown() OVERRIDE {
94 download_item_.reset();
96 CocoaProfileTest::TearDown();
99 DownloadItemController* CreateItemController() {
100 base::RunLoop run_loop;
101 base::scoped_nsobject<DownloadItemController> item(
102 [[DownloadItemControllerWithInitCallback alloc]
103 initWithDownload:download_item_.get()
105 initCallback:run_loop.QuitClosure()]);
107 [[test_window() contentView] addSubview:[item view]];
109 return item.release();
113 scoped_ptr<content::MockDownloadItem> download_item_;
114 base::scoped_nsobject<DownloadShelfController> shelf_;
117 TEST_F(DownloadItemControllerTest, ShelfNotifiedOfOpenedDownload) {
118 base::scoped_nsobject<DownloadItemController> item(CreateItemController());
119 [[(id)shelf_ expect] downloadWasOpened:[OCMArg any]];
120 download_item_->NotifyObserversDownloadOpened();
123 TEST_F(DownloadItemControllerTest, RemovesSelfWhenDownloadIsDestroyed) {
124 base::scoped_nsobject<DownloadItemController> item(CreateItemController());
125 [[(id)shelf_ expect] remove:[OCMArg any]];
126 download_item_.reset();
129 TEST_F(DownloadItemControllerTest, NormalDownload) {
130 base::scoped_nsobject<DownloadItemController> item(CreateItemController());
132 [item verifyProgressViewIsVisible:true];
133 [item verifyDangerousDownloadPromptIsVisible:false];
136 TEST_F(DownloadItemControllerTest, DangerousDownload) {
137 [[(id)shelf_ expect] layoutItems];
138 ON_CALL(*download_item_, GetDangerType())
139 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
140 ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(true));
141 base::scoped_nsobject<DownloadItemController> item(CreateItemController());
143 [item verifyProgressViewIsVisible:false];
144 [item verifyDangerousDownloadPromptIsVisible:true];
147 TEST_F(DownloadItemControllerTest, NormalDownloadBecomesDangerous) {
148 base::scoped_nsobject<DownloadItemController> item(CreateItemController());
150 [item verifyProgressViewIsVisible:true];
151 [item verifyDangerousDownloadPromptIsVisible:false];
153 // The download is now marked as dangerous.
154 [[(id)shelf_ expect] layoutItems];
155 ON_CALL(*download_item_, GetDangerType())
156 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
157 ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(true));
158 download_item_->NotifyObserversDownloadUpdated();
160 [item verifyProgressViewIsVisible:false];
161 [item verifyDangerousDownloadPromptIsVisible:true];
164 // And then marked as safe again.
165 [[(id)shelf_ expect] layoutItems];
166 ON_CALL(*download_item_, GetDangerType())
167 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
168 ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(false));
169 download_item_->NotifyObserversDownloadUpdated();
171 [item verifyProgressViewIsVisible:true];
172 [item verifyDangerousDownloadPromptIsVisible:false];