Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "chrome/installer/util/experiment_storage.h" | |
| 6 | |
|
grt (UTC plus 2)
2017/06/09 20:02:27
unused?
nikunjb
2017/06/13 00:27:36
Done.
| |
| 7 #include "base/base64.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | |
|
grt (UTC plus 2)
2017/06/09 20:02:27
unused?
nikunjb
2017/06/13 00:27:36
No, used for implicit conversion from string16 to
grt (UTC plus 2)
2017/06/13 12:24:48
seems like base/strings/string_piece.h should be i
| |
| 9 #include "base/test/test_reg_util_win.h" | |
| 10 #include "chrome/install_static/install_details.h" | |
| 11 #include "chrome/install_static/test/scoped_install_details.h" | |
| 12 #include "chrome/installer/util/experiment.h" | |
| 13 #include "chrome/installer/util/experiment_metrics.h" | |
| 14 #include "chrome/installer/util/google_update_settings.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace installer { | |
| 18 | |
| 19 // A test fixture that can tests saving experiment data in windows registry. | |
| 20 // Individual tests provide a parameter, which is true if Chrome is installed | |
| 21 // in system level. | |
| 22 class ExperimentStorageTest : public ::testing::TestWithParam<bool> { | |
| 23 protected: | |
| 24 ExperimentStorageTest() | |
| 25 : system_level_install_(GetParam()), | |
| 26 scoped_install_details_(system_level_install_, 0) {} | |
| 27 | |
| 28 void SetUp() override { | |
| 29 ::testing::TestWithParam<bool>::SetUp(); | |
| 30 HKEY root = system_level_install_ ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; | |
| 31 ASSERT_NO_FATAL_FAILURE(override_manager_.OverrideRegistry(root)); | |
| 32 | |
| 33 // Create an empty participation key since participation registry is assumed | |
| 34 // to be present for chrome build. | |
| 35 base::win::RegKey key; | |
| 36 ASSERT_EQ(ERROR_SUCCESS, key.Create(root, | |
| 37 install_static::InstallDetails::Get() | |
| 38 .GetClientStateKeyPath() | |
| 39 .c_str(), | |
| 40 KEY_WOW64_32KEY | KEY_QUERY_VALUE)); | |
| 41 } | |
| 42 | |
| 43 private: | |
| 44 bool system_level_install_; | |
| 45 install_static::ScopedInstallDetails scoped_install_details_; | |
| 46 registry_util::RegistryOverrideManager override_manager_; | |
| 47 | |
| 48 DISALLOW_COPY_AND_ASSIGN(ExperimentStorageTest); | |
| 49 }; | |
| 50 | |
| 51 TEST_P(ExperimentStorageTest, TestEncodeDecodeMetrics) { | |
| 52 ExperimentMetrics metrics; | |
| 53 metrics.state = ExperimentMetrics::kGroupAssigned; | |
| 54 metrics.toast_location = ExperimentMetrics::kOverTaskbarPin; | |
| 55 metrics.toast_count = 1; | |
| 56 metrics.first_toast_offset = 30; | |
| 57 metrics.toast_hour = 3; | |
| 58 metrics.last_used_bucket = 2; | |
| 59 metrics.action_delay_bucket = 11; | |
| 60 metrics.session_length_bucket = 36; | |
| 61 base::string16 encoded_metrics(ExperimentStorage::EncodeMetrics(metrics)); | |
| 62 EXPECT_EQ(L"5BIMD4IA", encoded_metrics); | |
| 63 ExperimentMetrics decoded_metrics; | |
| 64 ASSERT_TRUE( | |
| 65 ExperimentStorage::DecodeMetrics(encoded_metrics, &decoded_metrics)); | |
| 66 EXPECT_EQ(metrics, decoded_metrics); | |
| 67 } | |
| 68 | |
| 69 TEST_P(ExperimentStorageTest, TestEncodeDecodeForMax) { | |
| 70 Experiment experiment; | |
| 71 experiment.AssignGroup(ExperimentMetrics::kNumGroups - 1); | |
| 72 experiment.SetToastLocation(ExperimentMetrics::kOverNotificationArea); | |
| 73 experiment.SetInactiveDays(ExperimentMetrics::kMaxLastUsed); | |
| 74 experiment.SetToastCount(ExperimentMetrics::kMaxToastCount); | |
| 75 experiment.SetUserSessionUptime( | |
| 76 base::TimeDelta::FromMinutes(ExperimentMetrics::kMaxSessionLength)); | |
| 77 experiment.SetActionDelay( | |
| 78 base::TimeDelta::FromSeconds(ExperimentMetrics::kMaxActionDelay)); | |
| 79 experiment.SetDisplayTime( | |
| 80 base::Time::UnixEpoch() + | |
| 81 base::TimeDelta::FromSeconds(ExperimentMetrics::kExperimentStartSeconds) + | |
| 82 base::TimeDelta::FromDays(ExperimentMetrics::kMaxFirstToastOffset)); | |
| 83 experiment.SetState(ExperimentMetrics::kUserLogOff); // Max state. | |
| 84 ExperimentMetrics metrics = experiment.metrics(); | |
| 85 // toast_hour uses LocalMidnight whose value depend on local time. So, reset | |
| 86 // it to its maximum value. | |
| 87 metrics.toast_hour = 24; | |
| 88 base::string16 encoded_metrics(ExperimentStorage::EncodeMetrics(metrics)); | |
| 89 EXPECT_EQ(L"///j//8f", encoded_metrics); | |
| 90 ExperimentMetrics decoded_metrics; | |
| 91 ASSERT_TRUE( | |
| 92 ExperimentStorage::DecodeMetrics(encoded_metrics, &decoded_metrics)); | |
| 93 EXPECT_EQ(decoded_metrics.state, ExperimentMetrics::kUserLogOff); | |
| 94 EXPECT_EQ(decoded_metrics.group, ExperimentMetrics::kNumGroups - 1); | |
| 95 EXPECT_EQ(decoded_metrics.toast_location, | |
| 96 ExperimentMetrics::kOverNotificationArea); | |
| 97 EXPECT_EQ(decoded_metrics.toast_count, ExperimentMetrics::kMaxToastCount); | |
| 98 EXPECT_EQ(decoded_metrics.first_toast_offset, | |
| 99 ExperimentMetrics::kMaxFirstToastOffset); | |
| 100 EXPECT_EQ(decoded_metrics.toast_hour, 24); | |
| 101 // Following are exponential buckets. So, there max value will be | |
| 102 // 2^bits - 1 | |
| 103 EXPECT_EQ(decoded_metrics.last_used_bucket, | |
| 104 (1 << ExperimentMetrics::kLastUsedBucketBits) - 1); | |
| 105 EXPECT_EQ(decoded_metrics.action_delay_bucket, | |
| 106 (1 << ExperimentMetrics::kActionDelayBucketBits) - 1); | |
| 107 EXPECT_EQ(decoded_metrics.session_length_bucket, | |
| 108 (1 << ExperimentMetrics::kSessionLengthBucketBits) - 1); | |
| 109 } | |
| 110 | |
| 111 TEST_P(ExperimentStorageTest, TestEncodeDecodeForMin) { | |
| 112 ExperimentMetrics metrics; | |
| 113 metrics.state = ExperimentMetrics::kRelaunchFailed; | |
| 114 base::string16 encoded_metrics(ExperimentStorage::EncodeMetrics(metrics)); | |
| 115 EXPECT_EQ(L"AAAAAAAA", encoded_metrics); | |
| 116 ExperimentMetrics decoded_metrics; | |
| 117 ASSERT_TRUE( | |
| 118 ExperimentStorage::DecodeMetrics(encoded_metrics, &decoded_metrics)); | |
| 119 EXPECT_EQ(metrics, decoded_metrics); | |
| 120 } | |
| 121 | |
| 122 TEST_P(ExperimentStorageTest, TestReadWriteParticipation) { | |
| 123 ExperimentStorage storage; | |
| 124 ExperimentStorage::Participation expected = | |
| 125 ExperimentStorage::Participation::kIsParticipating; | |
| 126 ASSERT_TRUE(storage.AcquireLock()->WriteParticipation(expected)); | |
| 127 ExperimentStorage::Participation p; | |
| 128 ASSERT_TRUE(storage.AcquireLock()->ReadParticipation(&p)); | |
| 129 EXPECT_EQ(expected, p); | |
| 130 } | |
| 131 | |
| 132 TEST_P(ExperimentStorageTest, TestLoadStoreExperiment) { | |
| 133 Experiment experiment; | |
| 134 experiment.AssignGroup(5); | |
| 135 ExperimentStorage storage; | |
| 136 ASSERT_TRUE(storage.AcquireLock()->StoreExperiment(experiment)); | |
| 137 Experiment stored_experiment; | |
| 138 ASSERT_TRUE(storage.AcquireLock()->LoadExperiment(&stored_experiment)); | |
| 139 EXPECT_EQ(ExperimentMetrics::kGroupAssigned, stored_experiment.state()); | |
| 140 EXPECT_EQ(5, stored_experiment.group()); | |
| 141 } | |
| 142 | |
| 143 TEST_P(ExperimentStorageTest, TestLoadStoreMetrics) { | |
| 144 ExperimentStorage storage; | |
| 145 ExperimentMetrics metrics; | |
| 146 metrics.state = ExperimentMetrics::kGroupAssigned; | |
| 147 metrics.toast_location = ExperimentMetrics::kOverTaskbarPin; | |
| 148 metrics.toast_count = 1; | |
| 149 metrics.first_toast_offset = 30; | |
| 150 metrics.toast_hour = 3; | |
| 151 metrics.last_used_bucket = 2; | |
| 152 metrics.action_delay_bucket = 11; | |
| 153 metrics.session_length_bucket = 36; | |
| 154 ASSERT_TRUE(storage.AcquireLock()->StoreMetrics(metrics)); | |
| 155 ExperimentMetrics stored_metrics; | |
| 156 ASSERT_TRUE(storage.AcquireLock()->LoadMetrics(&stored_metrics)); | |
| 157 EXPECT_EQ(L"5BIMD4IA", ExperimentStorage::EncodeMetrics(stored_metrics)); | |
| 158 EXPECT_EQ(metrics, stored_metrics); | |
| 159 } | |
| 160 | |
| 161 INSTANTIATE_TEST_CASE_P(UserLevel, | |
| 162 ExperimentStorageTest, | |
| 163 ::testing::Values(false)); | |
| 164 | |
| 165 INSTANTIATE_TEST_CASE_P(SystemLevel, | |
| 166 ExperimentStorageTest, | |
| 167 ::testing::Values(true)); | |
| 168 | |
| 169 } // namespace installer | |
| OLD | NEW |