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

Side by Side Diff: chrome/browser/profiles/profile_statistics_unittest.cc

Issue 1838083006: Fix a use-after-free error in WaitOrCountBookmarks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: The test now checks that the bookmarking test runs exactly once. Created 4 years, 8 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 | « chrome/browser/profiles/profile_statistics_aggregator.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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 <map> 5 #include <map>
6 #include <set> 6 #include <set>
7 #include <utility> 7 #include <utility>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/run_loop.h"
13 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
14 #include "chrome/browser/bookmarks/chrome_bookmark_client.h"
15 #include "chrome/browser/bookmarks/managed_bookmark_service_factory.h"
11 #include "chrome/browser/profiles/profile_statistics.h" 16 #include "chrome/browser/profiles/profile_statistics.h"
17 #include "chrome/browser/profiles/profile_statistics_aggregator.h"
12 #include "chrome/browser/profiles/profile_statistics_common.h" 18 #include "chrome/browser/profiles/profile_statistics_common.h"
19 #include "chrome/browser/profiles/profile_statistics_factory.h"
20 #include "chrome/common/pref_names.h"
13 #include "chrome/test/base/testing_browser_process.h" 21 #include "chrome/test/base/testing_browser_process.h"
14 #include "chrome/test/base/testing_profile.h" 22 #include "chrome/test/base/testing_profile.h"
15 #include "chrome/test/base/testing_profile_manager.h" 23 #include "chrome/test/base/testing_profile_manager.h"
24 #include "components/bookmarks/browser/bookmark_model.h"
25 #include "components/prefs/pref_service.h"
26 #include "content/public/browser/browser_thread.h"
16 #include "content/public/test/test_browser_thread_bundle.h" 27 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
18 29
19 namespace { 30 namespace {
31
32 scoped_ptr<KeyedService> BuildBookmarkModelWithoutLoad(
33 content::BrowserContext* context) {
34 Profile* profile = Profile::FromBrowserContext(context);
35 scoped_ptr<bookmarks::BookmarkModel> bookmark_model(
36 new bookmarks::BookmarkModel(make_scoped_ptr(new ChromeBookmarkClient(
37 profile, ManagedBookmarkServiceFactory::GetForProfile(profile)))));
38 return std::move(bookmark_model);
39 }
40
41 void LoadBookmarkModel(Profile* profile,
42 bookmarks::BookmarkModel* bookmark_model) {
43 bookmark_model->Load(profile->GetPrefs(),
44 profile->GetPrefs()->GetString(prefs::kAcceptLanguages),
45 profile->GetPath(),
46 profile->GetIOTaskRunner(),
47 content::BrowserThread::GetMessageLoopProxyForThread(
48 content::BrowserThread::UI));
49 }
50
51 bookmarks::BookmarkModel* CreateBookmarkModelWithoutLoad(Profile* profile) {
52 return static_cast<bookmarks::BookmarkModel*>(
53 BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse(
54 profile, BuildBookmarkModelWithoutLoad));
55 }
56
57 class BookmarkStatHelper {
58 public:
59 BookmarkStatHelper() : num_of_times_called_(0) {}
60
61 void StatsCallback(profiles::ProfileCategoryStats stats) {
62 if (stats.back().category == profiles::kProfileStatisticsBookmarks)
63 ++num_of_times_called_;
64 }
65
66 int GetNumOfTimesCalled() { return num_of_times_called_; }
67
68 private:
69 base::Closure quit_closure_;
70 int num_of_times_called_;
71 };
72
20 void VerifyStatisticsCache(const base::FilePath& profile_path, 73 void VerifyStatisticsCache(const base::FilePath& profile_path,
21 const std::map<std::string, int>& expected, 74 const std::map<std::string, int>& expected,
22 const std::vector<std::string>& categories_to_check) { 75 const std::vector<std::string>& categories_to_check) {
23 const profiles::ProfileCategoryStats actual = 76 const profiles::ProfileCategoryStats actual =
24 ProfileStatistics::GetProfileStatisticsFromAttributesStorage( 77 ProfileStatistics::GetProfileStatisticsFromAttributesStorage(
25 profile_path); 78 profile_path);
26 79
27 EXPECT_EQ(categories_to_check.size(), actual.size()); 80 EXPECT_EQ(categories_to_check.size(), actual.size());
28 81
29 std::set<std::string> checked; 82 std::set<std::string> checked;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 // Now no keys are set. 132 // Now no keys are set.
80 VerifyStatisticsCache(profile_path, expected, categories_to_check); 133 VerifyStatisticsCache(profile_path, expected, categories_to_check);
81 // Insert items and test after each insert. 134 // Insert items and test after each insert.
82 for (const auto& item : insertions) { 135 for (const auto& item : insertions) {
83 ProfileStatistics::SetProfileStatisticsToAttributesStorage( 136 ProfileStatistics::SetProfileStatisticsToAttributesStorage(
84 profile_path, item.first, item.second); 137 profile_path, item.first, item.second);
85 expected[item.first] = item.second; 138 expected[item.first] = item.second;
86 VerifyStatisticsCache(profile_path, expected, categories_to_check); 139 VerifyStatisticsCache(profile_path, expected, categories_to_check);
87 } 140 }
88 } 141 }
142
143 TEST_F(ProfileStatisticsTest, WaitOrCountBookmarks) {
144 TestingProfile* profile = manager()->CreateTestingProfile("Test 1");
145 ASSERT_TRUE(profile);
146
147 bookmarks::BookmarkModel* bookmark_model =
148 CreateBookmarkModelWithoutLoad(profile);
149 ASSERT_TRUE(bookmark_model);
150
151 // Run ProfileStatisticsAggregator::WaitOrCountBookmarks.
152 ProfileStatisticsAggregator* aggregator;
153 BookmarkStatHelper bookmark_stat_helper;
154 base::RunLoop run_loop_aggregator_destruction;
155 // The following should run inside a scope, so the scoped_refptr gets deleted
156 // immediately.
157 {
158 scoped_refptr<ProfileStatisticsAggregator> aggregator_scoped =
Mike Lerman 2016/04/04 20:26:27 Why delete this aggregator? It won't actually comp
lwchkg 2016/04/04 23:22:52 By constructing the aggregator it makes a copy of
159 new ProfileStatisticsAggregator(
160 profile,
161 base::Bind(&BookmarkStatHelper::StatsCallback,
162 base::Unretained(&bookmark_stat_helper)),
163 run_loop_aggregator_destruction.QuitClosure());
164 aggregator = aggregator_scoped.get();
165 }
166 // Wait until ProfileStatisticsAggregator::WaitOrCountBookmarks is run.
167 base::RunLoop run_loop1;
168 run_loop1.RunUntilIdle();
169 EXPECT_EQ(0u, bookmark_stat_helper.GetNumOfTimesCalled());
170
171 // Run ProfileStatisticsAggregator::WaitOrCountBookmarks again.
172 aggregator->AddCallbackAndStartAggregator(
173 profiles::ProfileStatisticsCallback());
174 // Wait until ProfileStatisticsAggregator::WaitOrCountBookmarks is run.
175 base::RunLoop run_loop2;
176 run_loop2.RunUntilIdle();
177 EXPECT_EQ(0u, bookmark_stat_helper.GetNumOfTimesCalled());
178
179 // Load the bookmark model. When the model is loaded (asynchronously), the
180 // observer added by WaitOrCountBookmarks is run.
181 LoadBookmarkModel(profile, bookmark_model);
182
183 run_loop_aggregator_destruction.Run();
184 EXPECT_EQ(1u, bookmark_stat_helper.GetNumOfTimesCalled());
185 }
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profile_statistics_aggregator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698