[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
dcheng | abbf4465 | 2016-04-07 22:23:39 | [diff] [blame] | 5 | #include <memory> |
| 6 | |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 7 | #include "base/compiler_specific.h" |
[email protected] | f64a610 | 2013-07-18 00:24:16 | [diff] [blame] | 8 | #include "base/message_loop/message_loop.h" |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 9 | #include "base/run_loop.h" |
[email protected] | 423939d | 2013-07-31 20:00:08 | [diff] [blame] | 10 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 11 | #include "chrome/browser/download/download_item_model.h" |
[email protected] | 423939d | 2013-07-31 20:00:08 | [diff] [blame] | 12 | #include "chrome/browser/download/download_service_factory.h" |
rdsmith | a43ecbb | 2015-06-12 02:21:45 | [diff] [blame] | 13 | #include "chrome/browser/download/download_service_impl.h" |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 14 | #include "chrome/browser/download/test_download_shelf.h" |
[email protected] | 423939d | 2013-07-31 20:00:08 | [diff] [blame] | 15 | #include "chrome/test/base/testing_profile.h" |
| 16 | #include "content/public/browser/notification_service.h" |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 17 | #include "content/public/test/mock_download_item.h" |
| 18 | #include "content/public/test/mock_download_manager.h" |
| 19 | #include "content/public/test/test_browser_thread.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 20 | #include "extensions/common/extension.h" |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 21 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 22 | #include "testing/gtest/include/gtest/gtest.h" |
| 23 | |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 24 | using ::testing::Return; |
| 25 | using ::testing::ReturnRefOfCopy; |
| 26 | using ::testing::SaveArg; |
| 27 | using ::testing::_; |
| 28 | using content::DownloadItem; |
[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 29 | |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 30 | namespace { |
| 31 | |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 32 | class DownloadShelfTest : public testing::Test { |
| 33 | public: |
| 34 | DownloadShelfTest(); |
| 35 | |
| 36 | protected: |
| 37 | content::MockDownloadItem* download_item() { |
| 38 | return download_item_.get(); |
| 39 | } |
| 40 | content::MockDownloadManager* download_manager() { |
| 41 | return download_manager_.get(); |
| 42 | } |
| 43 | TestDownloadShelf* shelf() { |
| 44 | return &shelf_; |
| 45 | } |
[email protected] | 423939d | 2013-07-31 20:00:08 | [diff] [blame] | 46 | Profile* profile() { return profile_.get(); } |
| 47 | |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 48 | void SetUp() override { |
[email protected] | 423939d | 2013-07-31 20:00:08 | [diff] [blame] | 49 | } |
| 50 | |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 51 | void TearDown() override { |
[email protected] | 423939d | 2013-07-31 20:00:08 | [diff] [blame] | 52 | } |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 53 | |
| 54 | private: |
dcheng | abbf4465 | 2016-04-07 22:23:39 | [diff] [blame] | 55 | std::unique_ptr<content::MockDownloadItem> GetInProgressMockDownload(); |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 56 | |
[email protected] | b3a2509 | 2013-05-28 22:08:16 | [diff] [blame] | 57 | base::MessageLoopForUI message_loop_; |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 58 | content::TestBrowserThread ui_thread_; |
dcheng | abbf4465 | 2016-04-07 22:23:39 | [diff] [blame] | 59 | std::unique_ptr<content::MockDownloadItem> download_item_; |
| 60 | std::unique_ptr<content::MockDownloadManager> download_manager_; |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 61 | TestDownloadShelf shelf_; |
dcheng | abbf4465 | 2016-04-07 22:23:39 | [diff] [blame] | 62 | std::unique_ptr<TestingProfile> profile_; |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | DownloadShelfTest::DownloadShelfTest() |
[email protected] | 423939d | 2013-07-31 20:00:08 | [diff] [blame] | 66 | : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 67 | profile_(new TestingProfile()) { |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 68 | download_item_.reset(new ::testing::NiceMock<content::MockDownloadItem>()); |
| 69 | ON_CALL(*download_item_, GetAutoOpened()).WillByDefault(Return(false)); |
| 70 | ON_CALL(*download_item_, GetMimeType()).WillByDefault(Return("text/plain")); |
| 71 | ON_CALL(*download_item_, GetOpenWhenComplete()).WillByDefault(Return(false)); |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 72 | ON_CALL(*download_item_, GetTargetDisposition()) |
| 73 | .WillByDefault(Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE)); |
| 74 | ON_CALL(*download_item_, GetURL()) |
| 75 | .WillByDefault(ReturnRefOfCopy(GURL("http://example.com/foo"))); |
[email protected] | 9f259e1 | 2013-05-24 23:41:11 | [diff] [blame] | 76 | ON_CALL(*download_item_, GetState()) |
| 77 | .WillByDefault(Return(DownloadItem::IN_PROGRESS)); |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 78 | ON_CALL(*download_item_, IsTemporary()).WillByDefault(Return(false)); |
| 79 | ON_CALL(*download_item_, ShouldOpenFileBasedOnExtension()) |
| 80 | .WillByDefault(Return(false)); |
[email protected] | 423939d | 2013-07-31 20:00:08 | [diff] [blame] | 81 | ON_CALL(*download_item_, GetBrowserContext()) |
| 82 | .WillByDefault(Return(profile())); |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 83 | |
[email protected] | eba4a4d | 2013-05-29 02:18:06 | [diff] [blame] | 84 | download_manager_.reset( |
| 85 | new ::testing::NiceMock<content::MockDownloadManager>()); |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 86 | ON_CALL(*download_manager_, GetDownload(_)) |
| 87 | .WillByDefault(Return(download_item_.get())); |
[email protected] | 423939d | 2013-07-31 20:00:08 | [diff] [blame] | 88 | ON_CALL(*download_manager_, GetBrowserContext()) |
| 89 | .WillByDefault(Return(profile())); |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 90 | |
| 91 | shelf_.set_download_manager(download_manager_.get()); |
[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 92 | } |
| 93 | |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 94 | } // namespace |
| 95 | |
| 96 | TEST_F(DownloadShelfTest, ClosesShelfWhenHidden) { |
sdy | 7578925 | 2017-02-17 17:25:57 | [diff] [blame] | 97 | shelf()->Open(); |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 98 | EXPECT_TRUE(shelf()->IsShowing()); |
| 99 | shelf()->Hide(); |
| 100 | EXPECT_FALSE(shelf()->IsShowing()); |
| 101 | shelf()->Unhide(); |
| 102 | EXPECT_TRUE(shelf()->IsShowing()); |
[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 103 | } |
| 104 | |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 105 | TEST_F(DownloadShelfTest, CloseWhileHiddenPreventsShowOnUnhide) { |
sdy | 7578925 | 2017-02-17 17:25:57 | [diff] [blame] | 106 | shelf()->Open(); |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 107 | shelf()->Hide(); |
[email protected] | 6fab103 | 2013-04-01 17:29:17 | [diff] [blame] | 108 | shelf()->Close(DownloadShelf::AUTOMATIC); |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 109 | shelf()->Unhide(); |
| 110 | EXPECT_FALSE(shelf()->IsShowing()); |
[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 111 | } |
| 112 | |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 113 | TEST_F(DownloadShelfTest, UnhideDoesntShowIfNotShownOnHide) { |
| 114 | shelf()->Hide(); |
| 115 | shelf()->Unhide(); |
| 116 | EXPECT_FALSE(shelf()->IsShowing()); |
[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 119 | TEST_F(DownloadShelfTest, AddDownloadWhileHiddenUnhides) { |
sdy | 7578925 | 2017-02-17 17:25:57 | [diff] [blame] | 120 | shelf()->Open(); |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 121 | shelf()->Hide(); |
| 122 | shelf()->AddDownload(download_item()); |
| 123 | EXPECT_TRUE(shelf()->IsShowing()); |
| 124 | } |
| 125 | |
| 126 | TEST_F(DownloadShelfTest, AddDownloadWhileHiddenUnhidesAndShows) { |
| 127 | shelf()->Hide(); |
| 128 | shelf()->AddDownload(download_item()); |
| 129 | EXPECT_TRUE(shelf()->IsShowing()); |
| 130 | } |
| 131 | |
| 132 | // Normal downloads should be added synchronously and cause the shelf to show. |
| 133 | TEST_F(DownloadShelfTest, AddNormalDownload) { |
| 134 | EXPECT_FALSE(shelf()->IsShowing()); |
| 135 | shelf()->AddDownload(download_item()); |
| 136 | EXPECT_TRUE(shelf()->did_add_download()); |
| 137 | EXPECT_TRUE(shelf()->IsShowing()); |
| 138 | } |
| 139 | |
| 140 | // Add a transient download. It should not be added immediately. Instead it |
| 141 | // should be added after a delay. For testing, the delay is set to 0 seconds. So |
| 142 | // the download should be added once the message loop is flushed. |
| 143 | TEST_F(DownloadShelfTest, AddDelayedDownload) { |
| 144 | EXPECT_CALL(*download_item(), ShouldOpenFileBasedOnExtension()) |
| 145 | .WillRepeatedly(Return(true)); |
| 146 | ASSERT_TRUE(DownloadItemModel(download_item()) |
| 147 | .ShouldRemoveFromShelfWhenComplete()); |
| 148 | shelf()->AddDownload(download_item()); |
| 149 | |
| 150 | EXPECT_FALSE(shelf()->did_add_download()); |
| 151 | EXPECT_FALSE(shelf()->IsShowing()); |
| 152 | |
| 153 | base::RunLoop run_loop; |
| 154 | run_loop.RunUntilIdle(); |
| 155 | |
| 156 | EXPECT_TRUE(shelf()->did_add_download()); |
| 157 | EXPECT_TRUE(shelf()->IsShowing()); |
| 158 | } |
| 159 | |
| 160 | // Add a transient download that completes before the delay. It should not be |
| 161 | // displayed on the shelf. |
| 162 | TEST_F(DownloadShelfTest, AddDelayedCompletedDownload) { |
| 163 | EXPECT_CALL(*download_item(), ShouldOpenFileBasedOnExtension()) |
| 164 | .WillRepeatedly(Return(true)); |
| 165 | ASSERT_TRUE(DownloadItemModel(download_item()) |
| 166 | .ShouldRemoveFromShelfWhenComplete()); |
| 167 | shelf()->AddDownload(download_item()); |
| 168 | |
| 169 | EXPECT_FALSE(shelf()->did_add_download()); |
| 170 | EXPECT_FALSE(shelf()->IsShowing()); |
| 171 | |
[email protected] | 9f259e1 | 2013-05-24 23:41:11 | [diff] [blame] | 172 | EXPECT_CALL(*download_item(), GetState()) |
| 173 | .WillRepeatedly(Return(DownloadItem::COMPLETE)); |
[email protected] | 90c09c3f | 2013-07-18 19:33:16 | [diff] [blame] | 174 | EXPECT_CALL(*download_item(), GetAutoOpened()) |
| 175 | .WillRepeatedly(Return(true)); |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 176 | |
| 177 | base::RunLoop run_loop; |
| 178 | run_loop.RunUntilIdle(); |
| 179 | |
| 180 | EXPECT_FALSE(shelf()->did_add_download()); |
| 181 | EXPECT_FALSE(shelf()->IsShowing()); |
| 182 | } |
| 183 | |
| 184 | // Add a transient download that completes and becomes non-transient before the |
| 185 | // delay. It should be displayed on the shelf even though it is complete. |
| 186 | TEST_F(DownloadShelfTest, AddDelayedCompleteNonTransientDownload) { |
| 187 | EXPECT_CALL(*download_item(), ShouldOpenFileBasedOnExtension()) |
| 188 | .WillRepeatedly(Return(true)); |
| 189 | ASSERT_TRUE(DownloadItemModel(download_item()) |
| 190 | .ShouldRemoveFromShelfWhenComplete()); |
| 191 | shelf()->AddDownload(download_item()); |
| 192 | |
| 193 | EXPECT_FALSE(shelf()->did_add_download()); |
| 194 | EXPECT_FALSE(shelf()->IsShowing()); |
| 195 | |
[email protected] | 9f259e1 | 2013-05-24 23:41:11 | [diff] [blame] | 196 | EXPECT_CALL(*download_item(), GetState()) |
| 197 | .WillRepeatedly(Return(DownloadItem::COMPLETE)); |
[email protected] | 8542e68d | 2013-01-10 04:33:47 | [diff] [blame] | 198 | EXPECT_CALL(*download_item(), ShouldOpenFileBasedOnExtension()) |
| 199 | .WillRepeatedly(Return(false)); |
| 200 | ASSERT_FALSE(DownloadItemModel(download_item()) |
| 201 | .ShouldRemoveFromShelfWhenComplete()); |
| 202 | |
| 203 | base::RunLoop run_loop; |
| 204 | run_loop.RunUntilIdle(); |
| 205 | |
| 206 | EXPECT_TRUE(shelf()->did_add_download()); |
| 207 | EXPECT_TRUE(shelf()->IsShowing()); |
[email protected] | 1db76f2 | 2012-01-16 04:00:49 | [diff] [blame] | 208 | } |