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