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

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

Issue 1579433002: Make profile statistics tasks inspectable by tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 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 | « no previous file | chrome/chrome_tests.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stddef.h>
6
7 #include <algorithm>
8
9 #include "base/bind.h"
10 #include "base/macros.h"
11 #include "build/build_config.h"
12 #include "chrome/browser/password_manager/password_store_factory.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/profiles/profile_statistics.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "components/password_manager/core/browser/password_manager_test_utils.h "
18 #include "components/password_manager/core/browser/test_password_store.h"
19 #include "content/public/test/test_utils.h"
20
21 namespace profiles {
22 bool operator==(const ProfileCategoryStat& a,
23 const ProfileCategoryStat& b) {
24 return a.category == b.category && a.count == b.count &&
25 a.success == b.success;
26 }
27
28 bool operator<(const ProfileCategoryStat& a, const ProfileCategoryStat& b) {
Mike Lerman 2016/01/11 18:12:20 it would be kinda nice if we didn't need these ope
lwchkg 2016/01/13 12:58:47 Didn't recognize that the need to put operators/fu
29 return std::tie(a.category, a.count, a.success) <
30 std::tie(b.category, b.count, b.success);
31 }
32
33 void PrintTo(const ProfileCategoryStat& a, ::std::ostream* os) {
Mike Lerman 2016/01/11 18:12:20 What is this for?
lwchkg 2016/01/13 12:58:47 It would be nice if we can print out a whole struc
Mike Lerman 2016/01/14 15:21:53 I'd conversely prefer to test each individual part
34 *os << "category = " << a.category
35 << ", count = " << a.count
36 << ", success = " << (a.success ? "true" : "false");
37 }
38 } // namespace profiles
39
40 namespace {
41
42 const std::set<std::string> stats_categories {
43 profiles::kProfileStatisticsBrowsingHistory,
44 profiles::kProfileStatisticsPasswords,
45 profiles::kProfileStatisticsBookmarks,
46 profiles::kProfileStatisticsSettings};
47 const size_t num_of_stats_categories = stats_categories.size();
48
49 class ProfileStatisticsAggregatorState {
50 public:
51 void callback(base::RunLoop* run_loop,
52 profiles::ProfileCategoryStats stats_return) {
53 size_t oldCount = stats_.size();
54 size_t newCount = stats_return.size();
55
56 EXPECT_LT(oldCount, newCount);
57 if (oldCount < newCount) {
58 for (size_t i = 0; i < oldCount; i++) {
59 // Exisiting statistics must be the same.
60 EXPECT_EQ(stats_[i], stats_return[i]);
61 // The new statistic categories must be different.
62 for (size_t j = oldCount; j < newCount; j++)
63 EXPECT_NE(stats_[i].category, stats_return[j].category);
64 }
65
66 for (size_t j = oldCount; j < newCount; j++) {
67 EXPECT_EQ(1u, stats_categories.count(stats_return[j].category));
68 // Count the number of statistics failures (incrementally).
69 if (!stats_return[j].success)
70 num_of_fails_++;
71 }
72 stats_ = stats_return;
73 }
74
75 EXPECT_GE(num_of_stats_categories, newCount);
76 if (num_of_stats_categories <= newCount)
77 run_loop->Quit();
78 }
79
80 profiles::ProfileCategoryStats GetStats() const { return stats_; }
81 int GetNumOfFails() const { return num_of_fails_; }
82
83 private:
84 profiles::ProfileCategoryStats stats_;
85 int num_of_fails_ = 0;
86 };
87
88 void WaitMilliseconds(int time_in_milliseconds) {
89 base::RunLoop run_loop;
90 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
91 FROM_HERE, run_loop.QuitClosure(),
92 base::TimeDelta::FromMilliseconds(time_in_milliseconds));
93 run_loop.Run();
94 }
95
96 // Check if the statistics are the same. Since the statistics can be given in
97 // any order, the entries are sorted before comparison.
98 void ExpectProfileStatisticsEqual(profiles::ProfileCategoryStats actual_stats,
Mike Lerman 2016/01/11 18:12:20 these should be ref's (&)
lwchkg 2016/01/13 12:58:47 I sort the statistics before comparison, and doing
99 profiles::ProfileCategoryStats expected_stats) {
100 size_t expected_count = expected_stats.size();
101 size_t actual_count = actual_stats.size();
102 EXPECT_EQ(expected_count, actual_count);
103 if (expected_count == actual_count) {
104 std::sort(expected_stats.begin(), expected_stats.end());
105 std::sort(actual_stats.begin(), actual_stats.end());
106
107 for (size_t i = 0; i < expected_count; i++)
108 EXPECT_EQ(expected_stats[i], actual_stats[i]);
Mike Lerman 2016/01/11 18:12:20 Can we compare the members of ProfileCategoryStats
lwchkg 2016/01/13 12:58:47 Actually I want to compare the whole thing, and pr
109 }
110 }
111
112 } // namespace
113
114 class ProfileStatisticsBrowserTest : public InProcessBrowserTest {
115 public:
116 void SetUpOnMainThread() override {
117 // Use TestPasswordStore to remove a possible race. Normally the
118 // PasswordStore does its database manipulation on the DB thread, which
119 // creates a possible race during navigation. Specifically the
120 // PasswordManager will ignore any forms in a page if the load from the
121 // PasswordStore has not completed.
122 PasswordStoreFactory::GetInstance()->SetTestingFactory(
123 browser()->profile(),
124 password_manager::BuildPasswordStore<
125 content::BrowserContext, password_manager::TestPasswordStore>);
126 }
127 };
128
129 using ProfileStatisticsBrowserDeathTest = ProfileStatisticsBrowserTest;
130
131 IN_PROC_BROWSER_TEST_F(ProfileStatisticsBrowserTest, GatherStatistics) {
132 Profile* profile = ProfileManager::GetActiveUserProfile();
133 ASSERT_TRUE(profile);
134
135 ProfileStatisticsAggregatorState state;
136
137 base::RunLoop run_loop;
138 profiles::GatherProfileStatistics(
139 profile,
140 base::Bind(&ProfileStatisticsAggregatorState::callback,
141 base::Unretained(&state), &run_loop),
142 nullptr);
143 run_loop.Run();
144
145 EXPECT_EQ(0, state.GetNumOfFails());
146
147 profiles::ProfileCategoryStats stats = state.GetStats();
148 for (const auto& stat : stats) {
149 if (stat.category != profiles::kProfileStatisticsSettings)
150 EXPECT_EQ(0, stat.count);
151 }
152
153 ExpectProfileStatisticsEqual(stats,
154 profiles::GetProfileStatisticsFromCache(profile->GetPath()));
155 }
156
157 IN_PROC_BROWSER_TEST_F(ProfileStatisticsBrowserTest, CloseBrowser) {
158 Profile* profile = ProfileManager::GetActiveUserProfile();
159 ASSERT_TRUE(profile);
160
161 CloseBrowserSynchronously(browser());
162
163 profiles::ProfileCategoryStats stats;
164 // Wait for at most 2 seconds for the statistics to be gathered.
165 for (int i = 0; i < 10; i++) {
166 WaitMilliseconds(200);
Mike Lerman 2016/01/11 18:12:20 We really really should properly wait for the stat
lwchkg 2016/01/13 12:58:47 I'd like to use a callback, but it is not possible
Mike Lerman 2016/01/14 15:21:53 What are we really trying to test here? We're tryi
lwchkg 2016/01/16 18:07:50 Interesting. I have a question though: if we execu
167 stats = profiles::GetProfileStatisticsFromCache(profile->GetPath());
168 EXPECT_EQ(num_of_stats_categories, stats.size());
169
170 bool allSucceed = true;
171 for (const auto& stat : stats)
172 allSucceed &= stat.success;
173 if (allSucceed)
174 break;
175 }
176
177 for (const auto& stat : stats) {
178 EXPECT_EQ(1u, stats_categories.count(stat.category));
179 EXPECT_TRUE(stat.success);
180 if (stat.category != profiles::kProfileStatisticsSettings)
181 EXPECT_EQ(0, stat.count);
182 }
183 // Wait for background tasks to complete, otherwise some warning about
184 // the failure to post a task will be displayed.
185 WaitMilliseconds(500);
186 }
OLDNEW
« no previous file with comments | « no previous file | chrome/chrome_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698