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

Side by Side Diff: chrome/browser/safe_browsing/incident_reporting/download_metadata_manager_unittest.cc

Issue 703463006: Fix DCHECK when shutting down safe browsing DownloadMetadataManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « chrome/browser/safe_browsing/incident_reporting/download_metadata_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/safe_browsing/incident_reporting/download_metadata_mana ger.h" 5 #include "chrome/browser/safe_browsing/incident_reporting/download_metadata_mana ger.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 // Capture the metadata manager's observer on the download manager. 173 // Capture the metadata manager's observer on the download manager.
174 EXPECT_CALL(download_manager_, AddObserver(&manager_)) 174 EXPECT_CALL(download_manager_, AddObserver(&manager_))
175 .WillOnce(SaveArg<0>(&dm_observer_)); 175 .WillOnce(SaveArg<0>(&dm_observer_));
176 manager_.AddDownloadManager(&download_manager_); 176 manager_.AddDownloadManager(&download_manager_);
177 } 177 }
178 178
179 // Shuts down the DownloadManager. Safe to call any number of times. 179 // Shuts down the DownloadManager. Safe to call any number of times.
180 void ShutdownDownloadManager() { 180 void ShutdownDownloadManager() {
181 if (dm_observer_) { 181 if (dm_observer_) {
182 dm_observer_->ManagerGoingDown(&download_manager_); 182 dm_observer_->ManagerGoingDown(&download_manager_);
183 // Note: these calls may result in "Uninteresting mock function call"
184 // warnings as a result of MockDownloadItem invoking observers in its
185 // dtor. This happens after the NiceMock wrapper has removed its niceness
186 // hook. These can safely be ignored, as they are entirely expected. The
187 // values specified by ON_CALL invocations in AddDownloadItems are
188 // returned as desired.
robertshield 2014/11/05 01:04:50 I don't know enough gmock to know whether this is
grt (UTC plus 2) 2014/11/05 01:25:35 The right thing when you don't want an expectation
189 other_item_.reset();
190 test_item_.reset();
183 dm_observer_ = nullptr; 191 dm_observer_ = nullptr;
184 } 192 }
185 } 193 }
186 194
187 // Adds two test DownloadItems to the DownloadManager. 195 // Adds two test DownloadItems to the DownloadManager.
188 void AddDownloadItems() { 196 void AddDownloadItems() {
189 ASSERT_NE(nullptr, dm_observer_); 197 ASSERT_NE(nullptr, dm_observer_);
190 // Add the item under test. 198 // Add the item under test.
191 test_item_.reset(new NiceMock<content::MockDownloadItem>); 199 test_item_.reset(new NiceMock<content::MockDownloadItem>);
192 ON_CALL(*test_item_, GetId()) 200 ON_CALL(*test_item_, GetId())
193 .WillByDefault(Return(kTestDownloadId)); 201 .WillByDefault(Return(kTestDownloadId));
194 ON_CALL(*test_item_, GetBrowserContext()) 202 ON_CALL(*test_item_, GetBrowserContext())
195 .WillByDefault(Return(&profile_)); 203 .WillByDefault(Return(&profile_));
196 ON_CALL(*test_item_, GetEndTime()) 204 ON_CALL(*test_item_, GetEndTime())
197 .WillByDefault(Return(base::Time::FromJsTime(kTestDownloadEndTimeMs))); 205 .WillByDefault(Return(base::Time::FromJsTime(kTestDownloadEndTimeMs)));
198 dm_observer_->OnDownloadCreated(&download_manager_, test_item_.get()); 206 dm_observer_->OnDownloadCreated(&download_manager_, test_item_.get());
199 207
200 // Add another item. 208 // Add another item.
201 other_item_.reset(new NiceMock<content::MockDownloadItem>); 209 other_item_.reset(new NiceMock<content::MockDownloadItem>);
202 ON_CALL(*other_item_, GetId()) 210 ON_CALL(*other_item_, GetId())
203 .WillByDefault(Return(kOtherDownloadId)); 211 .WillByDefault(Return(kOtherDownloadId));
204 ON_CALL(*other_item_, GetBrowserContext()) 212 ON_CALL(*other_item_, GetBrowserContext())
205 .WillByDefault(Return(&profile_)); 213 .WillByDefault(Return(&profile_));
206 ON_CALL(*test_item_, GetEndTime()) 214 ON_CALL(*test_item_, GetEndTime())
207 .WillByDefault(Return(base::Time::FromJsTime(kTestDownloadEndTimeMs))); 215 .WillByDefault(Return(base::Time::FromJsTime(kTestDownloadEndTimeMs)));
208 dm_observer_->OnDownloadCreated(&download_manager_, other_item_.get()); 216 dm_observer_->OnDownloadCreated(&download_manager_, other_item_.get());
209 } 217 }
210 218
211 // Destroyes the DownloadItems added to the manager. Safe to call any number
212 // of times.
213 void DestroyDownloadItems() {
214 other_item_.reset();
215 test_item_.reset();
216 }
217
218 content::TestBrowserThreadBundle thread_bundle_; 219 content::TestBrowserThreadBundle thread_bundle_;
219 NiceMock<MockDownloadMetadataManager> manager_; 220 NiceMock<MockDownloadMetadataManager> manager_;
220 TestingProfile profile_; 221 TestingProfile profile_;
221 NiceMock<content::MockDownloadManager> download_manager_; 222 NiceMock<content::MockDownloadManager> download_manager_;
222 scoped_ptr<content::MockDownloadItem> test_item_; 223 scoped_ptr<content::MockDownloadItem> test_item_;
223 scoped_ptr<content::MockDownloadItem> other_item_; 224 scoped_ptr<content::MockDownloadItem> other_item_;
224 content::DownloadManager::Observer* dm_observer_; 225 content::DownloadManager::Observer* dm_observer_;
225 }; 226 };
226 227
227 // A parameterized test that exercises GetDownloadDetails. The parameters 228 // A parameterized test that exercises GetDownloadDetails. The parameters
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 ResultOf(GetDetailsOpenTime, Eq(0))))); 323 ResultOf(GetDetailsOpenTime, Eq(0)))));
323 } 324 }
324 } else { 325 } else {
325 // No file on disk, so expect that the callback is invoked with null. 326 // No file on disk, so expect that the callback is invoked with null.
326 EXPECT_CALL(details_getter, OnDownloadDetails(IsNull())); 327 EXPECT_CALL(details_getter, OnDownloadDetails(IsNull()));
327 } 328 }
328 329
329 // Fire in the hole! 330 // Fire in the hole!
330 manager_.GetDownloadDetails(&profile_, details_getter.GetCallback()); 331 manager_.GetDownloadDetails(&profile_, details_getter.GetCallback());
331 332
332 // Destroy download items and shutdown the download manager, if relevant. 333 // Shutdown the download manager, if relevant.
333 if (early_shutdown_) { 334 if (early_shutdown_)
334 DestroyDownloadItems();
335 ShutdownDownloadManager(); 335 ShutdownDownloadManager();
336 }
337 336
338 // Allow the read task and the response callback to run. 337 // Allow the read task and the response callback to run.
339 RunAllTasks(); 338 RunAllTasks();
340 339
341 // Destroy download items and shutdown the download manager, if relevant. 340 // Shutdown the download manager, if relevant.
342 DestroyDownloadItems();
343 ShutdownDownloadManager(); 341 ShutdownDownloadManager();
344 } 342 }
345 343
346 INSTANTIATE_TEST_CASE_P( 344 INSTANTIATE_TEST_CASE_P(
347 DownloadMetadataManager, 345 DownloadMetadataManager,
348 GetDetailsTest, 346 GetDetailsTest,
349 testing::Combine( 347 testing::Combine(
350 testing::Values("absent", "present"), 348 testing::Values("absent", "present"),
351 testing::Values("not_managed", "managed"), 349 testing::Values("not_managed", "managed"),
352 testing::Values("not_created", "created", "opened", "removed"), 350 testing::Values("not_created", "created", "opened", "removed"),
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
457 // Expect that the callback is invoked with details for this item. 455 // Expect that the callback is invoked with details for this item.
458 EXPECT_CALL( 456 EXPECT_CALL(
459 details_getter, 457 details_getter,
460 OnDownloadDetails(ResultOf(GetDetailsDownloadUrl, StrEq(kNewUrl)))); 458 OnDownloadDetails(ResultOf(GetDetailsDownloadUrl, StrEq(kNewUrl))));
461 } else { 459 } else {
462 // Expect that the callback is invoked with null to clear stale metadata. 460 // Expect that the callback is invoked with null to clear stale metadata.
463 EXPECT_CALL(details_getter, OnDownloadDetails(IsNull())); 461 EXPECT_CALL(details_getter, OnDownloadDetails(IsNull()));
464 } 462 }
465 manager_.GetDownloadDetails(&profile_, details_getter.GetCallback()); 463 manager_.GetDownloadDetails(&profile_, details_getter.GetCallback());
466 464
467 DestroyDownloadItems();
468 ShutdownDownloadManager(); 465 ShutdownDownloadManager();
469 466
470 scoped_ptr<DownloadMetadata> metadata(ReadTestMetadataFile()); 467 scoped_ptr<DownloadMetadata> metadata(ReadTestMetadataFile());
471 if (set_request_) { 468 if (set_request_) {
472 // Expect that the file contains metadata for the download. 469 // Expect that the file contains metadata for the download.
473 ASSERT_TRUE(metadata); 470 ASSERT_TRUE(metadata);
474 EXPECT_EQ(kTestDownloadId, metadata->download_id()); 471 EXPECT_EQ(kTestDownloadId, metadata->download_id());
475 EXPECT_STREQ(kNewUrl, metadata->download().download().url().c_str()); 472 EXPECT_STREQ(kNewUrl, metadata->download().download().url().c_str());
476 } else { 473 } else {
477 // Expect that the file is not present. 474 // Expect that the file is not present.
478 ASSERT_FALSE(metadata); 475 ASSERT_FALSE(metadata);
479 } 476 }
480 } 477 }
481 478
482 INSTANTIATE_TEST_CASE_P( 479 INSTANTIATE_TEST_CASE_P(
483 DownloadMetadataManager, 480 DownloadMetadataManager,
484 SetRequestTest, 481 SetRequestTest,
485 testing::Combine(testing::Values("absent", "this", "other", "unknown"), 482 testing::Combine(testing::Values("absent", "this", "other", "unknown"),
486 testing::Values("none", "pending"), 483 testing::Values("none", "pending"),
487 testing::Values("none", "pending"), 484 testing::Values("none", "pending"),
488 testing::Values("waiting", "loaded"), 485 testing::Values("waiting", "loaded"),
489 testing::Values("clear", "set"))); 486 testing::Values("clear", "set")));
490 487
491 } // namespace safe_browsing 488 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/incident_reporting/download_metadata_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698