Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / download / download_item_controller_unittest.mm
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.
4
5 #import <Cocoa/Cocoa.h>
6
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"
18
19 using ::testing::Return;
20 using ::testing::ReturnRefOfCopy;
21
22 @interface DownloadItemController (DownloadItemControllerTest)
23 - (void)verifyProgressViewIsVisible:(bool)visible;
24 - (void)verifyDangerousDownloadPromptIsVisible:(bool)visible;
25 @end
26
27 @implementation DownloadItemController (DownloadItemControllerTest)
28 - (void)verifyProgressViewIsVisible:(bool)visible {
29   EXPECT_EQ(visible, ![progressView_ isHidden]);
30 }
31
32 - (void)verifyDangerousDownloadPromptIsVisible:(bool)visible {
33   EXPECT_EQ(visible, ![dangerousDownloadView_ isHidden]);
34 }
35 @end
36
37 @interface DownloadItemControllerWithInitCallback : DownloadItemController {
38  @private
39   base::Closure initCallback_;
40 }
41 - (id)initWithDownload:(content::DownloadItem*)downloadItem
42                  shelf:(DownloadShelfController*)shelf
43           initCallback:(const base::Closure&)callback;
44 - (void)awakeFromNib;
45 @end
46
47 @implementation DownloadItemControllerWithInitCallback
48
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;
54   return self;
55 }
56
57 - (void)awakeFromNib {
58   [super awakeFromNib];
59   initCallback_.Run();
60 }
61 @end
62
63 namespace {
64
65 class DownloadItemControllerTest : public CocoaProfileTest {
66  public:
67   virtual void SetUp() OVERRIDE {
68     CocoaProfileTest::SetUp();
69     ASSERT_TRUE(browser());
70
71     id shelf_controller =
72         [OCMockObject mockForClass:[DownloadShelfController class]];
73     shelf_.reset([shelf_controller retain]);
74
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));
91   }
92
93   virtual void TearDown() OVERRIDE {
94     download_item_.reset();
95     [(id)shelf_ verify];
96     CocoaProfileTest::TearDown();
97   }
98
99   DownloadItemController* CreateItemController() {
100     base::RunLoop run_loop;
101     base::scoped_nsobject<DownloadItemController> item(
102         [[DownloadItemControllerWithInitCallback alloc]
103             initWithDownload:download_item_.get()
104                        shelf:shelf_.get()
105                 initCallback:run_loop.QuitClosure()]);
106
107     [[test_window() contentView] addSubview:[item view]];
108     run_loop.Run();
109     return item.release();
110   }
111
112  protected:
113   scoped_ptr<content::MockDownloadItem> download_item_;
114   base::scoped_nsobject<DownloadShelfController> shelf_;
115 };
116
117 TEST_F(DownloadItemControllerTest, ShelfNotifiedOfOpenedDownload) {
118   base::scoped_nsobject<DownloadItemController> item(CreateItemController());
119   [[(id)shelf_ expect] downloadWasOpened:[OCMArg any]];
120   download_item_->NotifyObserversDownloadOpened();
121 }
122
123 TEST_F(DownloadItemControllerTest, RemovesSelfWhenDownloadIsDestroyed) {
124   base::scoped_nsobject<DownloadItemController> item(CreateItemController());
125   [[(id)shelf_ expect] remove:[OCMArg any]];
126   download_item_.reset();
127 }
128
129 TEST_F(DownloadItemControllerTest, NormalDownload) {
130   base::scoped_nsobject<DownloadItemController> item(CreateItemController());
131
132   [item verifyProgressViewIsVisible:true];
133   [item verifyDangerousDownloadPromptIsVisible:false];
134 }
135
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());
142
143   [item verifyProgressViewIsVisible:false];
144   [item verifyDangerousDownloadPromptIsVisible:true];
145 }
146
147 TEST_F(DownloadItemControllerTest, NormalDownloadBecomesDangerous) {
148   base::scoped_nsobject<DownloadItemController> item(CreateItemController());
149
150   [item verifyProgressViewIsVisible:true];
151   [item verifyDangerousDownloadPromptIsVisible:false];
152
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();
159
160   [item verifyProgressViewIsVisible:false];
161   [item verifyDangerousDownloadPromptIsVisible:true];
162   [(id)shelf_ verify];
163
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();
170
171   [item verifyProgressViewIsVisible:true];
172   [item verifyDangerousDownloadPromptIsVisible:false];
173   [(id)shelf_ verify];
174 }
175
176 }  // namespace