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