| OLD | NEW |
| 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->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 |
| 56 class BookmarkStatHelper { |
| 57 public: |
| 58 BookmarkStatHelper() : num_of_times_called_(0) {} |
| 59 |
| 60 void StatsCallback(profiles::ProfileCategoryStats stats) { |
| 61 if (stats.back().category == profiles::kProfileStatisticsBookmarks) |
| 62 ++num_of_times_called_; |
| 63 } |
| 64 |
| 65 int GetNumOfTimesCalled() { return num_of_times_called_; } |
| 66 |
| 67 private: |
| 68 base::Closure quit_closure_; |
| 69 int num_of_times_called_; |
| 70 }; |
| 71 |
| 20 void VerifyStatisticsCache(const base::FilePath& profile_path, | 72 void VerifyStatisticsCache(const base::FilePath& profile_path, |
| 21 const std::map<std::string, int>& expected, | 73 const std::map<std::string, int>& expected, |
| 22 const std::vector<std::string>& categories_to_check) { | 74 const std::vector<std::string>& categories_to_check) { |
| 23 const profiles::ProfileCategoryStats actual = | 75 const profiles::ProfileCategoryStats actual = |
| 24 ProfileStatistics::GetProfileStatisticsFromAttributesStorage( | 76 ProfileStatistics::GetProfileStatisticsFromAttributesStorage( |
| 25 profile_path); | 77 profile_path); |
| 26 | 78 |
| 27 EXPECT_EQ(categories_to_check.size(), actual.size()); | 79 EXPECT_EQ(categories_to_check.size(), actual.size()); |
| 28 | 80 |
| 29 std::set<std::string> checked; | 81 std::set<std::string> checked; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 // Now no keys are set. | 131 // Now no keys are set. |
| 80 VerifyStatisticsCache(profile_path, expected, categories_to_check); | 132 VerifyStatisticsCache(profile_path, expected, categories_to_check); |
| 81 // Insert items and test after each insert. | 133 // Insert items and test after each insert. |
| 82 for (const auto& item : insertions) { | 134 for (const auto& item : insertions) { |
| 83 ProfileStatistics::SetProfileStatisticsToAttributesStorage( | 135 ProfileStatistics::SetProfileStatisticsToAttributesStorage( |
| 84 profile_path, item.first, item.second); | 136 profile_path, item.first, item.second); |
| 85 expected[item.first] = item.second; | 137 expected[item.first] = item.second; |
| 86 VerifyStatisticsCache(profile_path, expected, categories_to_check); | 138 VerifyStatisticsCache(profile_path, expected, categories_to_check); |
| 87 } | 139 } |
| 88 } | 140 } |
| 141 |
| 142 TEST_F(ProfileStatisticsTest, WaitOrCountBookmarks) { |
| 143 TestingProfile* profile = manager()->CreateTestingProfile("Test 1"); |
| 144 ASSERT_TRUE(profile); |
| 145 |
| 146 bookmarks::BookmarkModel* bookmark_model = |
| 147 CreateBookmarkModelWithoutLoad(profile); |
| 148 ASSERT_TRUE(bookmark_model); |
| 149 |
| 150 // Run ProfileStatisticsAggregator::WaitOrCountBookmarks. |
| 151 ProfileStatisticsAggregator* aggregator; |
| 152 BookmarkStatHelper bookmark_stat_helper; |
| 153 base::RunLoop run_loop_aggregator_destruction; |
| 154 // The following should run inside a scope, so the scoped_refptr gets deleted |
| 155 // immediately. |
| 156 { |
| 157 scoped_refptr<ProfileStatisticsAggregator> aggregator_scoped = |
| 158 new ProfileStatisticsAggregator( |
| 159 profile, |
| 160 base::Bind(&BookmarkStatHelper::StatsCallback, |
| 161 base::Unretained(&bookmark_stat_helper)), |
| 162 run_loop_aggregator_destruction.QuitClosure()); |
| 163 aggregator = aggregator_scoped.get(); |
| 164 } |
| 165 // Wait until ProfileStatisticsAggregator::WaitOrCountBookmarks is run. |
| 166 base::RunLoop run_loop1; |
| 167 run_loop1.RunUntilIdle(); |
| 168 EXPECT_EQ(0, bookmark_stat_helper.GetNumOfTimesCalled()); |
| 169 |
| 170 // Run ProfileStatisticsAggregator::WaitOrCountBookmarks again. |
| 171 aggregator->AddCallbackAndStartAggregator( |
| 172 profiles::ProfileStatisticsCallback()); |
| 173 // Wait until ProfileStatisticsAggregator::WaitOrCountBookmarks is run. |
| 174 base::RunLoop run_loop2; |
| 175 run_loop2.RunUntilIdle(); |
| 176 EXPECT_EQ(0, bookmark_stat_helper.GetNumOfTimesCalled()); |
| 177 |
| 178 // Load the bookmark model. When the model is loaded (asynchronously), the |
| 179 // observer added by WaitOrCountBookmarks is run. |
| 180 LoadBookmarkModel(profile, bookmark_model); |
| 181 |
| 182 run_loop_aggregator_destruction.Run(); |
| 183 EXPECT_EQ(1, bookmark_stat_helper.GetNumOfTimesCalled()); |
| 184 } |
| OLD | NEW |