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"
23 #include "base/win/registry.h"
26 using content::NavigationEntry
;
27 using testing::AssertionResult
;
28 using testing::AssertionSuccess
;
29 using testing::AssertionFailure
;
32 using base::win::RegKey
;
37 // Dummy RLZ string for the access points.
38 const char kOmniboxRlzString
[] = "test_omnibox";
39 const char kHomepageRlzString
[] = "test_homepage";
40 const char kAppListRlzString
[] = "test_applist";
41 const char kNewOmniboxRlzString
[] = "new_omnibox";
42 const char kNewHomepageRlzString
[] = "new_homepage";
43 const char kNewAppListRlzString
[] = "new_applist";
45 // Some helper macros to test it a string contains/does not contain a substring.
47 AssertionResult
CmpHelperSTRC(const char* str_expression
,
48 const char* substr_expression
,
51 if (NULL
!= strstr(str
, substr
)) {
52 return AssertionSuccess();
55 return AssertionFailure() << "Expected: (" << substr_expression
<< ") in ("
56 << str_expression
<< "), actual: '"
57 << substr
<< "' not in '" << str
<< "'";
60 AssertionResult
CmpHelperSTRNC(const char* str_expression
,
61 const char* substr_expression
,
64 if (NULL
== strstr(str
, substr
)) {
65 return AssertionSuccess();
68 return AssertionFailure() << "Expected: (" << substr_expression
69 << ") not in (" << str_expression
<< "), actual: '"
70 << substr
<< "' in '" << str
<< "'";
73 #define EXPECT_STR_CONTAINS(str, substr) \
74 EXPECT_PRED_FORMAT2(CmpHelperSTRC, str, substr)
76 #define EXPECT_STR_NOT_CONTAIN(str, substr) \
77 EXPECT_PRED_FORMAT2(CmpHelperSTRNC, str, substr)
81 // Test class for RLZ tracker. Makes some member functions public and
82 // overrides others to make it easier to test.
83 class TestRLZTracker
: public RLZTracker
{
85 using RLZTracker::InitRlzDelayed
;
86 using RLZTracker::DelayedInit
;
87 using RLZTracker::Observe
;
89 TestRLZTracker() : assume_not_ui_thread_(true) {
93 virtual ~TestRLZTracker() {
97 bool was_ping_sent_for_brand(const std::string
& brand
) const {
98 return pinged_brands_
.count(brand
) > 0;
101 void set_assume_not_ui_thread(bool assume_not_ui_thread
) {
102 assume_not_ui_thread_
= assume_not_ui_thread
;
106 virtual void ScheduleDelayedInit(base::TimeDelta delay
) OVERRIDE
{
107 // If the delay is 0, invoke the delayed init now. Otherwise,
108 // don't schedule anything, it will be manually called during tests.
109 if (delay
== base::TimeDelta())
113 virtual void ScheduleFinancialPing() OVERRIDE
{
117 virtual bool ScheduleRecordProductEvent(rlz_lib::Product product
,
118 rlz_lib::AccessPoint point
,
119 rlz_lib::Event event_id
) OVERRIDE
{
120 return !assume_not_ui_thread_
;
123 virtual bool ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point
) OVERRIDE
{
124 return !assume_not_ui_thread_
;
127 virtual bool ScheduleRecordFirstSearch(rlz_lib::AccessPoint point
) OVERRIDE
{
128 return !assume_not_ui_thread_
;
131 #if defined(OS_CHROMEOS)
132 virtual bool ScheduleClearRlzState() OVERRIDE
{
133 return !assume_not_ui_thread_
;
137 virtual bool SendFinancialPing(const std::string
& brand
,
138 const base::string16
& lang
,
139 const base::string16
& referral
) OVERRIDE
{
140 // Don't ping the server during tests, just pretend as if we did.
141 EXPECT_FALSE(brand
.empty());
142 pinged_brands_
.insert(brand
);
144 // Set new access points RLZ string, like the actual server ping would have
146 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_OMNIBOX
,
147 kNewOmniboxRlzString
);
148 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_HOME_PAGE
,
149 kNewHomepageRlzString
);
150 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_APP_LIST
,
151 kNewAppListRlzString
);
155 std::set
<std::string
> pinged_brands_
;
156 bool assume_not_ui_thread_
;
158 DISALLOW_COPY_AND_ASSIGN(TestRLZTracker
);
161 class RlzLibTest
: public RlzLibTestNoMachineState
{
163 virtual void SetUp() OVERRIDE
;
165 void SetMainBrand(const char* brand
);
166 void SetReactivationBrand(const char* brand
);
168 void SetRegistryBrandValue(const wchar_t* name
, const char* brand
);
171 void SimulateOmniboxUsage();
172 void SimulateHomepageUsage();
173 void SimulateAppListUsage();
174 void InvokeDelayedInit();
176 void ExpectEventRecorded(const char* event_name
, bool expected
);
177 void ExpectRlzPingSent(bool expected
);
178 void ExpectReactivationRlzPingSent(bool expected
);
180 TestRLZTracker tracker_
;
181 #if defined(OS_POSIX)
182 scoped_ptr
<google_util::BrandForTesting
> brand_override_
;
186 void RlzLibTest::SetUp() {
187 RlzLibTestNoMachineState::SetUp();
189 // Make sure a non-organic brand code is set in the registry or the RLZTracker
190 // is pretty much a no-op.
191 SetMainBrand("TEST");
192 SetReactivationBrand("");
195 void RlzLibTest::SetMainBrand(const char* brand
) {
197 SetRegistryBrandValue(google_update::kRegRLZBrandField
, brand
);
198 #elif defined(OS_POSIX)
199 brand_override_
.reset(new google_util::BrandForTesting(brand
));
201 std::string check_brand
;
202 google_util::GetBrand(&check_brand
);
203 EXPECT_EQ(brand
, check_brand
);
206 void RlzLibTest::SetReactivationBrand(const char* brand
) {
207 // TODO(thakis): Reactivation doesn't exist on Mac yet.
209 SetRegistryBrandValue(google_update::kRegRLZReactivationBrandField
, brand
);
210 std::string check_brand
;
211 google_util::GetReactivationBrand(&check_brand
);
212 EXPECT_EQ(brand
, check_brand
);
217 void RlzLibTest::SetRegistryBrandValue(const wchar_t* name
,
219 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
220 base::string16 reg_path
= dist
->GetStateKey();
221 RegKey
key(HKEY_CURRENT_USER
, reg_path
.c_str(), KEY_SET_VALUE
);
223 LONG result
= key
.DeleteValue(name
);
224 ASSERT_TRUE(ERROR_SUCCESS
== result
|| ERROR_FILE_NOT_FOUND
== result
);
226 base::string16 brand16
= base::ASCIIToUTF16(brand
);
227 ASSERT_EQ(ERROR_SUCCESS
, key
.WriteValue(name
, brand16
.c_str()));
232 void RlzLibTest::SimulateOmniboxUsage() {
233 tracker_
.Observe(chrome::NOTIFICATION_OMNIBOX_OPENED_URL
,
234 content::NotificationService::AllSources(),
235 content::Details
<OmniboxLog
>(NULL
));
238 void RlzLibTest::SimulateHomepageUsage() {
239 scoped_ptr
<NavigationEntry
> entry(NavigationEntry::Create());
241 entry
->SetTransitionType(content::PAGE_TRANSITION_HOME_PAGE
);
242 tracker_
.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING
,
243 content::NotificationService::AllSources(),
244 content::Details
<NavigationEntry
>(entry
.get()));
247 void RlzLibTest::SimulateAppListUsage() {
248 RLZTracker::RecordAppListSearch();
251 void RlzLibTest::InvokeDelayedInit() {
252 tracker_
.DelayedInit();
255 void RlzLibTest::ExpectEventRecorded(const char* event_name
, bool expected
) {
256 char cgi
[rlz_lib::kMaxCgiLength
];
257 GetProductEventsAsCgi(rlz_lib::CHROME
, cgi
, arraysize(cgi
));
259 EXPECT_STR_CONTAINS(cgi
, event_name
);
261 EXPECT_STR_NOT_CONTAIN(cgi
, event_name
);
265 void RlzLibTest::ExpectRlzPingSent(bool expected
) {
267 google_util::GetBrand(&brand
);
268 EXPECT_EQ(expected
, tracker_
.was_ping_sent_for_brand(brand
.c_str()));
271 void RlzLibTest::ExpectReactivationRlzPingSent(bool expected
) {
273 google_util::GetReactivationBrand(&brand
);
274 EXPECT_EQ(expected
, tracker_
.was_ping_sent_for_brand(brand
.c_str()));
277 // The events that affect the different RLZ scenarios are the following:
279 // A: the user starts chrome for the first time
280 // B: the user stops chrome
281 // C: the user start a subsequent time
282 // D: the user stops chrome again
283 // I: the RLZTracker::DelayedInit() method is invoked
284 // X: the user performs a search using the omnibox
285 // Y: the user performs a search using the home page
286 // Z: the user performs a search using the app list
288 // The events A to D happen in chronological order, but the other events
289 // may happen at any point between A-B or C-D, in no particular order.
291 // The visible results of the scenarios on Win are:
293 // C1I event is recorded
294 // C2I event is recorded
295 // C7I event is recorded
296 // C1F event is recorded
297 // C2F event is recorded
298 // C7F event is recorded
299 // C1S event is recorded
300 // C2S event is recorded
301 // C7S event is recorded
304 // On Mac, C5 / C6 / C8 are sent instead of C1 / C2 / C7.
305 // On ChromeOS, CA / CB / CC are sent, respectively.
307 // Variations on the above scenarios:
309 // - if the delay specified to InitRlzDelayed() is negative, then the RLZ
310 // ping should be sent out at the time of event X and not wait for I
312 // Also want to test that pre-warming the RLZ string cache works correctly.
315 const char kOmniboxInstall
[] = "C1I";
316 const char kOmniboxSetToGoogle
[] = "C1S";
317 const char kOmniboxFirstSearch
[] = "C1F";
319 const char kHomepageInstall
[] = "C2I";
320 const char kHomepageSetToGoogle
[] = "C2S";
321 const char kHomepageFirstSeach
[] = "C2F";
323 const char kAppListInstall
[] = "C7I";
324 const char kAppListSetToGoogle
[] = "C7S";
325 const char kAppListFirstSearch
[] = "C7F";
326 #elif defined(OS_MACOSX)
327 const char kOmniboxInstall
[] = "C5I";
328 const char kOmniboxSetToGoogle
[] = "C5S";
329 const char kOmniboxFirstSearch
[] = "C5F";
331 const char kHomepageInstall
[] = "C6I";
332 const char kHomepageSetToGoogle
[] = "C6S";
333 const char kHomepageFirstSeach
[] = "C6F";
335 const char kAppListInstall
[] = "C8I";
336 const char kAppListSetToGoogle
[] = "C8S";
337 const char kAppListFirstSearch
[] = "C8F";
338 #elif defined(OS_CHROMEOS)
339 const char kOmniboxInstall
[] = "CAI";
340 const char kOmniboxSetToGoogle
[] = "CAS";
341 const char kOmniboxFirstSearch
[] = "CAF";
343 const char kHomepageInstall
[] = "CBI";
344 const char kHomepageSetToGoogle
[] = "CBS";
345 const char kHomepageFirstSeach
[] = "CBF";
347 const char kAppListInstall
[] = "CCI";
348 const char kAppListSetToGoogle
[] = "CCS";
349 const char kAppListFirstSearch
[] = "CCF";
352 const base::TimeDelta kDelay
= base::TimeDelta::FromMilliseconds(20);
354 TEST_F(RlzLibTest
, RecordProductEvent
) {
355 RLZTracker::RecordProductEvent(rlz_lib::CHROME
, RLZTracker::CHROME_OMNIBOX
,
356 rlz_lib::FIRST_SEARCH
);
358 ExpectEventRecorded(kOmniboxFirstSearch
, true);
361 TEST_F(RlzLibTest
, QuickStopAfterStart
) {
362 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, true);
365 ExpectEventRecorded(kOmniboxInstall
, false);
366 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
367 ExpectEventRecorded(kOmniboxFirstSearch
, false);
370 ExpectEventRecorded(kHomepageInstall
, false);
371 ExpectEventRecorded(kHomepageSetToGoogle
, false);
372 ExpectEventRecorded(kHomepageFirstSeach
, false);
375 ExpectEventRecorded(kAppListInstall
, false);
376 ExpectEventRecorded(kAppListSetToGoogle
, false);
377 ExpectEventRecorded(kAppListFirstSearch
, false);
379 ExpectRlzPingSent(false);
382 TEST_F(RlzLibTest
, DelayedInitOnly
) {
383 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
387 ExpectEventRecorded(kOmniboxInstall
, true);
388 ExpectEventRecorded(kOmniboxSetToGoogle
, true);
389 ExpectEventRecorded(kOmniboxFirstSearch
, false);
392 ExpectEventRecorded(kHomepageInstall
, true);
393 ExpectEventRecorded(kHomepageSetToGoogle
, true);
394 ExpectEventRecorded(kHomepageFirstSeach
, false);
397 ExpectEventRecorded(kAppListInstall
, true);
398 ExpectEventRecorded(kAppListSetToGoogle
, true);
399 ExpectEventRecorded(kAppListFirstSearch
, false);
401 ExpectRlzPingSent(true);
404 TEST_F(RlzLibTest
, DelayedInitOnlyGoogleAsStartup
) {
405 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, false, false, true);
409 ExpectEventRecorded(kOmniboxInstall
, true);
410 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
411 ExpectEventRecorded(kOmniboxFirstSearch
, false);
414 ExpectEventRecorded(kHomepageInstall
, true);
415 ExpectEventRecorded(kHomepageSetToGoogle
, true);
416 ExpectEventRecorded(kHomepageFirstSeach
, true);
419 ExpectEventRecorded(kAppListInstall
, true);
420 ExpectEventRecorded(kAppListSetToGoogle
, false);
421 ExpectEventRecorded(kAppListFirstSearch
, false);
423 ExpectRlzPingSent(true);
426 TEST_F(RlzLibTest
, DelayedInitOnlyNoFirstRunNoRlzStrings
) {
427 TestRLZTracker::InitRlzDelayed(false, false, kDelay
, true, true, false);
431 ExpectEventRecorded(kOmniboxInstall
, true);
432 ExpectEventRecorded(kOmniboxSetToGoogle
, true);
433 ExpectEventRecorded(kOmniboxFirstSearch
, false);
436 ExpectEventRecorded(kHomepageInstall
, true);
437 ExpectEventRecorded(kHomepageSetToGoogle
, true);
438 ExpectEventRecorded(kHomepageFirstSeach
, false);
441 ExpectEventRecorded(kAppListInstall
, true);
442 ExpectEventRecorded(kAppListSetToGoogle
, true);
443 ExpectEventRecorded(kAppListFirstSearch
, false);
445 ExpectRlzPingSent(true);
448 TEST_F(RlzLibTest
, DelayedInitOnlyNoFirstRunNoRlzStringsGoogleAsStartup
) {
449 TestRLZTracker::InitRlzDelayed(false, false, kDelay
, false, false, true);
453 ExpectEventRecorded(kOmniboxInstall
, true);
454 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
455 ExpectEventRecorded(kOmniboxFirstSearch
, false);
458 ExpectEventRecorded(kHomepageInstall
, true);
459 ExpectEventRecorded(kHomepageSetToGoogle
, true);
460 ExpectEventRecorded(kHomepageFirstSeach
, true);
463 ExpectEventRecorded(kAppListInstall
, true);
464 ExpectEventRecorded(kAppListSetToGoogle
, false);
465 ExpectEventRecorded(kAppListFirstSearch
, false);
467 ExpectRlzPingSent(true);
470 TEST_F(RlzLibTest
, DelayedInitOnlyNoFirstRun
) {
471 // Set some dummy RLZ strings to simulate that we already ran before and
472 // performed a successful ping to the RLZ server.
473 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_OMNIBOX
, kOmniboxRlzString
);
474 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_HOME_PAGE
, kHomepageRlzString
);
475 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_APP_LIST
, kAppListRlzString
);
477 TestRLZTracker::InitRlzDelayed(false, false, kDelay
, true, true, true);
481 ExpectEventRecorded(kOmniboxInstall
, true);
482 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
483 ExpectEventRecorded(kOmniboxFirstSearch
, false);
486 ExpectEventRecorded(kHomepageInstall
, true);
487 ExpectEventRecorded(kHomepageSetToGoogle
, false);
488 ExpectEventRecorded(kHomepageFirstSeach
, true);
491 ExpectEventRecorded(kAppListInstall
, true);
492 ExpectEventRecorded(kAppListSetToGoogle
, false);
493 ExpectEventRecorded(kAppListFirstSearch
, false);
495 ExpectRlzPingSent(true);
498 TEST_F(RlzLibTest
, DelayedInitOnlyNoGoogleDefaultSearchOrHomepageOrStartup
) {
499 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, false, false, false);
503 ExpectEventRecorded(kOmniboxInstall
, true);
504 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
505 ExpectEventRecorded(kOmniboxFirstSearch
, false);
508 ExpectEventRecorded(kHomepageInstall
, true);
509 ExpectEventRecorded(kHomepageSetToGoogle
, false);
510 ExpectEventRecorded(kHomepageFirstSeach
, false);
513 ExpectEventRecorded(kAppListInstall
, true);
514 ExpectEventRecorded(kAppListSetToGoogle
, false);
515 ExpectEventRecorded(kAppListFirstSearch
, false);
517 ExpectRlzPingSent(true);
520 TEST_F(RlzLibTest
, OmniboxUsageOnly
) {
521 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
522 SimulateOmniboxUsage();
525 ExpectEventRecorded(kOmniboxInstall
, false);
526 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
527 ExpectEventRecorded(kOmniboxFirstSearch
, true);
530 ExpectEventRecorded(kHomepageInstall
, false);
531 ExpectEventRecorded(kHomepageSetToGoogle
, false);
532 ExpectEventRecorded(kHomepageFirstSeach
, false);
535 ExpectEventRecorded(kAppListInstall
, false);
536 ExpectEventRecorded(kAppListSetToGoogle
, false);
537 ExpectEventRecorded(kAppListFirstSearch
, false);
539 ExpectRlzPingSent(false);
542 TEST_F(RlzLibTest
, HomepageUsageOnly
) {
543 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
544 SimulateHomepageUsage();
547 ExpectEventRecorded(kOmniboxInstall
, false);
548 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
549 ExpectEventRecorded(kOmniboxFirstSearch
, false);
552 ExpectEventRecorded(kHomepageInstall
, false);
553 ExpectEventRecorded(kHomepageSetToGoogle
, false);
554 ExpectEventRecorded(kHomepageFirstSeach
, true);
557 ExpectEventRecorded(kAppListInstall
, false);
558 ExpectEventRecorded(kAppListSetToGoogle
, false);
559 ExpectEventRecorded(kAppListFirstSearch
, false);
561 ExpectRlzPingSent(false);
564 TEST_F(RlzLibTest
, AppListUsageOnly
) {
565 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
566 SimulateAppListUsage();
569 ExpectEventRecorded(kOmniboxInstall
, false);
570 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
571 ExpectEventRecorded(kOmniboxFirstSearch
, false);
574 ExpectEventRecorded(kHomepageInstall
, false);
575 ExpectEventRecorded(kHomepageSetToGoogle
, false);
576 ExpectEventRecorded(kHomepageFirstSeach
, false);
579 ExpectEventRecorded(kAppListInstall
, false);
580 ExpectEventRecorded(kAppListSetToGoogle
, false);
581 ExpectEventRecorded(kAppListFirstSearch
, true);
583 ExpectRlzPingSent(false);
586 TEST_F(RlzLibTest
, UsageBeforeDelayedInit
) {
587 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
588 SimulateOmniboxUsage();
589 SimulateHomepageUsage();
590 SimulateAppListUsage();
594 ExpectEventRecorded(kOmniboxInstall
, true);
595 ExpectEventRecorded(kOmniboxSetToGoogle
, true);
596 ExpectEventRecorded(kOmniboxFirstSearch
, true);
599 ExpectEventRecorded(kHomepageInstall
, true);
600 ExpectEventRecorded(kHomepageSetToGoogle
, true);
601 ExpectEventRecorded(kHomepageFirstSeach
, true);
604 ExpectEventRecorded(kAppListInstall
, true);
605 ExpectEventRecorded(kAppListSetToGoogle
, true);
606 ExpectEventRecorded(kAppListFirstSearch
, true);
608 ExpectRlzPingSent(true);
611 TEST_F(RlzLibTest
, UsageAfterDelayedInit
) {
612 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
614 SimulateOmniboxUsage();
615 SimulateHomepageUsage();
616 SimulateAppListUsage();
619 ExpectEventRecorded(kOmniboxInstall
, true);
620 ExpectEventRecorded(kOmniboxSetToGoogle
, true);
621 ExpectEventRecorded(kOmniboxFirstSearch
, true);
624 ExpectEventRecorded(kHomepageInstall
, true);
625 ExpectEventRecorded(kHomepageSetToGoogle
, true);
626 ExpectEventRecorded(kHomepageFirstSeach
, true);
629 ExpectEventRecorded(kAppListInstall
, true);
630 ExpectEventRecorded(kAppListSetToGoogle
, true);
631 ExpectEventRecorded(kAppListFirstSearch
, true);
633 ExpectRlzPingSent(true);
636 TEST_F(RlzLibTest
, OmniboxUsageSendsPingWhenSendPingImmediately
) {
637 TestRLZTracker::InitRlzDelayed(true, true, kDelay
, true, true, false);
638 SimulateOmniboxUsage();
641 ExpectEventRecorded(kOmniboxInstall
, true);
642 ExpectEventRecorded(kOmniboxSetToGoogle
, true);
643 ExpectEventRecorded(kOmniboxFirstSearch
, true);
646 ExpectEventRecorded(kHomepageInstall
, true);
647 ExpectEventRecorded(kHomepageSetToGoogle
, true);
648 ExpectEventRecorded(kHomepageFirstSeach
, false);
651 ExpectEventRecorded(kAppListInstall
, true);
652 ExpectEventRecorded(kAppListSetToGoogle
, true);
653 ExpectEventRecorded(kAppListFirstSearch
, false);
655 ExpectRlzPingSent(true);
658 TEST_F(RlzLibTest
, HomepageUsageDoesNotSendPingWhenSendPingImmediately
) {
659 TestRLZTracker::InitRlzDelayed(true, true, kDelay
, true, true, false);
660 SimulateHomepageUsage();
663 ExpectEventRecorded(kOmniboxInstall
, false);
664 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
665 ExpectEventRecorded(kOmniboxFirstSearch
, false);
668 ExpectEventRecorded(kHomepageInstall
, false);
669 ExpectEventRecorded(kHomepageSetToGoogle
, false);
670 ExpectEventRecorded(kHomepageFirstSeach
, true);
673 ExpectEventRecorded(kAppListInstall
, false);
674 ExpectEventRecorded(kAppListSetToGoogle
, false);
675 ExpectEventRecorded(kAppListFirstSearch
, false);
677 ExpectRlzPingSent(false);
680 TEST_F(RlzLibTest
, StartupUsageDoesNotSendPingWhenSendPingImmediately
) {
681 TestRLZTracker::InitRlzDelayed(true, true, kDelay
, true, false, true);
682 SimulateHomepageUsage();
685 ExpectEventRecorded(kOmniboxInstall
, false);
686 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
687 ExpectEventRecorded(kOmniboxFirstSearch
, false);
690 ExpectEventRecorded(kHomepageInstall
, false);
691 ExpectEventRecorded(kHomepageSetToGoogle
, false);
692 ExpectEventRecorded(kHomepageFirstSeach
, true);
695 ExpectEventRecorded(kAppListInstall
, false);
696 ExpectEventRecorded(kAppListSetToGoogle
, false);
697 ExpectEventRecorded(kAppListFirstSearch
, false);
699 ExpectRlzPingSent(false);
702 TEST_F(RlzLibTest
, AppListUsageDoesNotSendPingWhenSendPingImmediately
) {
703 TestRLZTracker::InitRlzDelayed(true, true, kDelay
, true, false, false);
704 SimulateAppListUsage();
707 ExpectEventRecorded(kOmniboxInstall
, false);
708 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
709 ExpectEventRecorded(kOmniboxFirstSearch
, false);
712 ExpectEventRecorded(kHomepageInstall
, false);
713 ExpectEventRecorded(kHomepageSetToGoogle
, false);
714 ExpectEventRecorded(kHomepageFirstSeach
, false);
717 ExpectEventRecorded(kAppListInstall
, false);
718 ExpectEventRecorded(kAppListSetToGoogle
, false);
719 ExpectEventRecorded(kAppListFirstSearch
, true);
721 ExpectRlzPingSent(false);
724 TEST_F(RlzLibTest
, GetAccessPointRlzOnIoThread
) {
725 // Set dummy RLZ string.
726 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_OMNIBOX
, kOmniboxRlzString
);
730 tracker_
.set_assume_not_ui_thread(true);
731 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX
, &rlz
));
732 EXPECT_STREQ(kOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
735 TEST_F(RlzLibTest
, GetAccessPointRlzNotOnIoThread
) {
736 // Set dummy RLZ string.
737 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_OMNIBOX
, kOmniboxRlzString
);
741 tracker_
.set_assume_not_ui_thread(false);
742 EXPECT_FALSE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX
, &rlz
));
745 TEST_F(RlzLibTest
, GetAccessPointRlzIsCached
) {
746 // Set dummy RLZ string.
747 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_OMNIBOX
, kOmniboxRlzString
);
751 tracker_
.set_assume_not_ui_thread(false);
752 EXPECT_FALSE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX
, &rlz
));
754 tracker_
.set_assume_not_ui_thread(true);
755 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX
, &rlz
));
756 EXPECT_STREQ(kOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
758 tracker_
.set_assume_not_ui_thread(false);
759 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX
, &rlz
));
760 EXPECT_STREQ(kOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
763 TEST_F(RlzLibTest
, PingUpdatesRlzCache
) {
764 // Set dummy RLZ string.
765 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_OMNIBOX
, kOmniboxRlzString
);
766 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_HOME_PAGE
, kHomepageRlzString
);
767 rlz_lib::SetAccessPointRlz(RLZTracker::CHROME_APP_LIST
, kAppListRlzString
);
772 tracker_
.set_assume_not_ui_thread(true);
774 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX
, &rlz
));
775 EXPECT_STREQ(kOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
776 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(
777 RLZTracker::CHROME_HOME_PAGE
, &rlz
));
778 EXPECT_STREQ(kHomepageRlzString
, base::UTF16ToUTF8(rlz
).c_str());
779 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_APP_LIST
, &rlz
));
780 EXPECT_STREQ(kAppListRlzString
, base::UTF16ToUTF8(rlz
).c_str());
782 // Make sure cache is valid.
783 tracker_
.set_assume_not_ui_thread(false);
785 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX
, &rlz
));
786 EXPECT_STREQ(kOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
787 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(
788 RLZTracker::CHROME_HOME_PAGE
, &rlz
));
789 EXPECT_STREQ(kHomepageRlzString
, base::UTF16ToUTF8(rlz
).c_str());
790 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_APP_LIST
, &rlz
));
791 EXPECT_STREQ(kAppListRlzString
, base::UTF16ToUTF8(rlz
).c_str());
794 tracker_
.set_assume_not_ui_thread(true);
795 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
797 ExpectRlzPingSent(true);
799 // Make sure cache is now updated.
800 tracker_
.set_assume_not_ui_thread(false);
802 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX
, &rlz
));
803 EXPECT_STREQ(kNewOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
804 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(
805 RLZTracker::CHROME_HOME_PAGE
, &rlz
));
806 EXPECT_STREQ(kNewHomepageRlzString
, base::UTF16ToUTF8(rlz
).c_str());
807 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_APP_LIST
, &rlz
));
808 EXPECT_STREQ(kNewAppListRlzString
, base::UTF16ToUTF8(rlz
).c_str());
811 TEST_F(RlzLibTest
, ObserveHandlesBadArgs
) {
812 scoped_ptr
<NavigationEntry
> entry(NavigationEntry::Create());
814 entry
->SetTransitionType(content::PAGE_TRANSITION_LINK
);
815 tracker_
.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING
,
816 content::NotificationService::AllSources(),
817 content::Details
<NavigationEntry
>(NULL
));
818 tracker_
.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING
,
819 content::NotificationService::AllSources(),
820 content::Details
<NavigationEntry
>(entry
.get()));
823 // TODO(thakis): Reactivation doesn't exist on Mac yet.
825 TEST_F(RlzLibTest
, ReactivationNonOrganicNonOrganic
) {
826 SetReactivationBrand("REAC");
828 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
831 ExpectRlzPingSent(true);
832 ExpectReactivationRlzPingSent(true);
835 TEST_F(RlzLibTest
, ReactivationOrganicNonOrganic
) {
836 SetMainBrand("GGLS");
837 SetReactivationBrand("REAC");
839 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
842 ExpectRlzPingSent(false);
843 ExpectReactivationRlzPingSent(true);
846 TEST_F(RlzLibTest
, ReactivationNonOrganicOrganic
) {
847 SetMainBrand("TEST");
848 SetReactivationBrand("GGLS");
850 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
853 ExpectRlzPingSent(true);
854 ExpectReactivationRlzPingSent(false);
857 TEST_F(RlzLibTest
, ReactivationOrganicOrganic
) {
858 SetMainBrand("GGLS");
859 SetReactivationBrand("GGRS");
861 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
864 ExpectRlzPingSent(false);
865 ExpectReactivationRlzPingSent(false);
867 #endif // defined(OS_WIN)
869 #if defined(OS_CHROMEOS)
870 TEST_F(RlzLibTest
, ClearRlzState
) {
871 RLZTracker::RecordProductEvent(rlz_lib::CHROME
, RLZTracker::CHROME_OMNIBOX
,
872 rlz_lib::FIRST_SEARCH
);
874 ExpectEventRecorded(kOmniboxFirstSearch
, true);
876 RLZTracker::ClearRlzState();
878 ExpectEventRecorded(kOmniboxFirstSearch
, false);
880 #endif // defined(OS_CHROMEOS)