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 "chrome/browser/download/download_item_model.h"
9 #include "base/i18n/rtl.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "content/public/test/mock_download_item.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/base/text/bytes_formatting.h"
21 #include "ui/gfx/font_list.h"
22 #include "ui/gfx/text_utils.h"
24 using content::DownloadItem
;
25 using ::testing::Mock
;
26 using ::testing::NiceMock
;
27 using ::testing::Return
;
28 using ::testing::ReturnRefOfCopy
;
29 using ::testing::SetArgPointee
;
34 // Create a char array that has as many elements as there are download
35 // interrupt reasons. We can then use that in a static_assert to make sure
36 // that all the interrupt reason codes are accounted for. The reason codes are
37 // unfortunately sparse, making this necessary.
38 char kInterruptReasonCounter
[] = {
39 0, // content::DOWNLOAD_INTERRUPT_REASON_NONE
40 #define INTERRUPT_REASON(name,value) 0,
41 #include "content/public/browser/download_interrupt_reason_values.h"
42 #undef INTERRUPT_REASON
44 const size_t kInterruptReasonCount
= arraysize(kInterruptReasonCounter
);
46 // Default target path for a mock download item in DownloadItemModelTest.
47 const base::FilePath::CharType kDefaultTargetFilePath
[] =
48 FILE_PATH_LITERAL("/foo/bar/foo.bar");
50 const base::FilePath::CharType kDefaultDisplayFileName
[] =
51 FILE_PATH_LITERAL("foo.bar");
53 // Default URL for a mock download item in DownloadItemModelTest.
54 const char kDefaultURL
[] = "http://example.com/foo.bar";
56 class DownloadItemModelTest
: public testing::Test
{
58 DownloadItemModelTest()
61 virtual ~DownloadItemModelTest() {
65 // Sets up defaults for the download item and sets |model_| to a new
66 // DownloadItemModel that uses the mock download item.
67 void SetupDownloadItemDefaults() {
68 ON_CALL(item_
, GetReceivedBytes()).WillByDefault(Return(1));
69 ON_CALL(item_
, GetTotalBytes()).WillByDefault(Return(2));
70 ON_CALL(item_
, TimeRemaining(_
)).WillByDefault(Return(false));
71 ON_CALL(item_
, GetMimeType()).WillByDefault(Return("text/html"));
72 ON_CALL(item_
, AllDataSaved()).WillByDefault(Return(false));
73 ON_CALL(item_
, GetOpenWhenComplete()).WillByDefault(Return(false));
74 ON_CALL(item_
, GetFileExternallyRemoved()).WillByDefault(Return(false));
75 ON_CALL(item_
, GetState())
76 .WillByDefault(Return(DownloadItem::IN_PROGRESS
));
77 ON_CALL(item_
, GetURL())
78 .WillByDefault(ReturnRefOfCopy(GURL(kDefaultURL
)));
79 ON_CALL(item_
, GetFileNameToReportUser())
80 .WillByDefault(Return(base::FilePath(kDefaultDisplayFileName
)));
81 ON_CALL(item_
, GetTargetFilePath())
82 .WillByDefault(ReturnRefOfCopy(base::FilePath(kDefaultTargetFilePath
)));
83 ON_CALL(item_
, GetTargetDisposition())
85 Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE
));
86 ON_CALL(item_
, IsPaused()).WillByDefault(Return(false));
89 void SetupInterruptedDownloadItem(content::DownloadInterruptReason reason
) {
90 EXPECT_CALL(item_
, GetLastReason()).WillRepeatedly(Return(reason
));
91 EXPECT_CALL(item_
, GetState())
92 .WillRepeatedly(Return(
93 (reason
== content::DOWNLOAD_INTERRUPT_REASON_NONE
) ?
94 DownloadItem::IN_PROGRESS
:
95 DownloadItem::INTERRUPTED
));
98 content::MockDownloadItem
& item() {
102 DownloadItemModel
& model() {
107 NiceMock
<content::MockDownloadItem
> item_
;
108 DownloadItemModel model_
;
113 TEST_F(DownloadItemModelTest
, InterruptedStatus
) {
114 // Test that we have the correct interrupt status message for downloads that
115 // are in the INTERRUPTED state.
116 const struct TestCase
{
118 content::DownloadInterruptReason reason
;
120 // Expected status string. This will include the progress as well.
121 const char* expected_status
;
123 { content::DOWNLOAD_INTERRUPT_REASON_NONE
,
125 { content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED
,
126 "Failed - Download error" },
127 { content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED
,
128 "Failed - Insufficient permissions" },
129 { content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE
,
130 "Failed - Disk full" },
131 { content::DOWNLOAD_INTERRUPT_REASON_FILE_NAME_TOO_LONG
,
132 "Failed - Path too long" },
133 { content::DOWNLOAD_INTERRUPT_REASON_FILE_TOO_LARGE
,
134 "Failed - File too large" },
135 { content::DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED
,
136 "Failed - Virus detected" },
137 { content::DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED
,
138 "Failed - Blocked" },
139 { content::DOWNLOAD_INTERRUPT_REASON_FILE_SECURITY_CHECK_FAILED
,
140 "Failed - Virus scan failed" },
141 { content::DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT
,
142 "Failed - File truncated" },
143 { content::DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR
,
144 "Failed - System busy" },
145 { content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED
,
146 "Failed - Network error" },
147 { content::DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT
,
148 "Failed - Network timeout" },
149 { content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED
,
150 "Failed - Network disconnected" },
151 { content::DOWNLOAD_INTERRUPT_REASON_NETWORK_SERVER_DOWN
,
152 "Failed - Server unavailable" },
153 { content::DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST
,
154 "Failed - Network error" },
155 { content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED
,
156 "Failed - Server problem" },
157 { content::DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE
,
158 "Failed - Download error" },
159 { content::DOWNLOAD_INTERRUPT_REASON_SERVER_PRECONDITION
,
160 "Failed - Download error" },
161 { content::DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT
,
162 "Failed - No file" },
163 { content::DOWNLOAD_INTERRUPT_REASON_SERVER_UNAUTHORIZED
,
164 "Failed - Needs authorization" },
165 { content::DOWNLOAD_INTERRUPT_REASON_SERVER_CERT_PROBLEM
,
166 "Failed - Bad certificate" },
167 { content::DOWNLOAD_INTERRUPT_REASON_SERVER_FORBIDDEN
,
168 "Failed - Forbidden" },
169 { content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED
,
171 { content::DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN
,
172 "Failed - Shutdown" },
173 { content::DOWNLOAD_INTERRUPT_REASON_CRASH
,
176 static_assert(kInterruptReasonCount
== arraysize(kTestCases
),
177 "interrupt reason mismatch");
179 SetupDownloadItemDefaults();
180 for (unsigned i
= 0; i
< arraysize(kTestCases
); ++i
) {
181 const TestCase
& test_case
= kTestCases
[i
];
182 SetupInterruptedDownloadItem(test_case
.reason
);
183 EXPECT_STREQ(test_case
.expected_status
,
184 base::UTF16ToUTF8(model().GetStatusText()).c_str());
188 // Note: This test is currently skipped on Android. See http://crbug.com/139398
189 TEST_F(DownloadItemModelTest
, InterruptTooltip
) {
190 // Test that we have the correct interrupt tooltip for downloads that are in
191 // the INTERRUPTED state.
192 const struct TestCase
{
194 content::DownloadInterruptReason reason
;
196 // Expected tooltip text. The tooltip text for interrupted downloads
197 // typically consist of two lines. One for the filename and one for the
198 // interrupt reason. The returned string contains a newline.
199 const char* expected_tooltip
;
201 { content::DOWNLOAD_INTERRUPT_REASON_NONE
,
203 { content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED
,
204 "foo.bar\nDownload error" },
205 { content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED
,
206 "foo.bar\nInsufficient permissions" },
207 { content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE
,
208 "foo.bar\nDisk full" },
209 { content::DOWNLOAD_INTERRUPT_REASON_FILE_NAME_TOO_LONG
,
210 "foo.bar\nPath too long" },
211 { content::DOWNLOAD_INTERRUPT_REASON_FILE_TOO_LARGE
,
212 "foo.bar\nFile too large" },
213 { content::DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED
,
214 "foo.bar\nVirus detected" },
215 { content::DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED
,
216 "foo.bar\nBlocked" },
217 { content::DOWNLOAD_INTERRUPT_REASON_FILE_SECURITY_CHECK_FAILED
,
218 "foo.bar\nVirus scan failed" },
219 { content::DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT
,
220 "foo.bar\nFile truncated" },
221 { content::DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR
,
222 "foo.bar\nSystem busy" },
223 { content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED
,
224 "foo.bar\nNetwork error" },
225 { content::DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT
,
226 "foo.bar\nNetwork timeout" },
227 { content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED
,
228 "foo.bar\nNetwork disconnected" },
229 { content::DOWNLOAD_INTERRUPT_REASON_NETWORK_SERVER_DOWN
,
230 "foo.bar\nServer unavailable" },
231 { content::DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST
,
232 "foo.bar\nNetwork error" },
233 { content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED
,
234 "foo.bar\nServer problem" },
235 { content::DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE
,
236 "foo.bar\nDownload error" },
237 { content::DOWNLOAD_INTERRUPT_REASON_SERVER_PRECONDITION
,
238 "foo.bar\nDownload error" },
239 { content::DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT
,
240 "foo.bar\nNo file" },
241 { content::DOWNLOAD_INTERRUPT_REASON_SERVER_UNAUTHORIZED
,
242 "foo.bar\nNeeds authorization" },
243 { content::DOWNLOAD_INTERRUPT_REASON_SERVER_CERT_PROBLEM
,
244 "foo.bar\nBad certificate" },
245 { content::DOWNLOAD_INTERRUPT_REASON_SERVER_FORBIDDEN
,
246 "foo.bar\nForbidden" },
247 { content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED
,
249 { content::DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN
,
250 "foo.bar\nShutdown" },
251 { content::DOWNLOAD_INTERRUPT_REASON_CRASH
,
254 static_assert(kInterruptReasonCount
== arraysize(kTestCases
),
255 "interrupt reason mismatch");
257 // Large tooltip width. Should be large enough to accommodate the entire
258 // tooltip without truncation.
259 const int kLargeTooltipWidth
= 1000;
261 // Small tooltip width. Small enough to require truncation of most
262 // tooltips. Used to test eliding logic.
263 const int kSmallTooltipWidth
= 40;
265 const gfx::FontList
& font_list
=
266 ui::ResourceBundle::GetSharedInstance().GetFontList(
267 ui::ResourceBundle::BaseFont
);
268 SetupDownloadItemDefaults();
269 for (unsigned i
= 0; i
< arraysize(kTestCases
); ++i
) {
270 const TestCase
& test_case
= kTestCases
[i
];
271 SetupInterruptedDownloadItem(test_case
.reason
);
273 // GetTooltipText() elides the tooltip so that the text would fit within a
274 // given width. The following test would fail if kLargeTooltipWidth isn't
275 // large enough to accomodate all the strings.
277 test_case
.expected_tooltip
,
278 base::UTF16ToUTF8(model().GetTooltipText(font_list
,
279 kLargeTooltipWidth
)).c_str());
281 // Check that if the width is small, the returned tooltip only contains
282 // lines of the given width or smaller.
283 base::string16 truncated_tooltip
=
284 model().GetTooltipText(font_list
, kSmallTooltipWidth
);
285 for (const base::string16
& line
:
286 base::SplitString(truncated_tooltip
, base::ASCIIToUTF16("\n"),
287 base::KEEP_WHITESPACE
, base::SPLIT_WANT_NONEMPTY
))
288 EXPECT_GE(kSmallTooltipWidth
, gfx::GetStringWidth(line
, font_list
));
292 TEST_F(DownloadItemModelTest
, InProgressStatus
) {
293 const struct TestCase
{
294 int64 received_bytes
; // Return value of GetReceivedBytes().
295 int64 total_bytes
; // Return value of GetTotalBytes().
296 bool time_remaining_known
; // If TimeRemaining() is known.
297 bool open_when_complete
; // GetOpenWhenComplete().
298 bool is_paused
; // IsPaused().
299 const char* expected_status
; // Expected status text.
301 // These are all the valid combinations of the above fields for a download
302 // that is in IN_PROGRESS state. Go through all of them and check the return
303 // value of DownloadItemModel::GetStatusText(). The point isn't to lock down
304 // the status strings, but to make sure we end up with something sane for
305 // all the circumstances we care about.
307 // For GetReceivedBytes()/GetTotalBytes(), we only check whether each is
308 // non-zero. In addition, if |total_bytes| is zero, then
309 // |time_remaining_known| is also false.
311 // .-- .TimeRemaining() is known.
312 // | .-- .GetOpenWhenComplete()
313 // | | .---- .IsPaused()
314 { 0, 0, false, false, false, "Starting..." },
315 { 1, 0, false, false, false, "1 B" },
316 { 0, 2, false, false, false, "Starting..." },
317 { 1, 2, false, false, false, "1/2 B" },
318 { 0, 2, true, false, false, "0/2 B, 10 secs left" },
319 { 1, 2, true, false, false, "1/2 B, 10 secs left" },
320 { 0, 0, false, true, false, "Opening when complete" },
321 { 1, 0, false, true, false, "Opening when complete" },
322 { 0, 2, false, true, false, "Opening when complete" },
323 { 1, 2, false, true, false, "Opening when complete" },
324 { 0, 2, true, true, false, "Opening in 10 secs..." },
325 { 1, 2, true, true, false, "Opening in 10 secs..." },
326 { 0, 0, false, false, true, "0 B, Paused" },
327 { 1, 0, false, false, true, "1 B, Paused" },
328 { 0, 2, false, false, true, "0/2 B, Paused" },
329 { 1, 2, false, false, true, "1/2 B, Paused" },
330 { 0, 2, true, false, true, "0/2 B, Paused" },
331 { 1, 2, true, false, true, "1/2 B, Paused" },
332 { 0, 0, false, true, true, "0 B, Paused" },
333 { 1, 0, false, true, true, "1 B, Paused" },
334 { 0, 2, false, true, true, "0/2 B, Paused" },
335 { 1, 2, false, true, true, "1/2 B, Paused" },
336 { 0, 2, true, true, true, "0/2 B, Paused" },
337 { 1, 2, true, true, true, "1/2 B, Paused" },
340 SetupDownloadItemDefaults();
342 for (unsigned i
= 0; i
< arraysize(kTestCases
); i
++) {
343 const TestCase
& test_case
= kTestCases
[i
];
344 Mock::VerifyAndClearExpectations(&item());
345 Mock::VerifyAndClearExpectations(&model());
346 EXPECT_CALL(item(), GetReceivedBytes())
347 .WillRepeatedly(Return(test_case
.received_bytes
));
348 EXPECT_CALL(item(), GetTotalBytes())
349 .WillRepeatedly(Return(test_case
.total_bytes
));
350 EXPECT_CALL(item(), TimeRemaining(_
))
351 .WillRepeatedly(testing::DoAll(
352 testing::SetArgPointee
<0>(base::TimeDelta::FromSeconds(10)),
353 Return(test_case
.time_remaining_known
)));
354 EXPECT_CALL(item(), GetOpenWhenComplete())
355 .WillRepeatedly(Return(test_case
.open_when_complete
));
356 EXPECT_CALL(item(), IsPaused())
357 .WillRepeatedly(Return(test_case
.is_paused
));
359 EXPECT_STREQ(test_case
.expected_status
,
360 base::UTF16ToUTF8(model().GetStatusText()).c_str());
364 TEST_F(DownloadItemModelTest
, ShouldShowInShelf
) {
365 SetupDownloadItemDefaults();
367 // By default the download item should be displayable on the shelf.
368 EXPECT_TRUE(model().ShouldShowInShelf());
370 // Once explicitly set, ShouldShowInShelf() should return the explicit value.
371 model().SetShouldShowInShelf(false);
372 EXPECT_FALSE(model().ShouldShowInShelf());
374 model().SetShouldShowInShelf(true);
375 EXPECT_TRUE(model().ShouldShowInShelf());
378 TEST_F(DownloadItemModelTest
, ShouldRemoveFromShelfWhenComplete
) {
379 const struct TestCase
{
380 DownloadItem::DownloadState state
;
381 bool is_dangerous
; // Expectation for IsDangerous().
382 bool is_auto_open
; // Expectation for GetOpenWhenComplete().
383 bool auto_opened
; // Whether the download was successfully
384 // auto-opened. Expecation for GetAutoOpened().
385 bool expected_result
;
387 // All the valid combinations of state, is_dangerous, is_auto_open and
390 // .--- Is dangerous.
391 // | .--- Auto open or temporary.
392 // | | .--- Auto opened.
393 // | | | .--- Expected result.
394 { DownloadItem::IN_PROGRESS
, false, false, false, false},
395 { DownloadItem::IN_PROGRESS
, false, true , false, true },
396 { DownloadItem::IN_PROGRESS
, true , false, false, false},
397 { DownloadItem::IN_PROGRESS
, true , true , false, false},
398 { DownloadItem::COMPLETE
, false, false, false, false},
399 { DownloadItem::COMPLETE
, false, true , false, false},
400 { DownloadItem::COMPLETE
, false, false, true , true },
401 { DownloadItem::COMPLETE
, false, true , true , true },
402 { DownloadItem::CANCELLED
, false, false, false, false},
403 { DownloadItem::CANCELLED
, false, true , false, false},
404 { DownloadItem::CANCELLED
, true , false, false, false},
405 { DownloadItem::CANCELLED
, true , true , false, false},
406 { DownloadItem::INTERRUPTED
, false, false, false, false},
407 { DownloadItem::INTERRUPTED
, false, true , false, false},
408 { DownloadItem::INTERRUPTED
, true , false, false, false},
409 { DownloadItem::INTERRUPTED
, true , true , false, false}
412 SetupDownloadItemDefaults();
414 for (unsigned i
= 0; i
< arraysize(kTestCases
); i
++) {
415 const TestCase
& test_case
= kTestCases
[i
];
416 EXPECT_CALL(item(), GetOpenWhenComplete())
417 .WillRepeatedly(Return(test_case
.is_auto_open
));
418 EXPECT_CALL(item(), GetState())
419 .WillRepeatedly(Return(test_case
.state
));
420 EXPECT_CALL(item(), IsDangerous())
421 .WillRepeatedly(Return(test_case
.is_dangerous
));
422 EXPECT_CALL(item(), GetAutoOpened())
423 .WillRepeatedly(Return(test_case
.auto_opened
));
425 EXPECT_EQ(test_case
.expected_result
,
426 model().ShouldRemoveFromShelfWhenComplete())
427 << "Test case: " << i
;
428 Mock::VerifyAndClearExpectations(&item());
429 Mock::VerifyAndClearExpectations(&model());