Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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) { | |
|
Nico
2014/09/16 22:48:14
do you need {} here?
asanka
2014/09/18 01:48:52
Removed.
| |
| 23 } | |
| 24 - (void)verifyProgressViewIsVisible:(bool)visible; | |
| 25 - (void)verifyDangerousDownloadPromptIsVisible:(bool)visible; | |
| 26 @end | |
| 27 | |
| 28 @implementation DownloadItemController (DownloadItemControllerTest) | |
| 29 - (void)verifyProgressViewIsVisible:(bool)visible { | |
| 30 EXPECT_EQ(visible, ![progressView_ isHidden]); | |
| 31 } | |
| 32 | |
| 33 - (void)verifyDangerousDownloadPromptIsVisible:(bool)visible { | |
| 34 EXPECT_EQ(visible, ![dangerousDownloadView_ isHidden]); | |
| 35 } | |
| 36 @end | |
| 37 | |
| 38 @interface DownloadItemControllerWithInitCallback : DownloadItemController { | |
| 39 @private | |
| 40 base::Closure initCallback_; | |
| 41 } | |
| 42 - (id)initWithDownload:(content::DownloadItem*)downloadItem | |
| 43 shelf:(DownloadShelfController*)shelf | |
| 44 initCallback:(const base::Closure&)callback; | |
| 45 - (void)awakeFromNib; | |
| 46 @end | |
| 47 | |
| 48 @implementation DownloadItemControllerWithInitCallback | |
| 49 | |
| 50 - (id)initWithDownload:(content::DownloadItem*)downloadItem | |
| 51 shelf:(DownloadShelfController*)shelf | |
| 52 initCallback:(const base::Closure&)callback { | |
| 53 initCallback_ = callback; | |
| 54 return [super initWithDownload:downloadItem shelf:shelf navigator:NULL]; | |
|
Nico
2014/09/16 22:48:14
the usual pattern is
if ((self = [super initWit
asanka
2014/09/18 01:48:52
Ah. Right. Fixed.
| |
| 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 [[(id)shelf_ expect] remove:[OCMArg any]]; | |
| 101 base::RunLoop run_loop; | |
| 102 DownloadItemController* item = | |
| 103 [[[DownloadItemControllerWithInitCallback alloc] | |
| 104 initWithDownload:download_item_.get() | |
| 105 shelf:shelf_.get() | |
| 106 initCallback:run_loop.QuitClosure()] retain]; | |
|
Nico
2014/09/16 22:48:14
This looks wrong: alloc returns a refcount of 1, t
asanka
2014/09/18 01:48:52
D'oh! Done.
| |
| 107 [[test_window() contentView] addSubview:[item view]]; | |
| 108 run_loop.Run(); | |
| 109 return item; | |
| 110 } | |
| 111 | |
| 112 protected: | |
| 113 scoped_ptr<content::MockDownloadItem> download_item_; | |
| 114 base::scoped_nsobject<DownloadShelfController> shelf_; | |
| 115 }; | |
| 116 | |
| 117 TEST_F(DownloadItemControllerTest, NormalDownload) { | |
| 118 base::scoped_nsobject<DownloadItemController> item(CreateItemController()); | |
| 119 | |
| 120 [item verifyProgressViewIsVisible:true]; | |
| 121 [item verifyDangerousDownloadPromptIsVisible:false]; | |
| 122 } | |
| 123 | |
| 124 TEST_F(DownloadItemControllerTest, DangerousDownload) { | |
| 125 [[(id)shelf_ expect] layoutItems]; | |
| 126 ON_CALL(*download_item_, GetDangerType()) | |
| 127 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE)); | |
| 128 ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(true)); | |
| 129 base::scoped_nsobject<DownloadItemController> item(CreateItemController()); | |
| 130 | |
| 131 [item verifyProgressViewIsVisible:false]; | |
| 132 [item verifyDangerousDownloadPromptIsVisible:true]; | |
| 133 } | |
| 134 | |
| 135 TEST_F(DownloadItemControllerTest, NormalDownloadBecomesDangerous) { | |
| 136 base::scoped_nsobject<DownloadItemController> item(CreateItemController()); | |
| 137 | |
| 138 [item verifyProgressViewIsVisible:true]; | |
| 139 [item verifyDangerousDownloadPromptIsVisible:false]; | |
| 140 | |
| 141 // The download is now marked as dangerous. | |
| 142 [[(id)shelf_ expect] layoutItems]; | |
| 143 ON_CALL(*download_item_, GetDangerType()) | |
| 144 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE)); | |
| 145 ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(true)); | |
| 146 download_item_->NotifyObserversDownloadUpdated(); | |
| 147 | |
| 148 [item verifyProgressViewIsVisible:false]; | |
| 149 [item verifyDangerousDownloadPromptIsVisible:true]; | |
| 150 | |
| 151 // And then marked as safe again. | |
| 152 [[(id)shelf_ expect] layoutItems]; | |
| 153 ON_CALL(*download_item_, GetDangerType()) | |
| 154 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)); | |
| 155 ON_CALL(*download_item_, IsDangerous()).WillByDefault(Return(false)); | |
| 156 download_item_->NotifyObserversDownloadUpdated(); | |
| 157 | |
| 158 [item verifyProgressViewIsVisible:true]; | |
| 159 [item verifyDangerousDownloadPromptIsVisible:false]; | |
| 160 } | |
| 161 | |
| 162 } // namespace | |
| OLD | NEW |