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

Side by Side Diff: components/safe_browsing_db/v4_database_unittest.cc

Issue 2649643002: [S] Use a weak_factory for V4Database callbacks + test (Closed)
Patch Set: Nit: s/weak_factory_/weak_factory_on_io_ Created 3 years, 11 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 unified diff | Download patch
« no previous file with comments | « components/safe_browsing_db/v4_database.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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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" 5 #include "base/bind.h"
6 #include "base/debug/leak_annotations.h" 6 #include "base/debug/leak_annotations.h"
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/test/test_simple_task_runner.h" 10 #include "base/test/test_simple_task_runner.h"
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 database_dirname_.AppendASCII("win_url_malware.fake")); 103 database_dirname_.AppendASCII("win_url_malware.fake"));
104 104
105 list_infos_.emplace_back(true, "linux_url_malware", linux_malware_id_, 105 list_infos_.emplace_back(true, "linux_url_malware", linux_malware_id_,
106 SB_THREAT_TYPE_URL_MALWARE); 106 SB_THREAT_TYPE_URL_MALWARE);
107 expected_identifiers_.push_back(linux_malware_id_); 107 expected_identifiers_.push_back(linux_malware_id_);
108 expected_store_paths_.push_back( 108 expected_store_paths_.push_back(
109 database_dirname_.AppendASCII("linux_url_malware.fake")); 109 database_dirname_.AppendASCII("linux_url_malware.fake"));
110 } 110 }
111 111
112 void DatabaseUpdated() {} 112 void DatabaseUpdated() {}
113
113 void NewDatabaseReadyWithExpectedStorePathsAndIds( 114 void NewDatabaseReadyWithExpectedStorePathsAndIds(
114 std::unique_ptr<V4Database> v4_database) { 115 std::unique_ptr<V4Database> v4_database) {
115 ASSERT_TRUE(v4_database); 116 ASSERT_TRUE(v4_database);
116 ASSERT_TRUE(v4_database->store_map_); 117 ASSERT_TRUE(v4_database->store_map_);
117 118
118 // The following check ensures that the callback was called asynchronously. 119 // The following check ensures that the callback was called asynchronously.
119 EXPECT_TRUE(created_but_not_called_back_); 120 EXPECT_TRUE(created_but_not_called_back_);
120 121
121 ASSERT_EQ(expected_store_paths_.size(), v4_database->store_map_->size()); 122 ASSERT_EQ(expected_store_paths_.size(), v4_database->store_map_->size());
122 ASSERT_EQ(expected_identifiers_.size(), v4_database->store_map_->size()); 123 ASSERT_EQ(expected_identifiers_.size(), v4_database->store_map_->size());
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 470
470 EXPECT_TRUE(v4_database_->AreStoresAvailable( 471 EXPECT_TRUE(v4_database_->AreStoresAvailable(
471 StoresToCheck({linux_malware_id_, win_malware_id_}))); 472 StoresToCheck({linux_malware_id_, win_malware_id_})));
472 473
473 EXPECT_FALSE(v4_database_->AreStoresAvailable( 474 EXPECT_FALSE(v4_database_->AreStoresAvailable(
474 StoresToCheck({linux_malware_id_, bogus_id}))); 475 StoresToCheck({linux_malware_id_, bogus_id})));
475 476
476 EXPECT_FALSE(v4_database_->AreStoresAvailable(StoresToCheck({bogus_id}))); 477 EXPECT_FALSE(v4_database_->AreStoresAvailable(StoresToCheck({bogus_id})));
477 } 478 }
478 479
480 // Test to ensure that the callback to the database is dropped when the database
481 // gets destroyed. See http://crbug.com/683147#c5 for more details.
482 TEST_F(V4DatabaseTest, UsingWeakPtrDropsCallback) {
483 RegisterFactory();
484
485 // Step 1: Create the database.
486 V4Database::Create(task_runner_, database_dirname_, list_infos_,
487 callback_db_ready_);
488 created_but_not_called_back_ = true;
489 WaitForTasksOnTaskRunner();
490
491 // Step 2: Try to update the database. This posts V4Store::ApplyUpdate on the
492 // task runner.
493 std::unique_ptr<ParsedServerResponse> parsed_server_response(
494 new ParsedServerResponse);
495 auto lur = base::MakeUnique<ListUpdateResponse>();
496 lur->set_platform_type(linux_malware_id_.platform_type());
497 lur->set_threat_entry_type(linux_malware_id_.threat_entry_type());
498 lur->set_threat_type(linux_malware_id_.threat_type());
499 lur->set_new_client_state("new_state");
500 lur->set_response_type(ListUpdateResponse::FULL_UPDATE);
501 parsed_server_response->push_back(std::move(lur));
502
503 // We pass |null_callback| as the second argument to |ApplyUpdate| since we
504 // expect it to not get called. This callback method is called from
505 // V4Database::UpdatedStoreReady but we expect that call to get dropped.
506 base::Callback<void()> null_callback;
507 v4_database_->ApplyUpdate(std::move(parsed_server_response), null_callback);
508
509 // Step 3: Before V4Store::ApplyUpdate gets executed on the task runner,
510 // destroy the database. This posts ~V4Database() on the task runner.
511 V4Database::Destroy(std::move(v4_database_));
512
513 // Step 4: Wait for the task runner to go to completion. The test should
514 // finish to completion and the |null_callback| should not get called.
515 WaitForTasksOnTaskRunner();
516 }
517
479 } // namespace safe_browsing 518 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « components/safe_browsing_db/v4_database.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698