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> |
| 8 |
7 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
10 #include "base/string16.h" | 12 #include "base/string16.h" |
| 13 #include "base/string_util.h" |
11 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
12 #include "content/test/mock_download_item.h" | 15 #include "content/test/mock_download_item.h" |
| 16 #include "grit/generated_resources.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "grit/generated_resources.h" | |
16 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
17 #include "ui/base/text/bytes_formatting.h" | 20 #include "ui/base/text/bytes_formatting.h" |
| 21 #include "ui/gfx/font.h" |
18 | 22 |
19 using ::testing::AtLeast; | 23 using ::testing::AtLeast; |
20 using ::testing::Mock; | 24 using ::testing::Mock; |
21 using ::testing::NiceMock; | 25 using ::testing::NiceMock; |
22 using ::testing::Return; | 26 using ::testing::Return; |
23 using ::testing::ReturnRef; | 27 using ::testing::ReturnRef; |
24 using ::testing::ReturnRefOfCopy; | 28 using ::testing::ReturnRefOfCopy; |
25 using ::testing::SetArgPointee; | 29 using ::testing::SetArgPointee; |
26 using ::testing::StrictMock; | 30 using ::testing::StrictMock; |
27 using ::testing::_; | 31 using ::testing::_; |
28 | 32 |
29 content::DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; | 33 namespace { |
| 34 |
| 35 // All the interrupt reasons we know of. The reason codes are unfortunately |
| 36 // sparse, making this array necessary. |
| 37 content::DownloadInterruptReason kInterruptReasons[] = { |
| 38 content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 39 #define INTERRUPT_REASON(name,value) \ |
| 40 content::DOWNLOAD_INTERRUPT_REASON_##name, |
| 41 #include "content/public/browser/download_interrupt_reason_values.h" |
| 42 #undef INTERRUPT_REASON |
| 43 }; |
30 | 44 |
31 class DownloadItemModelTest : public testing::Test { | 45 class DownloadItemModelTest : public testing::Test { |
32 public: | 46 public: |
33 DownloadItemModelTest() {} | 47 DownloadItemModelTest() {} |
34 | 48 |
35 virtual ~DownloadItemModelTest() { | 49 virtual ~DownloadItemModelTest() { |
36 } | 50 } |
37 | 51 |
38 protected: | 52 protected: |
39 void SetupDownloadItemDefaults() { | 53 void SetupDownloadItemDefaults(const GURL& url) { |
40 ON_CALL(item_, GetReceivedBytes()).WillByDefault(Return(1024)); | 54 ON_CALL(item_, GetReceivedBytes()).WillByDefault(Return(1024)); |
41 ON_CALL(item_, GetTotalBytes()).WillByDefault(Return(2048)); | 55 ON_CALL(item_, GetTotalBytes()).WillByDefault(Return(2048)); |
42 ON_CALL(item_, IsInProgress()).WillByDefault(Return(false)); | 56 ON_CALL(item_, IsInProgress()).WillByDefault(Return(false)); |
43 ON_CALL(item_, TimeRemaining(_)).WillByDefault(Return(false)); | 57 ON_CALL(item_, TimeRemaining(_)).WillByDefault(Return(false)); |
44 ON_CALL(item_, GetState()). | 58 ON_CALL(item_, PromptUserForSaveLocation()) |
45 WillByDefault(Return(content::DownloadItem::INTERRUPTED)); | 59 .WillByDefault(Return(false)); |
46 ON_CALL(item_, PromptUserForSaveLocation()). | |
47 WillByDefault(Return(false)); | |
48 ON_CALL(item_, GetMimeType()).WillByDefault(Return("text/html")); | 60 ON_CALL(item_, GetMimeType()).WillByDefault(Return("text/html")); |
49 ON_CALL(item_, AllDataSaved()).WillByDefault(Return(false)); | 61 ON_CALL(item_, AllDataSaved()).WillByDefault(Return(false)); |
50 ON_CALL(item_, GetOpenWhenComplete()).WillByDefault(Return(false)); | 62 ON_CALL(item_, GetOpenWhenComplete()).WillByDefault(Return(false)); |
51 ON_CALL(item_, GetFileExternallyRemoved()).WillByDefault(Return(false)); | 63 ON_CALL(item_, GetFileExternallyRemoved()).WillByDefault(Return(false)); |
| 64 ON_CALL(item_, GetState()) |
| 65 .WillByDefault(Return(content::DownloadItem::IN_PROGRESS)); |
| 66 ON_CALL(item_, GetURL()).WillByDefault(ReturnRefOfCopy(url)); |
| 67 ON_CALL(item_, GetFileNameToReportUser()) |
| 68 .WillByDefault(Return(FilePath(FILE_PATH_LITERAL("foo.bar")))); |
52 } | 69 } |
53 | 70 |
54 void SetupDownloadItem(const GURL& url, | 71 void SetupInterruptedDownloadItem(content::DownloadInterruptReason reason) { |
55 content::DownloadInterruptReason reason) { | 72 EXPECT_CALL(item_, GetLastReason()).WillRepeatedly(Return(reason)); |
| 73 EXPECT_CALL(item_, GetState()) |
| 74 .WillRepeatedly(Return( |
| 75 (reason == content::DOWNLOAD_INTERRUPT_REASON_NONE) ? |
| 76 content::DownloadItem::IN_PROGRESS : |
| 77 content::DownloadItem::INTERRUPTED)); |
56 model_.reset(new DownloadItemModel(&item_)); | 78 model_.reset(new DownloadItemModel(&item_)); |
57 EXPECT_CALL(item_, GetURL()).WillRepeatedly(ReturnRefOfCopy(url)); | |
58 EXPECT_CALL(item_, GetLastReason()).WillOnce(Return(reason)); | |
59 } | 79 } |
60 | 80 |
61 void TestDownloadItemModelStatusText( | 81 void TestStatusText( |
62 const GURL& url, content::DownloadInterruptReason reason) { | 82 content::DownloadInterruptReason reason) { |
63 SetupDownloadItem(url, reason); | 83 SetupInterruptedDownloadItem(reason); |
64 | 84 |
65 int64 size = item_.GetReceivedBytes(); | 85 int64 size = item_.GetReceivedBytes(); |
66 int64 total = item_.GetTotalBytes(); | 86 int64 total = item_.GetTotalBytes(); |
67 bool know_size = (total > 0); | |
68 | 87 |
69 ui::DataUnits amount_units = ui::GetByteDisplayUnits(total); | 88 ui::DataUnits amount_units = ui::GetByteDisplayUnits(total); |
70 string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, false); | 89 string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, false); |
71 string16 simple_total = base::i18n::GetDisplayStringInLTRDirectionality( | 90 string16 simple_total = base::i18n::GetDisplayStringInLTRDirectionality( |
72 ui::FormatBytesWithUnits(total, amount_units, true)); | 91 ui::FormatBytesWithUnits(total, amount_units, true)); |
73 | 92 |
74 string16 status_text; | 93 string16 status_text; |
75 string16 size_text; | 94 if (reason != content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED) { |
| 95 string16 size_text = l10n_util::GetStringFUTF16( |
| 96 IDS_DOWNLOAD_RECEIVED_SIZE, simple_size, simple_total); |
| 97 status_text = size_text; |
| 98 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 99 status_text = status_text + ASCIIToUTF16(" ") + |
| 100 DownloadItemModel::InterruptReasonStatusMessage(reason); |
| 101 } |
| 102 } else { |
| 103 status_text = DownloadItemModel::InterruptReasonStatusMessage(reason); |
| 104 } |
76 | 105 |
77 status_text = DownloadItemModel::InterruptReasonStatusMessage(reason); | 106 DVLOG(1) << " " << __FUNCTION__ << "()" << " '" << status_text << "'"; |
78 | |
79 if (know_size) { | |
80 size_text = l10n_util::GetStringFUTF16(IDS_DOWNLOAD_RECEIVED_SIZE, | |
81 simple_size, | |
82 simple_total); | |
83 } else { | |
84 size_text = ui::FormatBytes(size); | |
85 } | |
86 size_text = size_text + ASCIIToUTF16(" "); | |
87 if (reason != content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED) | |
88 status_text = size_text + status_text; | |
89 | |
90 DVLOG(2) << " " << __FUNCTION__ << "()" << " '" << status_text << "'"; | |
91 | 107 |
92 string16 text = model_->GetStatusText(); | 108 string16 text = model_->GetStatusText(); |
93 | |
94 EXPECT_EQ(status_text, text); | 109 EXPECT_EQ(status_text, text); |
| 110 ::testing::Mock::VerifyAndClearExpectations(&item_); |
95 } | 111 } |
96 | 112 |
97 void TestDownloadItemModelStatusTextAllReasons(const GURL& url) { | 113 void TestTooltipWithFixedWidth(const gfx::Font& font, int width) { |
98 SetupDownloadItemDefaults(); | 114 content::DownloadInterruptReason reason = item_.GetLastReason(); |
| 115 string16 tooltip = model_->GetTooltipText(font, width); |
| 116 std::vector<string16> lines; |
99 | 117 |
100 #define INTERRUPT_REASON(name, value) \ | 118 DVLOG(1) << "Tooltip: '" << tooltip << "'"; |
101 TestDownloadItemModelStatusText(url, \ | 119 Tokenize(tooltip, ASCIIToUTF16("\n"), &lines); |
102 content::DOWNLOAD_INTERRUPT_REASON_##name); | 120 for (unsigned i = 0; i < lines.size(); ++i) |
| 121 EXPECT_GE(width, font.GetStringWidth(lines[i])); |
| 122 if (reason == content::DOWNLOAD_INTERRUPT_REASON_NONE || |
| 123 reason == content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED) { |
| 124 // Only expect the filename for non-interrupted and canceled downloads. |
| 125 EXPECT_EQ(1u, lines.size()); |
| 126 } else { |
| 127 EXPECT_EQ(2u, lines.size()); |
| 128 } |
| 129 } |
103 | 130 |
104 #include "content/public/browser/download_interrupt_reason_values.h" | 131 void TestTooltipText(content::DownloadInterruptReason reason) { |
| 132 const int kSmallTooltipWidth = 40; |
| 133 const int kLargeTooltipWidth = 1000; |
| 134 gfx::Font font; |
105 | 135 |
106 #undef INTERRUPT_REASON | 136 SetupInterruptedDownloadItem(reason); |
| 137 TestTooltipWithFixedWidth(font, kSmallTooltipWidth); |
| 138 TestTooltipWithFixedWidth(font, kLargeTooltipWidth); |
| 139 ::testing::Mock::VerifyAndClearExpectations(&item_); |
107 } | 140 } |
108 | 141 |
109 private: | 142 private: |
110 scoped_ptr<DownloadItemModel> model_; | 143 scoped_ptr<DownloadItemModel> model_; |
111 | 144 |
112 NiceMock<content::MockDownloadItem> item_; | 145 NiceMock<content::MockDownloadItem> item_; |
113 }; | 146 }; |
114 | 147 |
| 148 } // namespace |
| 149 |
115 // Test download error messages. | 150 // Test download error messages. |
116 TEST_F(DownloadItemModelTest, Interrupts) { | 151 TEST_F(DownloadItemModelTest, InterruptStatus) { |
117 TestDownloadItemModelStatusTextAllReasons(GURL("http://foo.bar/")); | 152 SetupDownloadItemDefaults(GURL("http://foo.bar/")); |
| 153 // We are iterating through all the reason codes to make sure all message |
| 154 // strings have the correct format placeholders. If not, GetStringFUTF16() |
| 155 // will throw up. |
| 156 for (unsigned i = 0; i < arraysize(kInterruptReasons); ++i) |
| 157 TestStatusText(kInterruptReasons[i]); |
118 } | 158 } |
| 159 |
| 160 TEST_F(DownloadItemModelTest, InterruptTooltip) { |
| 161 SetupDownloadItemDefaults(GURL("http://foo.bar/")); |
| 162 // Iterating through all the reason codes to exercise all the status strings. |
| 163 // We also check whether the strings are properly elided. |
| 164 for (unsigned i = 0; i < arraysize(kInterruptReasons); ++i) |
| 165 TestTooltipText(kInterruptReasons[i]); |
| 166 } |
OLD | NEW |