Use RegistryOverrideManager properly in RLZ tests.
[chromium-blink-merge.git] / chrome / browser / rlz / rlz_unittest.cc
blob198c62a650f3e8c187d05b56e3c101b47aba1b73
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
3 // found in the LICENSE file.
5 #include "chrome/browser/rlz/rlz.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/google/google_util.h"
11 #include "chrome/browser/omnibox/omnibox_log.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/installer/util/browser_distribution.h"
14 #include "chrome/installer/util/google_update_constants.h"
15 #include "content/public/browser/navigation_entry.h"
16 #include "content/public/browser/notification_details.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/browser/notification_source.h"
19 #include "rlz/test/rlz_test_helpers.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 #if defined(OS_WIN)
23 #include "base/win/registry.h"
24 #endif
26 using content::NavigationEntry;
27 using testing::AssertionResult;
28 using testing::AssertionSuccess;
29 using testing::AssertionFailure;
31 #if defined(OS_WIN)
32 using base::win::RegKey;
33 #endif
35 namespace {
37 // Dummy RLZ string for the access points.
38 const char kOmniboxRlzString[] = "test_omnibox";
39 const char kHomepageRlzString[] = "test_homepage";
40 const char kNewOmniboxRlzString[] = "new_omnibox";
41 const char kNewHomepageRlzString[] = "new_homepage";
43 // Some helper macros to test it a string contains/does not contain a substring.
45 AssertionResult CmpHelperSTRC(const char* str_expression,
46 const char* substr_expression,
47 const char* str,
48 const char* substr) {
49 if (NULL != strstr(str, substr)) {
50 return AssertionSuccess();
53 return AssertionFailure() << "Expected: (" << substr_expression << ") in ("
54 << str_expression << "), actual: '"
55 << substr << "' not in '" << str << "'";
58 AssertionResult CmpHelperSTRNC(const char* str_expression,
59 const char* substr_expression,
60 const char* str,
61 const char* substr) {
62 if (NULL == strstr(str, substr)) {
63 return AssertionSuccess();
66 return AssertionFailure() << "Expected: (" << substr_expression
67 << ") not in (" << str_expression << "), actual: '"
68 << substr << "' in '" << str << "'";
71 #define EXPECT_STR_CONTAINS(str, substr) \
72 EXPECT_PRED_FORMAT2(CmpHelperSTRC, str, substr)
74 #define EXPECT_STR_NOT_CONTAIN(str, substr) \
75 EXPECT_PRED_FORMAT2(CmpHelperSTRNC, str, substr)
77 } // namespace
79 // Test class for RLZ tracker. Makes some member functions public and
80 // overrides others to make it easier to test.
81 class TestRLZTracker : public RLZTracker {
82 public:
83 using RLZTracker::InitRlzDelayed;
84 using RLZTracker::DelayedInit;
85 using RLZTracker::Observe;
87 TestRLZTracker() : assume_not_ui_thread_(true) {
88 set_tracker(this);
91 virtual ~TestRLZTracker() {
92 set_tracker(NULL);
95 bool was_ping_sent_for_brand(const std::string& brand) const {
96 return pinged_brands_.count(brand) > 0;
99 void set_assume_not_ui_thread(bool assume_not_ui_thread) {
100 assume_not_ui_thread_ = assume_not_ui_thread;
103 private:
104 virtual void ScheduleDelayedInit(base::TimeDelta delay) OVERRIDE {
105 // If the delay is 0, invoke the delayed init now. Otherwise,
106 // don't schedule anything, it will be manually called during tests.
107 if (delay == base::TimeDelta())
108 DelayedInit();
111 virtual void ScheduleFinancialPing() OVERRIDE {
112 PingNowImpl();
115 virtual bool ScheduleRecordProductEvent(rlz_lib::Product product,
116 rlz_lib::AccessPoint point,
117 rlz_lib::Event event_id) OVERRIDE {
118 return !assume_not_ui_thread_;
121 virtual bool ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point) OVERRIDE {
122 return !assume_not_ui_thread_;
125 virtual bool ScheduleRecordFirstSearch(rlz_lib::AccessPoint point) OVERRIDE {
126 return !assume_not_ui_thread_;
129 #if defined(OS_CHROMEOS)
130 virtual bool ScheduleClearRlzState() OVERRIDE {
131 return !assume_not_ui_thread_;
133 #endif
135 virtual bool SendFinancialPing(const std::string& brand,
136 const string16& lang,
137 const string16& referral) OVERRIDE {
138 // Don't ping the server during tests, just pretend as if we did.
139 EXPECT_FALSE(brand.empty());
140 pinged_brands_.insert(brand);
142 // Set new access points RLZ string, like the actual server ping would have
143 // done.
144 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_OMNIBOX,
145 kNewOmniboxRlzString);
146 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_HOME_PAGE,
147 kNewHomepageRlzString);
148 return true;
151 std::set<std::string> pinged_brands_;
152 bool assume_not_ui_thread_;
154 DISALLOW_COPY_AND_ASSIGN(TestRLZTracker);
157 class RlzLibTest : public RlzLibTestNoMachineState {
158 protected:
159 virtual void SetUp() OVERRIDE;
161 void SetMainBrand(const char* brand);
162 void SetReactivationBrand(const char* brand);
163 #if defined(OS_WIN)
164 void SetRegistryBrandValue(const wchar_t* name, const char* brand);
165 #endif
167 void SimulateOmniboxUsage();
168 void SimulateHomepageUsage();
169 void InvokeDelayedInit();
171 void ExpectEventRecorded(const char* event_name, bool expected);
172 void ExpectRlzPingSent(bool expected);
173 void ExpectReactivationRlzPingSent(bool expected);
175 TestRLZTracker tracker_;
176 #if defined(OS_POSIX)
177 scoped_ptr<google_util::BrandForTesting> brand_override_;
178 #endif
181 void RlzLibTest::SetUp() {
182 RlzLibTestNoMachineState::SetUp();
184 // Make sure a non-organic brand code is set in the registry or the RLZTracker
185 // is pretty much a no-op.
186 SetMainBrand("TEST");
187 SetReactivationBrand("");
190 void RlzLibTest::SetMainBrand(const char* brand) {
191 #if defined(OS_WIN)
192 SetRegistryBrandValue(google_update::kRegRLZBrandField, brand);
193 #elif defined(OS_POSIX)
194 brand_override_.reset(new google_util::BrandForTesting(brand));
195 #endif
196 std::string check_brand;
197 google_util::GetBrand(&check_brand);
198 EXPECT_EQ(brand, check_brand);
201 void RlzLibTest::SetReactivationBrand(const char* brand) {
202 // TODO(thakis): Reactivation doesn't exist on Mac yet.
203 #if defined(OS_WIN)
204 SetRegistryBrandValue(google_update::kRegRLZReactivationBrandField, brand);
205 std::string check_brand;
206 google_util::GetReactivationBrand(&check_brand);
207 EXPECT_EQ(brand, check_brand);
208 #endif
211 #if defined(OS_WIN)
212 void RlzLibTest::SetRegistryBrandValue(const wchar_t* name,
213 const char* brand) {
214 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
215 string16 reg_path = dist->GetStateKey();
216 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_SET_VALUE);
217 if (*brand == 0) {
218 LONG result = key.DeleteValue(name);
219 ASSERT_TRUE(ERROR_SUCCESS == result || ERROR_FILE_NOT_FOUND == result);
220 } else {
221 string16 brand16 = ASCIIToUTF16(brand);
222 ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(name, brand16.c_str()));
225 #endif
227 void RlzLibTest::SimulateOmniboxUsage() {
228 tracker_.Observe(chrome::NOTIFICATION_OMNIBOX_OPENED_URL,
229 content::NotificationService::AllSources(),
230 content::Details<OmniboxLog>(NULL));
233 void RlzLibTest::SimulateHomepageUsage() {
234 scoped_ptr<NavigationEntry> entry(NavigationEntry::Create());
235 entry->SetPageID(0);
236 entry->SetTransitionType(content::PAGE_TRANSITION_HOME_PAGE);
237 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING,
238 content::NotificationService::AllSources(),
239 content::Details<NavigationEntry>(entry.get()));
242 void RlzLibTest::InvokeDelayedInit() {
243 tracker_.DelayedInit();
246 void RlzLibTest::ExpectEventRecorded(const char* event_name, bool expected) {
247 char cgi[rlz_lib::kMaxCgiLength];
248 GetProductEventsAsCgi(rlz_lib::CHROME, cgi, arraysize(cgi));
249 if (expected) {
250 EXPECT_STR_CONTAINS(cgi, event_name);
251 } else {
252 EXPECT_STR_NOT_CONTAIN(cgi, event_name);
256 void RlzLibTest::ExpectRlzPingSent(bool expected) {
257 std::string brand;
258 google_util::GetBrand(&brand);
259 EXPECT_EQ(expected, tracker_.was_ping_sent_for_brand(brand.c_str()));
262 void RlzLibTest::ExpectReactivationRlzPingSent(bool expected) {
263 std::string brand;
264 google_util::GetReactivationBrand(&brand);
265 EXPECT_EQ(expected, tracker_.was_ping_sent_for_brand(brand.c_str()));
268 // The events that affect the different RLZ scenarios are the following:
270 // A: the user starts chrome for the first time
271 // B: the user stops chrome
272 // C: the user start a subsequent time
273 // D: the user stops chrome again
274 // I: the RLZTracker::DelayedInit() method is invoked
275 // X: the user performs a search using the omnibox
276 // Y: the user performs a search using the home page
278 // The events A to D happen in chronological order, but the other events
279 // may happen at any point between A-B or C-D, in no particular order.
281 // The visible results of the scenarios on Win are:
283 // C1I event is recorded
284 // C2I event is recorded
285 // C1F event is recorded
286 // C2F event is recorded
287 // C1S event is recorded
288 // C2S event is recorded
289 // RLZ ping sent
291 // On Mac, C5 / C6 are sent instead of C1 / C2.
292 // On ChromeOS, CA / CB are sent, respectively.
294 // Variations on the above scenarios:
296 // - if the delay specified to InitRlzDelayed() is negative, then the RLZ
297 // ping should be sent out at the time of event X and not wait for I
299 // Also want to test that pre-warming the RLZ string cache works correctly.
301 #if defined(OS_WIN)
302 const char kOmniboxInstall[] = "C1I";
303 const char kOmniboxSetToGoogle[] = "C1S";
304 const char kOmniboxFirstSearch[] = "C1F";
306 const char kHomepageInstall[] = "C2I";
307 const char kHomepageSetToGoogle[] = "C2S";
308 const char kHomepageFirstSeach[] = "C2F";
309 #elif defined(OS_MACOSX)
310 const char kOmniboxInstall[] = "C5I";
311 const char kOmniboxSetToGoogle[] = "C5S";
312 const char kOmniboxFirstSearch[] = "C5F";
314 const char kHomepageInstall[] = "C6I";
315 const char kHomepageSetToGoogle[] = "C6S";
316 const char kHomepageFirstSeach[] = "C6F";
317 #elif defined(OS_CHROMEOS)
318 const char kOmniboxInstall[] = "CAI";
319 const char kOmniboxSetToGoogle[] = "CAS";
320 const char kOmniboxFirstSearch[] = "CAF";
322 const char kHomepageInstall[] = "CBI";
323 const char kHomepageSetToGoogle[] = "CBS";
324 const char kHomepageFirstSeach[] = "CBF";
325 #endif
327 const base::TimeDelta kDelay = base::TimeDelta::FromMilliseconds(20);
329 TEST_F(RlzLibTest, RecordProductEvent) {
330 RLZTracker::RecordProductEvent(rlz_lib::CHROME, RLZTracker::CHROME_OMNIBOX,
331 rlz_lib::FIRST_SEARCH);
333 ExpectEventRecorded(kOmniboxFirstSearch, true);
336 TEST_F(RlzLibTest, QuickStopAfterStart) {
337 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, true);
339 // Omnibox events.
340 ExpectEventRecorded(kOmniboxInstall, false);
341 ExpectEventRecorded(kOmniboxSetToGoogle, false);
342 ExpectEventRecorded(kOmniboxFirstSearch, false);
344 // Home page events.
345 ExpectEventRecorded(kHomepageInstall, false);
346 ExpectEventRecorded(kHomepageSetToGoogle, false);
347 ExpectEventRecorded(kHomepageFirstSeach, false);
349 ExpectRlzPingSent(false);
352 TEST_F(RlzLibTest, DelayedInitOnly) {
353 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
354 InvokeDelayedInit();
356 // Omnibox events.
357 ExpectEventRecorded(kOmniboxInstall, true);
358 ExpectEventRecorded(kOmniboxSetToGoogle, true);
359 ExpectEventRecorded(kOmniboxFirstSearch, false);
361 // Home page events.
362 ExpectEventRecorded(kHomepageInstall, true);
363 ExpectEventRecorded(kHomepageSetToGoogle, true);
364 ExpectEventRecorded(kHomepageFirstSeach, false);
366 ExpectRlzPingSent(true);
369 TEST_F(RlzLibTest, DelayedInitOnlyGoogleAsStartup) {
370 TestRLZTracker::InitRlzDelayed(true, false, kDelay, false, false, true);
371 InvokeDelayedInit();
373 // Omnibox events.
374 ExpectEventRecorded(kOmniboxInstall, true);
375 ExpectEventRecorded(kOmniboxSetToGoogle, false);
376 ExpectEventRecorded(kOmniboxFirstSearch, false);
378 // Home page events.
379 ExpectEventRecorded(kHomepageInstall, true);
380 ExpectEventRecorded(kHomepageSetToGoogle, true);
381 ExpectEventRecorded(kHomepageFirstSeach, true);
383 ExpectRlzPingSent(true);
386 TEST_F(RlzLibTest, DelayedInitOnlyNoFirstRunNoRlzStrings) {
387 TestRLZTracker::InitRlzDelayed(false, false, kDelay, true, true, false);
388 InvokeDelayedInit();
390 // Omnibox events.
391 ExpectEventRecorded(kOmniboxInstall, true);
392 ExpectEventRecorded(kOmniboxSetToGoogle, true);
393 ExpectEventRecorded(kOmniboxFirstSearch, false);
395 // Home page events.
396 ExpectEventRecorded(kHomepageInstall, true);
397 ExpectEventRecorded(kHomepageSetToGoogle, true);
398 ExpectEventRecorded(kHomepageFirstSeach, false);
400 ExpectRlzPingSent(true);
403 TEST_F(RlzLibTest, DelayedInitOnlyNoFirstRunNoRlzStringsGoogleAsStartup) {
404 TestRLZTracker::InitRlzDelayed(false, false, kDelay, false, false, true);
405 InvokeDelayedInit();
407 // Omnibox events.
408 ExpectEventRecorded(kOmniboxInstall, true);
409 ExpectEventRecorded(kOmniboxSetToGoogle, false);
410 ExpectEventRecorded(kOmniboxFirstSearch, false);
412 // Home page events.
413 ExpectEventRecorded(kHomepageInstall, true);
414 ExpectEventRecorded(kHomepageSetToGoogle, true);
415 ExpectEventRecorded(kHomepageFirstSeach, true);
417 ExpectRlzPingSent(true);
420 TEST_F(RlzLibTest, DelayedInitOnlyNoFirstRun) {
421 // Set some dummy RLZ strings to simulate that we already ran before and
422 // performed a successful ping to the RLZ server.
423 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, kOmniboxRlzString);
424 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_HOME_PAGE, kHomepageRlzString);
426 TestRLZTracker::InitRlzDelayed(false, false, kDelay, true, true, true);
427 InvokeDelayedInit();
429 // Omnibox events.
430 ExpectEventRecorded(kOmniboxInstall, true);
431 ExpectEventRecorded(kOmniboxSetToGoogle, false);
432 ExpectEventRecorded(kOmniboxFirstSearch, false);
434 // Home page events.
435 ExpectEventRecorded(kHomepageInstall, true);
436 ExpectEventRecorded(kHomepageSetToGoogle, false);
437 ExpectEventRecorded(kHomepageFirstSeach, true);
439 ExpectRlzPingSent(true);
442 TEST_F(RlzLibTest, DelayedInitOnlyNoGoogleDefaultSearchOrHomepageOrStartup) {
443 TestRLZTracker::InitRlzDelayed(true, false, kDelay, false, false, false);
444 InvokeDelayedInit();
446 // Omnibox events.
447 ExpectEventRecorded(kOmniboxInstall, true);
448 ExpectEventRecorded(kOmniboxSetToGoogle, false);
449 ExpectEventRecorded(kOmniboxFirstSearch, false);
451 // Home page events.
452 ExpectEventRecorded(kHomepageInstall, true);
453 ExpectEventRecorded(kHomepageSetToGoogle, false);
454 ExpectEventRecorded(kHomepageFirstSeach, false);
456 ExpectRlzPingSent(true);
459 TEST_F(RlzLibTest, OmniboxUsageOnly) {
460 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
461 SimulateOmniboxUsage();
463 // Omnibox events.
464 ExpectEventRecorded(kOmniboxInstall, false);
465 ExpectEventRecorded(kOmniboxSetToGoogle, false);
466 ExpectEventRecorded(kOmniboxFirstSearch, true);
468 // Home page events.
469 ExpectEventRecorded(kHomepageInstall, false);
470 ExpectEventRecorded(kHomepageSetToGoogle, false);
471 ExpectEventRecorded(kHomepageFirstSeach, false);
473 ExpectRlzPingSent(false);
476 TEST_F(RlzLibTest, HomepageUsageOnly) {
477 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
478 SimulateHomepageUsage();
480 // Omnibox events.
481 ExpectEventRecorded(kOmniboxInstall, false);
482 ExpectEventRecorded(kOmniboxSetToGoogle, false);
483 ExpectEventRecorded(kOmniboxFirstSearch, false);
485 // Home page events.
486 ExpectEventRecorded(kHomepageInstall, false);
487 ExpectEventRecorded(kHomepageSetToGoogle, false);
488 ExpectEventRecorded(kHomepageFirstSeach, true);
490 ExpectRlzPingSent(false);
493 TEST_F(RlzLibTest, UsageBeforeDelayedInit) {
494 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
495 SimulateOmniboxUsage();
496 SimulateHomepageUsage();
497 InvokeDelayedInit();
499 // Omnibox events.
500 ExpectEventRecorded(kOmniboxInstall, true);
501 ExpectEventRecorded(kOmniboxSetToGoogle, true);
502 ExpectEventRecorded(kOmniboxFirstSearch, true);
504 // Home page events.
505 ExpectEventRecorded(kHomepageInstall, true);
506 ExpectEventRecorded(kHomepageSetToGoogle, true);
507 ExpectEventRecorded(kHomepageFirstSeach, true);
509 ExpectRlzPingSent(true);
512 TEST_F(RlzLibTest, OmniboxUsageAfterDelayedInit) {
513 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
514 InvokeDelayedInit();
515 SimulateOmniboxUsage();
516 SimulateHomepageUsage();
518 // Omnibox events.
519 ExpectEventRecorded(kOmniboxInstall, true);
520 ExpectEventRecorded(kOmniboxSetToGoogle, true);
521 ExpectEventRecorded(kOmniboxFirstSearch, true);
523 // Home page events.
524 ExpectEventRecorded(kHomepageInstall, true);
525 ExpectEventRecorded(kHomepageSetToGoogle, true);
526 ExpectEventRecorded(kHomepageFirstSeach, true);
528 ExpectRlzPingSent(true);
531 TEST_F(RlzLibTest, OmniboxUsageSendsPingWhenSendPingImmediately) {
532 TestRLZTracker::InitRlzDelayed(true, true, kDelay, true, true, false);
533 SimulateOmniboxUsage();
535 // Omnibox events.
536 ExpectEventRecorded(kOmniboxInstall, true);
537 ExpectEventRecorded(kOmniboxSetToGoogle, true);
538 ExpectEventRecorded(kOmniboxFirstSearch, true);
540 // Home page events.
541 ExpectEventRecorded(kHomepageInstall, true);
542 ExpectEventRecorded(kHomepageSetToGoogle, true);
543 ExpectEventRecorded(kHomepageFirstSeach, false);
545 ExpectRlzPingSent(true);
548 TEST_F(RlzLibTest, HomepageUsageDoesNotSendPingWhenSendPingImmediately) {
549 TestRLZTracker::InitRlzDelayed(true, true, kDelay, true, true, false);
550 SimulateHomepageUsage();
552 // Omnibox events.
553 ExpectEventRecorded(kOmniboxInstall, false);
554 ExpectEventRecorded(kOmniboxSetToGoogle, false);
555 ExpectEventRecorded(kOmniboxFirstSearch, false);
557 // Home page events.
558 ExpectEventRecorded(kHomepageInstall, false);
559 ExpectEventRecorded(kHomepageSetToGoogle, false);
560 ExpectEventRecorded(kHomepageFirstSeach, true);
562 ExpectRlzPingSent(false);
565 TEST_F(RlzLibTest, StartupUsageDoesNotSendPingWhenSendPingImmediately) {
566 TestRLZTracker::InitRlzDelayed(true, true, kDelay, true, false, true);
567 SimulateHomepageUsage();
569 // Omnibox events.
570 ExpectEventRecorded(kOmniboxInstall, false);
571 ExpectEventRecorded(kOmniboxSetToGoogle, false);
572 ExpectEventRecorded(kOmniboxFirstSearch, false);
574 // Home page events.
575 ExpectEventRecorded(kHomepageInstall, false);
576 ExpectEventRecorded(kHomepageSetToGoogle, false);
577 ExpectEventRecorded(kHomepageFirstSeach, true);
579 ExpectRlzPingSent(false);
582 TEST_F(RlzLibTest, GetAccessPointRlzOnIoThread) {
583 // Set dummy RLZ string.
584 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, kOmniboxRlzString);
586 string16 rlz;
588 tracker_.set_assume_not_ui_thread(true);
589 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz));
590 EXPECT_STREQ(kOmniboxRlzString, UTF16ToUTF8(rlz).c_str());
593 TEST_F(RlzLibTest, GetAccessPointRlzNotOnIoThread) {
594 // Set dummy RLZ string.
595 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, kOmniboxRlzString);
597 string16 rlz;
599 tracker_.set_assume_not_ui_thread(false);
600 EXPECT_FALSE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz));
603 TEST_F(RlzLibTest, GetAccessPointRlzIsCached) {
604 // Set dummy RLZ string.
605 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, kOmniboxRlzString);
607 string16 rlz;
609 tracker_.set_assume_not_ui_thread(false);
610 EXPECT_FALSE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz));
612 tracker_.set_assume_not_ui_thread(true);
613 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz));
614 EXPECT_STREQ(kOmniboxRlzString, UTF16ToUTF8(rlz).c_str());
616 tracker_.set_assume_not_ui_thread(false);
617 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz));
618 EXPECT_STREQ(kOmniboxRlzString, UTF16ToUTF8(rlz).c_str());
621 TEST_F(RlzLibTest, PingUpdatesRlzCache) {
622 // Set dummy RLZ string.
623 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, kOmniboxRlzString);
624 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_HOME_PAGE, kHomepageRlzString);
626 string16 rlz;
628 // Prime the cache.
629 tracker_.set_assume_not_ui_thread(true);
631 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz));
632 EXPECT_STREQ(kOmniboxRlzString, UTF16ToUTF8(rlz).c_str());
633 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(
634 RLZTracker::CHROME_HOME_PAGE, &rlz));
635 EXPECT_STREQ(kHomepageRlzString, UTF16ToUTF8(rlz).c_str());
637 // Make sure cache is valid.
638 tracker_.set_assume_not_ui_thread(false);
640 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz));
641 EXPECT_STREQ(kOmniboxRlzString, UTF16ToUTF8(rlz).c_str());
642 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(
643 RLZTracker::CHROME_HOME_PAGE, &rlz));
644 EXPECT_STREQ(kHomepageRlzString, UTF16ToUTF8(rlz).c_str());
646 // Perform ping.
647 tracker_.set_assume_not_ui_thread(true);
648 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
649 InvokeDelayedInit();
650 ExpectRlzPingSent(true);
652 // Make sure cache is now updated.
653 tracker_.set_assume_not_ui_thread(false);
655 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz));
656 EXPECT_STREQ(kNewOmniboxRlzString, UTF16ToUTF8(rlz).c_str());
657 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(
658 RLZTracker::CHROME_HOME_PAGE, &rlz));
659 EXPECT_STREQ(kNewHomepageRlzString, UTF16ToUTF8(rlz).c_str());
662 TEST_F(RlzLibTest, ObserveHandlesBadArgs) {
663 scoped_ptr<NavigationEntry> entry(NavigationEntry::Create());
664 entry->SetPageID(0);
665 entry->SetTransitionType(content::PAGE_TRANSITION_LINK);
666 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING,
667 content::NotificationService::AllSources(),
668 content::Details<NavigationEntry>(NULL));
669 tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING,
670 content::NotificationService::AllSources(),
671 content::Details<NavigationEntry>(entry.get()));
674 // TODO(thakis): Reactivation doesn't exist on Mac yet.
675 #if defined(OS_WIN)
676 TEST_F(RlzLibTest, ReactivationNonOrganicNonOrganic) {
677 SetReactivationBrand("REAC");
679 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
680 InvokeDelayedInit();
682 ExpectRlzPingSent(true);
683 ExpectReactivationRlzPingSent(true);
686 TEST_F(RlzLibTest, ReactivationOrganicNonOrganic) {
687 SetMainBrand("GGLS");
688 SetReactivationBrand("REAC");
690 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
691 InvokeDelayedInit();
693 ExpectRlzPingSent(false);
694 ExpectReactivationRlzPingSent(true);
697 TEST_F(RlzLibTest, ReactivationNonOrganicOrganic) {
698 SetMainBrand("TEST");
699 SetReactivationBrand("GGLS");
701 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
702 InvokeDelayedInit();
704 ExpectRlzPingSent(true);
705 ExpectReactivationRlzPingSent(false);
708 TEST_F(RlzLibTest, ReactivationOrganicOrganic) {
709 SetMainBrand("GGLS");
710 SetReactivationBrand("GGRS");
712 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
713 InvokeDelayedInit();
715 ExpectRlzPingSent(false);
716 ExpectReactivationRlzPingSent(false);
718 #endif // defined(OS_WIN)
720 #if defined(OS_CHROMEOS)
721 TEST_F(RlzLibTest, ClearRlzState) {
722 RLZTracker::RecordProductEvent(rlz_lib::CHROME, RLZTracker::CHROME_OMNIBOX,
723 rlz_lib::FIRST_SEARCH);
725 ExpectEventRecorded(kOmniboxFirstSearch, true);
727 RLZTracker::ClearRlzState();
729 ExpectEventRecorded(kOmniboxFirstSearch, false);
731 #endif // defined(OS_CHROMEOS)