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 "base/time/time.h"
10 #include "chrome/browser/autocomplete/autocomplete_controller.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/google/google_brand.h"
13 #include "chrome/browser/omnibox/omnibox_log.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/installer/util/browser_distribution.h"
16 #include "chrome/installer/util/google_update_constants.h"
17 #include "components/metrics/proto/omnibox_event.pb.h"
18 #include "content/public/browser/navigation_entry.h"
19 #include "content/public/browser/notification_details.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/notification_source.h"
22 #include "rlz/test/rlz_test_helpers.h"
23 #include "testing/gtest/include/gtest/gtest.h"
26 #include "base/win/registry.h"
29 using content::NavigationEntry
;
30 using testing::AssertionResult
;
31 using testing::AssertionSuccess
;
32 using testing::AssertionFailure
;
35 using base::win::RegKey
;
40 // Dummy RLZ string for the access points.
41 const char kOmniboxRlzString
[] = "test_omnibox";
42 const char kHomepageRlzString
[] = "test_homepage";
43 const char kAppListRlzString
[] = "test_applist";
44 const char kNewOmniboxRlzString
[] = "new_omnibox";
45 const char kNewHomepageRlzString
[] = "new_homepage";
46 const char kNewAppListRlzString
[] = "new_applist";
48 // Some helper macros to test it a string contains/does not contain a substring.
50 AssertionResult
CmpHelperSTRC(const char* str_expression
,
51 const char* substr_expression
,
54 if (NULL
!= strstr(str
, substr
)) {
55 return AssertionSuccess();
58 return AssertionFailure() << "Expected: (" << substr_expression
<< ") in ("
59 << str_expression
<< "), actual: '"
60 << substr
<< "' not in '" << str
<< "'";
63 AssertionResult
CmpHelperSTRNC(const char* str_expression
,
64 const char* substr_expression
,
67 if (NULL
== strstr(str
, substr
)) {
68 return AssertionSuccess();
71 return AssertionFailure() << "Expected: (" << substr_expression
72 << ") not in (" << str_expression
<< "), actual: '"
73 << substr
<< "' in '" << str
<< "'";
76 #define EXPECT_STR_CONTAINS(str, substr) \
77 EXPECT_PRED_FORMAT2(CmpHelperSTRC, str, substr)
79 #define EXPECT_STR_NOT_CONTAIN(str, substr) \
80 EXPECT_PRED_FORMAT2(CmpHelperSTRNC, str, substr)
84 // Test class for RLZ tracker. Makes some member functions public and
85 // overrides others to make it easier to test.
86 class TestRLZTracker
: public RLZTracker
{
88 using RLZTracker::InitRlzDelayed
;
89 using RLZTracker::DelayedInit
;
90 using RLZTracker::Observe
;
92 TestRLZTracker() : assume_not_ui_thread_(true) {
96 virtual ~TestRLZTracker() {
100 bool was_ping_sent_for_brand(const std::string
& brand
) const {
101 return pinged_brands_
.count(brand
) > 0;
104 void set_assume_not_ui_thread(bool assume_not_ui_thread
) {
105 assume_not_ui_thread_
= assume_not_ui_thread
;
109 virtual void ScheduleDelayedInit(base::TimeDelta delay
) OVERRIDE
{
110 // If the delay is 0, invoke the delayed init now. Otherwise,
111 // don't schedule anything, it will be manually called during tests.
112 if (delay
== base::TimeDelta())
116 virtual void ScheduleFinancialPing() OVERRIDE
{
120 virtual bool ScheduleRecordProductEvent(rlz_lib::Product product
,
121 rlz_lib::AccessPoint point
,
122 rlz_lib::Event event_id
) OVERRIDE
{
123 return !assume_not_ui_thread_
;
126 virtual bool ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point
) OVERRIDE
{
127 return !assume_not_ui_thread_
;
130 virtual bool ScheduleRecordFirstSearch(rlz_lib::AccessPoint point
) OVERRIDE
{
131 return !assume_not_ui_thread_
;
134 #if defined(OS_CHROMEOS)
135 virtual bool ScheduleClearRlzState() OVERRIDE
{
136 return !assume_not_ui_thread_
;
140 virtual bool SendFinancialPing(const std::string
& brand
,
141 const base::string16
& lang
,
142 const base::string16
& referral
) OVERRIDE
{
143 // Don't ping the server during tests, just pretend as if we did.
144 EXPECT_FALSE(brand
.empty());
145 pinged_brands_
.insert(brand
);
147 // Set new access points RLZ string, like the actual server ping would have
149 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(),
150 kNewOmniboxRlzString
);
151 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeHomePage(),
152 kNewHomepageRlzString
);
153 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeAppList(),
154 kNewAppListRlzString
);
158 std::set
<std::string
> pinged_brands_
;
159 bool assume_not_ui_thread_
;
161 DISALLOW_COPY_AND_ASSIGN(TestRLZTracker
);
164 class RlzLibTest
: public RlzLibTestNoMachineState
{
166 virtual void SetUp() OVERRIDE
;
168 void SetMainBrand(const char* brand
);
169 void SetReactivationBrand(const char* brand
);
171 void SetRegistryBrandValue(const wchar_t* name
, const char* brand
);
174 void SimulateOmniboxUsage();
175 void SimulateHomepageUsage();
176 void SimulateAppListUsage();
177 void InvokeDelayedInit();
179 void ExpectEventRecorded(const char* event_name
, bool expected
);
180 void ExpectRlzPingSent(bool expected
);
181 void ExpectReactivationRlzPingSent(bool expected
);
183 TestRLZTracker tracker_
;
184 #if defined(OS_POSIX)
185 scoped_ptr
<google_brand::BrandForTesting
> brand_override_
;
189 void RlzLibTest::SetUp() {
190 RlzLibTestNoMachineState::SetUp();
192 // Make sure a non-organic brand code is set in the registry or the RLZTracker
193 // is pretty much a no-op.
194 SetMainBrand("TEST");
195 SetReactivationBrand("");
198 void RlzLibTest::SetMainBrand(const char* brand
) {
200 SetRegistryBrandValue(google_update::kRegRLZBrandField
, brand
);
201 #elif defined(OS_POSIX)
202 brand_override_
.reset(new google_brand::BrandForTesting(brand
));
204 std::string check_brand
;
205 google_brand::GetBrand(&check_brand
);
206 EXPECT_EQ(brand
, check_brand
);
209 void RlzLibTest::SetReactivationBrand(const char* brand
) {
210 // TODO(thakis): Reactivation doesn't exist on Mac yet.
212 SetRegistryBrandValue(google_update::kRegRLZReactivationBrandField
, brand
);
213 std::string check_brand
;
214 google_brand::GetReactivationBrand(&check_brand
);
215 EXPECT_EQ(brand
, check_brand
);
220 void RlzLibTest::SetRegistryBrandValue(const wchar_t* name
,
222 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
223 base::string16 reg_path
= dist
->GetStateKey();
224 RegKey
key(HKEY_CURRENT_USER
, reg_path
.c_str(), KEY_SET_VALUE
);
226 LONG result
= key
.DeleteValue(name
);
227 ASSERT_TRUE(ERROR_SUCCESS
== result
|| ERROR_FILE_NOT_FOUND
== result
);
229 base::string16 brand16
= base::ASCIIToUTF16(brand
);
230 ASSERT_EQ(ERROR_SUCCESS
, key
.WriteValue(name
, brand16
.c_str()));
235 void RlzLibTest::SimulateOmniboxUsage() {
236 // Create a dummy OmniboxLog object. The 'is_popup_open' field needs to be
237 // true to trigger record of the first search. All other fields are passed in
238 // with empty or invalid values.
239 AutocompleteResult empty_result
;
240 OmniboxLog
dummy(base::string16(), false, metrics::OmniboxInputType::INVALID
,
242 metrics::OmniboxEventProto::INVALID_SPEC
,
243 base::TimeDelta::FromSeconds(0), 0,
244 base::TimeDelta::FromSeconds(0),
245 AutocompleteResult());
247 tracker_
.Observe(chrome::NOTIFICATION_OMNIBOX_OPENED_URL
,
248 content::NotificationService::AllSources(),
249 content::Details
<OmniboxLog
>(&dummy
));
252 void RlzLibTest::SimulateHomepageUsage() {
253 scoped_ptr
<NavigationEntry
> entry(NavigationEntry::Create());
255 entry
->SetTransitionType(content::PAGE_TRANSITION_HOME_PAGE
);
256 tracker_
.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING
,
257 content::NotificationService::AllSources(),
258 content::Details
<NavigationEntry
>(entry
.get()));
261 void RlzLibTest::SimulateAppListUsage() {
262 RLZTracker::RecordAppListSearch();
265 void RlzLibTest::InvokeDelayedInit() {
266 tracker_
.DelayedInit();
269 void RlzLibTest::ExpectEventRecorded(const char* event_name
, bool expected
) {
270 char cgi
[rlz_lib::kMaxCgiLength
];
271 GetProductEventsAsCgi(rlz_lib::CHROME
, cgi
, arraysize(cgi
));
273 EXPECT_STR_CONTAINS(cgi
, event_name
);
275 EXPECT_STR_NOT_CONTAIN(cgi
, event_name
);
279 void RlzLibTest::ExpectRlzPingSent(bool expected
) {
281 google_brand::GetBrand(&brand
);
282 EXPECT_EQ(expected
, tracker_
.was_ping_sent_for_brand(brand
.c_str()));
285 void RlzLibTest::ExpectReactivationRlzPingSent(bool expected
) {
287 google_brand::GetReactivationBrand(&brand
);
288 EXPECT_EQ(expected
, tracker_
.was_ping_sent_for_brand(brand
.c_str()));
291 // The events that affect the different RLZ scenarios are the following:
293 // A: the user starts chrome for the first time
294 // B: the user stops chrome
295 // C: the user start a subsequent time
296 // D: the user stops chrome again
297 // I: the RLZTracker::DelayedInit() method is invoked
298 // X: the user performs a search using the omnibox
299 // Y: the user performs a search using the home page
300 // Z: the user performs a search using the app list
302 // The events A to D happen in chronological order, but the other events
303 // may happen at any point between A-B or C-D, in no particular order.
305 // The visible results of the scenarios on Win are:
307 // C1I event is recorded
308 // C2I event is recorded
309 // C7I event is recorded
310 // C1F event is recorded
311 // C2F event is recorded
312 // C7F event is recorded
313 // C1S event is recorded
314 // C2S event is recorded
315 // C7S event is recorded
318 // On Mac, C5 / C6 / C8 are sent instead of C1 / C2 / C7.
319 // On ChromeOS, CA / CB / CC are sent, respectively.
321 // Variations on the above scenarios:
323 // - if the delay specified to InitRlzDelayed() is negative, then the RLZ
324 // ping should be sent out at the time of event X and not wait for I
326 // Also want to test that pre-warming the RLZ string cache works correctly.
329 const char kOmniboxInstall
[] = "C1I";
330 const char kOmniboxSetToGoogle
[] = "C1S";
331 const char kOmniboxFirstSearch
[] = "C1F";
333 const char kHomepageInstall
[] = "C2I";
334 const char kHomepageSetToGoogle
[] = "C2S";
335 const char kHomepageFirstSeach
[] = "C2F";
337 const char kAppListInstall
[] = "C7I";
338 const char kAppListSetToGoogle
[] = "C7S";
339 const char kAppListFirstSearch
[] = "C7F";
340 #elif defined(OS_MACOSX)
341 const char kOmniboxInstall
[] = "C5I";
342 const char kOmniboxSetToGoogle
[] = "C5S";
343 const char kOmniboxFirstSearch
[] = "C5F";
345 const char kHomepageInstall
[] = "C6I";
346 const char kHomepageSetToGoogle
[] = "C6S";
347 const char kHomepageFirstSeach
[] = "C6F";
349 const char kAppListInstall
[] = "C8I";
350 const char kAppListSetToGoogle
[] = "C8S";
351 const char kAppListFirstSearch
[] = "C8F";
352 #elif defined(OS_CHROMEOS)
353 const char kOmniboxInstall
[] = "CAI";
354 const char kOmniboxSetToGoogle
[] = "CAS";
355 const char kOmniboxFirstSearch
[] = "CAF";
357 const char kHomepageInstall
[] = "CBI";
358 const char kHomepageSetToGoogle
[] = "CBS";
359 const char kHomepageFirstSeach
[] = "CBF";
361 const char kAppListInstall
[] = "CCI";
362 const char kAppListSetToGoogle
[] = "CCS";
363 const char kAppListFirstSearch
[] = "CCF";
366 const base::TimeDelta kDelay
= base::TimeDelta::FromMilliseconds(20);
368 TEST_F(RlzLibTest
, RecordProductEvent
) {
369 RLZTracker::RecordProductEvent(rlz_lib::CHROME
, RLZTracker::ChromeOmnibox(),
370 rlz_lib::FIRST_SEARCH
);
372 ExpectEventRecorded(kOmniboxFirstSearch
, true);
375 TEST_F(RlzLibTest
, QuickStopAfterStart
) {
376 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, true);
379 ExpectEventRecorded(kOmniboxInstall
, false);
380 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
381 ExpectEventRecorded(kOmniboxFirstSearch
, false);
384 ExpectEventRecorded(kHomepageInstall
, false);
385 ExpectEventRecorded(kHomepageSetToGoogle
, false);
386 ExpectEventRecorded(kHomepageFirstSeach
, false);
389 ExpectEventRecorded(kAppListInstall
, false);
390 ExpectEventRecorded(kAppListSetToGoogle
, false);
391 ExpectEventRecorded(kAppListFirstSearch
, false);
393 ExpectRlzPingSent(false);
396 TEST_F(RlzLibTest
, DelayedInitOnly
) {
397 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
401 ExpectEventRecorded(kOmniboxInstall
, true);
402 ExpectEventRecorded(kOmniboxSetToGoogle
, true);
403 ExpectEventRecorded(kOmniboxFirstSearch
, false);
406 ExpectEventRecorded(kHomepageInstall
, true);
407 ExpectEventRecorded(kHomepageSetToGoogle
, true);
408 ExpectEventRecorded(kHomepageFirstSeach
, false);
411 ExpectEventRecorded(kAppListInstall
, true);
412 ExpectEventRecorded(kAppListSetToGoogle
, true);
413 ExpectEventRecorded(kAppListFirstSearch
, false);
415 ExpectRlzPingSent(true);
418 TEST_F(RlzLibTest
, DelayedInitOnlyGoogleAsStartup
) {
419 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, false, false, true);
423 ExpectEventRecorded(kOmniboxInstall
, true);
424 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
425 ExpectEventRecorded(kOmniboxFirstSearch
, false);
428 ExpectEventRecorded(kHomepageInstall
, true);
429 ExpectEventRecorded(kHomepageSetToGoogle
, true);
430 ExpectEventRecorded(kHomepageFirstSeach
, true);
433 ExpectEventRecorded(kAppListInstall
, true);
434 ExpectEventRecorded(kAppListSetToGoogle
, false);
435 ExpectEventRecorded(kAppListFirstSearch
, false);
437 ExpectRlzPingSent(true);
440 TEST_F(RlzLibTest
, DelayedInitOnlyNoFirstRunNoRlzStrings
) {
441 TestRLZTracker::InitRlzDelayed(false, false, kDelay
, true, true, false);
445 ExpectEventRecorded(kOmniboxInstall
, true);
446 ExpectEventRecorded(kOmniboxSetToGoogle
, true);
447 ExpectEventRecorded(kOmniboxFirstSearch
, false);
450 ExpectEventRecorded(kHomepageInstall
, true);
451 ExpectEventRecorded(kHomepageSetToGoogle
, true);
452 ExpectEventRecorded(kHomepageFirstSeach
, false);
455 ExpectEventRecorded(kAppListInstall
, true);
456 ExpectEventRecorded(kAppListSetToGoogle
, true);
457 ExpectEventRecorded(kAppListFirstSearch
, false);
459 ExpectRlzPingSent(true);
462 TEST_F(RlzLibTest
, DelayedInitOnlyNoFirstRunNoRlzStringsGoogleAsStartup
) {
463 TestRLZTracker::InitRlzDelayed(false, false, kDelay
, false, false, true);
467 ExpectEventRecorded(kOmniboxInstall
, true);
468 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
469 ExpectEventRecorded(kOmniboxFirstSearch
, false);
472 ExpectEventRecorded(kHomepageInstall
, true);
473 ExpectEventRecorded(kHomepageSetToGoogle
, true);
474 ExpectEventRecorded(kHomepageFirstSeach
, true);
477 ExpectEventRecorded(kAppListInstall
, true);
478 ExpectEventRecorded(kAppListSetToGoogle
, false);
479 ExpectEventRecorded(kAppListFirstSearch
, false);
481 ExpectRlzPingSent(true);
484 TEST_F(RlzLibTest
, DelayedInitOnlyNoFirstRun
) {
485 // Set some dummy RLZ strings to simulate that we already ran before and
486 // performed a successful ping to the RLZ server.
487 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString
);
488 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeHomePage(), kHomepageRlzString
);
489 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeAppList(), kAppListRlzString
);
491 TestRLZTracker::InitRlzDelayed(false, false, kDelay
, true, true, true);
495 ExpectEventRecorded(kOmniboxInstall
, true);
496 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
497 ExpectEventRecorded(kOmniboxFirstSearch
, false);
500 ExpectEventRecorded(kHomepageInstall
, true);
501 ExpectEventRecorded(kHomepageSetToGoogle
, false);
502 ExpectEventRecorded(kHomepageFirstSeach
, true);
505 ExpectEventRecorded(kAppListInstall
, true);
506 ExpectEventRecorded(kAppListSetToGoogle
, false);
507 ExpectEventRecorded(kAppListFirstSearch
, false);
509 ExpectRlzPingSent(true);
512 TEST_F(RlzLibTest
, DelayedInitOnlyNoGoogleDefaultSearchOrHomepageOrStartup
) {
513 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, false, false, false);
517 ExpectEventRecorded(kOmniboxInstall
, true);
518 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
519 ExpectEventRecorded(kOmniboxFirstSearch
, false);
522 ExpectEventRecorded(kHomepageInstall
, true);
523 ExpectEventRecorded(kHomepageSetToGoogle
, false);
524 ExpectEventRecorded(kHomepageFirstSeach
, false);
527 ExpectEventRecorded(kAppListInstall
, true);
528 ExpectEventRecorded(kAppListSetToGoogle
, false);
529 ExpectEventRecorded(kAppListFirstSearch
, false);
531 ExpectRlzPingSent(true);
534 TEST_F(RlzLibTest
, OmniboxUsageOnly
) {
535 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
536 SimulateOmniboxUsage();
539 ExpectEventRecorded(kOmniboxInstall
, false);
540 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
541 ExpectEventRecorded(kOmniboxFirstSearch
, true);
544 ExpectEventRecorded(kHomepageInstall
, false);
545 ExpectEventRecorded(kHomepageSetToGoogle
, false);
546 ExpectEventRecorded(kHomepageFirstSeach
, false);
549 ExpectEventRecorded(kAppListInstall
, false);
550 ExpectEventRecorded(kAppListSetToGoogle
, false);
551 ExpectEventRecorded(kAppListFirstSearch
, false);
553 ExpectRlzPingSent(false);
556 TEST_F(RlzLibTest
, HomepageUsageOnly
) {
557 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
558 SimulateHomepageUsage();
561 ExpectEventRecorded(kOmniboxInstall
, false);
562 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
563 ExpectEventRecorded(kOmniboxFirstSearch
, false);
566 ExpectEventRecorded(kHomepageInstall
, false);
567 ExpectEventRecorded(kHomepageSetToGoogle
, false);
568 ExpectEventRecorded(kHomepageFirstSeach
, true);
571 ExpectEventRecorded(kAppListInstall
, false);
572 ExpectEventRecorded(kAppListSetToGoogle
, false);
573 ExpectEventRecorded(kAppListFirstSearch
, false);
575 ExpectRlzPingSent(false);
578 TEST_F(RlzLibTest
, AppListUsageOnly
) {
579 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
580 SimulateAppListUsage();
583 ExpectEventRecorded(kOmniboxInstall
, false);
584 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
585 ExpectEventRecorded(kOmniboxFirstSearch
, false);
588 ExpectEventRecorded(kHomepageInstall
, false);
589 ExpectEventRecorded(kHomepageSetToGoogle
, false);
590 ExpectEventRecorded(kHomepageFirstSeach
, false);
593 ExpectEventRecorded(kAppListInstall
, false);
594 ExpectEventRecorded(kAppListSetToGoogle
, false);
595 ExpectEventRecorded(kAppListFirstSearch
, true);
597 ExpectRlzPingSent(false);
600 TEST_F(RlzLibTest
, UsageBeforeDelayedInit
) {
601 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
602 SimulateOmniboxUsage();
603 SimulateHomepageUsage();
604 SimulateAppListUsage();
608 ExpectEventRecorded(kOmniboxInstall
, true);
609 ExpectEventRecorded(kOmniboxSetToGoogle
, true);
610 ExpectEventRecorded(kOmniboxFirstSearch
, true);
613 ExpectEventRecorded(kHomepageInstall
, true);
614 ExpectEventRecorded(kHomepageSetToGoogle
, true);
615 ExpectEventRecorded(kHomepageFirstSeach
, true);
618 ExpectEventRecorded(kAppListInstall
, true);
619 ExpectEventRecorded(kAppListSetToGoogle
, true);
620 ExpectEventRecorded(kAppListFirstSearch
, true);
622 ExpectRlzPingSent(true);
625 TEST_F(RlzLibTest
, UsageAfterDelayedInit
) {
626 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
628 SimulateOmniboxUsage();
629 SimulateHomepageUsage();
630 SimulateAppListUsage();
633 ExpectEventRecorded(kOmniboxInstall
, true);
634 ExpectEventRecorded(kOmniboxSetToGoogle
, true);
635 ExpectEventRecorded(kOmniboxFirstSearch
, true);
638 ExpectEventRecorded(kHomepageInstall
, true);
639 ExpectEventRecorded(kHomepageSetToGoogle
, true);
640 ExpectEventRecorded(kHomepageFirstSeach
, true);
643 ExpectEventRecorded(kAppListInstall
, true);
644 ExpectEventRecorded(kAppListSetToGoogle
, true);
645 ExpectEventRecorded(kAppListFirstSearch
, true);
647 ExpectRlzPingSent(true);
650 TEST_F(RlzLibTest
, OmniboxUsageSendsPingWhenSendPingImmediately
) {
651 TestRLZTracker::InitRlzDelayed(true, true, kDelay
, true, true, false);
652 SimulateOmniboxUsage();
655 ExpectEventRecorded(kOmniboxInstall
, true);
656 ExpectEventRecorded(kOmniboxSetToGoogle
, true);
657 ExpectEventRecorded(kOmniboxFirstSearch
, true);
660 ExpectEventRecorded(kHomepageInstall
, true);
661 ExpectEventRecorded(kHomepageSetToGoogle
, true);
662 ExpectEventRecorded(kHomepageFirstSeach
, false);
665 ExpectEventRecorded(kAppListInstall
, true);
666 ExpectEventRecorded(kAppListSetToGoogle
, true);
667 ExpectEventRecorded(kAppListFirstSearch
, false);
669 ExpectRlzPingSent(true);
672 TEST_F(RlzLibTest
, HomepageUsageDoesNotSendPingWhenSendPingImmediately
) {
673 TestRLZTracker::InitRlzDelayed(true, true, kDelay
, true, true, false);
674 SimulateHomepageUsage();
677 ExpectEventRecorded(kOmniboxInstall
, false);
678 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
679 ExpectEventRecorded(kOmniboxFirstSearch
, false);
682 ExpectEventRecorded(kHomepageInstall
, false);
683 ExpectEventRecorded(kHomepageSetToGoogle
, false);
684 ExpectEventRecorded(kHomepageFirstSeach
, true);
687 ExpectEventRecorded(kAppListInstall
, false);
688 ExpectEventRecorded(kAppListSetToGoogle
, false);
689 ExpectEventRecorded(kAppListFirstSearch
, false);
691 ExpectRlzPingSent(false);
694 TEST_F(RlzLibTest
, StartupUsageDoesNotSendPingWhenSendPingImmediately
) {
695 TestRLZTracker::InitRlzDelayed(true, true, kDelay
, true, false, true);
696 SimulateHomepageUsage();
699 ExpectEventRecorded(kOmniboxInstall
, false);
700 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
701 ExpectEventRecorded(kOmniboxFirstSearch
, false);
704 ExpectEventRecorded(kHomepageInstall
, false);
705 ExpectEventRecorded(kHomepageSetToGoogle
, false);
706 ExpectEventRecorded(kHomepageFirstSeach
, true);
709 ExpectEventRecorded(kAppListInstall
, false);
710 ExpectEventRecorded(kAppListSetToGoogle
, false);
711 ExpectEventRecorded(kAppListFirstSearch
, false);
713 ExpectRlzPingSent(false);
716 TEST_F(RlzLibTest
, AppListUsageDoesNotSendPingWhenSendPingImmediately
) {
717 TestRLZTracker::InitRlzDelayed(true, true, kDelay
, true, false, false);
718 SimulateAppListUsage();
721 ExpectEventRecorded(kOmniboxInstall
, false);
722 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
723 ExpectEventRecorded(kOmniboxFirstSearch
, false);
726 ExpectEventRecorded(kHomepageInstall
, false);
727 ExpectEventRecorded(kHomepageSetToGoogle
, false);
728 ExpectEventRecorded(kHomepageFirstSeach
, false);
731 ExpectEventRecorded(kAppListInstall
, false);
732 ExpectEventRecorded(kAppListSetToGoogle
, false);
733 ExpectEventRecorded(kAppListFirstSearch
, true);
735 ExpectRlzPingSent(false);
738 TEST_F(RlzLibTest
, GetAccessPointRlzOnIoThread
) {
739 // Set dummy RLZ string.
740 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString
);
744 tracker_
.set_assume_not_ui_thread(true);
745 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz
));
746 EXPECT_STREQ(kOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
749 TEST_F(RlzLibTest
, GetAccessPointRlzNotOnIoThread
) {
750 // Set dummy RLZ string.
751 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString
);
755 tracker_
.set_assume_not_ui_thread(false);
757 RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz
));
760 TEST_F(RlzLibTest
, GetAccessPointRlzIsCached
) {
761 // Set dummy RLZ string.
762 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString
);
766 tracker_
.set_assume_not_ui_thread(false);
768 RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz
));
770 tracker_
.set_assume_not_ui_thread(true);
771 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz
));
772 EXPECT_STREQ(kOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
774 tracker_
.set_assume_not_ui_thread(false);
775 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz
));
776 EXPECT_STREQ(kOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
779 TEST_F(RlzLibTest
, PingUpdatesRlzCache
) {
780 // Set dummy RLZ string.
781 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString
);
782 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeHomePage(), kHomepageRlzString
);
783 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeAppList(), kAppListRlzString
);
788 tracker_
.set_assume_not_ui_thread(true);
790 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz
));
791 EXPECT_STREQ(kOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
792 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(
793 RLZTracker::ChromeHomePage(), &rlz
));
794 EXPECT_STREQ(kHomepageRlzString
, base::UTF16ToUTF8(rlz
).c_str());
795 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeAppList(), &rlz
));
796 EXPECT_STREQ(kAppListRlzString
, base::UTF16ToUTF8(rlz
).c_str());
798 // Make sure cache is valid.
799 tracker_
.set_assume_not_ui_thread(false);
801 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz
));
802 EXPECT_STREQ(kOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
803 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(
804 RLZTracker::ChromeHomePage(), &rlz
));
805 EXPECT_STREQ(kHomepageRlzString
, base::UTF16ToUTF8(rlz
).c_str());
806 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeAppList(), &rlz
));
807 EXPECT_STREQ(kAppListRlzString
, base::UTF16ToUTF8(rlz
).c_str());
810 tracker_
.set_assume_not_ui_thread(true);
811 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
813 ExpectRlzPingSent(true);
815 // Make sure cache is now updated.
816 tracker_
.set_assume_not_ui_thread(false);
818 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz
));
819 EXPECT_STREQ(kNewOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
820 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(
821 RLZTracker::ChromeHomePage(), &rlz
));
822 EXPECT_STREQ(kNewHomepageRlzString
, base::UTF16ToUTF8(rlz
).c_str());
823 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeAppList(), &rlz
));
824 EXPECT_STREQ(kNewAppListRlzString
, base::UTF16ToUTF8(rlz
).c_str());
827 TEST_F(RlzLibTest
, ObserveHandlesBadArgs
) {
828 scoped_ptr
<NavigationEntry
> entry(NavigationEntry::Create());
830 entry
->SetTransitionType(content::PAGE_TRANSITION_LINK
);
831 tracker_
.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING
,
832 content::NotificationService::AllSources(),
833 content::Details
<NavigationEntry
>(NULL
));
834 tracker_
.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING
,
835 content::NotificationService::AllSources(),
836 content::Details
<NavigationEntry
>(entry
.get()));
839 // TODO(thakis): Reactivation doesn't exist on Mac yet.
841 TEST_F(RlzLibTest
, ReactivationNonOrganicNonOrganic
) {
842 SetReactivationBrand("REAC");
844 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
847 ExpectRlzPingSent(true);
848 ExpectReactivationRlzPingSent(true);
851 TEST_F(RlzLibTest
, ReactivationOrganicNonOrganic
) {
852 SetMainBrand("GGLS");
853 SetReactivationBrand("REAC");
855 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
858 ExpectRlzPingSent(false);
859 ExpectReactivationRlzPingSent(true);
862 TEST_F(RlzLibTest
, ReactivationNonOrganicOrganic
) {
863 SetMainBrand("TEST");
864 SetReactivationBrand("GGLS");
866 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
869 ExpectRlzPingSent(true);
870 ExpectReactivationRlzPingSent(false);
873 TEST_F(RlzLibTest
, ReactivationOrganicOrganic
) {
874 SetMainBrand("GGLS");
875 SetReactivationBrand("GGRS");
877 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
880 ExpectRlzPingSent(false);
881 ExpectReactivationRlzPingSent(false);
883 #endif // defined(OS_WIN)
885 #if defined(OS_CHROMEOS)
886 TEST_F(RlzLibTest
, ClearRlzState
) {
887 RLZTracker::RecordProductEvent(rlz_lib::CHROME
, RLZTracker::ChromeOmnibox(),
888 rlz_lib::FIRST_SEARCH
);
890 ExpectEventRecorded(kOmniboxFirstSearch
, true);
892 RLZTracker::ClearRlzState();
894 ExpectEventRecorded(kOmniboxFirstSearch
, false);
896 #endif // defined(OS_CHROMEOS)