OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/rlz/rlz.h" | 5 #include "chrome/browser/rlz/rlz.h" |
6 | 6 |
7 #include "base/files/scoped_temp_dir.h" | |
8 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/path_service.h" | |
11 #include "base/strings/stringprintf.h" | |
12 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
13 #include "base/threading/thread.h" | |
14 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
15 #include "chrome/browser/google/google_util.h" | 10 #include "chrome/browser/google/google_util.h" |
16 #include "chrome/browser/omnibox/omnibox_log.h" | 11 #include "chrome/browser/omnibox/omnibox_log.h" |
17 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/common/env_vars.h" | |
19 #include "chrome/installer/util/browser_distribution.h" | 13 #include "chrome/installer/util/browser_distribution.h" |
20 #include "chrome/installer/util/google_update_constants.h" | 14 #include "chrome/installer/util/google_update_constants.h" |
21 #include "content/public/browser/navigation_entry.h" | 15 #include "content/public/browser/navigation_entry.h" |
22 #include "content/public/browser/notification_details.h" | 16 #include "content/public/browser/notification_details.h" |
23 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
24 #include "content/public/browser/notification_source.h" | 18 #include "content/public/browser/notification_source.h" |
| 19 #include "rlz/test/rlz_test_helpers.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
26 | 21 |
27 #if defined(OS_WIN) | 22 #if defined(OS_WIN) |
28 #include "base/test/test_reg_util_win.h" | |
29 #include "base/win/registry.h" | 23 #include "base/win/registry.h" |
30 #include "rlz/win/lib/rlz_lib.h" // InitializeTempHivesForTesting | |
31 #elif defined(OS_POSIX) | |
32 #include "rlz/lib/rlz_value_store.h" // SetRlzStoreDirectory | |
33 #endif | 24 #endif |
34 | 25 |
35 using content::NavigationEntry; | 26 using content::NavigationEntry; |
36 using testing::AssertionResult; | 27 using testing::AssertionResult; |
37 using testing::AssertionSuccess; | 28 using testing::AssertionSuccess; |
38 using testing::AssertionFailure; | 29 using testing::AssertionFailure; |
39 | 30 |
40 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
41 using base::win::RegKey; | 32 using base::win::RegKey; |
42 using registry_util::RegistryOverrideManager; | |
43 #endif | 33 #endif |
44 | 34 |
45 namespace { | 35 namespace { |
46 | 36 |
47 #if defined(OS_WIN) | |
48 // Registry path to overridden hive. | |
49 const wchar_t kRlzTempHkcu[] = L"rlz_hkcu"; | |
50 const wchar_t kRlzTempHklm[] = L"rlz_hklm"; | |
51 #endif | |
52 | |
53 // Dummy RLZ string for the access points. | 37 // Dummy RLZ string for the access points. |
54 const char kOmniboxRlzString[] = "test_omnibox"; | 38 const char kOmniboxRlzString[] = "test_omnibox"; |
55 const char kHomepageRlzString[] = "test_homepage"; | 39 const char kHomepageRlzString[] = "test_homepage"; |
56 const char kNewOmniboxRlzString[] = "new_omnibox"; | 40 const char kNewOmniboxRlzString[] = "new_omnibox"; |
57 const char kNewHomepageRlzString[] = "new_homepage"; | 41 const char kNewHomepageRlzString[] = "new_homepage"; |
58 | 42 |
59 // Some helper macros to test it a string contains/does not contain a substring. | 43 // Some helper macros to test it a string contains/does not contain a substring. |
60 | 44 |
61 AssertionResult CmpHelperSTRC(const char* str_expression, | 45 AssertionResult CmpHelperSTRC(const char* str_expression, |
62 const char* substr_expression, | 46 const char* substr_expression, |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 kNewHomepageRlzString); | 147 kNewHomepageRlzString); |
164 return true; | 148 return true; |
165 } | 149 } |
166 | 150 |
167 std::set<std::string> pinged_brands_; | 151 std::set<std::string> pinged_brands_; |
168 bool assume_not_ui_thread_; | 152 bool assume_not_ui_thread_; |
169 | 153 |
170 DISALLOW_COPY_AND_ASSIGN(TestRLZTracker); | 154 DISALLOW_COPY_AND_ASSIGN(TestRLZTracker); |
171 }; | 155 }; |
172 | 156 |
173 class RlzLibTest : public testing::Test { | 157 class RlzLibTest : public RlzLibTestNoMachineState { |
174 public: | 158 protected: |
175 virtual void SetUp() OVERRIDE; | 159 virtual void SetUp() OVERRIDE; |
176 virtual void TearDown() OVERRIDE; | |
177 | 160 |
178 protected: | |
179 void SetMainBrand(const char* brand); | 161 void SetMainBrand(const char* brand); |
180 void SetReactivationBrand(const char* brand); | 162 void SetReactivationBrand(const char* brand); |
181 #if defined(OS_WIN) | 163 #if defined(OS_WIN) |
182 void SetRegistryBrandValue(const wchar_t* name, const char* brand); | 164 void SetRegistryBrandValue(const wchar_t* name, const char* brand); |
183 #endif | 165 #endif |
184 | 166 |
185 void SimulateOmniboxUsage(); | 167 void SimulateOmniboxUsage(); |
186 void SimulateHomepageUsage(); | 168 void SimulateHomepageUsage(); |
187 void InvokeDelayedInit(); | 169 void InvokeDelayedInit(); |
188 | 170 |
189 void ExpectEventRecorded(const char* event_name, bool expected); | 171 void ExpectEventRecorded(const char* event_name, bool expected); |
190 void ExpectRlzPingSent(bool expected); | 172 void ExpectRlzPingSent(bool expected); |
191 void ExpectReactivationRlzPingSent(bool expected); | 173 void ExpectReactivationRlzPingSent(bool expected); |
192 | 174 |
193 TestRLZTracker tracker_; | 175 TestRLZTracker tracker_; |
194 #if defined(OS_WIN) | 176 #if defined(OS_POSIX) |
195 RegistryOverrideManager override_manager_; | |
196 #elif defined(OS_POSIX) | |
197 base::ScopedTempDir temp_dir_; | |
198 scoped_ptr<google_util::BrandForTesting> brand_override_; | 177 scoped_ptr<google_util::BrandForTesting> brand_override_; |
199 #endif | 178 #endif |
200 }; | 179 }; |
201 | 180 |
202 void RlzLibTest::SetUp() { | 181 void RlzLibTest::SetUp() { |
203 testing::Test::SetUp(); | 182 RlzLibTestNoMachineState::SetUp(); |
204 | |
205 #if defined(OS_WIN) | |
206 // Before overriding HKLM for the tests, we need to set it up correctly | |
207 // so that the rlz_lib calls work. This needs to be done before we do the | |
208 // override. | |
209 | |
210 string16 temp_hklm_path = base::StringPrintf( | |
211 L"%ls\\%ls", | |
212 RegistryOverrideManager::kTempTestKeyPath, | |
213 kRlzTempHklm); | |
214 | |
215 base::win::RegKey hklm; | |
216 ASSERT_EQ(ERROR_SUCCESS, hklm.Create(HKEY_CURRENT_USER, | |
217 temp_hklm_path.c_str(), | |
218 KEY_READ)); | |
219 | |
220 string16 temp_hkcu_path = base::StringPrintf( | |
221 L"%ls\\%ls", | |
222 RegistryOverrideManager::kTempTestKeyPath, | |
223 kRlzTempHkcu); | |
224 | |
225 base::win::RegKey hkcu; | |
226 ASSERT_EQ(ERROR_SUCCESS, hkcu.Create(HKEY_CURRENT_USER, | |
227 temp_hkcu_path.c_str(), | |
228 KEY_READ)); | |
229 | |
230 rlz_lib::InitializeTempHivesForTesting(hklm, hkcu); | |
231 | |
232 // Its important to override HKLM before HKCU because of the registry | |
233 // initialization performed above. | |
234 override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE, kRlzTempHklm); | |
235 override_manager_.OverrideRegistry(HKEY_CURRENT_USER, kRlzTempHkcu); | |
236 #elif defined(OS_POSIX) | |
237 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
238 rlz_lib::testing::SetRlzStoreDirectory(temp_dir_.path()); | |
239 #endif | |
240 | 183 |
241 // Make sure a non-organic brand code is set in the registry or the RLZTracker | 184 // Make sure a non-organic brand code is set in the registry or the RLZTracker |
242 // is pretty much a no-op. | 185 // is pretty much a no-op. |
243 SetMainBrand("TEST"); | 186 SetMainBrand("TEST"); |
244 SetReactivationBrand(""); | 187 SetReactivationBrand(""); |
245 } | 188 } |
246 | 189 |
247 void RlzLibTest::TearDown() { | |
248 #if defined(OS_POSIX) | |
249 rlz_lib::testing::SetRlzStoreDirectory(base::FilePath()); | |
250 #endif | |
251 testing::Test::TearDown(); | |
252 } | |
253 | |
254 void RlzLibTest::SetMainBrand(const char* brand) { | 190 void RlzLibTest::SetMainBrand(const char* brand) { |
255 #if defined(OS_WIN) | 191 #if defined(OS_WIN) |
256 SetRegistryBrandValue(google_update::kRegRLZBrandField, brand); | 192 SetRegistryBrandValue(google_update::kRegRLZBrandField, brand); |
257 #elif defined(OS_POSIX) | 193 #elif defined(OS_POSIX) |
258 brand_override_.reset(new google_util::BrandForTesting(brand)); | 194 brand_override_.reset(new google_util::BrandForTesting(brand)); |
259 #endif | 195 #endif |
260 std::string check_brand; | 196 std::string check_brand; |
261 google_util::GetBrand(&check_brand); | 197 google_util::GetBrand(&check_brand); |
262 EXPECT_EQ(brand, check_brand); | 198 EXPECT_EQ(brand, check_brand); |
263 } | 199 } |
(...skipping 522 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
786 RLZTracker::RecordProductEvent(rlz_lib::CHROME, RLZTracker::CHROME_OMNIBOX, | 722 RLZTracker::RecordProductEvent(rlz_lib::CHROME, RLZTracker::CHROME_OMNIBOX, |
787 rlz_lib::FIRST_SEARCH); | 723 rlz_lib::FIRST_SEARCH); |
788 | 724 |
789 ExpectEventRecorded(kOmniboxFirstSearch, true); | 725 ExpectEventRecorded(kOmniboxFirstSearch, true); |
790 | 726 |
791 RLZTracker::ClearRlzState(); | 727 RLZTracker::ClearRlzState(); |
792 | 728 |
793 ExpectEventRecorded(kOmniboxFirstSearch, false); | 729 ExpectEventRecorded(kOmniboxFirstSearch, false); |
794 } | 730 } |
795 #endif // defined(OS_CHROMEOS) | 731 #endif // defined(OS_CHROMEOS) |
OLD | NEW |