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

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: Add testing 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"
12 #include "chrome/browser/profiles/profile_statistics_common.h" 17 #include "chrome/browser/profiles/profile_statistics_common.h"
18 #include "chrome/browser/profiles/profile_statistics_factory.h"
19 #include "chrome/common/pref_names.h"
13 #include "chrome/test/base/testing_browser_process.h" 20 #include "chrome/test/base/testing_browser_process.h"
14 #include "chrome/test/base/testing_profile.h" 21 #include "chrome/test/base/testing_profile.h"
15 #include "chrome/test/base/testing_profile_manager.h" 22 #include "chrome/test/base/testing_profile_manager.h"
23 #include "components/bookmarks/browser/bookmark_model.h"
24 #include "components/prefs/pref_service.h"
25 #include "content/public/browser/browser_thread.h"
16 #include "content/public/test/test_browser_thread_bundle.h" 26 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
18 28
19 namespace { 29 namespace {
30
31 scoped_ptr<KeyedService> BuildBookmarkModelWithoutLoad(
32 content::BrowserContext* context) {
33 Profile* profile = Profile::FromBrowserContext(context);
34 scoped_ptr<bookmarks::BookmarkModel> bookmark_model(
35 new bookmarks::BookmarkModel(make_scoped_ptr(new ChromeBookmarkClient(
36 profile, ManagedBookmarkServiceFactory::GetForProfile(profile)))));
37 return std::move(bookmark_model);
38 }
39
40 void LoadBookmarkModel(Profile* profile,
41 bookmarks::BookmarkModel* bookmark_model) {
42 bookmark_model->Load(profile->GetPrefs(),
43 profile->GetPrefs()->GetString(prefs::kAcceptLanguages),
44 profile->GetPath(),
45 profile->GetIOTaskRunner(),
46 content::BrowserThread::GetMessageLoopProxyForThread(
47 content::BrowserThread::UI));
48 }
49
50 bookmarks::BookmarkModel* CreateBookmarkModelWithoutLoad(Profile* profile) {
51 return static_cast<bookmarks::BookmarkModel*>(
52 BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse(
53 profile, BuildBookmarkModelWithoutLoad));
54 }
55
20 void VerifyStatisticsCache(const base::FilePath& profile_path, 56 void VerifyStatisticsCache(const base::FilePath& profile_path,
21 const std::map<std::string, int>& expected, 57 const std::map<std::string, int>& expected,
22 const std::vector<std::string>& categories_to_check) { 58 const std::vector<std::string>& categories_to_check) {
23 const profiles::ProfileCategoryStats actual = 59 const profiles::ProfileCategoryStats actual =
24 ProfileStatistics::GetProfileStatisticsFromAttributesStorage( 60 ProfileStatistics::GetProfileStatisticsFromAttributesStorage(
25 profile_path); 61 profile_path);
26 62
27 EXPECT_EQ(categories_to_check.size(), actual.size()); 63 EXPECT_EQ(categories_to_check.size(), actual.size());
28 64
29 std::set<std::string> checked; 65 std::set<std::string> checked;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 // Now no keys are set. 115 // Now no keys are set.
80 VerifyStatisticsCache(profile_path, expected, categories_to_check); 116 VerifyStatisticsCache(profile_path, expected, categories_to_check);
81 // Insert items and test after each insert. 117 // Insert items and test after each insert.
82 for (const auto& item : insertions) { 118 for (const auto& item : insertions) {
83 ProfileStatistics::SetProfileStatisticsToAttributesStorage( 119 ProfileStatistics::SetProfileStatisticsToAttributesStorage(
84 profile_path, item.first, item.second); 120 profile_path, item.first, item.second);
85 expected[item.first] = item.second; 121 expected[item.first] = item.second;
86 VerifyStatisticsCache(profile_path, expected, categories_to_check); 122 VerifyStatisticsCache(profile_path, expected, categories_to_check);
87 } 123 }
88 } 124 }
125
126 TEST_F(ProfileStatisticsTest, WaitOrCountBookmarks) {
127 TestingProfile* profile = manager()->CreateTestingProfile("Test 1");
128 ASSERT_TRUE(profile);
129
130 bookmarks::BookmarkModel* bookmark_model =
131 CreateBookmarkModelWithoutLoad(profile);
132 ASSERT_TRUE(bookmark_model);
133
134 ProfileStatistics* profile_stat =
135 ProfileStatisticsFactory::GetForProfile(profile);
136
137 // Run ProfileStatisticsAggregator::WaitOrCountBookmarks twice.
138 for (int i = 0; i < 2; i++) {
139 profile_stat->GatherStatistics(profiles::ProfileStatisticsCallback());
140 // Wait until ProfileStatisticsAggregator::WaitOrCountBookmarks is run.
141 base::RunLoop run_loop;
142 run_loop.RunUntilIdle();
143 }
144
145 // Load the bookmark model to allow the bookmark counting task to finish. This
Mike Lerman 2016/04/04 15:38:58 Rather than saying "this checks for a bug..." you
lwchkg 2016/04/04 19:29:03 Verification is hard... but done.
146 // checks for a bug in ProfileStatisticsAggregator::WaitOrCountBookmarks. See
147 // https://crbug.com/596693.
148 LoadBookmarkModel(profile, bookmark_model);
149 }
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