OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/download/download_item_model.h" | 5 #include "chrome/browser/download/download_item_model.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 // that all the interrupt reason codes are accounted for. The reason codes are | 34 // that all the interrupt reason codes are accounted for. The reason codes are |
35 // unfortunately sparse, making this necessary. | 35 // unfortunately sparse, making this necessary. |
36 char kInterruptReasonCounter[] = { | 36 char kInterruptReasonCounter[] = { |
37 0, // content::DOWNLOAD_INTERRUPT_REASON_NONE | 37 0, // content::DOWNLOAD_INTERRUPT_REASON_NONE |
38 #define INTERRUPT_REASON(name,value) 0, | 38 #define INTERRUPT_REASON(name,value) 0, |
39 #include "content/public/browser/download_interrupt_reason_values.h" | 39 #include "content/public/browser/download_interrupt_reason_values.h" |
40 #undef INTERRUPT_REASON | 40 #undef INTERRUPT_REASON |
41 }; | 41 }; |
42 const size_t kInterruptReasonCount = ARRAYSIZE_UNSAFE(kInterruptReasonCounter); | 42 const size_t kInterruptReasonCount = ARRAYSIZE_UNSAFE(kInterruptReasonCounter); |
43 | 43 |
44 // DownloadItemModel with mocks several methods. | 44 // Default target path for a mock download item in DownloadItemModelTest. |
45 class TestDownloadItemModel : public DownloadItemModel { | 45 const FilePath::CharType kDefaultTargetFilePath[] = |
46 public: | 46 FILE_PATH_LITERAL("/foo/bar/foo.bar"); |
47 explicit TestDownloadItemModel(content::DownloadItem* download) | |
48 : DownloadItemModel(download) { | |
49 } | |
50 | 47 |
51 MOCK_CONST_METHOD0(GetTotalBytes, int64()); | 48 const FilePath::CharType kDefaultDisplayFileName[] = |
52 MOCK_CONST_METHOD0(GetCompletedBytes, int64()); | 49 FILE_PATH_LITERAL("foo.bar"); |
53 }; | 50 |
| 51 // Default URL for a mock download item in DownloadItemModelTest. |
| 52 const char kDefaultURL[] = "http://example.com/foo.bar"; |
54 | 53 |
55 class DownloadItemModelTest : public testing::Test { | 54 class DownloadItemModelTest : public testing::Test { |
56 public: | 55 public: |
57 DownloadItemModelTest() {} | 56 DownloadItemModelTest() |
| 57 : model_(&item_) {} |
58 | 58 |
59 virtual ~DownloadItemModelTest() { | 59 virtual ~DownloadItemModelTest() { |
60 } | 60 } |
61 | 61 |
62 protected: | 62 protected: |
63 // Sets up defaults for the download item and sets |model_| to a new | 63 // Sets up defaults for the download item and sets |model_| to a new |
64 // DownloadItemModel that uses the mock download item. | 64 // DownloadItemModel that uses the mock download item. |
65 void SetupDownloadItemDefaults() { | 65 void SetupDownloadItemDefaults() { |
66 ON_CALL(item_, GetReceivedBytes()).WillByDefault(Return(1)); | 66 ON_CALL(item_, GetReceivedBytes()).WillByDefault(Return(1)); |
67 ON_CALL(item_, GetTotalBytes()).WillByDefault(Return(2)); | 67 ON_CALL(item_, GetTotalBytes()).WillByDefault(Return(2)); |
68 ON_CALL(item_, IsInProgress()).WillByDefault(Return(true)); | 68 ON_CALL(item_, IsInProgress()).WillByDefault(Return(true)); |
69 ON_CALL(item_, TimeRemaining(_)).WillByDefault(Return(false)); | 69 ON_CALL(item_, TimeRemaining(_)).WillByDefault(Return(false)); |
70 ON_CALL(item_, GetMimeType()).WillByDefault(Return("text/html")); | 70 ON_CALL(item_, GetMimeType()).WillByDefault(Return("text/html")); |
71 ON_CALL(item_, AllDataSaved()).WillByDefault(Return(false)); | 71 ON_CALL(item_, AllDataSaved()).WillByDefault(Return(false)); |
72 ON_CALL(item_, GetOpenWhenComplete()).WillByDefault(Return(false)); | 72 ON_CALL(item_, GetOpenWhenComplete()).WillByDefault(Return(false)); |
73 ON_CALL(item_, GetFileExternallyRemoved()).WillByDefault(Return(false)); | 73 ON_CALL(item_, GetFileExternallyRemoved()).WillByDefault(Return(false)); |
74 ON_CALL(item_, GetState()) | 74 ON_CALL(item_, GetState()) |
75 .WillByDefault(Return(content::DownloadItem::IN_PROGRESS)); | 75 .WillByDefault(Return(content::DownloadItem::IN_PROGRESS)); |
76 ON_CALL(item_, GetURL()) | 76 ON_CALL(item_, GetURL()) |
77 .WillByDefault(ReturnRefOfCopy(GURL("http://example.com/foo.bar"))); | 77 .WillByDefault(ReturnRefOfCopy(GURL(kDefaultURL))); |
78 ON_CALL(item_, GetFileNameToReportUser()) | 78 ON_CALL(item_, GetFileNameToReportUser()) |
79 .WillByDefault(Return(FilePath(FILE_PATH_LITERAL("foo.bar")))); | 79 .WillByDefault(Return(FilePath(kDefaultDisplayFileName))); |
| 80 ON_CALL(item_, GetTargetFilePath()) |
| 81 .WillByDefault(ReturnRefOfCopy(FilePath(kDefaultTargetFilePath))); |
80 ON_CALL(item_, GetTargetDisposition()) | 82 ON_CALL(item_, GetTargetDisposition()) |
81 .WillByDefault( | 83 .WillByDefault( |
82 Return(content::DownloadItem::TARGET_DISPOSITION_OVERWRITE)); | 84 Return(content::DownloadItem::TARGET_DISPOSITION_OVERWRITE)); |
83 ON_CALL(item_, IsPaused()).WillByDefault(Return(false)); | 85 ON_CALL(item_, IsPaused()).WillByDefault(Return(false)); |
84 | |
85 // Setup the model: | |
86 model_.reset(new NiceMock<TestDownloadItemModel>(&item_)); | |
87 ON_CALL(*model_.get(), GetTotalBytes()) | |
88 .WillByDefault(Return(2)); | |
89 ON_CALL(*model_.get(), GetCompletedBytes()) | |
90 .WillByDefault(Return(1)); | |
91 } | 86 } |
92 | 87 |
93 void SetupInterruptedDownloadItem(content::DownloadInterruptReason reason) { | 88 void SetupInterruptedDownloadItem(content::DownloadInterruptReason reason) { |
94 EXPECT_CALL(item_, GetLastReason()).WillRepeatedly(Return(reason)); | 89 EXPECT_CALL(item_, GetLastReason()).WillRepeatedly(Return(reason)); |
95 EXPECT_CALL(item_, GetState()) | 90 EXPECT_CALL(item_, GetState()) |
96 .WillRepeatedly(Return( | 91 .WillRepeatedly(Return( |
97 (reason == content::DOWNLOAD_INTERRUPT_REASON_NONE) ? | 92 (reason == content::DOWNLOAD_INTERRUPT_REASON_NONE) ? |
98 content::DownloadItem::IN_PROGRESS : | 93 content::DownloadItem::IN_PROGRESS : |
99 content::DownloadItem::INTERRUPTED)); | 94 content::DownloadItem::INTERRUPTED)); |
100 EXPECT_CALL(item_, IsInProgress()) | 95 EXPECT_CALL(item_, IsInProgress()) |
101 .WillRepeatedly(Return( | 96 .WillRepeatedly(Return( |
102 reason == content::DOWNLOAD_INTERRUPT_REASON_NONE)); | 97 reason == content::DOWNLOAD_INTERRUPT_REASON_NONE)); |
103 } | 98 } |
104 | 99 |
105 content::MockDownloadItem& item() { | 100 content::MockDownloadItem& item() { |
106 return item_; | 101 return item_; |
107 } | 102 } |
108 | 103 |
109 TestDownloadItemModel& model() { | 104 DownloadItemModel& model() { |
110 return *model_; | 105 return model_; |
111 } | 106 } |
112 | 107 |
113 private: | 108 private: |
114 scoped_ptr<TestDownloadItemModel> model_; | |
115 | |
116 NiceMock<content::MockDownloadItem> item_; | 109 NiceMock<content::MockDownloadItem> item_; |
| 110 DownloadItemModel model_; |
117 }; | 111 }; |
118 | 112 |
119 } // namespace | 113 } // namespace |
120 | 114 |
121 TEST_F(DownloadItemModelTest, InterruptedStatus) { | 115 TEST_F(DownloadItemModelTest, InterruptedStatus) { |
122 // Test that we have the correct interrupt status message for downloads that | 116 // Test that we have the correct interrupt status message for downloads that |
123 // are in the INTERRUPTED state. | 117 // are in the INTERRUPTED state. |
124 const struct TestCase { | 118 const struct TestCase { |
125 // The reason. | 119 // The reason. |
126 content::DownloadInterruptReason reason; | 120 content::DownloadInterruptReason reason; |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 { 0, 2, true, true, true, "0/2 B, Paused" }, | 315 { 0, 2, true, true, true, "0/2 B, Paused" }, |
322 { 1, 2, true, true, true, "1/2 B, Paused" }, | 316 { 1, 2, true, true, true, "1/2 B, Paused" }, |
323 }; | 317 }; |
324 | 318 |
325 SetupDownloadItemDefaults(); | 319 SetupDownloadItemDefaults(); |
326 | 320 |
327 for (unsigned i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); i++) { | 321 for (unsigned i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); i++) { |
328 TestCase& test_case = kTestCases[i]; | 322 TestCase& test_case = kTestCases[i]; |
329 Mock::VerifyAndClearExpectations(&item()); | 323 Mock::VerifyAndClearExpectations(&item()); |
330 Mock::VerifyAndClearExpectations(&model()); | 324 Mock::VerifyAndClearExpectations(&model()); |
331 EXPECT_CALL(model(), GetCompletedBytes()) | 325 EXPECT_CALL(item(), GetReceivedBytes()) |
332 .WillRepeatedly(Return(test_case.received_bytes)); | 326 .WillRepeatedly(Return(test_case.received_bytes)); |
333 EXPECT_CALL(model(), GetTotalBytes()) | 327 EXPECT_CALL(item(), GetTotalBytes()) |
334 .WillRepeatedly(Return(test_case.total_bytes)); | 328 .WillRepeatedly(Return(test_case.total_bytes)); |
335 EXPECT_CALL(item(), TimeRemaining(_)) | 329 EXPECT_CALL(item(), TimeRemaining(_)) |
336 .WillRepeatedly(testing::DoAll( | 330 .WillRepeatedly(testing::DoAll( |
337 testing::SetArgPointee<0>(base::TimeDelta::FromSeconds(10)), | 331 testing::SetArgPointee<0>(base::TimeDelta::FromSeconds(10)), |
338 Return(test_case.time_remaining_known))); | 332 Return(test_case.time_remaining_known))); |
339 EXPECT_CALL(item(), GetOpenWhenComplete()) | 333 EXPECT_CALL(item(), GetOpenWhenComplete()) |
340 .WillRepeatedly(Return(test_case.open_when_complete)); | 334 .WillRepeatedly(Return(test_case.open_when_complete)); |
341 EXPECT_CALL(item(), IsPaused()) | 335 EXPECT_CALL(item(), IsPaused()) |
342 .WillRepeatedly(Return(test_case.is_paused)); | 336 .WillRepeatedly(Return(test_case.is_paused)); |
343 | 337 |
344 EXPECT_STREQ(test_case.expected_status, | 338 EXPECT_STREQ(test_case.expected_status, |
345 UTF16ToUTF8(model().GetStatusText()).c_str()); | 339 UTF16ToUTF8(model().GetStatusText()).c_str()); |
346 } | 340 } |
347 } | 341 } |
| 342 |
| 343 TEST_F(DownloadItemModelTest, ShouldShowInShelf) { |
| 344 SetupDownloadItemDefaults(); |
| 345 |
| 346 // By default the download item should be displayable on the shelf. |
| 347 EXPECT_TRUE(model().ShouldShowInShelf()); |
| 348 |
| 349 // Once explicitly set, ShouldShowInShelf() should return the explicit value. |
| 350 model().SetShouldShowInShelf(false); |
| 351 EXPECT_FALSE(model().ShouldShowInShelf()); |
| 352 |
| 353 model().SetShouldShowInShelf(true); |
| 354 EXPECT_TRUE(model().ShouldShowInShelf()); |
| 355 } |
OLD | NEW |