OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "base/stl_util.h" | |
7 #include "base/threading/thread.h" | 6 #include "base/threading/thread.h" |
8 #include "chrome/test/base/testing_profile.h" | 7 #include "chrome/test/base/testing_profile.h" |
9 #include "content/browser/download/download_create_info.h" | 8 #include "content/browser/download/download_create_info.h" |
10 #include "content/browser/download/download_id.h" | 9 #include "content/browser/download/download_id.h" |
11 #include "content/browser/download/download_id_factory.h" | 10 #include "content/browser/download/download_id_factory.h" |
12 #include "content/browser/download/download_item_impl.h" | 11 #include "content/browser/download/download_item.h" |
13 #include "content/browser/download/download_request_handle.h" | 12 #include "content/browser/download/download_request_handle.h" |
14 #include "content/browser/download/download_status_updater.h" | 13 #include "content/browser/download/download_status_updater.h" |
15 #include "content/browser/download/interrupt_reasons.h" | 14 #include "content/browser/download/interrupt_reasons.h" |
16 #include "content/browser/download/mock_download_item.h" | 15 #include "content/browser/download/mock_download_item.h" |
| 16 #include "content/browser/download/mock_download_manager.h" |
| 17 #include "content/browser/download/mock_download_manager_delegate.h" |
17 #include "content/test/test_browser_thread.h" | 18 #include "content/test/test_browser_thread.h" |
18 #include "testing/gmock/include/gmock/gmock.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 | 20 |
21 using content::BrowserThread; | 21 using content::BrowserThread; |
22 | 22 |
23 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; | 23 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; |
24 | 24 |
25 namespace { | |
26 class MockDelegate : public DownloadItemImpl::Delegate { | |
27 public: | |
28 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath& path)); | |
29 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItem* download)); | |
30 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItem* download)); | |
31 MOCK_METHOD1(MaybeCompleteDownload, void(DownloadItem* download)); | |
32 MOCK_CONST_METHOD0(BrowserContext, content::BrowserContext*()); | |
33 MOCK_METHOD1(DownloadCancelled, void(DownloadItem* download)); | |
34 MOCK_METHOD1(DownloadCompleted, void(DownloadItem* download)); | |
35 MOCK_METHOD1(DownloadOpened, void(DownloadItem* download)); | |
36 MOCK_METHOD1(DownloadRemoved, void(DownloadItem* download)); | |
37 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItem* download)); | |
38 }; | |
39 | |
40 class MockRequestHandle : public DownloadRequestHandleInterface { | |
41 public: | |
42 MOCK_CONST_METHOD0(GetTabContents, TabContents*()); | |
43 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); | |
44 MOCK_CONST_METHOD0(PauseRequest, void()); | |
45 MOCK_CONST_METHOD0(ResumeRequest, void()); | |
46 MOCK_CONST_METHOD0(CancelRequest, void()); | |
47 MOCK_CONST_METHOD0(DebugString, std::string()); | |
48 }; | |
49 | |
50 } | |
51 | |
52 class DownloadItemTest : public testing::Test { | 25 class DownloadItemTest : public testing::Test { |
53 public: | 26 public: |
54 class MockObserver : public DownloadItem::Observer { | 27 class MockObserver : public DownloadItem::Observer { |
55 public: | 28 public: |
56 explicit MockObserver(DownloadItem* item) : item_(item), updated_(false) { | 29 explicit MockObserver(DownloadItem* item) : item_(item), updated_(false) { |
57 item_->AddObserver(this); | 30 item_->AddObserver(this); |
58 } | 31 } |
59 ~MockObserver() { item_->RemoveObserver(this); } | 32 ~MockObserver() { item_->RemoveObserver(this); } |
60 | 33 |
61 virtual void OnDownloadUpdated(DownloadItem* download) { updated_ = true; } | 34 virtual void OnDownloadUpdated(DownloadItem* download) { updated_ = true; } |
62 | 35 |
63 virtual void OnDownloadOpened(DownloadItem* download) { } | 36 virtual void OnDownloadOpened(DownloadItem* download) { } |
64 | 37 |
65 bool CheckUpdated() { | 38 bool CheckUpdated() { |
66 bool was_updated = updated_; | 39 bool was_updated = updated_; |
67 updated_ = false; | 40 updated_ = false; |
68 return was_updated; | 41 return was_updated; |
69 } | 42 } |
70 | 43 |
71 private: | 44 private: |
72 DownloadItem* item_; | 45 DownloadItem* item_; |
73 bool updated_; | 46 bool updated_; |
74 }; | 47 }; |
75 | 48 |
76 DownloadItemTest() | 49 DownloadItemTest() |
77 : id_factory_(new DownloadIdFactory(kValidDownloadItemIdDomain)), | 50 : id_factory_(new DownloadIdFactory(kValidDownloadItemIdDomain)), |
| 51 profile_(new TestingProfile()), |
78 ui_thread_(BrowserThread::UI, &loop_) { | 52 ui_thread_(BrowserThread::UI, &loop_) { |
79 } | 53 } |
80 | 54 |
81 ~DownloadItemTest() { | 55 ~DownloadItemTest() { |
82 } | 56 } |
83 | 57 |
84 virtual void SetUp() { | 58 virtual void SetUp() { |
| 59 download_manager_delegate_.reset(new MockDownloadManagerDelegate()); |
| 60 download_manager_ = new MockDownloadManager( |
| 61 download_manager_delegate_.get(), |
| 62 id_factory_, |
| 63 &download_status_updater_); |
| 64 download_manager_->Init(profile_.get()); |
85 } | 65 } |
86 | 66 |
87 virtual void TearDown() { | 67 virtual void TearDown() { |
| 68 download_manager_->Shutdown(); |
| 69 // When a DownloadManager's reference count drops to 0, it is not |
| 70 // deleted immediately. Instead, a task is posted to the UI thread's |
| 71 // message loop to delete it. |
| 72 // So, drop the reference count to 0 and run the message loop once |
| 73 // to ensure that all resources are cleaned up before the test exits. |
| 74 download_manager_ = NULL; |
| 75 profile_.reset(NULL); |
88 ui_thread_.DeprecatedGetThreadObject()->message_loop()->RunAllPending(); | 76 ui_thread_.DeprecatedGetThreadObject()->message_loop()->RunAllPending(); |
89 STLDeleteElements(&allocated_downloads_); | |
90 allocated_downloads_.clear(); | |
91 } | 77 } |
92 | 78 |
93 // This class keeps ownership of the created download item; it will | |
94 // be torn down at the end of the test. | |
95 DownloadItem* CreateDownloadItem(DownloadItem::DownloadState state) { | 79 DownloadItem* CreateDownloadItem(DownloadItem::DownloadState state) { |
96 // Normally, the download system takes ownership of info, and is | 80 // Normally, the download system takes ownership of info, and is |
97 // responsible for deleting it. In these unit tests, however, we | 81 // responsible for deleting it. In these unit tests, however, we |
98 // don't call the function that deletes it, so we do so ourselves. | 82 // don't call the function that deletes it, so we do so ourselves. |
99 scoped_ptr<DownloadCreateInfo> info_; | 83 scoped_ptr<DownloadCreateInfo> info_; |
100 | 84 |
101 info_.reset(new DownloadCreateInfo()); | 85 info_.reset(new DownloadCreateInfo()); |
102 info_->download_id = id_factory_->GetNextId(); | 86 info_->download_id = id_factory_->GetNextId(); |
103 info_->prompt_user_for_save_location = false; | 87 info_->prompt_user_for_save_location = false; |
104 info_->url_chain.push_back(GURL()); | 88 info_->url_chain.push_back(GURL()); |
105 info_->state = state; | 89 info_->state = state; |
106 | 90 |
107 MockRequestHandle* request_handle = | 91 download_manager_->CreateDownloadItem(info_.get(), DownloadRequestHandle()); |
108 new testing::NiceMock<MockRequestHandle>; | 92 return download_manager_->GetActiveDownloadItem(info_->download_id.local()); |
109 DownloadItem* download = | |
110 new DownloadItemImpl(&delegate_, *(info_.get()), | |
111 request_handle, false); | |
112 allocated_downloads_.push_back(download); | |
113 return download; | |
114 } | 93 } |
115 | 94 |
116 protected: | 95 protected: |
117 DownloadStatusUpdater download_status_updater_; | 96 DownloadStatusUpdater download_status_updater_; |
| 97 scoped_ptr<MockDownloadManagerDelegate> download_manager_delegate_; |
| 98 scoped_refptr<DownloadManager> download_manager_; |
118 | 99 |
119 private: | 100 private: |
120 scoped_refptr<DownloadIdFactory> id_factory_; | 101 scoped_refptr<DownloadIdFactory> id_factory_; |
| 102 scoped_ptr<TestingProfile> profile_; |
121 MessageLoopForUI loop_; | 103 MessageLoopForUI loop_; |
122 // UI thread. | 104 // UI thread. |
123 content::TestBrowserThread ui_thread_; | 105 content::TestBrowserThread ui_thread_; |
124 testing::NiceMock<MockDelegate> delegate_; | |
125 std::vector<DownloadItem*> allocated_downloads_; | |
126 }; | 106 }; |
127 | 107 |
128 namespace { | 108 namespace { |
129 | 109 |
130 const int kDownloadChunkSize = 1000; | 110 const int kDownloadChunkSize = 1000; |
131 const int kDownloadSpeed = 1000; | 111 const int kDownloadSpeed = 1000; |
132 const int kDummyDBHandle = 10; | 112 const int kDummyDBHandle = 10; |
133 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); | 113 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); |
134 | 114 |
135 } // namespace | 115 } // namespace |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 item->TogglePause(); | 250 item->TogglePause(); |
271 ASSERT_TRUE(observer.CheckUpdated()); | 251 ASSERT_TRUE(observer.CheckUpdated()); |
272 | 252 |
273 item->TogglePause(); | 253 item->TogglePause(); |
274 ASSERT_TRUE(observer.CheckUpdated()); | 254 ASSERT_TRUE(observer.CheckUpdated()); |
275 } | 255 } |
276 | 256 |
277 TEST(MockDownloadItem, Compiles) { | 257 TEST(MockDownloadItem, Compiles) { |
278 MockDownloadItem mock_item; | 258 MockDownloadItem mock_item; |
279 } | 259 } |
OLD | NEW |