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.
5 #include "base/compiler_specific.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/download/download_item_model.h"
11 #include "chrome/browser/download/download_service_factory.h"
12 #include "chrome/browser/download/download_service_impl.h"
13 #include "chrome/browser/download/test_download_shelf.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/browser/notification_service.h"
16 #include "content/public/test/mock_download_item.h"
17 #include "content/public/test/mock_download_manager.h"
18 #include "content/public/test/test_browser_thread.h"
19 #include "extensions/common/extension.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using ::testing::Return
;
24 using ::testing::ReturnRefOfCopy
;
25 using ::testing::SaveArg
;
27 using content::DownloadItem
;
31 class DownloadShelfTest
: public testing::Test
{
36 content::MockDownloadItem
* download_item() {
37 return download_item_
.get();
39 content::MockDownloadManager
* download_manager() {
40 return download_manager_
.get();
42 TestDownloadShelf
* shelf() {
45 Profile
* profile() { return profile_
.get(); }
47 void SetUp() override
{
50 void TearDown() override
{
54 scoped_ptr
<content::MockDownloadItem
> GetInProgressMockDownload();
56 base::MessageLoopForUI message_loop_
;
57 content::TestBrowserThread ui_thread_
;
58 scoped_ptr
<content::MockDownloadItem
> download_item_
;
59 scoped_ptr
<content::MockDownloadManager
> download_manager_
;
60 TestDownloadShelf shelf_
;
61 scoped_ptr
<TestingProfile
> profile_
;
64 DownloadShelfTest::DownloadShelfTest()
65 : ui_thread_(content::BrowserThread::UI
, &message_loop_
),
66 profile_(new TestingProfile()) {
67 download_item_
.reset(new ::testing::NiceMock
<content::MockDownloadItem
>());
68 ON_CALL(*download_item_
, GetAutoOpened()).WillByDefault(Return(false));
69 ON_CALL(*download_item_
, GetMimeType()).WillByDefault(Return("text/plain"));
70 ON_CALL(*download_item_
, GetOpenWhenComplete()).WillByDefault(Return(false));
71 ON_CALL(*download_item_
, GetTargetDisposition())
72 .WillByDefault(Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE
));
73 ON_CALL(*download_item_
, GetURL())
74 .WillByDefault(ReturnRefOfCopy(GURL("http://example.com/foo")));
75 ON_CALL(*download_item_
, GetState())
76 .WillByDefault(Return(DownloadItem::IN_PROGRESS
));
77 ON_CALL(*download_item_
, IsTemporary()).WillByDefault(Return(false));
78 ON_CALL(*download_item_
, ShouldOpenFileBasedOnExtension())
79 .WillByDefault(Return(false));
80 ON_CALL(*download_item_
, GetBrowserContext())
81 .WillByDefault(Return(profile()));
83 download_manager_
.reset(
84 new ::testing::NiceMock
<content::MockDownloadManager
>());
85 ON_CALL(*download_manager_
, GetDownload(_
))
86 .WillByDefault(Return(download_item_
.get()));
87 ON_CALL(*download_manager_
, GetBrowserContext())
88 .WillByDefault(Return(profile()));
90 shelf_
.set_download_manager(download_manager_
.get());
95 TEST_F(DownloadShelfTest
, ClosesShelfWhenHidden
) {
97 EXPECT_TRUE(shelf()->IsShowing());
99 EXPECT_FALSE(shelf()->IsShowing());
101 EXPECT_TRUE(shelf()->IsShowing());
104 TEST_F(DownloadShelfTest
, CloseWhileHiddenPreventsShowOnUnhide
) {
107 shelf()->Close(DownloadShelf::AUTOMATIC
);
109 EXPECT_FALSE(shelf()->IsShowing());
112 TEST_F(DownloadShelfTest
, UnhideDoesntShowIfNotShownOnHide
) {
115 EXPECT_FALSE(shelf()->IsShowing());
118 TEST_F(DownloadShelfTest
, AddDownloadWhileHiddenUnhides
) {
121 shelf()->AddDownload(download_item());
122 EXPECT_TRUE(shelf()->IsShowing());
125 TEST_F(DownloadShelfTest
, AddDownloadWhileHiddenUnhidesAndShows
) {
127 shelf()->AddDownload(download_item());
128 EXPECT_TRUE(shelf()->IsShowing());
131 // Normal downloads should be added synchronously and cause the shelf to show.
132 TEST_F(DownloadShelfTest
, AddNormalDownload
) {
133 EXPECT_FALSE(shelf()->IsShowing());
134 shelf()->AddDownload(download_item());
135 EXPECT_TRUE(shelf()->did_add_download());
136 EXPECT_TRUE(shelf()->IsShowing());
139 // Add a transient download. It should not be added immediately. Instead it
140 // should be added after a delay. For testing, the delay is set to 0 seconds. So
141 // the download should be added once the message loop is flushed.
142 TEST_F(DownloadShelfTest
, AddDelayedDownload
) {
143 EXPECT_CALL(*download_item(), ShouldOpenFileBasedOnExtension())
144 .WillRepeatedly(Return(true));
145 ASSERT_TRUE(DownloadItemModel(download_item())
146 .ShouldRemoveFromShelfWhenComplete());
147 shelf()->AddDownload(download_item());
149 EXPECT_FALSE(shelf()->did_add_download());
150 EXPECT_FALSE(shelf()->IsShowing());
152 base::RunLoop run_loop
;
153 run_loop
.RunUntilIdle();
155 EXPECT_TRUE(shelf()->did_add_download());
156 EXPECT_TRUE(shelf()->IsShowing());
159 // Add a transient download that completes before the delay. It should not be
160 // displayed on the shelf.
161 TEST_F(DownloadShelfTest
, AddDelayedCompletedDownload
) {
162 EXPECT_CALL(*download_item(), ShouldOpenFileBasedOnExtension())
163 .WillRepeatedly(Return(true));
164 ASSERT_TRUE(DownloadItemModel(download_item())
165 .ShouldRemoveFromShelfWhenComplete());
166 shelf()->AddDownload(download_item());
168 EXPECT_FALSE(shelf()->did_add_download());
169 EXPECT_FALSE(shelf()->IsShowing());
171 EXPECT_CALL(*download_item(), GetState())
172 .WillRepeatedly(Return(DownloadItem::COMPLETE
));
173 EXPECT_CALL(*download_item(), GetAutoOpened())
174 .WillRepeatedly(Return(true));
176 base::RunLoop run_loop
;
177 run_loop
.RunUntilIdle();
179 EXPECT_FALSE(shelf()->did_add_download());
180 EXPECT_FALSE(shelf()->IsShowing());
183 // Add a transient download that completes and becomes non-transient before the
184 // delay. It should be displayed on the shelf even though it is complete.
185 TEST_F(DownloadShelfTest
, AddDelayedCompleteNonTransientDownload
) {
186 EXPECT_CALL(*download_item(), ShouldOpenFileBasedOnExtension())
187 .WillRepeatedly(Return(true));
188 ASSERT_TRUE(DownloadItemModel(download_item())
189 .ShouldRemoveFromShelfWhenComplete());
190 shelf()->AddDownload(download_item());
192 EXPECT_FALSE(shelf()->did_add_download());
193 EXPECT_FALSE(shelf()->IsShowing());
195 EXPECT_CALL(*download_item(), GetState())
196 .WillRepeatedly(Return(DownloadItem::COMPLETE
));
197 EXPECT_CALL(*download_item(), ShouldOpenFileBasedOnExtension())
198 .WillRepeatedly(Return(false));
199 ASSERT_FALSE(DownloadItemModel(download_item())
200 .ShouldRemoveFromShelfWhenComplete());
202 base::RunLoop run_loop
;
203 run_loop
.RunUntilIdle();
205 EXPECT_TRUE(shelf()->did_add_download());
206 EXPECT_TRUE(shelf()->IsShowing());