| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/test/test_reg_util_win.h" | 10 #include "base/test/test_reg_util_win.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "base/win/registry.h" | 12 #include "base/win/registry.h" |
| 13 #include "chrome/browser/autocomplete/autocomplete.h" | 13 #include "chrome/browser/autocomplete/autocomplete.h" |
| 14 #include "chrome/browser/google/google_util.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "chrome/common/env_vars.h" | 17 #include "chrome/common/env_vars.h" |
| 17 #include "chrome/installer/util/browser_distribution.h" | 18 #include "chrome/installer/util/browser_distribution.h" |
| 18 #include "chrome/installer/util/google_update_constants.h" | 19 #include "chrome/installer/util/google_update_constants.h" |
| 19 #include "content/browser/tab_contents/navigation_entry.h" | 20 #include "content/browser/tab_contents/navigation_entry.h" |
| 20 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/browser/notification_details.h" | 22 #include "content/public/browser/notification_details.h" |
| 22 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 | 77 |
| 77 } // namespace | 78 } // namespace |
| 78 | 79 |
| 79 // Test class for RLZ tracker. Makes some member functions public and | 80 // Test class for RLZ tracker. Makes some member functions public and |
| 80 // overrides others to make it easier to test. | 81 // overrides others to make it easier to test. |
| 81 class TestRLZTracker : public RLZTracker { | 82 class TestRLZTracker : public RLZTracker { |
| 82 public: | 83 public: |
| 83 using RLZTracker::DelayedInit; | 84 using RLZTracker::DelayedInit; |
| 84 using RLZTracker::Observe; | 85 using RLZTracker::Observe; |
| 85 | 86 |
| 86 TestRLZTracker() : pingnow_called_(false), assume_not_ui_thread_(false) { | 87 TestRLZTracker() : assume_not_ui_thread_(false) { |
| 87 set_tracker(this); | 88 set_tracker(this); |
| 88 } | 89 } |
| 89 | 90 |
| 90 virtual ~TestRLZTracker() { | 91 virtual ~TestRLZTracker() { |
| 91 set_tracker(NULL); | 92 set_tracker(NULL); |
| 92 } | 93 } |
| 93 | 94 |
| 94 bool pingnow_called() const { | 95 bool was_ping_sent_for_brand(const std::string& brand) const { |
| 95 return pingnow_called_; | 96 return pinged_brands_.count(brand) > 0; |
| 96 } | 97 } |
| 97 | 98 |
| 98 void set_assume_not_ui_thread(bool assume_not_ui_thread) { | 99 void set_assume_not_ui_thread(bool assume_not_ui_thread) { |
| 99 assume_not_ui_thread_ = assume_not_ui_thread; | 100 assume_not_ui_thread_ = assume_not_ui_thread; |
| 100 } | 101 } |
| 101 | 102 |
| 102 private: | 103 private: |
| 103 virtual void ScheduleDelayedInit(int delay) OVERRIDE { | 104 virtual void ScheduleDelayedInit(int delay) OVERRIDE { |
| 104 // If the delay is 0, invoke the delayed init now. Otherwise, | 105 // If the delay is 0, invoke the delayed init now. Otherwise, |
| 105 // don't schedule anything, it will be manually called during tests. | 106 // don't schedule anything, it will be manually called during tests. |
| 106 if (delay == 0) | 107 if (delay == 0) |
| 107 DelayedInit(); | 108 DelayedInit(); |
| 108 } | 109 } |
| 109 | 110 |
| 110 virtual void ScheduleFinancialPing() OVERRIDE { | 111 virtual void ScheduleFinancialPing() OVERRIDE { |
| 111 PingNow(this); | 112 PingNow(this); |
| 112 } | 113 } |
| 113 | 114 |
| 114 virtual bool ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point) OVERRIDE { | 115 virtual bool ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point) OVERRIDE { |
| 115 return !assume_not_ui_thread_; | 116 return !assume_not_ui_thread_; |
| 116 } | 117 } |
| 117 | 118 |
| 118 virtual bool SendFinancialPing(const std::string& brand, | 119 virtual bool SendFinancialPing(const std::string& brand, |
| 119 const string16& lang, | 120 const string16& lang, |
| 120 const string16& referral) OVERRIDE { | 121 const string16& referral) OVERRIDE { |
| 121 // Don't ping the server during tests. | 122 // Don't ping the server during tests, just pretend as if we did. |
| 122 pingnow_called_ = true; | 123 EXPECT_FALSE(brand.empty()); |
| 124 pinged_brands_.insert(brand); |
| 123 | 125 |
| 124 // Set new access points RLZ string, like the actual server ping would have | 126 // Set new access points RLZ string, like the actual server ping would have |
| 125 // done. | 127 // done. |
| 126 rlz_lib::SetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, kNewOmniboxRlzString); | 128 rlz_lib::SetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, kNewOmniboxRlzString); |
| 127 rlz_lib::SetAccessPointRlz(rlz_lib::CHROME_HOME_PAGE, | 129 rlz_lib::SetAccessPointRlz(rlz_lib::CHROME_HOME_PAGE, |
| 128 kNewHomepageRlzString); | 130 kNewHomepageRlzString); |
| 129 return true; | 131 return true; |
| 130 } | 132 } |
| 131 | 133 |
| 132 bool pingnow_called_; | 134 std::set<std::string> pinged_brands_; |
| 133 bool assume_not_ui_thread_; | 135 bool assume_not_ui_thread_; |
| 134 | 136 |
| 135 DISALLOW_COPY_AND_ASSIGN(TestRLZTracker); | 137 DISALLOW_COPY_AND_ASSIGN(TestRLZTracker); |
| 136 }; | 138 }; |
| 137 | 139 |
| 138 class RlzLibTest : public testing::Test { | 140 class RlzLibTest : public testing::Test { |
| 139 virtual void SetUp() OVERRIDE; | 141 virtual void SetUp() OVERRIDE; |
| 140 virtual void TearDown() OVERRIDE; | 142 virtual void TearDown() OVERRIDE; |
| 141 | 143 |
| 142 protected: | 144 protected: |
| 145 void SetMainBrand(const char* brand); |
| 146 void SetReactivationBrand(const char* brand); |
| 147 void SetRegistryBrandValue(const wchar_t* name, const char* brand); |
| 148 |
| 143 void SimulateOmniboxUsage(); | 149 void SimulateOmniboxUsage(); |
| 144 void SimulateHomepageUsage(); | 150 void SimulateHomepageUsage(); |
| 145 void InvokeDelayedInit(); | 151 void InvokeDelayedInit(); |
| 146 | 152 |
| 147 void ExpectEventRecorded(const char* event_name, bool expected); | 153 void ExpectEventRecorded(const char* event_name, bool expected); |
| 148 void ExpectRlzPingSent(bool expected); | 154 void ExpectRlzPingSent(bool expected); |
| 155 void ExpectReactivationRlzPingSent(bool expected); |
| 149 | 156 |
| 150 TestRLZTracker tracker_; | 157 TestRLZTracker tracker_; |
| 151 RegistryOverrideManager override_manager_; | 158 RegistryOverrideManager override_manager_; |
| 152 }; | 159 }; |
| 153 | 160 |
| 154 void RlzLibTest::SetUp() { | 161 void RlzLibTest::SetUp() { |
| 155 testing::Test::SetUp(); | 162 testing::Test::SetUp(); |
| 156 | 163 |
| 157 // Before overriding HKLM for the tests, we need to set it up correctly | 164 // Before overriding HKLM for the tests, we need to set it up correctly |
| 158 // so that the rlz_lib calls work. This needs to be done before we do the | 165 // so that the rlz_lib calls work. This needs to be done before we do the |
| (...skipping 21 matching lines...) Expand all Loading... |
| 180 | 187 |
| 181 rlz_lib::InitializeTempHivesForTesting(hklm, hkcu); | 188 rlz_lib::InitializeTempHivesForTesting(hklm, hkcu); |
| 182 | 189 |
| 183 // Its important to override HKLM before HKCU because of the registry | 190 // Its important to override HKLM before HKCU because of the registry |
| 184 // initialization performed above. | 191 // initialization performed above. |
| 185 override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE, kRlzTempHklm); | 192 override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE, kRlzTempHklm); |
| 186 override_manager_.OverrideRegistry(HKEY_CURRENT_USER, kRlzTempHkcu); | 193 override_manager_.OverrideRegistry(HKEY_CURRENT_USER, kRlzTempHkcu); |
| 187 | 194 |
| 188 // Make sure a non-organic brand code is set in the registry or the RLZTracker | 195 // Make sure a non-organic brand code is set in the registry or the RLZTracker |
| 189 // is pretty much a no-op. | 196 // is pretty much a no-op. |
| 190 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 197 SetMainBrand("TEST"); |
| 191 string16 reg_path = dist->GetStateKey(); | 198 SetReactivationBrand(""); |
| 192 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_SET_VALUE); | |
| 193 ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(google_update::kRegRLZBrandField, | |
| 194 L"TEST")); | |
| 195 } | 199 } |
| 196 | 200 |
| 197 void RlzLibTest::TearDown() { | 201 void RlzLibTest::TearDown() { |
| 198 testing::Test::TearDown(); | 202 testing::Test::TearDown(); |
| 199 } | 203 } |
| 200 | 204 |
| 205 void RlzLibTest::SetMainBrand(const char* brand) { |
| 206 SetRegistryBrandValue(google_update::kRegRLZBrandField, brand); |
| 207 std::string check_brand; |
| 208 google_util::GetBrand(&check_brand); |
| 209 EXPECT_EQ(brand, check_brand); |
| 210 } |
| 211 |
| 212 void RlzLibTest::SetReactivationBrand(const char* brand) { |
| 213 SetRegistryBrandValue(google_update::kRegRLZReactivationBrandField, brand); |
| 214 std::string check_brand; |
| 215 google_util::GetReactivationBrand(&check_brand); |
| 216 EXPECT_EQ(brand, check_brand); |
| 217 } |
| 218 |
| 219 void RlzLibTest::SetRegistryBrandValue(const wchar_t* name, |
| 220 const char* brand) { |
| 221 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 222 string16 reg_path = dist->GetStateKey(); |
| 223 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_SET_VALUE); |
| 224 if (*brand == 0) { |
| 225 LONG result = key.DeleteValue(name); |
| 226 ASSERT_TRUE(ERROR_SUCCESS == result || ERROR_FILE_NOT_FOUND == result); |
| 227 } else { |
| 228 string16 brand16 = ASCIIToWide(brand); |
| 229 ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(name, brand16.c_str())); |
| 230 } |
| 231 } |
| 232 |
| 201 void RlzLibTest::SimulateOmniboxUsage() { | 233 void RlzLibTest::SimulateOmniboxUsage() { |
| 202 tracker_.Observe(chrome::NOTIFICATION_OMNIBOX_OPENED_URL, | 234 tracker_.Observe(chrome::NOTIFICATION_OMNIBOX_OPENED_URL, |
| 203 content::NotificationService::AllSources(), | 235 content::NotificationService::AllSources(), |
| 204 content::Details<AutocompleteLog>(NULL)); | 236 content::Details<AutocompleteLog>(NULL)); |
| 205 } | 237 } |
| 206 | 238 |
| 207 void RlzLibTest::SimulateHomepageUsage() { | 239 void RlzLibTest::SimulateHomepageUsage() { |
| 208 NavigationEntry entry(NULL, 0, GURL(), GURL(), string16(), | 240 NavigationEntry entry(NULL, 0, GURL(), GURL(), string16(), |
| 209 content::PAGE_TRANSITION_HOME_PAGE, false); | 241 content::PAGE_TRANSITION_HOME_PAGE, false); |
| 210 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING, | 242 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING, |
| 211 content::NotificationService::AllSources(), | 243 content::NotificationService::AllSources(), |
| 212 content::Details<NavigationEntry>(&entry)); | 244 content::Details<NavigationEntry>(&entry)); |
| 213 } | 245 } |
| 214 | 246 |
| 215 void RlzLibTest::InvokeDelayedInit() { | 247 void RlzLibTest::InvokeDelayedInit() { |
| 216 tracker_.DelayedInit(); | 248 tracker_.DelayedInit(); |
| 217 } | 249 } |
| 218 | 250 |
| 219 void RlzLibTest::ExpectEventRecorded(const char* event_name, bool expected) { | 251 void RlzLibTest::ExpectEventRecorded(const char* event_name, bool expected) { |
| 220 char cgi[rlz_lib::kMaxCgiLength]; | 252 char cgi[rlz_lib::kMaxCgiLength]; |
| 221 GetProductEventsAsCgi(rlz_lib::CHROME, cgi, arraysize(cgi)); | 253 GetProductEventsAsCgi(rlz_lib::CHROME, cgi, arraysize(cgi)); |
| 222 if (expected) { | 254 if (expected) { |
| 223 EXPECT_STR_CONTAINS(cgi, event_name); | 255 EXPECT_STR_CONTAINS(cgi, event_name); |
| 224 } else { | 256 } else { |
| 225 EXPECT_STR_NOT_CONTAIN(cgi, event_name); | 257 EXPECT_STR_NOT_CONTAIN(cgi, event_name); |
| 226 } | 258 } |
| 227 } | 259 } |
| 228 | 260 |
| 229 void RlzLibTest::ExpectRlzPingSent(bool expected) { | 261 void RlzLibTest::ExpectRlzPingSent(bool expected) { |
| 230 EXPECT_EQ(expected, tracker_.pingnow_called()); | 262 std::string brand; |
| 263 google_util::GetBrand(&brand); |
| 264 EXPECT_EQ(expected, tracker_.was_ping_sent_for_brand(brand.c_str())); |
| 265 } |
| 266 |
| 267 void RlzLibTest::ExpectReactivationRlzPingSent(bool expected) { |
| 268 std::string brand; |
| 269 google_util::GetReactivationBrand(&brand); |
| 270 EXPECT_EQ(expected, tracker_.was_ping_sent_for_brand(brand.c_str())); |
| 231 } | 271 } |
| 232 | 272 |
| 233 TEST_F(RlzLibTest, RecordProductEvent) { | 273 TEST_F(RlzLibTest, RecordProductEvent) { |
| 234 RLZTracker::RecordProductEvent(rlz_lib::CHROME, rlz_lib::CHROME_OMNIBOX, | 274 RLZTracker::RecordProductEvent(rlz_lib::CHROME, rlz_lib::CHROME_OMNIBOX, |
| 235 rlz_lib::FIRST_SEARCH); | 275 rlz_lib::FIRST_SEARCH); |
| 236 | 276 |
| 237 ExpectEventRecorded("C1F", true); | 277 ExpectEventRecorded("C1F", true); |
| 238 } | 278 } |
| 239 | 279 |
| 240 // The events that affect the different RLZ scenarios are the following: | 280 // The events that affect the different RLZ scenarios are the following: |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 TEST_F(RlzLibTest, ObserveHandlesBadArgs) { | 582 TEST_F(RlzLibTest, ObserveHandlesBadArgs) { |
| 543 NavigationEntry entry(NULL, 0, GURL(), GURL(), string16(), | 583 NavigationEntry entry(NULL, 0, GURL(), GURL(), string16(), |
| 544 content::PAGE_TRANSITION_LINK, false); | 584 content::PAGE_TRANSITION_LINK, false); |
| 545 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING, | 585 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING, |
| 546 content::NotificationService::AllSources(), | 586 content::NotificationService::AllSources(), |
| 547 content::Details<NavigationEntry>(NULL)); | 587 content::Details<NavigationEntry>(NULL)); |
| 548 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING, | 588 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING, |
| 549 content::NotificationService::AllSources(), | 589 content::NotificationService::AllSources(), |
| 550 content::Details<NavigationEntry>(&entry)); | 590 content::Details<NavigationEntry>(&entry)); |
| 551 } | 591 } |
| 592 |
| 593 TEST_F(RlzLibTest, ReactivationNonOrganicNonOrganic) { |
| 594 SetReactivationBrand("REAC"); |
| 595 |
| 596 RLZTracker::InitRlzDelayed(true, 20, true, true); |
| 597 InvokeDelayedInit(); |
| 598 |
| 599 ExpectRlzPingSent(true); |
| 600 ExpectReactivationRlzPingSent(true); |
| 601 } |
| 602 |
| 603 TEST_F(RlzLibTest, ReactivationOrganicNonOrganic) { |
| 604 SetMainBrand("GGLS"); |
| 605 SetReactivationBrand("REAC"); |
| 606 |
| 607 RLZTracker::InitRlzDelayed(true, 20, true, true); |
| 608 InvokeDelayedInit(); |
| 609 |
| 610 ExpectRlzPingSent(false); |
| 611 ExpectReactivationRlzPingSent(true); |
| 612 } |
| 613 |
| 614 TEST_F(RlzLibTest, ReactivationNonOrganicOrganic) { |
| 615 SetMainBrand("TEST"); |
| 616 SetReactivationBrand("GGLS"); |
| 617 |
| 618 RLZTracker::InitRlzDelayed(true, 20, true, true); |
| 619 InvokeDelayedInit(); |
| 620 |
| 621 ExpectRlzPingSent(true); |
| 622 ExpectReactivationRlzPingSent(false); |
| 623 } |
| 624 |
| 625 TEST_F(RlzLibTest, ReactivationOrganicOrganic) { |
| 626 SetMainBrand("GGLS"); |
| 627 SetReactivationBrand("GGRS"); |
| 628 |
| 629 RLZTracker::InitRlzDelayed(true, 20, true, true); |
| 630 InvokeDelayedInit(); |
| 631 |
| 632 ExpectRlzPingSent(false); |
| 633 ExpectReactivationRlzPingSent(false); |
| 634 } |
| 635 |
| OLD | NEW |