blob: 097cc829c15d1a0a377c8f0de53c526d4d2079cb [file] [log] [blame]
[email protected]1db76f22012-01-16 04:00:491// 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
dchengabbf44652016-04-07 22:23:395#include <memory>
6
[email protected]8542e68d2013-01-10 04:33:477#include "base/compiler_specific.h"
[email protected]f64a6102013-07-18 00:24:168#include "base/message_loop/message_loop.h"
[email protected]8542e68d2013-01-10 04:33:479#include "base/run_loop.h"
[email protected]423939d2013-07-31 20:00:0810#include "chrome/browser/chrome_notification_types.h"
[email protected]8542e68d2013-01-10 04:33:4711#include "chrome/browser/download/download_item_model.h"
[email protected]423939d2013-07-31 20:00:0812#include "chrome/browser/download/download_service_factory.h"
rdsmitha43ecbb2015-06-12 02:21:4513#include "chrome/browser/download/download_service_impl.h"
[email protected]8542e68d2013-01-10 04:33:4714#include "chrome/browser/download/test_download_shelf.h"
[email protected]423939d2013-07-31 20:00:0815#include "chrome/test/base/testing_profile.h"
16#include "content/public/browser/notification_service.h"
[email protected]8542e68d2013-01-10 04:33:4717#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]e4452d32013-11-15 23:07:4120#include "extensions/common/extension.h"
[email protected]8542e68d2013-01-10 04:33:4721#include "testing/gmock/include/gmock/gmock.h"
[email protected]1db76f22012-01-16 04:00:4922#include "testing/gtest/include/gtest/gtest.h"
23
[email protected]8542e68d2013-01-10 04:33:4724using ::testing::Return;
25using ::testing::ReturnRefOfCopy;
26using ::testing::SaveArg;
27using ::testing::_;
28using content::DownloadItem;
[email protected]1db76f22012-01-16 04:00:4929
[email protected]8542e68d2013-01-10 04:33:4730namespace {
31
[email protected]8542e68d2013-01-10 04:33:4732class 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]423939d2013-07-31 20:00:0846 Profile* profile() { return profile_.get(); }
47
dchenge1bc7982014-10-30 00:32:4048 void SetUp() override {
[email protected]423939d2013-07-31 20:00:0849 }
50
dchenge1bc7982014-10-30 00:32:4051 void TearDown() override {
[email protected]423939d2013-07-31 20:00:0852 }
[email protected]8542e68d2013-01-10 04:33:4753
54 private:
dchengabbf44652016-04-07 22:23:3955 std::unique_ptr<content::MockDownloadItem> GetInProgressMockDownload();
[email protected]8542e68d2013-01-10 04:33:4756
[email protected]b3a25092013-05-28 22:08:1657 base::MessageLoopForUI message_loop_;
[email protected]8542e68d2013-01-10 04:33:4758 content::TestBrowserThread ui_thread_;
dchengabbf44652016-04-07 22:23:3959 std::unique_ptr<content::MockDownloadItem> download_item_;
60 std::unique_ptr<content::MockDownloadManager> download_manager_;
[email protected]8542e68d2013-01-10 04:33:4761 TestDownloadShelf shelf_;
dchengabbf44652016-04-07 22:23:3962 std::unique_ptr<TestingProfile> profile_;
[email protected]8542e68d2013-01-10 04:33:4763};
64
65DownloadShelfTest::DownloadShelfTest()
[email protected]423939d2013-07-31 20:00:0866 : ui_thread_(content::BrowserThread::UI, &message_loop_),
67 profile_(new TestingProfile()) {
[email protected]8542e68d2013-01-10 04:33:4768 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]8542e68d2013-01-10 04:33:4772 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]9f259e12013-05-24 23:41:1176 ON_CALL(*download_item_, GetState())
77 .WillByDefault(Return(DownloadItem::IN_PROGRESS));
[email protected]8542e68d2013-01-10 04:33:4778 ON_CALL(*download_item_, IsTemporary()).WillByDefault(Return(false));
79 ON_CALL(*download_item_, ShouldOpenFileBasedOnExtension())
80 .WillByDefault(Return(false));
[email protected]423939d2013-07-31 20:00:0881 ON_CALL(*download_item_, GetBrowserContext())
82 .WillByDefault(Return(profile()));
[email protected]8542e68d2013-01-10 04:33:4783
[email protected]eba4a4d2013-05-29 02:18:0684 download_manager_.reset(
85 new ::testing::NiceMock<content::MockDownloadManager>());
[email protected]8542e68d2013-01-10 04:33:4786 ON_CALL(*download_manager_, GetDownload(_))
87 .WillByDefault(Return(download_item_.get()));
[email protected]423939d2013-07-31 20:00:0888 ON_CALL(*download_manager_, GetBrowserContext())
89 .WillByDefault(Return(profile()));
[email protected]8542e68d2013-01-10 04:33:4790
91 shelf_.set_download_manager(download_manager_.get());
[email protected]1db76f22012-01-16 04:00:4992}
93
[email protected]8542e68d2013-01-10 04:33:4794} // namespace
95
96TEST_F(DownloadShelfTest, ClosesShelfWhenHidden) {
sdy75789252017-02-17 17:25:5797 shelf()->Open();
[email protected]8542e68d2013-01-10 04:33:4798 EXPECT_TRUE(shelf()->IsShowing());
99 shelf()->Hide();
100 EXPECT_FALSE(shelf()->IsShowing());
101 shelf()->Unhide();
102 EXPECT_TRUE(shelf()->IsShowing());
[email protected]1db76f22012-01-16 04:00:49103}
104
[email protected]8542e68d2013-01-10 04:33:47105TEST_F(DownloadShelfTest, CloseWhileHiddenPreventsShowOnUnhide) {
sdy75789252017-02-17 17:25:57106 shelf()->Open();
[email protected]8542e68d2013-01-10 04:33:47107 shelf()->Hide();
[email protected]6fab1032013-04-01 17:29:17108 shelf()->Close(DownloadShelf::AUTOMATIC);
[email protected]8542e68d2013-01-10 04:33:47109 shelf()->Unhide();
110 EXPECT_FALSE(shelf()->IsShowing());
[email protected]1db76f22012-01-16 04:00:49111}
112
[email protected]8542e68d2013-01-10 04:33:47113TEST_F(DownloadShelfTest, UnhideDoesntShowIfNotShownOnHide) {
114 shelf()->Hide();
115 shelf()->Unhide();
116 EXPECT_FALSE(shelf()->IsShowing());
[email protected]1db76f22012-01-16 04:00:49117}
118
[email protected]8542e68d2013-01-10 04:33:47119TEST_F(DownloadShelfTest, AddDownloadWhileHiddenUnhides) {
sdy75789252017-02-17 17:25:57120 shelf()->Open();
[email protected]8542e68d2013-01-10 04:33:47121 shelf()->Hide();
122 shelf()->AddDownload(download_item());
123 EXPECT_TRUE(shelf()->IsShowing());
124}
125
126TEST_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.
133TEST_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.
143TEST_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.
162TEST_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]9f259e12013-05-24 23:41:11172 EXPECT_CALL(*download_item(), GetState())
173 .WillRepeatedly(Return(DownloadItem::COMPLETE));
[email protected]90c09c3f2013-07-18 19:33:16174 EXPECT_CALL(*download_item(), GetAutoOpened())
175 .WillRepeatedly(Return(true));
[email protected]8542e68d2013-01-10 04:33:47176
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.
186TEST_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]9f259e12013-05-24 23:41:11196 EXPECT_CALL(*download_item(), GetState())
197 .WillRepeatedly(Return(DownloadItem::COMPLETE));
[email protected]8542e68d2013-01-10 04:33:47198 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]1db76f22012-01-16 04:00:49208}