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 "chrome/test/base/chrome_render_view_host_test_harness.h"
18 #include "components/metrics/proto/omnibox_event.pb.h"
19 #include "content/public/browser/navigation_details.h"
20 #include "content/public/browser/navigation_entry.h"
21 #include "content/public/browser/notification_details.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/notification_source.h"
24 #include "content/public/test/test_renderer_host.h"
25 #include "rlz/test/rlz_test_helpers.h"
26 #include "testing/gtest/include/gtest/gtest.h"
30 #include "base/win/registry.h"
33 using content::NavigationEntry
;
34 using content::LoadCommittedDetails
;
35 using testing::AssertionResult
;
36 using testing::AssertionSuccess
;
37 using testing::AssertionFailure
;
40 using base::win::RegKey
;
45 // Dummy RLZ string for the access points.
46 const char kOmniboxRlzString
[] = "test_omnibox";
47 const char kHomepageRlzString
[] = "test_homepage";
48 const char kAppListRlzString
[] = "test_applist";
49 const char kNewOmniboxRlzString
[] = "new_omnibox";
50 const char kNewHomepageRlzString
[] = "new_homepage";
51 const char kNewAppListRlzString
[] = "new_applist";
53 // Some helper macros to test it a string contains/does not contain a substring.
55 AssertionResult
CmpHelperSTRC(const char* str_expression
,
56 const char* substr_expression
,
59 if (NULL
!= strstr(str
, substr
)) {
60 return AssertionSuccess();
63 return AssertionFailure() << "Expected: (" << substr_expression
<< ") in ("
64 << str_expression
<< "), actual: '"
65 << substr
<< "' not in '" << str
<< "'";
68 AssertionResult
CmpHelperSTRNC(const char* str_expression
,
69 const char* substr_expression
,
72 if (NULL
== strstr(str
, substr
)) {
73 return AssertionSuccess();
76 return AssertionFailure() << "Expected: (" << substr_expression
77 << ") not in (" << str_expression
<< "), actual: '"
78 << substr
<< "' in '" << str
<< "'";
81 #define EXPECT_STR_CONTAINS(str, substr) \
82 EXPECT_PRED_FORMAT2(CmpHelperSTRC, str, substr)
84 #define EXPECT_STR_NOT_CONTAIN(str, substr) \
85 EXPECT_PRED_FORMAT2(CmpHelperSTRNC, str, substr)
89 // Test class for RLZ tracker. Makes some member functions public and
90 // overrides others to make it easier to test.
91 class TestRLZTracker
: public RLZTracker
{
93 using RLZTracker::InitRlzDelayed
;
94 using RLZTracker::DelayedInit
;
95 using RLZTracker::Observe
;
97 TestRLZTracker() : assume_not_ui_thread_(true) {
101 ~TestRLZTracker() override
{
105 bool was_ping_sent_for_brand(const std::string
& brand
) const {
106 return pinged_brands_
.count(brand
) > 0;
109 void set_assume_not_ui_thread(bool assume_not_ui_thread
) {
110 assume_not_ui_thread_
= assume_not_ui_thread
;
114 void ScheduleDelayedInit(base::TimeDelta delay
) override
{
115 // If the delay is 0, invoke the delayed init now. Otherwise,
116 // don't schedule anything, it will be manually called during tests.
117 if (delay
== base::TimeDelta())
121 void ScheduleFinancialPing() override
{
125 bool ScheduleRecordProductEvent(rlz_lib::Product product
,
126 rlz_lib::AccessPoint point
,
127 rlz_lib::Event event_id
) override
{
128 return !assume_not_ui_thread_
;
131 bool ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point
) override
{
132 return !assume_not_ui_thread_
;
135 bool ScheduleRecordFirstSearch(rlz_lib::AccessPoint point
) override
{
136 return !assume_not_ui_thread_
;
139 #if defined(OS_CHROMEOS)
140 bool ScheduleClearRlzState() override
{
141 return !assume_not_ui_thread_
;
145 bool SendFinancialPing(const std::string
& brand
,
146 const base::string16
& lang
,
147 const base::string16
& referral
) override
{
148 // Don't ping the server during tests, just pretend as if we did.
149 EXPECT_FALSE(brand
.empty());
150 pinged_brands_
.insert(brand
);
152 // Set new access points RLZ string, like the actual server ping would have
154 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(),
155 kNewOmniboxRlzString
);
156 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeHomePage(),
157 kNewHomepageRlzString
);
158 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeAppList(),
159 kNewAppListRlzString
);
163 std::set
<std::string
> pinged_brands_
;
164 bool assume_not_ui_thread_
;
166 DISALLOW_COPY_AND_ASSIGN(TestRLZTracker
);
169 class RlzLibTest
: public ChromeRenderViewHostTestHarness
{
171 void SetUp() override
;
172 void TearDown() override
;
174 void SetMainBrand(const char* brand
);
175 void SetReactivationBrand(const char* brand
);
177 void SetRegistryBrandValue(const wchar_t* name
, const char* brand
);
180 void SimulateOmniboxUsage();
181 void SimulateHomepageUsage();
182 void SimulateAppListUsage();
183 void InvokeDelayedInit();
185 void ExpectEventRecorded(const char* event_name
, bool expected
);
186 void ExpectRlzPingSent(bool expected
);
187 void ExpectReactivationRlzPingSent(bool expected
);
189 TestRLZTracker tracker_
;
190 RlzLibTestNoMachineStateHelper m_rlz_test_helper_
;
191 #if defined(OS_POSIX)
192 scoped_ptr
<google_brand::BrandForTesting
> brand_override_
;
196 void RlzLibTest::SetUp() {
197 ChromeRenderViewHostTestHarness::SetUp();
198 m_rlz_test_helper_
.SetUp();
200 // Make sure a non-organic brand code is set in the registry or the RLZTracker
201 // is pretty much a no-op.
202 SetMainBrand("TEST");
203 SetReactivationBrand("");
206 void RlzLibTest::TearDown() {
207 ChromeRenderViewHostTestHarness::TearDown();
208 m_rlz_test_helper_
.TearDown();
211 void RlzLibTest::SetMainBrand(const char* brand
) {
213 SetRegistryBrandValue(google_update::kRegRLZBrandField
, brand
);
214 #elif defined(OS_POSIX)
215 brand_override_
.reset(new google_brand::BrandForTesting(brand
));
217 std::string check_brand
;
218 google_brand::GetBrand(&check_brand
);
219 EXPECT_EQ(brand
, check_brand
);
222 void RlzLibTest::SetReactivationBrand(const char* brand
) {
223 // TODO(thakis): Reactivation doesn't exist on Mac yet.
225 SetRegistryBrandValue(google_update::kRegRLZReactivationBrandField
, brand
);
226 std::string check_brand
;
227 google_brand::GetReactivationBrand(&check_brand
);
228 EXPECT_EQ(brand
, check_brand
);
233 void RlzLibTest::SetRegistryBrandValue(const wchar_t* name
,
235 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
236 base::string16 reg_path
= dist
->GetStateKey();
237 RegKey
key(HKEY_CURRENT_USER
, reg_path
.c_str(), KEY_SET_VALUE
);
239 LONG result
= key
.DeleteValue(name
);
240 ASSERT_TRUE(ERROR_SUCCESS
== result
|| ERROR_FILE_NOT_FOUND
== result
);
242 base::string16 brand16
= base::ASCIIToUTF16(brand
);
243 ASSERT_EQ(ERROR_SUCCESS
, key
.WriteValue(name
, brand16
.c_str()));
248 void RlzLibTest::SimulateOmniboxUsage() {
249 // Create a dummy OmniboxLog object. The 'is_popup_open' field needs to be
250 // true to trigger record of the first search. All other fields are passed in
251 // with empty or invalid values.
252 AutocompleteResult empty_result
;
253 OmniboxLog
dummy(base::string16(), false, metrics::OmniboxInputType::INVALID
,
255 metrics::OmniboxEventProto::INVALID_SPEC
,
256 base::TimeDelta::FromSeconds(0), 0,
257 base::TimeDelta::FromSeconds(0),
258 AutocompleteResult());
260 tracker_
.Observe(chrome::NOTIFICATION_OMNIBOX_OPENED_URL
,
261 content::NotificationService::AllSources(),
262 content::Details
<OmniboxLog
>(&dummy
));
265 void RlzLibTest::SimulateHomepageUsage() {
266 GURL home_url
= GURL("https://www.google.com/");
267 GURL search_url
= GURL("https://www.google.com/#q=search");
269 content::RenderFrameHostTester
* rfht
=
270 content::RenderFrameHostTester::For(main_rfh());
272 // Simulate a navigation to homepage first.
273 rfht
->SendNavigateWithTransition(0, home_url
, ui::PAGE_TRANSITION_HOME_PAGE
);
274 // Then simulate a search from homepage.
275 rfht
->SendNavigateWithTransition(1, search_url
, ui::PAGE_TRANSITION_LINK
);
278 void RlzLibTest::SimulateAppListUsage() {
279 RLZTracker::RecordAppListSearch();
282 void RlzLibTest::InvokeDelayedInit() {
283 tracker_
.DelayedInit();
286 void RlzLibTest::ExpectEventRecorded(const char* event_name
, bool expected
) {
287 char cgi
[rlz_lib::kMaxCgiLength
];
288 GetProductEventsAsCgi(rlz_lib::CHROME
, cgi
, arraysize(cgi
));
290 EXPECT_STR_CONTAINS(cgi
, event_name
);
292 EXPECT_STR_NOT_CONTAIN(cgi
, event_name
);
296 void RlzLibTest::ExpectRlzPingSent(bool expected
) {
298 google_brand::GetBrand(&brand
);
299 EXPECT_EQ(expected
, tracker_
.was_ping_sent_for_brand(brand
.c_str()));
302 void RlzLibTest::ExpectReactivationRlzPingSent(bool expected
) {
304 google_brand::GetReactivationBrand(&brand
);
305 EXPECT_EQ(expected
, tracker_
.was_ping_sent_for_brand(brand
.c_str()));
308 // The events that affect the different RLZ scenarios are the following:
310 // A: the user starts chrome for the first time
311 // B: the user stops chrome
312 // C: the user start a subsequent time
313 // D: the user stops chrome again
314 // I: the RLZTracker::DelayedInit() method is invoked
315 // X: the user performs a search using the omnibox
316 // Y: the user performs a search using the home page
317 // Z: the user performs a search using the app list
319 // The events A to D happen in chronological order, but the other events
320 // may happen at any point between A-B or C-D, in no particular order.
322 // The visible results of the scenarios on Win are:
324 // C1I event is recorded
325 // C2I event is recorded
326 // C7I event is recorded
327 // C1F event is recorded
328 // C2F event is recorded
329 // C7F event is recorded
330 // C1S event is recorded
331 // C2S event is recorded
332 // C7S event is recorded
335 // On Mac, C5 / C6 / C8 are sent instead of C1 / C2 / C7.
336 // On ChromeOS, CA / CB / CC are sent, respectively.
338 // Variations on the above scenarios:
340 // - if the delay specified to InitRlzDelayed() is negative, then the RLZ
341 // ping should be sent out at the time of event X and not wait for I
343 // Also want to test that pre-warming the RLZ string cache works correctly.
346 const char kOmniboxInstall
[] = "C1I";
347 const char kOmniboxSetToGoogle
[] = "C1S";
348 const char kOmniboxFirstSearch
[] = "C1F";
350 const char kHomepageInstall
[] = "C2I";
351 const char kHomepageSetToGoogle
[] = "C2S";
352 const char kHomepageFirstSeach
[] = "C2F";
354 const char kAppListInstall
[] = "C7I";
355 const char kAppListSetToGoogle
[] = "C7S";
356 const char kAppListFirstSearch
[] = "C7F";
357 #elif defined(OS_MACOSX)
358 const char kOmniboxInstall
[] = "C5I";
359 const char kOmniboxSetToGoogle
[] = "C5S";
360 const char kOmniboxFirstSearch
[] = "C5F";
362 const char kHomepageInstall
[] = "C6I";
363 const char kHomepageSetToGoogle
[] = "C6S";
364 const char kHomepageFirstSeach
[] = "C6F";
366 const char kAppListInstall
[] = "C8I";
367 const char kAppListSetToGoogle
[] = "C8S";
368 const char kAppListFirstSearch
[] = "C8F";
369 #elif defined(OS_CHROMEOS)
370 const char kOmniboxInstall
[] = "CAI";
371 const char kOmniboxSetToGoogle
[] = "CAS";
372 const char kOmniboxFirstSearch
[] = "CAF";
374 const char kHomepageInstall
[] = "CBI";
375 const char kHomepageSetToGoogle
[] = "CBS";
376 const char kHomepageFirstSeach
[] = "CBF";
378 const char kAppListInstall
[] = "CCI";
379 const char kAppListSetToGoogle
[] = "CCS";
380 const char kAppListFirstSearch
[] = "CCF";
383 const base::TimeDelta kDelay
= base::TimeDelta::FromMilliseconds(20);
385 TEST_F(RlzLibTest
, RecordProductEvent
) {
386 RLZTracker::RecordProductEvent(rlz_lib::CHROME
, RLZTracker::ChromeOmnibox(),
387 rlz_lib::FIRST_SEARCH
);
389 ExpectEventRecorded(kOmniboxFirstSearch
, true);
392 TEST_F(RlzLibTest
, QuickStopAfterStart
) {
393 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, true);
396 ExpectEventRecorded(kOmniboxInstall
, false);
397 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
398 ExpectEventRecorded(kOmniboxFirstSearch
, false);
401 ExpectEventRecorded(kHomepageInstall
, false);
402 ExpectEventRecorded(kHomepageSetToGoogle
, false);
403 ExpectEventRecorded(kHomepageFirstSeach
, false);
406 ExpectEventRecorded(kAppListInstall
, false);
407 ExpectEventRecorded(kAppListSetToGoogle
, false);
408 ExpectEventRecorded(kAppListFirstSearch
, false);
410 ExpectRlzPingSent(false);
413 TEST_F(RlzLibTest
, DelayedInitOnly
) {
414 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
418 ExpectEventRecorded(kOmniboxInstall
, true);
419 ExpectEventRecorded(kOmniboxSetToGoogle
, true);
420 ExpectEventRecorded(kOmniboxFirstSearch
, false);
423 ExpectEventRecorded(kHomepageInstall
, true);
424 ExpectEventRecorded(kHomepageSetToGoogle
, true);
425 ExpectEventRecorded(kHomepageFirstSeach
, false);
428 ExpectEventRecorded(kAppListInstall
, true);
429 ExpectEventRecorded(kAppListSetToGoogle
, true);
430 ExpectEventRecorded(kAppListFirstSearch
, false);
432 ExpectRlzPingSent(true);
435 TEST_F(RlzLibTest
, DelayedInitOnlyGoogleAsStartup
) {
436 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, false, false, true);
440 ExpectEventRecorded(kOmniboxInstall
, true);
441 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
442 ExpectEventRecorded(kOmniboxFirstSearch
, false);
445 ExpectEventRecorded(kHomepageInstall
, true);
446 ExpectEventRecorded(kHomepageSetToGoogle
, true);
447 ExpectEventRecorded(kHomepageFirstSeach
, true);
450 ExpectEventRecorded(kAppListInstall
, true);
451 ExpectEventRecorded(kAppListSetToGoogle
, false);
452 ExpectEventRecorded(kAppListFirstSearch
, false);
454 ExpectRlzPingSent(true);
457 TEST_F(RlzLibTest
, DelayedInitOnlyNoFirstRunNoRlzStrings
) {
458 TestRLZTracker::InitRlzDelayed(false, false, kDelay
, true, true, false);
462 ExpectEventRecorded(kOmniboxInstall
, true);
463 ExpectEventRecorded(kOmniboxSetToGoogle
, true);
464 ExpectEventRecorded(kOmniboxFirstSearch
, false);
467 ExpectEventRecorded(kHomepageInstall
, true);
468 ExpectEventRecorded(kHomepageSetToGoogle
, true);
469 ExpectEventRecorded(kHomepageFirstSeach
, false);
472 ExpectEventRecorded(kAppListInstall
, true);
473 ExpectEventRecorded(kAppListSetToGoogle
, true);
474 ExpectEventRecorded(kAppListFirstSearch
, false);
476 ExpectRlzPingSent(true);
479 TEST_F(RlzLibTest
, DelayedInitOnlyNoFirstRunNoRlzStringsGoogleAsStartup
) {
480 TestRLZTracker::InitRlzDelayed(false, false, kDelay
, false, false, true);
484 ExpectEventRecorded(kOmniboxInstall
, true);
485 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
486 ExpectEventRecorded(kOmniboxFirstSearch
, false);
489 ExpectEventRecorded(kHomepageInstall
, true);
490 ExpectEventRecorded(kHomepageSetToGoogle
, true);
491 ExpectEventRecorded(kHomepageFirstSeach
, true);
494 ExpectEventRecorded(kAppListInstall
, true);
495 ExpectEventRecorded(kAppListSetToGoogle
, false);
496 ExpectEventRecorded(kAppListFirstSearch
, false);
498 ExpectRlzPingSent(true);
501 TEST_F(RlzLibTest
, DelayedInitOnlyNoFirstRun
) {
502 // Set some dummy RLZ strings to simulate that we already ran before and
503 // performed a successful ping to the RLZ server.
504 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString
);
505 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeHomePage(), kHomepageRlzString
);
506 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeAppList(), kAppListRlzString
);
508 TestRLZTracker::InitRlzDelayed(false, false, kDelay
, true, true, true);
512 ExpectEventRecorded(kOmniboxInstall
, true);
513 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
514 ExpectEventRecorded(kOmniboxFirstSearch
, false);
517 ExpectEventRecorded(kHomepageInstall
, true);
518 ExpectEventRecorded(kHomepageSetToGoogle
, false);
519 ExpectEventRecorded(kHomepageFirstSeach
, true);
522 ExpectEventRecorded(kAppListInstall
, true);
523 ExpectEventRecorded(kAppListSetToGoogle
, false);
524 ExpectEventRecorded(kAppListFirstSearch
, false);
526 ExpectRlzPingSent(true);
529 TEST_F(RlzLibTest
, DelayedInitOnlyNoGoogleDefaultSearchOrHomepageOrStartup
) {
530 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, false, false, false);
534 ExpectEventRecorded(kOmniboxInstall
, true);
535 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
536 ExpectEventRecorded(kOmniboxFirstSearch
, false);
539 ExpectEventRecorded(kHomepageInstall
, true);
540 ExpectEventRecorded(kHomepageSetToGoogle
, false);
541 ExpectEventRecorded(kHomepageFirstSeach
, false);
544 ExpectEventRecorded(kAppListInstall
, true);
545 ExpectEventRecorded(kAppListSetToGoogle
, false);
546 ExpectEventRecorded(kAppListFirstSearch
, false);
548 ExpectRlzPingSent(true);
551 TEST_F(RlzLibTest
, OmniboxUsageOnly
) {
552 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
553 SimulateOmniboxUsage();
556 ExpectEventRecorded(kOmniboxInstall
, false);
557 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
558 ExpectEventRecorded(kOmniboxFirstSearch
, true);
561 ExpectEventRecorded(kHomepageInstall
, false);
562 ExpectEventRecorded(kHomepageSetToGoogle
, false);
563 ExpectEventRecorded(kHomepageFirstSeach
, false);
566 ExpectEventRecorded(kAppListInstall
, false);
567 ExpectEventRecorded(kAppListSetToGoogle
, false);
568 ExpectEventRecorded(kAppListFirstSearch
, false);
570 ExpectRlzPingSent(false);
573 TEST_F(RlzLibTest
, HomepageUsageOnly
) {
574 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
575 SimulateHomepageUsage();
578 ExpectEventRecorded(kOmniboxInstall
, false);
579 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
580 ExpectEventRecorded(kOmniboxFirstSearch
, false);
583 ExpectEventRecorded(kHomepageInstall
, false);
584 ExpectEventRecorded(kHomepageSetToGoogle
, false);
585 ExpectEventRecorded(kHomepageFirstSeach
, true);
588 ExpectEventRecorded(kAppListInstall
, false);
589 ExpectEventRecorded(kAppListSetToGoogle
, false);
590 ExpectEventRecorded(kAppListFirstSearch
, false);
592 ExpectRlzPingSent(false);
595 TEST_F(RlzLibTest
, AppListUsageOnly
) {
596 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
597 SimulateAppListUsage();
600 ExpectEventRecorded(kOmniboxInstall
, false);
601 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
602 ExpectEventRecorded(kOmniboxFirstSearch
, false);
605 ExpectEventRecorded(kHomepageInstall
, false);
606 ExpectEventRecorded(kHomepageSetToGoogle
, false);
607 ExpectEventRecorded(kHomepageFirstSeach
, false);
610 ExpectEventRecorded(kAppListInstall
, false);
611 ExpectEventRecorded(kAppListSetToGoogle
, false);
612 ExpectEventRecorded(kAppListFirstSearch
, true);
614 ExpectRlzPingSent(false);
617 TEST_F(RlzLibTest
, UsageBeforeDelayedInit
) {
618 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
619 SimulateOmniboxUsage();
620 SimulateHomepageUsage();
621 SimulateAppListUsage();
625 ExpectEventRecorded(kOmniboxInstall
, true);
626 ExpectEventRecorded(kOmniboxSetToGoogle
, true);
627 ExpectEventRecorded(kOmniboxFirstSearch
, true);
630 ExpectEventRecorded(kHomepageInstall
, true);
631 ExpectEventRecorded(kHomepageSetToGoogle
, true);
632 ExpectEventRecorded(kHomepageFirstSeach
, true);
635 ExpectEventRecorded(kAppListInstall
, true);
636 ExpectEventRecorded(kAppListSetToGoogle
, true);
637 ExpectEventRecorded(kAppListFirstSearch
, true);
639 ExpectRlzPingSent(true);
642 TEST_F(RlzLibTest
, UsageAfterDelayedInit
) {
643 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
645 SimulateOmniboxUsage();
646 SimulateHomepageUsage();
647 SimulateAppListUsage();
650 ExpectEventRecorded(kOmniboxInstall
, true);
651 ExpectEventRecorded(kOmniboxSetToGoogle
, true);
652 ExpectEventRecorded(kOmniboxFirstSearch
, true);
655 ExpectEventRecorded(kHomepageInstall
, true);
656 ExpectEventRecorded(kHomepageSetToGoogle
, true);
657 ExpectEventRecorded(kHomepageFirstSeach
, true);
660 ExpectEventRecorded(kAppListInstall
, true);
661 ExpectEventRecorded(kAppListSetToGoogle
, true);
662 ExpectEventRecorded(kAppListFirstSearch
, true);
664 ExpectRlzPingSent(true);
667 TEST_F(RlzLibTest
, OmniboxUsageSendsPingWhenSendPingImmediately
) {
668 TestRLZTracker::InitRlzDelayed(true, true, kDelay
, true, true, false);
669 SimulateOmniboxUsage();
672 ExpectEventRecorded(kOmniboxInstall
, true);
673 ExpectEventRecorded(kOmniboxSetToGoogle
, true);
674 ExpectEventRecorded(kOmniboxFirstSearch
, true);
677 ExpectEventRecorded(kHomepageInstall
, true);
678 ExpectEventRecorded(kHomepageSetToGoogle
, true);
679 ExpectEventRecorded(kHomepageFirstSeach
, false);
682 ExpectEventRecorded(kAppListInstall
, true);
683 ExpectEventRecorded(kAppListSetToGoogle
, true);
684 ExpectEventRecorded(kAppListFirstSearch
, false);
686 ExpectRlzPingSent(true);
689 TEST_F(RlzLibTest
, HomepageUsageDoesNotSendPingWhenSendPingImmediately
) {
690 TestRLZTracker::InitRlzDelayed(true, true, kDelay
, true, true, false);
691 SimulateHomepageUsage();
694 ExpectEventRecorded(kOmniboxInstall
, false);
695 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
696 ExpectEventRecorded(kOmniboxFirstSearch
, false);
699 ExpectEventRecorded(kHomepageInstall
, false);
700 ExpectEventRecorded(kHomepageSetToGoogle
, false);
701 ExpectEventRecorded(kHomepageFirstSeach
, true);
704 ExpectEventRecorded(kAppListInstall
, false);
705 ExpectEventRecorded(kAppListSetToGoogle
, false);
706 ExpectEventRecorded(kAppListFirstSearch
, false);
708 ExpectRlzPingSent(false);
711 TEST_F(RlzLibTest
, StartupUsageDoesNotSendPingWhenSendPingImmediately
) {
712 TestRLZTracker::InitRlzDelayed(true, true, kDelay
, true, false, true);
713 SimulateHomepageUsage();
716 ExpectEventRecorded(kOmniboxInstall
, false);
717 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
718 ExpectEventRecorded(kOmniboxFirstSearch
, false);
721 ExpectEventRecorded(kHomepageInstall
, false);
722 ExpectEventRecorded(kHomepageSetToGoogle
, false);
723 ExpectEventRecorded(kHomepageFirstSeach
, true);
726 ExpectEventRecorded(kAppListInstall
, false);
727 ExpectEventRecorded(kAppListSetToGoogle
, false);
728 ExpectEventRecorded(kAppListFirstSearch
, false);
730 ExpectRlzPingSent(false);
733 TEST_F(RlzLibTest
, AppListUsageDoesNotSendPingWhenSendPingImmediately
) {
734 TestRLZTracker::InitRlzDelayed(true, true, kDelay
, true, false, false);
735 SimulateAppListUsage();
738 ExpectEventRecorded(kOmniboxInstall
, false);
739 ExpectEventRecorded(kOmniboxSetToGoogle
, false);
740 ExpectEventRecorded(kOmniboxFirstSearch
, false);
743 ExpectEventRecorded(kHomepageInstall
, false);
744 ExpectEventRecorded(kHomepageSetToGoogle
, false);
745 ExpectEventRecorded(kHomepageFirstSeach
, false);
748 ExpectEventRecorded(kAppListInstall
, false);
749 ExpectEventRecorded(kAppListSetToGoogle
, false);
750 ExpectEventRecorded(kAppListFirstSearch
, true);
752 ExpectRlzPingSent(false);
755 TEST_F(RlzLibTest
, GetAccessPointRlzOnIoThread
) {
756 // Set dummy RLZ string.
757 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString
);
761 tracker_
.set_assume_not_ui_thread(true);
762 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz
));
763 EXPECT_STREQ(kOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
766 TEST_F(RlzLibTest
, GetAccessPointRlzNotOnIoThread
) {
767 // Set dummy RLZ string.
768 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString
);
772 tracker_
.set_assume_not_ui_thread(false);
774 RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz
));
777 TEST_F(RlzLibTest
, GetAccessPointRlzIsCached
) {
778 // Set dummy RLZ string.
779 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString
);
783 tracker_
.set_assume_not_ui_thread(false);
785 RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz
));
787 tracker_
.set_assume_not_ui_thread(true);
788 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz
));
789 EXPECT_STREQ(kOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
791 tracker_
.set_assume_not_ui_thread(false);
792 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz
));
793 EXPECT_STREQ(kOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
796 TEST_F(RlzLibTest
, PingUpdatesRlzCache
) {
797 // Set dummy RLZ string.
798 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString
);
799 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeHomePage(), kHomepageRlzString
);
800 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeAppList(), kAppListRlzString
);
805 tracker_
.set_assume_not_ui_thread(true);
807 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz
));
808 EXPECT_STREQ(kOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
809 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(
810 RLZTracker::ChromeHomePage(), &rlz
));
811 EXPECT_STREQ(kHomepageRlzString
, base::UTF16ToUTF8(rlz
).c_str());
812 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeAppList(), &rlz
));
813 EXPECT_STREQ(kAppListRlzString
, base::UTF16ToUTF8(rlz
).c_str());
815 // Make sure cache is valid.
816 tracker_
.set_assume_not_ui_thread(false);
818 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz
));
819 EXPECT_STREQ(kOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
820 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(
821 RLZTracker::ChromeHomePage(), &rlz
));
822 EXPECT_STREQ(kHomepageRlzString
, base::UTF16ToUTF8(rlz
).c_str());
823 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeAppList(), &rlz
));
824 EXPECT_STREQ(kAppListRlzString
, base::UTF16ToUTF8(rlz
).c_str());
827 tracker_
.set_assume_not_ui_thread(true);
828 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
830 ExpectRlzPingSent(true);
832 // Make sure cache is now updated.
833 tracker_
.set_assume_not_ui_thread(false);
835 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz
));
836 EXPECT_STREQ(kNewOmniboxRlzString
, base::UTF16ToUTF8(rlz
).c_str());
837 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(
838 RLZTracker::ChromeHomePage(), &rlz
));
839 EXPECT_STREQ(kNewHomepageRlzString
, base::UTF16ToUTF8(rlz
).c_str());
840 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeAppList(), &rlz
));
841 EXPECT_STREQ(kNewAppListRlzString
, base::UTF16ToUTF8(rlz
).c_str());
844 TEST_F(RlzLibTest
, ObserveHandlesBadArgs
) {
845 scoped_ptr
<LoadCommittedDetails
> details(new LoadCommittedDetails());
846 details
->entry
= NavigationEntry::Create();
847 details
->entry
->SetPageID(0);
848 details
->entry
->SetTransitionType(ui::PAGE_TRANSITION_LINK
);
850 tracker_
.Observe(content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
851 content::NotificationService::AllSources(),
852 content::Details
<NavigationEntry
>(NULL
));
853 tracker_
.Observe(content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
854 content::NotificationService::AllSources(),
855 content::Details
<LoadCommittedDetails
>(details
.get()));
858 // TODO(thakis): Reactivation doesn't exist on Mac yet.
860 TEST_F(RlzLibTest
, ReactivationNonOrganicNonOrganic
) {
861 SetReactivationBrand("REAC");
863 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
866 ExpectRlzPingSent(true);
867 ExpectReactivationRlzPingSent(true);
870 TEST_F(RlzLibTest
, ReactivationOrganicNonOrganic
) {
871 SetMainBrand("GGLS");
872 SetReactivationBrand("REAC");
874 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
877 ExpectRlzPingSent(false);
878 ExpectReactivationRlzPingSent(true);
881 TEST_F(RlzLibTest
, ReactivationNonOrganicOrganic
) {
882 SetMainBrand("TEST");
883 SetReactivationBrand("GGLS");
885 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
888 ExpectRlzPingSent(true);
889 ExpectReactivationRlzPingSent(false);
892 TEST_F(RlzLibTest
, ReactivationOrganicOrganic
) {
893 SetMainBrand("GGLS");
894 SetReactivationBrand("GGRS");
896 TestRLZTracker::InitRlzDelayed(true, false, kDelay
, true, true, false);
899 ExpectRlzPingSent(false);
900 ExpectReactivationRlzPingSent(false);
902 #endif // defined(OS_WIN)
904 #if defined(OS_CHROMEOS)
905 TEST_F(RlzLibTest
, ClearRlzState
) {
906 RLZTracker::RecordProductEvent(rlz_lib::CHROME
, RLZTracker::ChromeOmnibox(),
907 rlz_lib::FIRST_SEARCH
);
909 ExpectEventRecorded(kOmniboxFirstSearch
, true);
911 RLZTracker::ClearRlzState();
913 ExpectEventRecorded(kOmniboxFirstSearch
, false);
915 #endif // defined(OS_CHROMEOS)