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); | |
Alexei Svitkine (slow)
2011/12/02 20:40:17
Can you add:
std::string check_brand;
google_util
Roger Tawa OOO till Jul 10th
2011/12/02 20:53:56
Done.
| |
207 } | |
208 | |
209 void RlzLibTest::SetReactivationBrand(const char* brand) { | |
210 SetRegistryBrandValue(google_update::kRegRLZReactivationBrandField, brand); | |
Alexei Svitkine (slow)
2011/12/02 20:40:17
Can you add:
std::string check_brand;
google_util
Roger Tawa OOO till Jul 10th
2011/12/02 20:53:56
Done.
| |
211 } | |
212 | |
213 void RlzLibTest::SetRegistryBrandValue(const wchar_t* name, | |
214 const char* brand) { | |
215 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
216 string16 reg_path = dist->GetStateKey(); | |
217 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_SET_VALUE); | |
218 if (*brand == 0) { | |
219 LONG result = key.DeleteValue(name); | |
220 ASSERT_TRUE(ERROR_SUCCESS == result || ERROR_FILE_NOT_FOUND == result); | |
221 } else { | |
222 string16 brand16 = ASCIIToWide(brand); | |
223 ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(name, brand16.c_str())); | |
224 } | |
225 } | |
226 | |
201 void RlzLibTest::SimulateOmniboxUsage() { | 227 void RlzLibTest::SimulateOmniboxUsage() { |
202 tracker_.Observe(chrome::NOTIFICATION_OMNIBOX_OPENED_URL, | 228 tracker_.Observe(chrome::NOTIFICATION_OMNIBOX_OPENED_URL, |
203 content::NotificationService::AllSources(), | 229 content::NotificationService::AllSources(), |
204 content::Details<AutocompleteLog>(NULL)); | 230 content::Details<AutocompleteLog>(NULL)); |
205 } | 231 } |
206 | 232 |
207 void RlzLibTest::SimulateHomepageUsage() { | 233 void RlzLibTest::SimulateHomepageUsage() { |
208 NavigationEntry entry(NULL, 0, GURL(), GURL(), string16(), | 234 NavigationEntry entry(NULL, 0, GURL(), GURL(), string16(), |
209 content::PAGE_TRANSITION_HOME_PAGE, false); | 235 content::PAGE_TRANSITION_HOME_PAGE, false); |
210 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING, | 236 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING, |
211 content::NotificationService::AllSources(), | 237 content::NotificationService::AllSources(), |
212 content::Details<NavigationEntry>(&entry)); | 238 content::Details<NavigationEntry>(&entry)); |
213 } | 239 } |
214 | 240 |
215 void RlzLibTest::InvokeDelayedInit() { | 241 void RlzLibTest::InvokeDelayedInit() { |
216 tracker_.DelayedInit(); | 242 tracker_.DelayedInit(); |
217 } | 243 } |
218 | 244 |
219 void RlzLibTest::ExpectEventRecorded(const char* event_name, bool expected) { | 245 void RlzLibTest::ExpectEventRecorded(const char* event_name, bool expected) { |
220 char cgi[rlz_lib::kMaxCgiLength]; | 246 char cgi[rlz_lib::kMaxCgiLength]; |
221 GetProductEventsAsCgi(rlz_lib::CHROME, cgi, arraysize(cgi)); | 247 GetProductEventsAsCgi(rlz_lib::CHROME, cgi, arraysize(cgi)); |
222 if (expected) { | 248 if (expected) { |
223 EXPECT_STR_CONTAINS(cgi, event_name); | 249 EXPECT_STR_CONTAINS(cgi, event_name); |
224 } else { | 250 } else { |
225 EXPECT_STR_NOT_CONTAIN(cgi, event_name); | 251 EXPECT_STR_NOT_CONTAIN(cgi, event_name); |
226 } | 252 } |
227 } | 253 } |
228 | 254 |
229 void RlzLibTest::ExpectRlzPingSent(bool expected) { | 255 void RlzLibTest::ExpectRlzPingSent(bool expected) { |
230 EXPECT_EQ(expected, tracker_.pingnow_called()); | 256 std::string brand; |
257 google_util::GetBrand(&brand); | |
258 EXPECT_EQ(expected, tracker_.was_ping_sent_for_brand(brand.c_str())); | |
259 } | |
260 | |
261 void RlzLibTest::ExpectReactivationRlzPingSent(bool expected) { | |
262 std::string brand; | |
263 google_util::GetReactivationBrand(&brand); | |
264 EXPECT_EQ(expected, tracker_.was_ping_sent_for_brand(brand.c_str())); | |
231 } | 265 } |
232 | 266 |
233 TEST_F(RlzLibTest, RecordProductEvent) { | 267 TEST_F(RlzLibTest, RecordProductEvent) { |
234 RLZTracker::RecordProductEvent(rlz_lib::CHROME, rlz_lib::CHROME_OMNIBOX, | 268 RLZTracker::RecordProductEvent(rlz_lib::CHROME, rlz_lib::CHROME_OMNIBOX, |
235 rlz_lib::FIRST_SEARCH); | 269 rlz_lib::FIRST_SEARCH); |
236 | 270 |
237 ExpectEventRecorded("C1F", true); | 271 ExpectEventRecorded("C1F", true); |
238 } | 272 } |
239 | 273 |
240 // The events that affect the different RLZ scenarios are the following: | 274 // 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) { | 576 TEST_F(RlzLibTest, ObserveHandlesBadArgs) { |
543 NavigationEntry entry(NULL, 0, GURL(), GURL(), string16(), | 577 NavigationEntry entry(NULL, 0, GURL(), GURL(), string16(), |
544 content::PAGE_TRANSITION_LINK, false); | 578 content::PAGE_TRANSITION_LINK, false); |
545 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING, | 579 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING, |
546 content::NotificationService::AllSources(), | 580 content::NotificationService::AllSources(), |
547 content::Details<NavigationEntry>(NULL)); | 581 content::Details<NavigationEntry>(NULL)); |
548 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING, | 582 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING, |
549 content::NotificationService::AllSources(), | 583 content::NotificationService::AllSources(), |
550 content::Details<NavigationEntry>(&entry)); | 584 content::Details<NavigationEntry>(&entry)); |
551 } | 585 } |
586 | |
587 TEST_F(RlzLibTest, ReactivationNonOrganicNonOrganic) { | |
588 SetReactivationBrand("REAC"); | |
589 | |
590 RLZTracker::InitRlzDelayed(true, 20, true, true); | |
591 InvokeDelayedInit(); | |
592 | |
593 ExpectRlzPingSent(true); | |
594 ExpectReactivationRlzPingSent(true); | |
595 } | |
596 | |
597 TEST_F(RlzLibTest, ReactivationOrganicNonOrganic) { | |
598 SetMainBrand("GGLS"); | |
599 SetReactivationBrand("REAC"); | |
600 | |
601 RLZTracker::InitRlzDelayed(true, 20, true, true); | |
602 InvokeDelayedInit(); | |
603 | |
604 ExpectRlzPingSent(false); | |
605 ExpectReactivationRlzPingSent(true); | |
606 } | |
607 | |
608 TEST_F(RlzLibTest, ReactivationNonOrganicOrganic) { | |
609 SetMainBrand("TEST"); | |
610 SetReactivationBrand("GGLS"); | |
611 | |
612 RLZTracker::InitRlzDelayed(true, 20, true, true); | |
613 InvokeDelayedInit(); | |
614 | |
615 ExpectRlzPingSent(true); | |
616 ExpectReactivationRlzPingSent(false); | |
617 } | |
618 | |
619 TEST_F(RlzLibTest, ReactivationOrganicOrganic) { | |
620 SetMainBrand("GGLS"); | |
621 SetReactivationBrand("GGRS"); | |
622 | |
623 RLZTracker::InitRlzDelayed(true, 20, true, true); | |
624 InvokeDelayedInit(); | |
625 | |
626 ExpectRlzPingSent(false); | |
627 ExpectReactivationRlzPingSent(false); | |
628 } | |
629 | |
OLD | NEW |