| 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" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "content/public/browser/navigation_entry.h" | 21 #include "content/public/browser/navigation_entry.h" |
| 22 #include "content/public/browser/notification_details.h" | 22 #include "content/public/browser/notification_details.h" |
| 23 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
| 24 #include "content/public/browser/notification_source.h" | 24 #include "content/public/browser/notification_source.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 26 |
| 27 #if defined(OS_WIN) | 27 #if defined(OS_WIN) |
| 28 #include "base/test/test_reg_util_win.h" | 28 #include "base/test/test_reg_util_win.h" |
| 29 #include "base/win/registry.h" | 29 #include "base/win/registry.h" |
| 30 #include "rlz/win/lib/rlz_lib.h" // InitializeTempHivesForTesting | 30 #include "rlz/win/lib/rlz_lib.h" // InitializeTempHivesForTesting |
| 31 #elif defined(OS_MACOSX) || defined(OS_CHROMEOS) | 31 #elif defined(OS_POSIX) |
| 32 #include "rlz/lib/rlz_value_store.h" // SetRlzStoreDirectory | 32 #include "rlz/lib/rlz_value_store.h" // SetRlzStoreDirectory |
| 33 #endif | 33 #endif |
| 34 #if defined(OS_CHROMEOS) | |
| 35 #include "rlz/chromeos/lib/rlz_value_store_chromeos.h" | |
| 36 #endif | |
| 37 | 34 |
| 38 using content::NavigationEntry; | 35 using content::NavigationEntry; |
| 39 using testing::AssertionResult; | 36 using testing::AssertionResult; |
| 40 using testing::AssertionSuccess; | 37 using testing::AssertionSuccess; |
| 41 using testing::AssertionFailure; | 38 using testing::AssertionFailure; |
| 42 | 39 |
| 43 #if defined(OS_WIN) | 40 #if defined(OS_WIN) |
| 44 using base::win::RegKey; | 41 using base::win::RegKey; |
| 45 using registry_util::RegistryOverrideManager; | 42 using registry_util::RegistryOverrideManager; |
| 46 #endif | 43 #endif |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 109 |
| 113 bool was_ping_sent_for_brand(const std::string& brand) const { | 110 bool was_ping_sent_for_brand(const std::string& brand) const { |
| 114 return pinged_brands_.count(brand) > 0; | 111 return pinged_brands_.count(brand) > 0; |
| 115 } | 112 } |
| 116 | 113 |
| 117 void set_assume_not_ui_thread(bool assume_not_ui_thread) { | 114 void set_assume_not_ui_thread(bool assume_not_ui_thread) { |
| 118 assume_not_ui_thread_ = assume_not_ui_thread; | 115 assume_not_ui_thread_ = assume_not_ui_thread; |
| 119 } | 116 } |
| 120 | 117 |
| 121 private: | 118 private: |
| 122 virtual bool InitWorkers() OVERRIDE { | |
| 123 return true; | |
| 124 } | |
| 125 | |
| 126 virtual void ScheduleDelayedInit(int delay) OVERRIDE { | 119 virtual void ScheduleDelayedInit(int delay) OVERRIDE { |
| 127 // If the delay is 0, invoke the delayed init now. Otherwise, | 120 // If the delay is 0, invoke the delayed init now. Otherwise, |
| 128 // don't schedule anything, it will be manually called during tests. | 121 // don't schedule anything, it will be manually called during tests. |
| 129 if (delay == 0) | 122 if (delay == 0) |
| 130 DelayedInit(); | 123 DelayedInit(); |
| 131 } | 124 } |
| 132 | 125 |
| 133 virtual void ScheduleFinancialPing() OVERRIDE { | 126 virtual void ScheduleFinancialPing() OVERRIDE { |
| 134 PingNowImpl(); | 127 PingNowImpl(); |
| 135 } | 128 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 165 } | 158 } |
| 166 | 159 |
| 167 std::set<std::string> pinged_brands_; | 160 std::set<std::string> pinged_brands_; |
| 168 bool assume_not_ui_thread_; | 161 bool assume_not_ui_thread_; |
| 169 | 162 |
| 170 DISALLOW_COPY_AND_ASSIGN(TestRLZTracker); | 163 DISALLOW_COPY_AND_ASSIGN(TestRLZTracker); |
| 171 }; | 164 }; |
| 172 | 165 |
| 173 class RlzLibTest : public testing::Test { | 166 class RlzLibTest : public testing::Test { |
| 174 public: | 167 public: |
| 175 #if defined(OS_CHROMEOS) | |
| 176 RlzLibTest(); | |
| 177 #endif | |
| 178 | |
| 179 virtual void SetUp() OVERRIDE; | 168 virtual void SetUp() OVERRIDE; |
| 180 virtual void TearDown() OVERRIDE; | 169 virtual void TearDown() OVERRIDE; |
| 181 | 170 |
| 182 protected: | 171 protected: |
| 183 void SetMainBrand(const char* brand); | 172 void SetMainBrand(const char* brand); |
| 184 void SetReactivationBrand(const char* brand); | 173 void SetReactivationBrand(const char* brand); |
| 185 #if defined(OS_WIN) | 174 #if defined(OS_WIN) |
| 186 void SetRegistryBrandValue(const wchar_t* name, const char* brand); | 175 void SetRegistryBrandValue(const wchar_t* name, const char* brand); |
| 187 #endif | 176 #endif |
| 188 | 177 |
| 189 void SimulateOmniboxUsage(); | 178 void SimulateOmniboxUsage(); |
| 190 void SimulateHomepageUsage(); | 179 void SimulateHomepageUsage(); |
| 191 void InvokeDelayedInit(); | 180 void InvokeDelayedInit(); |
| 192 | 181 |
| 193 void ExpectEventRecorded(const char* event_name, bool expected); | 182 void ExpectEventRecorded(const char* event_name, bool expected); |
| 194 void ExpectRlzPingSent(bool expected); | 183 void ExpectRlzPingSent(bool expected); |
| 195 void ExpectReactivationRlzPingSent(bool expected); | 184 void ExpectReactivationRlzPingSent(bool expected); |
| 196 | 185 |
| 197 TestRLZTracker tracker_; | 186 TestRLZTracker tracker_; |
| 198 #if defined(OS_WIN) | 187 #if defined(OS_WIN) |
| 199 RegistryOverrideManager override_manager_; | 188 RegistryOverrideManager override_manager_; |
| 200 #elif defined(OS_MACOSX) || defined(OS_CHROMEOS) | 189 #elif defined(OS_POSIX) |
| 201 base::ScopedTempDir temp_dir_; | 190 base::ScopedTempDir temp_dir_; |
| 202 scoped_ptr<google_util::BrandForTesting> brand_override_; | 191 scoped_ptr<google_util::BrandForTesting> brand_override_; |
| 203 #endif | 192 #endif |
| 204 #if defined(OS_CHROMEOS) | |
| 205 MessageLoop loop_; | |
| 206 base::Thread io_thread_; | |
| 207 #endif | |
| 208 }; | 193 }; |
| 209 | 194 |
| 210 #if defined(OS_CHROMEOS) | |
| 211 RlzLibTest::RlzLibTest() | |
| 212 : io_thread_("RlzLibTest-io") { | |
| 213 } | |
| 214 #endif | |
| 215 | |
| 216 void RlzLibTest::SetUp() { | 195 void RlzLibTest::SetUp() { |
| 217 testing::Test::SetUp(); | 196 testing::Test::SetUp(); |
| 218 | 197 |
| 219 #if defined(OS_WIN) | 198 #if defined(OS_WIN) |
| 220 // Before overriding HKLM for the tests, we need to set it up correctly | 199 // Before overriding HKLM for the tests, we need to set it up correctly |
| 221 // so that the rlz_lib calls work. This needs to be done before we do the | 200 // so that the rlz_lib calls work. This needs to be done before we do the |
| 222 // override. | 201 // override. |
| 223 | 202 |
| 224 string16 temp_hklm_path = base::StringPrintf( | 203 string16 temp_hklm_path = base::StringPrintf( |
| 225 L"%ls\\%ls", | 204 L"%ls\\%ls", |
| (...skipping 14 matching lines...) Expand all Loading... |
| 240 ASSERT_EQ(ERROR_SUCCESS, hkcu.Create(HKEY_CURRENT_USER, | 219 ASSERT_EQ(ERROR_SUCCESS, hkcu.Create(HKEY_CURRENT_USER, |
| 241 temp_hkcu_path.c_str(), | 220 temp_hkcu_path.c_str(), |
| 242 KEY_READ)); | 221 KEY_READ)); |
| 243 | 222 |
| 244 rlz_lib::InitializeTempHivesForTesting(hklm, hkcu); | 223 rlz_lib::InitializeTempHivesForTesting(hklm, hkcu); |
| 245 | 224 |
| 246 // Its important to override HKLM before HKCU because of the registry | 225 // Its important to override HKLM before HKCU because of the registry |
| 247 // initialization performed above. | 226 // initialization performed above. |
| 248 override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE, kRlzTempHklm); | 227 override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE, kRlzTempHklm); |
| 249 override_manager_.OverrideRegistry(HKEY_CURRENT_USER, kRlzTempHkcu); | 228 override_manager_.OverrideRegistry(HKEY_CURRENT_USER, kRlzTempHkcu); |
| 250 #elif defined(OS_MACOSX) || defined(OS_CHROMEOS) | 229 #elif defined(OS_POSIX) |
| 251 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 230 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 252 rlz_lib::testing::SetRlzStoreDirectory(temp_dir_.path()); | 231 rlz_lib::testing::SetRlzStoreDirectory(temp_dir_.path()); |
| 253 #endif | 232 #endif |
| 254 | 233 |
| 255 #if defined(OS_CHROMEOS) | |
| 256 base::Thread::Options options; | |
| 257 options.message_loop_type = MessageLoop::TYPE_IO; | |
| 258 ASSERT_TRUE(io_thread_.StartWithOptions(options)); | |
| 259 rlz_lib::SetIOTaskRunner(io_thread_.message_loop_proxy()); | |
| 260 rlz_lib::RlzValueStoreChromeOS::ResetForTesting(); | |
| 261 #endif | |
| 262 | |
| 263 // Make sure a non-organic brand code is set in the registry or the RLZTracker | 234 // Make sure a non-organic brand code is set in the registry or the RLZTracker |
| 264 // is pretty much a no-op. | 235 // is pretty much a no-op. |
| 265 SetMainBrand("TEST"); | 236 SetMainBrand("TEST"); |
| 266 SetReactivationBrand(""); | 237 SetReactivationBrand(""); |
| 267 } | 238 } |
| 268 | 239 |
| 269 void RlzLibTest::TearDown() { | 240 void RlzLibTest::TearDown() { |
| 270 #if defined(OS_MACOSX) || defined(OS_CHROMEOS) | 241 #if defined(OS_POSIX) |
| 271 rlz_lib::testing::SetRlzStoreDirectory(FilePath()); | 242 rlz_lib::testing::SetRlzStoreDirectory(FilePath()); |
| 272 #endif | 243 #endif |
| 273 #if defined(OS_CHROMEOS) | |
| 274 io_thread_.Stop(); | |
| 275 #endif // defined(OS_CHROMEOS) | |
| 276 testing::Test::TearDown(); | 244 testing::Test::TearDown(); |
| 277 } | 245 } |
| 278 | 246 |
| 279 void RlzLibTest::SetMainBrand(const char* brand) { | 247 void RlzLibTest::SetMainBrand(const char* brand) { |
| 280 #if defined(OS_WIN) | 248 #if defined(OS_WIN) |
| 281 SetRegistryBrandValue(google_update::kRegRLZBrandField, brand); | 249 SetRegistryBrandValue(google_update::kRegRLZBrandField, brand); |
| 282 #elif defined(OS_MACOSX) || defined(OS_CHROMEOS) | 250 #elif defined(OS_POSIX) |
| 283 brand_override_.reset(new google_util::BrandForTesting(brand)); | 251 brand_override_.reset(new google_util::BrandForTesting(brand)); |
| 284 #endif | 252 #endif |
| 285 std::string check_brand; | 253 std::string check_brand; |
| 286 google_util::GetBrand(&check_brand); | 254 google_util::GetBrand(&check_brand); |
| 287 EXPECT_EQ(brand, check_brand); | 255 EXPECT_EQ(brand, check_brand); |
| 288 } | 256 } |
| 289 | 257 |
| 290 void RlzLibTest::SetReactivationBrand(const char* brand) { | 258 void RlzLibTest::SetReactivationBrand(const char* brand) { |
| 291 // TODO(thakis): Reactivation doesn't exist on Mac yet. | 259 // TODO(thakis): Reactivation doesn't exist on Mac yet. |
| 292 #if defined(OS_WIN) | 260 #if defined(OS_WIN) |
| (...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 796 SetMainBrand("GGLS"); | 764 SetMainBrand("GGLS"); |
| 797 SetReactivationBrand("GGRS"); | 765 SetReactivationBrand("GGRS"); |
| 798 | 766 |
| 799 RLZTracker::InitRlzDelayed(true, 20, true, true, false); | 767 RLZTracker::InitRlzDelayed(true, 20, true, true, false); |
| 800 InvokeDelayedInit(); | 768 InvokeDelayedInit(); |
| 801 | 769 |
| 802 ExpectRlzPingSent(false); | 770 ExpectRlzPingSent(false); |
| 803 ExpectReactivationRlzPingSent(false); | 771 ExpectReactivationRlzPingSent(false); |
| 804 } | 772 } |
| 805 #endif // defined(OS_WIN) | 773 #endif // defined(OS_WIN) |
| OLD | NEW |