Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(414)

Unified Diff: content/browser/download/download_item_impl_unittest.cc

Issue 10665049: Make DownloadHistory observe manager, items (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/download/download_item_impl_unittest.cc
diff --git a/content/browser/download/download_item_impl_unittest.cc b/content/browser/download/download_item_impl_unittest.cc
index 502e426aa5417f472f1692991b7df8ed06805d5f..abbd52e75247763268a7d78a4a3db39a2d690b1b 100644
--- a/content/browser/download/download_item_impl_unittest.cc
+++ b/content/browser/download/download_item_impl_unittest.cc
@@ -131,16 +131,42 @@ class DownloadItemTest : public testing::Test {
public:
class MockObserver : public DownloadItem::Observer {
public:
- explicit MockObserver(DownloadItem* item) : item_(item), updated_(false) {
+ explicit MockObserver(DownloadItem* item)
+ : item_(item),
+ removed_(false),
+ destroyed_(false),
+ updated_(false) {
item_->AddObserver(this);
}
- ~MockObserver() { item_->RemoveObserver(this); }
+
+ virtual ~MockObserver() {
+ if (item_) item_->RemoveObserver(this);
+ }
+
+ virtual void OnDownloadRemoved(DownloadItem* download) {
+ removed_ = true;
+ }
virtual void OnDownloadUpdated(DownloadItem* download) {
updated_ = true;
}
- virtual void OnDownloadOpened(DownloadItem* download) { }
+ virtual void OnDownloadOpened(DownloadItem* download) {
+ }
+
+ virtual void OnDownloadDestroyed(DownloadItem* download) {
+ destroyed_ = true;
+ item_->RemoveObserver(this);
+ item_ = NULL;
+ }
+
+ bool CheckRemoved() {
+ return removed_;
+ }
+
+ bool CheckDestroyed() {
+ return destroyed_;
+ }
bool CheckUpdated() {
bool was_updated = updated_;
@@ -150,6 +176,8 @@ class DownloadItemTest : public testing::Test {
private:
DownloadItem* item_;
+ bool removed_;
+ bool destroyed_;
bool updated_;
};
@@ -302,12 +330,21 @@ TEST_F(DownloadItemTest, NotificationAfterDelete) {
ASSERT_TRUE(observer.CheckUpdated());
}
+TEST_F(DownloadItemTest, NotificationAfterDestroyed) {
+ DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
+ MockObserver observer(item);
+
+ DestroyDownloadItem(item);
+ ASSERT_TRUE(observer.CheckDestroyed());
+}
+
TEST_F(DownloadItemTest, NotificationAfterRemove) {
DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
MockObserver observer(item);
item->Remove();
ASSERT_TRUE(observer.CheckUpdated());
+ ASSERT_TRUE(observer.CheckRemoved());
}
TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) {
@@ -408,73 +445,6 @@ TEST_F(DownloadItemTest, DisplayName) {
item->GetFileNameToReportUser().value());
}
-static char external_data_test_string[] = "External data test";
-static int destructor_called = 0;
-
-class TestExternalData : public DownloadItem::ExternalData {
- public:
- int value;
- virtual ~TestExternalData() {
- destructor_called++;
- }
-};
-
-TEST_F(DownloadItemTest, ExternalData) {
- DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
- const DownloadItemImpl* const_item = item;
-
- // Shouldn't be anything there before set.
- EXPECT_EQ(NULL, item->GetExternalData(&external_data_test_string));
- EXPECT_EQ(NULL, const_item->GetExternalData(&external_data_test_string));
-
- TestExternalData* test1(new TestExternalData());
- test1->value = 2;
-
- // Should be able to get back what you set.
- item->SetExternalData(&external_data_test_string, test1);
- TestExternalData* test_result =
- static_cast<TestExternalData*>(
- item->GetExternalData(&external_data_test_string));
- EXPECT_EQ(test1, test_result);
-
- // Ditto for const lookup.
- const TestExternalData* test_const_result =
- static_cast<const TestExternalData*>(
- const_item->GetExternalData(&external_data_test_string));
- EXPECT_EQ(static_cast<const TestExternalData*>(test1),
- test_const_result);
-
- // Destructor should be called if value overwritten. New value
- // should then be retrievable.
- TestExternalData* test2(new TestExternalData());
- test2->value = 3;
- EXPECT_EQ(0, destructor_called);
- item->SetExternalData(&external_data_test_string, test2);
- EXPECT_EQ(1, destructor_called);
- EXPECT_EQ(static_cast<DownloadItem::ExternalData*>(test2),
- item->GetExternalData(&external_data_test_string));
-
- // Overwriting with the same value shouldn't do anything.
- EXPECT_EQ(1, destructor_called);
- item->SetExternalData(&external_data_test_string, test2);
- EXPECT_EQ(1, destructor_called);
- EXPECT_EQ(static_cast<DownloadItem::ExternalData*>(test2),
- item->GetExternalData(&external_data_test_string));
-
- // Overwriting with NULL should result in destruction.
- item->SetExternalData(&external_data_test_string, NULL);
- EXPECT_EQ(2, destructor_called);
-
- // Destroying the download item should destroy the external data.
-
- TestExternalData* test3(new TestExternalData());
- item->SetExternalData(&external_data_test_string, test3);
- EXPECT_EQ(static_cast<DownloadItem::ExternalData*>(test3),
- item->GetExternalData(&external_data_test_string));
- DestroyDownloadItem(item);
- EXPECT_EQ(3, destructor_called);
-}
-
// Test that the delegate is invoked after the download file is renamed.
// Delegate::DownloadRenamedToIntermediateName() should be invoked when the
// download is renamed to the intermediate name.

Powered by Google App Engine
This is Rietveld 408576698