blob: 22801739ce3fd9bc480817bd2d6d7a185bd26db0 [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
[email protected]8542e68d2013-01-10 04:33:475#include "base/compiler_specific.h"
6#include "base/memory/scoped_ptr.h"
[email protected]f64a6102013-07-18 00:24:167#include "base/message_loop/message_loop.h"
[email protected]8542e68d2013-01-10 04:33:478#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]1db76f22012-01-16 04:00:4915#include "testing/gtest/include/gtest/gtest.h"
16
[email protected]8542e68d2013-01-10 04:33:4717using ::testing::Return;
18using ::testing::ReturnRefOfCopy;
19using ::testing::SaveArg;
20using ::testing::_;
21using content::DownloadItem;
[email protected]1db76f22012-01-16 04:00:4922
[email protected]8542e68d2013-01-10 04:33:4723namespace {
24
25class 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
[email protected]b3a25092013-05-28 22:08:1643 base::MessageLoopForUI message_loop_;
[email protected]8542e68d2013-01-10 04:33:4744 content::TestBrowserThread ui_thread_;
45 scoped_ptr<content::MockDownloadItem> download_item_;
[email protected]eba4a4d2013-05-29 02:18:0646 scoped_ptr<content::MockDownloadManager> download_manager_;
[email protected]8542e68d2013-01-10 04:33:4747 TestDownloadShelf shelf_;
48};
49
50DownloadShelfTest::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]8542e68d2013-01-10 04:33:4756 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]9f259e12013-05-24 23:41:1160 ON_CALL(*download_item_, GetState())
61 .WillByDefault(Return(DownloadItem::IN_PROGRESS));
[email protected]8542e68d2013-01-10 04:33:4762 ON_CALL(*download_item_, IsTemporary()).WillByDefault(Return(false));
63 ON_CALL(*download_item_, ShouldOpenFileBasedOnExtension())
64 .WillByDefault(Return(false));
65
[email protected]eba4a4d2013-05-29 02:18:0666 download_manager_.reset(
67 new ::testing::NiceMock<content::MockDownloadManager>());
[email protected]8542e68d2013-01-10 04:33:4768 ON_CALL(*download_manager_, GetDownload(_))
69 .WillByDefault(Return(download_item_.get()));
70
71 shelf_.set_download_manager(download_manager_.get());
[email protected]1db76f22012-01-16 04:00:4972}
73
[email protected]8542e68d2013-01-10 04:33:4774} // namespace
75
76TEST_F(DownloadShelfTest, ClosesShelfWhenHidden) {
77 shelf()->Show();
78 EXPECT_TRUE(shelf()->IsShowing());
79 shelf()->Hide();
80 EXPECT_FALSE(shelf()->IsShowing());
81 shelf()->Unhide();
82 EXPECT_TRUE(shelf()->IsShowing());
[email protected]1db76f22012-01-16 04:00:4983}
84
[email protected]8542e68d2013-01-10 04:33:4785TEST_F(DownloadShelfTest, CloseWhileHiddenPreventsShowOnUnhide) {
86 shelf()->Show();
87 shelf()->Hide();
[email protected]6fab1032013-04-01 17:29:1788 shelf()->Close(DownloadShelf::AUTOMATIC);
[email protected]8542e68d2013-01-10 04:33:4789 shelf()->Unhide();
90 EXPECT_FALSE(shelf()->IsShowing());
[email protected]1db76f22012-01-16 04:00:4991}
92
[email protected]8542e68d2013-01-10 04:33:4793TEST_F(DownloadShelfTest, UnhideDoesntShowIfNotShownOnHide) {
94 shelf()->Hide();
95 shelf()->Unhide();
96 EXPECT_FALSE(shelf()->IsShowing());
[email protected]1db76f22012-01-16 04:00:4997}
98
[email protected]8542e68d2013-01-10 04:33:4799TEST_F(DownloadShelfTest, AddDownloadWhileHiddenUnhides) {
100 shelf()->Show();
101 shelf()->Hide();
102 shelf()->AddDownload(download_item());
103 EXPECT_TRUE(shelf()->IsShowing());
104}
105
106TEST_F(DownloadShelfTest, AddDownloadWhileHiddenUnhidesAndShows) {
107 shelf()->Hide();
108 shelf()->AddDownload(download_item());
109 EXPECT_TRUE(shelf()->IsShowing());
110}
111
112// Normal downloads should be added synchronously and cause the shelf to show.
113TEST_F(DownloadShelfTest, AddNormalDownload) {
114 EXPECT_FALSE(shelf()->IsShowing());
115 shelf()->AddDownload(download_item());
116 EXPECT_TRUE(shelf()->did_add_download());
117 EXPECT_TRUE(shelf()->IsShowing());
118}
119
120// Add a transient download. It should not be added immediately. Instead it
121// should be added after a delay. For testing, the delay is set to 0 seconds. So
122// the download should be added once the message loop is flushed.
123TEST_F(DownloadShelfTest, AddDelayedDownload) {
124 EXPECT_CALL(*download_item(), ShouldOpenFileBasedOnExtension())
125 .WillRepeatedly(Return(true));
126 ASSERT_TRUE(DownloadItemModel(download_item())
127 .ShouldRemoveFromShelfWhenComplete());
128 shelf()->AddDownload(download_item());
129
130 EXPECT_FALSE(shelf()->did_add_download());
131 EXPECT_FALSE(shelf()->IsShowing());
132
133 base::RunLoop run_loop;
134 run_loop.RunUntilIdle();
135
136 EXPECT_TRUE(shelf()->did_add_download());
137 EXPECT_TRUE(shelf()->IsShowing());
138}
139
140// Add a transient download that completes before the delay. It should not be
141// displayed on the shelf.
142TEST_F(DownloadShelfTest, AddDelayedCompletedDownload) {
143 EXPECT_CALL(*download_item(), ShouldOpenFileBasedOnExtension())
144 .WillRepeatedly(Return(true));
145 ASSERT_TRUE(DownloadItemModel(download_item())
146 .ShouldRemoveFromShelfWhenComplete());
147 shelf()->AddDownload(download_item());
148
149 EXPECT_FALSE(shelf()->did_add_download());
150 EXPECT_FALSE(shelf()->IsShowing());
151
[email protected]9f259e12013-05-24 23:41:11152 EXPECT_CALL(*download_item(), GetState())
153 .WillRepeatedly(Return(DownloadItem::COMPLETE));
[email protected]8542e68d2013-01-10 04:33:47154
155 base::RunLoop run_loop;
156 run_loop.RunUntilIdle();
157
158 EXPECT_FALSE(shelf()->did_add_download());
159 EXPECT_FALSE(shelf()->IsShowing());
160}
161
162// Add a transient download that completes and becomes non-transient before the
163// delay. It should be displayed on the shelf even though it is complete.
164TEST_F(DownloadShelfTest, AddDelayedCompleteNonTransientDownload) {
165 EXPECT_CALL(*download_item(), ShouldOpenFileBasedOnExtension())
166 .WillRepeatedly(Return(true));
167 ASSERT_TRUE(DownloadItemModel(download_item())
168 .ShouldRemoveFromShelfWhenComplete());
169 shelf()->AddDownload(download_item());
170
171 EXPECT_FALSE(shelf()->did_add_download());
172 EXPECT_FALSE(shelf()->IsShowing());
173
[email protected]9f259e12013-05-24 23:41:11174 EXPECT_CALL(*download_item(), GetState())
175 .WillRepeatedly(Return(DownloadItem::COMPLETE));
[email protected]8542e68d2013-01-10 04:33:47176 EXPECT_CALL(*download_item(), ShouldOpenFileBasedOnExtension())
177 .WillRepeatedly(Return(false));
178 ASSERT_FALSE(DownloadItemModel(download_item())
179 .ShouldRemoveFromShelfWhenComplete());
180
181 base::RunLoop run_loop;
182 run_loop.RunUntilIdle();
183
184 EXPECT_TRUE(shelf()->did_add_download());
185 EXPECT_TRUE(shelf()->IsShowing());
[email protected]1db76f22012-01-16 04:00:49186}