Index: chrome/browser/download/download_item_model_unittest.cc |
diff --git a/chrome/browser/download/download_item_model_unittest.cc b/chrome/browser/download/download_item_model_unittest.cc |
index 9f32de28eda2b59f289588bf0f296e3839796b13..8a4bea39a6402b6e1ceac8fddd8cafa9b91ff799 100644 |
--- a/chrome/browser/download/download_item_model_unittest.cc |
+++ b/chrome/browser/download/download_item_model_unittest.cc |
@@ -4,17 +4,21 @@ |
#include "chrome/browser/download/download_item_model.h" |
+#include <vector> |
+ |
#include "base/i18n/rtl.h" |
#include "base/logging.h" |
#include "base/message_loop.h" |
#include "base/string16.h" |
+#include "base/string_util.h" |
#include "base/utf_string_conversions.h" |
#include "content/test/mock_download_item.h" |
+#include "grit/generated_resources.h" |
#include "testing/gmock/include/gmock/gmock.h" |
#include "testing/gtest/include/gtest/gtest.h" |
-#include "grit/generated_resources.h" |
#include "ui/base/l10n/l10n_util.h" |
#include "ui/base/text/bytes_formatting.h" |
+#include "ui/gfx/font.h" |
using ::testing::AtLeast; |
using ::testing::Mock; |
@@ -26,7 +30,17 @@ using ::testing::SetArgPointee; |
using ::testing::StrictMock; |
using ::testing::_; |
-content::DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; |
+namespace { |
+ |
+// All the interrupt reasons we know of. The reason codes are unfortunately |
+// sparse, making this array necessary. |
+content::DownloadInterruptReason kInterruptReasons[] = { |
+ content::DOWNLOAD_INTERRUPT_REASON_NONE, |
+#define INTERRUPT_REASON(name,value) \ |
+ content::DOWNLOAD_INTERRUPT_REASON_##name, |
+#include "content/public/browser/download_interrupt_reason_values.h" |
+#undef INTERRUPT_REASON |
+}; |
class DownloadItemModelTest : public testing::Test { |
public: |
@@ -36,35 +50,40 @@ class DownloadItemModelTest : public testing::Test { |
} |
protected: |
- void SetupDownloadItemDefaults() { |
+ void SetupDownloadItemDefaults(const GURL& url) { |
ON_CALL(item_, GetReceivedBytes()).WillByDefault(Return(1024)); |
ON_CALL(item_, GetTotalBytes()).WillByDefault(Return(2048)); |
ON_CALL(item_, IsInProgress()).WillByDefault(Return(false)); |
ON_CALL(item_, TimeRemaining(_)).WillByDefault(Return(false)); |
- ON_CALL(item_, GetState()). |
- WillByDefault(Return(content::DownloadItem::INTERRUPTED)); |
- ON_CALL(item_, PromptUserForSaveLocation()). |
- WillByDefault(Return(false)); |
+ ON_CALL(item_, PromptUserForSaveLocation()) |
+ .WillByDefault(Return(false)); |
ON_CALL(item_, GetMimeType()).WillByDefault(Return("text/html")); |
ON_CALL(item_, AllDataSaved()).WillByDefault(Return(false)); |
ON_CALL(item_, GetOpenWhenComplete()).WillByDefault(Return(false)); |
ON_CALL(item_, GetFileExternallyRemoved()).WillByDefault(Return(false)); |
+ ON_CALL(item_, GetState()) |
+ .WillByDefault(Return(content::DownloadItem::IN_PROGRESS)); |
+ ON_CALL(item_, GetURL()).WillByDefault(ReturnRefOfCopy(url)); |
+ ON_CALL(item_, GetFileNameToReportUser()) |
+ .WillByDefault(Return(FilePath(FILE_PATH_LITERAL("foo.bar")))); |
} |
- void SetupDownloadItem(const GURL& url, |
- content::DownloadInterruptReason reason) { |
+ void SetupInterruptedDownloadItem(content::DownloadInterruptReason reason) { |
+ EXPECT_CALL(item_, GetLastReason()).WillRepeatedly(Return(reason)); |
+ EXPECT_CALL(item_, GetState()) |
+ .WillRepeatedly(Return( |
+ (reason == content::DOWNLOAD_INTERRUPT_REASON_NONE) ? |
+ content::DownloadItem::IN_PROGRESS : |
+ content::DownloadItem::INTERRUPTED)); |
model_.reset(new DownloadItemModel(&item_)); |
- EXPECT_CALL(item_, GetURL()).WillRepeatedly(ReturnRefOfCopy(url)); |
- EXPECT_CALL(item_, GetLastReason()).WillOnce(Return(reason)); |
} |
- void TestDownloadItemModelStatusText( |
- const GURL& url, content::DownloadInterruptReason reason) { |
- SetupDownloadItem(url, reason); |
+ void TestStatusText( |
+ content::DownloadInterruptReason reason) { |
+ SetupInterruptedDownloadItem(reason); |
int64 size = item_.GetReceivedBytes(); |
int64 total = item_.GetTotalBytes(); |
- bool know_size = (total > 0); |
ui::DataUnits amount_units = ui::GetByteDisplayUnits(total); |
string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, false); |
@@ -72,38 +91,52 @@ class DownloadItemModelTest : public testing::Test { |
ui::FormatBytesWithUnits(total, amount_units, true)); |
string16 status_text; |
- string16 size_text; |
- |
- status_text = DownloadItemModel::InterruptReasonStatusMessage(reason); |
- |
- if (know_size) { |
- size_text = l10n_util::GetStringFUTF16(IDS_DOWNLOAD_RECEIVED_SIZE, |
- simple_size, |
- simple_total); |
+ if (reason != content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED) { |
+ string16 size_text = l10n_util::GetStringFUTF16( |
+ IDS_DOWNLOAD_RECEIVED_SIZE, simple_size, simple_total); |
+ status_text = size_text; |
+ if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) { |
+ status_text = status_text + ASCIIToUTF16(" ") + |
+ DownloadItemModel::InterruptReasonStatusMessage(reason); |
+ } |
} else { |
- size_text = ui::FormatBytes(size); |
+ status_text = DownloadItemModel::InterruptReasonStatusMessage(reason); |
} |
- size_text = size_text + ASCIIToUTF16(" "); |
- if (reason != content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED) |
- status_text = size_text + status_text; |
- DVLOG(2) << " " << __FUNCTION__ << "()" << " '" << status_text << "'"; |
+ DVLOG(1) << " " << __FUNCTION__ << "()" << " '" << status_text << "'"; |
string16 text = model_->GetStatusText(); |
- |
EXPECT_EQ(status_text, text); |
+ ::testing::Mock::VerifyAndClearExpectations(&item_); |
} |
- void TestDownloadItemModelStatusTextAllReasons(const GURL& url) { |
- SetupDownloadItemDefaults(); |
- |
-#define INTERRUPT_REASON(name, value) \ |
- TestDownloadItemModelStatusText(url, \ |
- content::DOWNLOAD_INTERRUPT_REASON_##name); |
+ void TestTooltipWithFixedWidth(const gfx::Font& font, int width) { |
+ content::DownloadInterruptReason reason = item_.GetLastReason(); |
+ string16 tooltip = model_->GetTooltipText(font, width); |
+ std::vector<string16> lines; |
+ |
+ DVLOG(1) << "Tooltip: '" << tooltip << "'"; |
+ Tokenize(tooltip, ASCIIToUTF16("\n"), &lines); |
+ for (unsigned i = 0; i < lines.size(); ++i) |
+ EXPECT_GE(width, font.GetStringWidth(lines[i])); |
+ if (reason == content::DOWNLOAD_INTERRUPT_REASON_NONE || |
+ reason == content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED) { |
+ // Only expect the filename for non-interrupted and canceled downloads. |
+ EXPECT_EQ(1u, lines.size()); |
+ } else { |
+ EXPECT_EQ(2u, lines.size()); |
+ } |
+ } |
- #include "content/public/browser/download_interrupt_reason_values.h" |
+ void TestTooltipText(content::DownloadInterruptReason reason) { |
+ const int kSmallTooltipWidth = 40; |
+ const int kLargeTooltipWidth = 1000; |
+ gfx::Font font; |
-#undef INTERRUPT_REASON |
+ SetupInterruptedDownloadItem(reason); |
+ TestTooltipWithFixedWidth(font, kSmallTooltipWidth); |
+ TestTooltipWithFixedWidth(font, kLargeTooltipWidth); |
+ ::testing::Mock::VerifyAndClearExpectations(&item_); |
} |
private: |
@@ -112,7 +145,22 @@ class DownloadItemModelTest : public testing::Test { |
NiceMock<content::MockDownloadItem> item_; |
}; |
+} // namespace |
+ |
// Test download error messages. |
-TEST_F(DownloadItemModelTest, Interrupts) { |
- TestDownloadItemModelStatusTextAllReasons(GURL("http://foo.bar/")); |
+TEST_F(DownloadItemModelTest, InterruptStatus) { |
+ SetupDownloadItemDefaults(GURL("http://foo.bar/")); |
+ // We are iterating through all the reason codes to make sure all message |
+ // strings have the correct format placeholders. If not, GetStringFUTF16() |
+ // will throw up. |
+ for (unsigned i = 0; i < arraysize(kInterruptReasons); ++i) |
+ TestStatusText(kInterruptReasons[i]); |
+} |
+ |
+TEST_F(DownloadItemModelTest, InterruptTooltip) { |
+ SetupDownloadItemDefaults(GURL("http://foo.bar/")); |
+ // Iterating through all the reason codes to exercise all the status strings. |
+ // We also check whether the strings are properly elided. |
+ for (unsigned i = 0; i < arraysize(kInterruptReasons); ++i) |
+ TestTooltipText(kInterruptReasons[i]); |
} |