Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / metrics / perf_provider_chromeos_unittest.cc
blob12828a318d1b231ce87a3a557a63c3a9450824ec
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
3 // found in the LICENSE file.
5 #include "chrome/browser/metrics/perf_provider_chromeos.h"
7 #include <string>
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/test/test_simple_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "chrome/browser/metrics/windowed_incognito_observer.h"
15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "chromeos/login/login_state.h"
17 #include "components/metrics/proto/sampled_profile.pb.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 namespace metrics {
22 namespace {
24 // Return values for perf.
25 const int kPerfSuccess = 0;
26 const int kPerfFailure = 1;
28 // Converts a protobuf to serialized format as a byte vector.
29 std::vector<uint8_t> SerializeMessageToVector(
30 const google::protobuf::MessageLite& message) {
31 std::vector<uint8_t> result(message.ByteSize());
32 message.SerializeToArray(result.data(), result.size());
33 return result;
36 // Returns an example PerfDataProto. The contents don't have to make sense. They
37 // just need to constitute a semantically valid protobuf.
38 // |proto| is an output parameter that will contain the created protobuf.
39 PerfDataProto GetExamplePerfDataProto() {
40 PerfDataProto proto;
41 proto.set_timestamp_sec(1435604013); // Time since epoch in seconds->
43 PerfDataProto_PerfFileAttr* file_attr = proto.add_file_attrs();
44 file_attr->add_ids(61);
45 file_attr->add_ids(62);
46 file_attr->add_ids(63);
48 PerfDataProto_PerfEventAttr* attr = file_attr->mutable_attr();
49 attr->set_type(1);
50 attr->set_size(2);
51 attr->set_config(3);
52 attr->set_sample_period(4);
53 attr->set_sample_freq(5);
55 PerfDataProto_PerfEventStats* stats = proto.mutable_stats();
56 stats->set_num_events_read(100);
57 stats->set_num_sample_events(200);
58 stats->set_num_mmap_events(300);
59 stats->set_num_fork_events(400);
60 stats->set_num_exit_events(500);
62 return proto;
65 // Returns an example PerfStatProto. The contents don't have to make sense. They
66 // just need to constitute a semantically valid protobuf.
67 // |result| is an output parameter that will contain the created protobuf.
68 PerfStatProto GetExamplePerfStatProto() {
69 PerfStatProto proto;
70 proto.set_command_line(
71 "perf stat -a -e cycles -e instructions -e branches -- sleep 2");
73 PerfStatProto_PerfStatLine* line1 = proto.add_line();
74 line1->set_time_ms(1000);
75 line1->set_count(2000);
76 line1->set_event("cycles");
78 PerfStatProto_PerfStatLine* line2 = proto.add_line();
79 line2->set_time_ms(2000);
80 line2->set_count(5678);
81 line2->set_event("instructions");
83 PerfStatProto_PerfStatLine* line3 = proto.add_line();
84 line3->set_time_ms(3000);
85 line3->set_count(9999);
86 line3->set_event("branches");
88 return proto;
91 // Allows testing of PerfProvider behavior when an incognito window is opened.
92 class TestIncognitoObserver : public WindowedIncognitoObserver {
93 public:
94 // Factory function to create a TestIncognitoObserver object contained in a
95 // scoped_ptr<WindowedIncognitoObserver> object. |incognito_launched|
96 // simulates the presence of an open incognito window, or the lack thereof.
97 // Used for passing observers to ParseOutputProtoIfValid().
98 static scoped_ptr<WindowedIncognitoObserver> CreateWithIncognitoLaunched(
99 bool incognito_launched) {
100 scoped_ptr<TestIncognitoObserver> observer(new TestIncognitoObserver);
101 observer->set_incognito_launched(incognito_launched);
102 return observer.Pass();
105 private:
106 TestIncognitoObserver() {}
108 DISALLOW_COPY_AND_ASSIGN(TestIncognitoObserver);
111 // Allows access to PerfProvider::ParseOutputProtoIfValid() for testing.
112 class TestPerfProvider : public PerfProvider {
113 public:
114 TestPerfProvider() {}
116 using PerfProvider::ParseOutputProtoIfValid;
118 private:
119 std::vector<SampledProfile> stored_profiles_;
121 DISALLOW_COPY_AND_ASSIGN(TestPerfProvider);
124 } // namespace
126 class PerfProviderTest : public testing::Test {
127 public:
128 PerfProviderTest() : task_runner_(new base::TestSimpleTaskRunner),
129 task_runner_handle_(task_runner_),
130 perf_data_proto_(GetExamplePerfDataProto()),
131 perf_stat_proto_(GetExamplePerfStatProto()) {}
133 void SetUp() override {
134 // PerfProvider requires chromeos::LoginState and
135 // chromeos::DBusThreadManagerto be initialized.
136 chromeos::LoginState::Initialize();
137 chromeos::DBusThreadManager::Initialize();
139 perf_provider_.reset(new TestPerfProvider);
141 // PerfProvider requires the user to be logged in.
142 chromeos::LoginState::Get()->SetLoggedInState(
143 chromeos::LoginState::LOGGED_IN_ACTIVE,
144 chromeos::LoginState::LOGGED_IN_USER_REGULAR);
147 void TearDown() override {
148 perf_provider_.reset();
149 chromeos::DBusThreadManager::Shutdown();
150 chromeos::LoginState::Shutdown();
153 protected:
154 scoped_ptr<TestPerfProvider> perf_provider_;
156 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
157 base::ThreadTaskRunnerHandle task_runner_handle_;
159 // These store example perf data/stat protobufs for testing.
160 PerfDataProto perf_data_proto_;
161 PerfStatProto perf_stat_proto_;
163 DISALLOW_COPY_AND_ASSIGN(PerfProviderTest);
166 TEST_F(PerfProviderTest, CheckSetup) {
167 EXPECT_GT(perf_data_proto_.ByteSize(), 0);
168 EXPECT_GT(perf_stat_proto_.ByteSize(), 0);
170 std::vector<SampledProfile> stored_profiles;
171 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles));
172 EXPECT_TRUE(stored_profiles.empty());
174 EXPECT_FALSE(
175 TestIncognitoObserver::CreateWithIncognitoLaunched(false)->
176 incognito_launched());
177 EXPECT_TRUE(
178 TestIncognitoObserver::CreateWithIncognitoLaunched(true)->
179 incognito_launched());
182 TEST_F(PerfProviderTest, NoPerfData) {
183 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
184 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
186 perf_provider_->ParseOutputProtoIfValid(
187 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
188 sampled_profile.Pass(),
189 kPerfSuccess,
190 std::vector<uint8_t>(),
191 std::vector<uint8_t>());
193 std::vector<SampledProfile> stored_profiles;
194 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles));
197 TEST_F(PerfProviderTest, PerfDataProtoOnly) {
198 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
199 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
201 perf_provider_->ParseOutputProtoIfValid(
202 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
203 sampled_profile.Pass(),
204 kPerfSuccess,
205 SerializeMessageToVector(perf_data_proto_),
206 std::vector<uint8_t>());
208 std::vector<SampledProfile> stored_profiles;
209 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles));
210 ASSERT_EQ(1U, stored_profiles.size());
212 const SampledProfile& profile = stored_profiles[0];
213 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile.trigger_event());
214 EXPECT_TRUE(profile.has_ms_after_login());
216 ASSERT_TRUE(profile.has_perf_data());
217 EXPECT_FALSE(profile.has_perf_stat());
218 EXPECT_EQ(SerializeMessageToVector(perf_data_proto_),
219 SerializeMessageToVector(profile.perf_data()));
222 TEST_F(PerfProviderTest, PerfStatProtoOnly) {
223 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
224 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
226 perf_provider_->ParseOutputProtoIfValid(
227 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
228 sampled_profile.Pass(),
229 kPerfSuccess,
230 std::vector<uint8_t>(),
231 SerializeMessageToVector(perf_stat_proto_));
233 std::vector<SampledProfile> stored_profiles;
234 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles));
235 ASSERT_EQ(1U, stored_profiles.size());
237 const SampledProfile& profile = stored_profiles[0];
238 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile.trigger_event());
239 EXPECT_TRUE(profile.has_ms_after_login());
241 EXPECT_FALSE(profile.has_perf_data());
242 ASSERT_TRUE(profile.has_perf_stat());
243 EXPECT_EQ(SerializeMessageToVector(perf_stat_proto_),
244 SerializeMessageToVector(profile.perf_stat()));
247 TEST_F(PerfProviderTest, BothPerfDataProtoAndPerfStatProto) {
248 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
249 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
251 perf_provider_->ParseOutputProtoIfValid(
252 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
253 sampled_profile.Pass(),
254 kPerfSuccess,
255 SerializeMessageToVector(perf_data_proto_),
256 SerializeMessageToVector(perf_stat_proto_));
258 std::vector<SampledProfile> stored_profiles;
259 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles));
260 EXPECT_TRUE(stored_profiles.empty());
263 TEST_F(PerfProviderTest, InvalidPerfOutputResult) {
264 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
265 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
267 perf_provider_->ParseOutputProtoIfValid(
268 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
269 sampled_profile.Pass(),
270 kPerfFailure,
271 SerializeMessageToVector(perf_data_proto_),
272 std::vector<uint8_t>());
274 // Should not have been stored.
275 std::vector<SampledProfile> stored_profiles;
276 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles));
277 EXPECT_TRUE(stored_profiles.empty());
280 // Change |sampled_profile| between calls to ParseOutputProtoIfValid().
281 TEST_F(PerfProviderTest, MultipleCalls) {
282 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
283 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
285 perf_provider_->ParseOutputProtoIfValid(
286 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
287 sampled_profile.Pass(),
288 kPerfSuccess,
289 SerializeMessageToVector(perf_data_proto_),
290 std::vector<uint8_t>());
292 sampled_profile.reset(new SampledProfile);
293 sampled_profile->set_trigger_event(SampledProfile::RESTORE_SESSION);
294 sampled_profile->set_ms_after_restore(3000);
295 perf_provider_->ParseOutputProtoIfValid(
296 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
297 sampled_profile.Pass(),
298 kPerfSuccess,
299 std::vector<uint8_t>(),
300 SerializeMessageToVector(perf_stat_proto_));
302 sampled_profile.reset(new SampledProfile);
303 sampled_profile->set_trigger_event(SampledProfile::RESUME_FROM_SUSPEND);
304 sampled_profile->set_suspend_duration_ms(60000);
305 sampled_profile->set_ms_after_resume(1500);
306 perf_provider_->ParseOutputProtoIfValid(
307 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
308 sampled_profile.Pass(),
309 kPerfSuccess,
310 SerializeMessageToVector(perf_data_proto_),
311 std::vector<uint8_t>());
313 sampled_profile.reset(new SampledProfile);
314 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
315 perf_provider_->ParseOutputProtoIfValid(
316 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
317 sampled_profile.Pass(),
318 kPerfSuccess,
319 std::vector<uint8_t>(),
320 SerializeMessageToVector(perf_stat_proto_));
322 std::vector<SampledProfile> stored_profiles;
323 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles));
324 ASSERT_EQ(4U, stored_profiles.size());
326 const SampledProfile& profile1 = stored_profiles[0];
327 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile1.trigger_event());
328 EXPECT_TRUE(profile1.has_ms_after_login());
329 ASSERT_TRUE(profile1.has_perf_data());
330 EXPECT_FALSE(profile1.has_perf_stat());
331 EXPECT_EQ(SerializeMessageToVector(perf_data_proto_),
332 SerializeMessageToVector(profile1.perf_data()));
334 const SampledProfile& profile2 = stored_profiles[1];
335 EXPECT_EQ(SampledProfile::RESTORE_SESSION, profile2.trigger_event());
336 EXPECT_TRUE(profile2.has_ms_after_login());
337 EXPECT_EQ(3000, profile2.ms_after_restore());
338 EXPECT_FALSE(profile2.has_perf_data());
339 ASSERT_TRUE(profile2.has_perf_stat());
340 EXPECT_EQ(SerializeMessageToVector(perf_stat_proto_),
341 SerializeMessageToVector(profile2.perf_stat()));
343 const SampledProfile& profile3 = stored_profiles[2];
344 EXPECT_EQ(SampledProfile::RESUME_FROM_SUSPEND, profile3.trigger_event());
345 EXPECT_TRUE(profile3.has_ms_after_login());
346 EXPECT_EQ(60000, profile3.suspend_duration_ms());
347 EXPECT_EQ(1500, profile3.ms_after_resume());
348 ASSERT_TRUE(profile3.has_perf_data());
349 EXPECT_FALSE(profile3.has_perf_stat());
350 EXPECT_EQ(SerializeMessageToVector(perf_data_proto_),
351 SerializeMessageToVector(profile3.perf_data()));
353 const SampledProfile& profile4 = stored_profiles[3];
354 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile4.trigger_event());
355 EXPECT_TRUE(profile4.has_ms_after_login());
356 EXPECT_FALSE(profile4.has_perf_data());
357 ASSERT_TRUE(profile4.has_perf_stat());
358 EXPECT_EQ(SerializeMessageToVector(perf_stat_proto_),
359 SerializeMessageToVector(profile4.perf_stat()));
362 // Simulate opening and closing of incognito window in between calls to
363 // ParseOutputProtoIfValid().
364 TEST_F(PerfProviderTest, IncognitoWindowOpened) {
365 scoped_ptr<SampledProfile> sampled_profile(new SampledProfile);
366 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
368 perf_provider_->ParseOutputProtoIfValid(
369 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
370 sampled_profile.Pass(),
371 kPerfSuccess,
372 SerializeMessageToVector(perf_data_proto_),
373 std::vector<uint8_t>());
375 std::vector<SampledProfile> stored_profiles1;
376 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles1));
377 ASSERT_EQ(1U, stored_profiles1.size());
379 const SampledProfile& profile1 = stored_profiles1[0];
380 EXPECT_EQ(SampledProfile::PERIODIC_COLLECTION, profile1.trigger_event());
381 EXPECT_TRUE(profile1.has_ms_after_login());
382 ASSERT_TRUE(profile1.has_perf_data());
383 EXPECT_FALSE(profile1.has_perf_stat());
384 EXPECT_EQ(SerializeMessageToVector(perf_data_proto_),
385 SerializeMessageToVector(profile1.perf_data()));
387 sampled_profile.reset(new SampledProfile);
388 sampled_profile->set_trigger_event(SampledProfile::RESTORE_SESSION);
389 sampled_profile->set_ms_after_restore(3000);
390 perf_provider_->ParseOutputProtoIfValid(
391 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
392 sampled_profile.Pass(),
393 kPerfSuccess,
394 std::vector<uint8_t>(),
395 SerializeMessageToVector(perf_stat_proto_));
397 std::vector<SampledProfile> stored_profiles2;
398 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles2));
399 ASSERT_EQ(1U, stored_profiles2.size());
401 const SampledProfile& profile2 = stored_profiles2[0];
402 EXPECT_EQ(SampledProfile::RESTORE_SESSION, profile2.trigger_event());
403 EXPECT_TRUE(profile2.has_ms_after_login());
404 EXPECT_EQ(3000, profile2.ms_after_restore());
405 EXPECT_FALSE(profile2.has_perf_data());
406 ASSERT_TRUE(profile2.has_perf_stat());
407 EXPECT_EQ(SerializeMessageToVector(perf_stat_proto_),
408 SerializeMessageToVector(profile2.perf_stat()));
410 sampled_profile.reset(new SampledProfile);
411 sampled_profile->set_trigger_event(SampledProfile::RESUME_FROM_SUSPEND);
412 // An incognito window opens.
413 perf_provider_->ParseOutputProtoIfValid(
414 TestIncognitoObserver::CreateWithIncognitoLaunched(true),
415 sampled_profile.Pass(),
416 kPerfSuccess,
417 SerializeMessageToVector(perf_data_proto_),
418 std::vector<uint8_t>());
420 std::vector<SampledProfile> stored_profiles_empty;
421 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles_empty));
423 sampled_profile.reset(new SampledProfile);
424 sampled_profile->set_trigger_event(SampledProfile::PERIODIC_COLLECTION);
425 // Incognito window is still open.
426 perf_provider_->ParseOutputProtoIfValid(
427 TestIncognitoObserver::CreateWithIncognitoLaunched(true),
428 sampled_profile.Pass(),
429 kPerfSuccess,
430 std::vector<uint8_t>(),
431 SerializeMessageToVector(perf_stat_proto_));
433 EXPECT_FALSE(perf_provider_->GetSampledProfiles(&stored_profiles_empty));
435 sampled_profile.reset(new SampledProfile);
436 sampled_profile->set_trigger_event(SampledProfile::RESUME_FROM_SUSPEND);
437 sampled_profile->set_suspend_duration_ms(60000);
438 sampled_profile->set_ms_after_resume(1500);
439 // Incognito window closes.
440 perf_provider_->ParseOutputProtoIfValid(
441 TestIncognitoObserver::CreateWithIncognitoLaunched(false),
442 sampled_profile.Pass(),
443 kPerfSuccess,
444 SerializeMessageToVector(perf_data_proto_),
445 std::vector<uint8_t>());
447 std::vector<SampledProfile> stored_profiles3;
448 EXPECT_TRUE(perf_provider_->GetSampledProfiles(&stored_profiles3));
449 ASSERT_EQ(1U, stored_profiles3.size());
451 const SampledProfile& profile3 = stored_profiles3[0];
452 EXPECT_EQ(SampledProfile::RESUME_FROM_SUSPEND, profile3.trigger_event());
453 EXPECT_TRUE(profile3.has_ms_after_login());
454 EXPECT_EQ(60000, profile3.suspend_duration_ms());
455 EXPECT_EQ(1500, profile3.ms_after_resume());
456 ASSERT_TRUE(profile3.has_perf_data());
457 EXPECT_FALSE(profile3.has_perf_stat());
458 EXPECT_EQ(SerializeMessageToVector(perf_data_proto_),
459 SerializeMessageToVector(profile3.perf_data()));
462 } // namespace metrics