OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/bind.h" | |
6 #include "base/callback.h" | |
5 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
6 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
7 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
8 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
11 #include "base/observer_list.h" | |
12 #include "chrome/browser/download/download_history.h" | |
13 #include "chrome/browser/download/download_service.h" | |
14 #include "chrome/browser/download/download_service_factory.h" | |
9 #include "chrome/browser/download/download_ui_controller.h" | 15 #include "chrome/browser/download/download_ui_controller.h" |
16 #include "chrome/browser/history/download_row.h" | |
17 #include "chrome/browser/profiles/profile.h" | |
18 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
10 #include "content/public/test/mock_download_item.h" | 19 #include "content/public/test/mock_download_item.h" |
11 #include "content/public/test/mock_download_manager.h" | 20 #include "content/public/test/mock_download_manager.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
14 | 23 |
15 using content::MockDownloadItem; | 24 using content::MockDownloadItem; |
16 using content::MockDownloadManager; | 25 using content::MockDownloadManager; |
17 using testing::AnyNumber; | 26 using testing::AnyNumber; |
18 using testing::Assign; | 27 using testing::Assign; |
19 using testing::Return; | 28 using testing::Return; |
20 using testing::ReturnRefOfCopy; | 29 using testing::ReturnRefOfCopy; |
21 using testing::SaveArg; | 30 using testing::SaveArg; |
22 using testing::_; | 31 using testing::_; |
23 | 32 |
24 namespace { | 33 namespace { |
25 | 34 |
26 // A DownloadUIController::Delegate that stores the DownloadItem* for the last | 35 // A DownloadUIController::Delegate that stores the DownloadItem* for the last |
27 // download that was sent to the UI. | 36 // download that was sent to the UI. |
28 class TestDelegate : public DownloadUIController::Delegate { | 37 class TestDelegate : public DownloadUIController::Delegate { |
29 public: | 38 public: |
30 explicit TestDelegate(base::WeakPtr<content::DownloadItem*> receiver); | 39 explicit TestDelegate(base::WeakPtr<content::DownloadItem*> receiver); |
31 virtual ~TestDelegate() {} | 40 virtual ~TestDelegate() {} |
32 | 41 |
33 private: | 42 private: |
34 virtual void NotifyDownloadStarting(content::DownloadItem* item) OVERRIDE; | 43 virtual void OnNewDownloadReady(content::DownloadItem* item) OVERRIDE; |
35 | 44 |
36 base::WeakPtr<content::DownloadItem*> receiver_; | 45 base::WeakPtr<content::DownloadItem*> receiver_; |
37 }; | 46 }; |
38 | 47 |
39 TestDelegate::TestDelegate(base::WeakPtr<content::DownloadItem*> receiver) | 48 TestDelegate::TestDelegate(base::WeakPtr<content::DownloadItem*> receiver) |
40 : receiver_(receiver) { | 49 : receiver_(receiver) { |
41 } | 50 } |
42 | 51 |
43 void TestDelegate::NotifyDownloadStarting(content::DownloadItem* item) { | 52 void TestDelegate::OnNewDownloadReady(content::DownloadItem* item) { |
44 if (receiver_.get()) | 53 if (receiver_.get()) |
45 *receiver_ = item; | 54 *receiver_ = item; |
46 } | 55 } |
47 | 56 |
48 class DownloadUIControllerTest : public testing::Test { | 57 // A DownloadService that returns a custom DownloadHistory. |
58 class TestDownloadService : public DownloadService { | |
59 public: | |
60 explicit TestDownloadService(Profile* profile); | |
61 virtual ~TestDownloadService(); | |
62 | |
63 void set_download_history(scoped_ptr<DownloadHistory> download_history) { | |
64 download_history_.swap(download_history); | |
65 } | |
66 virtual DownloadHistory* GetDownloadHistory() OVERRIDE; | |
67 | |
68 private: | |
69 scoped_ptr<DownloadHistory> download_history_; | |
70 }; | |
71 | |
72 TestDownloadService::TestDownloadService(Profile* profile) | |
73 : DownloadService(profile) { | |
74 } | |
75 | |
76 TestDownloadService::~TestDownloadService() { | |
77 } | |
78 | |
79 DownloadHistory* TestDownloadService::GetDownloadHistory() { | |
80 return download_history_.get(); | |
81 } | |
82 | |
83 // A MockDownloadItem with a real observer list. Used because observers need to | |
84 // be reliably notified with a OnDownloadDestroyed() notification when the | |
85 // DownloadItem is being destroyed. | |
Randy Smith (Not in Mondays)
2014/04/21 18:21:22
This is the second time this functionality has bee
asanka
2014/04/23 05:51:09
Good catch. I added it to MockDownloadItem in a se
| |
86 class MockDownloadItemWithObservers : public MockDownloadItem { | |
87 public: | |
88 virtual ~MockDownloadItemWithObservers() { | |
89 FOR_EACH_OBSERVER(Observer, observer_list_, OnDownloadDestroyed(this)); | |
90 } | |
91 virtual void AddObserver(content::DownloadItem::Observer* observer) OVERRIDE { | |
92 observer_list_.AddObserver(observer); | |
93 } | |
94 virtual void RemoveObserver(content::DownloadItem::Observer* observer) | |
95 OVERRIDE { | |
96 observer_list_.RemoveObserver(observer); | |
97 } | |
98 virtual void UpdateObservers() OVERRIDE { | |
99 FOR_EACH_OBSERVER(Observer, observer_list_, OnDownloadUpdated(this)); | |
100 } | |
101 | |
102 private: | |
103 ObserverList<content::DownloadItem::Observer> observer_list_; | |
104 }; | |
105 | |
106 // The test fixture: | |
107 class DownloadUIControllerTest : public ChromeRenderViewHostTestHarness { | |
49 public: | 108 public: |
50 DownloadUIControllerTest(); | 109 DownloadUIControllerTest(); |
51 | 110 |
52 protected: | 111 protected: |
53 // testing::Test | 112 // testing::Test |
54 virtual void SetUp() OVERRIDE; | 113 virtual void SetUp() OVERRIDE; |
55 | 114 |
56 // Returns a MockDownloadItem that has AddObserver and RemoveObserver | 115 // Returns a MockDownloadItem representing an IN_PROGRESS download that has |
57 // expectations set up to store the observer in |item_observer_|. | 116 // non-empty current and target paths. |
58 scoped_ptr<MockDownloadItem> GetMockDownload(); | 117 scoped_ptr<MockDownloadItem> GetMockDownload(); |
59 | 118 |
60 // Returns a TestDelegate. Invoking NotifyDownloadStarting on the returned | 119 // Returns a TestDelegate. Invoking OnNewDownloadReady on the returned |
61 // delegate results in the DownloadItem* being stored in |received_item_|. | 120 // delegate results in the DownloadItem* being stored in |notified_item_|. |
62 scoped_ptr<DownloadUIController::Delegate> GetTestDelegate(); | 121 scoped_ptr<DownloadUIController::Delegate> GetTestDelegate(); |
63 | 122 |
64 MockDownloadManager* manager() { return manager_.get(); } | 123 MockDownloadManager* manager() { return manager_.get(); } |
124 | |
125 // Returns the DownloadManager::Observer registered by a test case. This is | |
126 // the DownloadUIController's observer for all current test cases. | |
65 content::DownloadManager::Observer* manager_observer() { | 127 content::DownloadManager::Observer* manager_observer() { |
66 return manager_observer_; | 128 return manager_observer_; |
67 } | 129 } |
68 content::DownloadItem::Observer* item_observer() { return item_observer_; } | 130 |
69 content::DownloadItem* received_item() { return received_item_; } | 131 // The most recent DownloadItem that was passed into OnNewDownloadReady(). |
132 content::DownloadItem* notified_item() { return notified_item_; } | |
133 | |
134 // DownloadHistory performs a query of existing downloads when it is first | |
135 // instantiated. This method returns the completion callback for that query. | |
136 // It can be used to inject history downloads. | |
137 const HistoryService::DownloadQueryCallback& history_query_callback() const { | |
138 return history_adapter_->download_query_callback_; | |
139 } | |
140 | |
141 // DownloadManager::Observer registered by DownloadHistory. | |
142 content::DownloadManager::Observer* download_history_manager_observer() { | |
143 return download_history_manager_observer_; | |
144 } | |
70 | 145 |
71 private: | 146 private: |
147 // A private history adapter that stores the DownloadQueryCallback when | |
148 // QueryDownloads is called. | |
149 class HistoryAdapter : public DownloadHistory::HistoryAdapter { | |
150 public: | |
151 HistoryAdapter() : DownloadHistory::HistoryAdapter(NULL) {} | |
152 HistoryService::DownloadQueryCallback download_query_callback_; | |
153 | |
154 private: | |
155 virtual void QueryDownloads( | |
156 const HistoryService::DownloadQueryCallback& callback) OVERRIDE { | |
157 download_query_callback_ = callback; | |
158 } | |
159 }; | |
160 | |
161 // Constructs and returns a TestDownloadService. | |
162 static KeyedService* TestingDownloadServiceFactory( | |
163 content::BrowserContext* brwoser_context); | |
164 | |
72 scoped_ptr<MockDownloadManager> manager_; | 165 scoped_ptr<MockDownloadManager> manager_; |
166 content::DownloadManager::Observer* download_history_manager_observer_; | |
73 content::DownloadManager::Observer* manager_observer_; | 167 content::DownloadManager::Observer* manager_observer_; |
74 content::DownloadItem::Observer* item_observer_; | 168 content::DownloadItem* notified_item_; |
75 content::DownloadItem* received_item_; | 169 base::WeakPtrFactory<content::DownloadItem*> notified_item_receiver_factory_; |
76 | 170 |
77 base::WeakPtrFactory<content::DownloadItem*> receiver_factory_; | 171 HistoryAdapter* history_adapter_; |
78 }; | 172 }; |
79 | 173 |
174 // static | |
175 KeyedService* DownloadUIControllerTest::TestingDownloadServiceFactory( | |
176 content::BrowserContext* browser_context) { | |
177 return new TestDownloadService(Profile::FromBrowserContext(browser_context)); | |
178 } | |
179 | |
80 DownloadUIControllerTest::DownloadUIControllerTest() | 180 DownloadUIControllerTest::DownloadUIControllerTest() |
81 : manager_observer_(NULL), | 181 : download_history_manager_observer_(NULL), |
82 item_observer_(NULL), | 182 manager_observer_(NULL), |
83 received_item_(NULL), | 183 notified_item_(NULL), |
84 receiver_factory_(&received_item_) { | 184 notified_item_receiver_factory_(¬ified_item_) { |
185 } | |
186 | |
187 // Mock action for conditionally resettting a pointer. Used as: | |
188 // EXPECT_CALL(Foo(_)) | |
189 // .WillOnce(ResetToNullIfEqual(&stored_pointer)); | |
190 ACTION_P(ResetToNullIfEqual, pointer) { | |
191 if (*pointer == arg0) | |
192 *pointer = NULL; | |
85 } | 193 } |
86 | 194 |
87 void DownloadUIControllerTest::SetUp() { | 195 void DownloadUIControllerTest::SetUp() { |
196 ChromeRenderViewHostTestHarness::SetUp(); | |
197 | |
88 manager_.reset(new testing::StrictMock<MockDownloadManager>()); | 198 manager_.reset(new testing::StrictMock<MockDownloadManager>()); |
89 EXPECT_CALL(*manager_, AddObserver(_)) | 199 EXPECT_CALL(*manager_, AddObserver(_)) |
200 .WillOnce(SaveArg<0>(&download_history_manager_observer_)); | |
201 EXPECT_CALL(*manager_, GetAllDownloads(_)).Times(AnyNumber()); | |
202 | |
203 scoped_ptr<HistoryAdapter> history_adapter(new HistoryAdapter); | |
204 history_adapter_ = history_adapter.get(); | |
205 scoped_ptr<DownloadHistory> download_history(new DownloadHistory( | |
206 manager_.get(), | |
207 history_adapter.PassAs<DownloadHistory::HistoryAdapter>())); | |
208 ASSERT_TRUE(download_history_manager_observer_); | |
209 | |
210 EXPECT_CALL(*manager_, AddObserver(_)) | |
Randy Smith (Not in Mondays)
2014/04/21 18:21:22
At this point, I trust your expertise with gmock m
asanka
2014/04/23 05:51:09
I changed the expectation to explicitly require ma
Randy Smith (Not in Mondays)
2014/04/23 19:18:57
Sorry, still confused. There are two EXPECT_CALL(
asanka
2014/05/07 23:19:53
There's an ASSERT_TRUE(download_history_manager_ob
Randy Smith (Not in Mondays)
2014/05/08 17:21:04
Ah, sorry, missed that. LGTM.
| |
90 .WillOnce(SaveArg<0>(&manager_observer_)); | 211 .WillOnce(SaveArg<0>(&manager_observer_)); |
91 EXPECT_CALL(*manager_, RemoveObserver(_)) | 212 EXPECT_CALL(*manager_, RemoveObserver(_)) |
92 .WillOnce(Assign(&manager_observer_, | 213 .WillRepeatedly(ResetToNullIfEqual(&manager_observer_)); |
93 static_cast<content::DownloadManager::Observer*>(NULL))); | 214 TestDownloadService* download_service = static_cast<TestDownloadService*>( |
94 EXPECT_CALL(*manager_, GetAllDownloads(_)); | 215 DownloadServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
216 browser_context(), &TestingDownloadServiceFactory)); | |
217 ASSERT_TRUE(download_service); | |
218 download_service->set_download_history(download_history.Pass()); | |
95 } | 219 } |
96 | 220 |
97 scoped_ptr<MockDownloadItem> DownloadUIControllerTest::GetMockDownload() { | 221 scoped_ptr<MockDownloadItem> DownloadUIControllerTest::GetMockDownload() { |
98 scoped_ptr<MockDownloadItem> item( | 222 scoped_ptr<MockDownloadItem> item( |
99 new testing::StrictMock<MockDownloadItem>()); | 223 new testing::StrictMock<MockDownloadItemWithObservers>()); |
100 EXPECT_CALL(*item, AddObserver(_)) | 224 EXPECT_CALL(*item, GetBrowserContext()) |
101 .WillOnce(SaveArg<0>(&item_observer_)); | 225 .WillRepeatedly(Return(browser_context())); |
102 EXPECT_CALL(*item, RemoveObserver(_)) | 226 EXPECT_CALL(*item, GetId()).WillRepeatedly(Return(1)); |
103 .WillOnce(Assign(&item_observer_, | 227 EXPECT_CALL(*item, GetTargetFilePath()).WillRepeatedly( |
104 static_cast<content::DownloadItem::Observer*>(NULL))); | 228 ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); |
229 EXPECT_CALL(*item, GetFullPath()).WillRepeatedly( | |
230 ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); | |
231 EXPECT_CALL(*item, GetState()) | |
232 .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS)); | |
233 EXPECT_CALL(*item, GetUrlChain()) | |
234 .WillRepeatedly(testing::ReturnRefOfCopy(std::vector<GURL>())); | |
235 EXPECT_CALL(*item, GetReferrerUrl()) | |
236 .WillRepeatedly(testing::ReturnRefOfCopy(GURL())); | |
237 EXPECT_CALL(*item, GetStartTime()).WillRepeatedly(Return(base::Time())); | |
238 EXPECT_CALL(*item, GetEndTime()).WillRepeatedly(Return(base::Time())); | |
239 EXPECT_CALL(*item, GetETag()).WillRepeatedly(ReturnRefOfCopy(std::string())); | |
240 EXPECT_CALL(*item, GetLastModifiedTime()) | |
241 .WillRepeatedly(ReturnRefOfCopy(std::string())); | |
242 EXPECT_CALL(*item, GetDangerType()) | |
243 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)); | |
244 EXPECT_CALL(*item, GetLastReason()) | |
245 .WillRepeatedly(Return(content::DOWNLOAD_INTERRUPT_REASON_NONE)); | |
246 EXPECT_CALL(*item, GetReceivedBytes()).WillRepeatedly(Return(0)); | |
247 EXPECT_CALL(*item, GetTotalBytes()).WillRepeatedly(Return(0)); | |
248 EXPECT_CALL(*item, GetTargetDisposition()).WillRepeatedly( | |
249 Return(content::DownloadItem::TARGET_DISPOSITION_OVERWRITE)); | |
250 EXPECT_CALL(*item, GetOpened()).WillRepeatedly(Return(false)); | |
251 EXPECT_CALL(*item, GetMimeType()).WillRepeatedly(Return(std::string())); | |
252 EXPECT_CALL(*item, GetURL()).WillRepeatedly(testing::ReturnRefOfCopy(GURL())); | |
253 EXPECT_CALL(*item, IsTemporary()).WillRepeatedly(Return(false)); | |
105 return item.Pass(); | 254 return item.Pass(); |
106 } | 255 } |
107 | 256 |
108 scoped_ptr<DownloadUIController::Delegate> | 257 scoped_ptr<DownloadUIController::Delegate> |
109 DownloadUIControllerTest::GetTestDelegate() { | 258 DownloadUIControllerTest::GetTestDelegate() { |
110 scoped_ptr<DownloadUIController::Delegate> delegate( | 259 scoped_ptr<DownloadUIController::Delegate> delegate( |
111 new TestDelegate(receiver_factory_.GetWeakPtr())); | 260 new TestDelegate(notified_item_receiver_factory_.GetWeakPtr())); |
112 return delegate.Pass(); | 261 return delegate.Pass(); |
113 } | 262 } |
114 | 263 |
115 // Normal downloads that are constructed in the IN_PROGRESS state should be | 264 // New downloads should be presented to the UI when GetTargetFilePath() returns |
116 // presented to the UI when GetTargetFilePath() returns a non-empty path. | 265 // a non-empty path. I.e. once the download target has been determined. |
117 // I.e. once the download target has been determined. | |
118 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) { | 266 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) { |
119 scoped_ptr<MockDownloadItem> item = GetMockDownload(); | 267 scoped_ptr<MockDownloadItem> item = GetMockDownload(); |
120 DownloadUIController controller(manager(), GetTestDelegate()); | 268 DownloadUIController controller(manager(), GetTestDelegate()); |
121 EXPECT_CALL(*item, GetTargetFilePath()) | 269 EXPECT_CALL(*item, GetTargetFilePath()) |
122 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 270 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
123 EXPECT_CALL(*item, GetState()) | |
124 .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS)); | |
125 | 271 |
126 ASSERT_TRUE(manager_observer()); | 272 ASSERT_TRUE(manager_observer()); |
127 manager_observer()->OnDownloadCreated(manager(), item.get()); | 273 manager_observer()->OnDownloadCreated(manager(), item.get()); |
128 | 274 |
129 // The destination for the download hasn't been determined yet. It should not | 275 // The destination for the download hasn't been determined yet. It should not |
130 // be displayed. | 276 // be displayed. |
131 EXPECT_FALSE(received_item()); | 277 EXPECT_FALSE(notified_item()); |
132 ASSERT_TRUE(item_observer()); | |
133 | 278 |
134 // Once the destination has been determined, then it should be displayed. | 279 // Once the destination has been determined, then it should be displayed. |
135 EXPECT_CALL(*item, GetTargetFilePath()) | 280 EXPECT_CALL(*item, GetTargetFilePath()) |
136 .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); | 281 .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); |
137 item_observer()->OnDownloadUpdated(item.get()); | 282 item->UpdateObservers(); |
138 | 283 |
139 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item()); | 284 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), notified_item()); |
285 } | |
286 | |
287 // A download that's created in an interrupted state should also be displayed. | |
288 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic_Interrupted) { | |
289 scoped_ptr<MockDownloadItem> item = GetMockDownload(); | |
290 DownloadUIController controller(manager(), GetTestDelegate()); | |
291 EXPECT_CALL(*item, GetState()) | |
292 .WillRepeatedly(Return(content::DownloadItem::INTERRUPTED)); | |
293 | |
294 ASSERT_TRUE(manager_observer()); | |
295 manager_observer()->OnDownloadCreated(manager(), item.get()); | |
296 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), notified_item()); | |
140 } | 297 } |
141 | 298 |
142 // Downloads that have a target path on creation and are in the IN_PROGRESS | 299 // Downloads that have a target path on creation and are in the IN_PROGRESS |
143 // state should be displayed in the UI immediately without requiring an | 300 // state should be displayed in the UI immediately without requiring an |
144 // additional OnDownloadUpdated() notification. | 301 // additional OnDownloadUpdated() notification. |
145 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) { | 302 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) { |
146 scoped_ptr<MockDownloadItem> item = GetMockDownload(); | 303 scoped_ptr<MockDownloadItem> item = GetMockDownload(); |
147 DownloadUIController controller(manager(), GetTestDelegate()); | 304 DownloadUIController controller(manager(), GetTestDelegate()); |
148 EXPECT_CALL(*item, GetTargetFilePath()) | |
149 .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); | |
150 EXPECT_CALL(*item, GetState()) | |
151 .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS)); | |
152 | 305 |
153 ASSERT_TRUE(manager_observer()); | 306 ASSERT_TRUE(manager_observer()); |
154 manager_observer()->OnDownloadCreated(manager(), item.get()); | 307 manager_observer()->OnDownloadCreated(manager(), item.get()); |
155 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item()); | 308 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), notified_item()); |
156 } | 309 } |
157 | 310 |
158 // History downloads (downloads that are not in IN_PROGRESS on create) should | 311 // The UI shouldn't be notified of downloads that were restored from history. |
159 // not be displayed on the shelf. | 312 TEST_F(DownloadUIControllerTest, DownloadUIController_HistoryDownload) { |
160 TEST_F(DownloadUIControllerTest, DownloadUIController_NoNotifyHistory) { | 313 DownloadUIController controller(manager(), GetTestDelegate()); |
314 // DownloadHistory should already have been created. It performs a query of | |
315 // existing downloads upon creation. We'll use the callback to inject a | |
316 // history download. | |
317 ASSERT_FALSE(history_query_callback().is_null()); | |
318 | |
319 // download_history_manager_observer is the DownloadManager::Observer | |
320 // registered by the DownloadHistory. DownloadHistory relies on the | |
321 // OnDownloadCreated notification to mark a download as having been restored | |
322 // from history. | |
323 ASSERT_TRUE(download_history_manager_observer()); | |
324 | |
325 scoped_ptr<std::vector<history::DownloadRow> > history_downloads; | |
326 history_downloads.reset(new std::vector<history::DownloadRow>()); | |
327 history_downloads->push_back(history::DownloadRow()); | |
328 history_downloads->front().id = 1; | |
329 | |
330 std::vector<GURL> url_chain; | |
331 GURL url; | |
161 scoped_ptr<MockDownloadItem> item = GetMockDownload(); | 332 scoped_ptr<MockDownloadItem> item = GetMockDownload(); |
162 DownloadUIController controller(manager(), GetTestDelegate()); | |
163 EXPECT_CALL(*item, GetState()) | |
164 .WillRepeatedly(Return(content::DownloadItem::COMPLETE)); | |
165 | 333 |
334 EXPECT_CALL(*manager(), CheckForHistoryFilesRemoval()); | |
335 | |
336 { | |
337 testing::InSequence s; | |
338 testing::MockFunction<void()> mock_function; | |
339 // DownloadHistory will immediately try to create a download using the info | |
340 // we push through the query callback. When DownloadManager::CreateDownload | |
341 // is called, we need to first invoke the OnDownloadCreated callback for | |
342 // DownloadHistory before returning the DownloadItem since that's the | |
343 // sequence of events expected by DownloadHistory. | |
344 base::Closure history_on_created_callback = | |
345 base::Bind(&content::DownloadManager::Observer::OnDownloadCreated, | |
346 base::Unretained(download_history_manager_observer()), | |
347 manager(), | |
348 item.get()); | |
349 EXPECT_CALL(*manager(), MockCreateDownloadItem(_)).WillOnce( | |
350 testing::DoAll(testing::InvokeWithoutArgs(&history_on_created_callback, | |
351 &base::Closure::Run), | |
352 Return(item.get()))); | |
353 EXPECT_CALL(mock_function, Call()); | |
354 | |
355 history_query_callback().Run(history_downloads.Pass()); | |
356 mock_function.Call(); | |
357 } | |
358 | |
359 // Now pass along the notification to the OnDownloadCreated observer from | |
360 // DownloadUIController. It should ignore the download since it's marked as | |
361 // being restored from history. | |
166 ASSERT_TRUE(manager_observer()); | 362 ASSERT_TRUE(manager_observer()); |
167 manager_observer()->OnDownloadCreated(manager(), item.get()); | 363 manager_observer()->OnDownloadCreated(manager(), item.get()); |
168 EXPECT_FALSE(received_item()); | |
169 | 364 |
170 item_observer()->OnDownloadUpdated(item.get()); | 365 // Finally, the expectation we've been waiting for: |
171 EXPECT_FALSE(received_item()); | 366 EXPECT_FALSE(notified_item()); |
172 } | 367 } |
173 | 368 |
174 } // namespace | 369 } // namespace |
OLD | NEW |