Remove obsolete entries from .gitignore.
[chromium-blink-merge.git] / components / rlz / rlz_tracker_unittest.cc
blobeb33d891b612ffaefd8741d981f8ecde2455d4cc
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 "components/rlz/rlz_tracker.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/threading/sequenced_worker_pool.h"
13 #include "base/time/time.h"
14 #include "components/rlz/rlz_tracker_delegate.h"
15 #include "net/url_request/url_request_test_util.h"
16 #include "rlz/test/rlz_test_helpers.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 #if defined(OS_IOS)
20 #include "ui/base/device_form_factor.h"
21 #endif
23 using testing::AssertionResult;
24 using testing::AssertionSuccess;
25 using testing::AssertionFailure;
27 namespace rlz {
28 namespace {
30 class TestRLZTrackerDelegate : public RLZTrackerDelegate {
31 public:
32 TestRLZTrackerDelegate()
33 : worker_pool_(new base::SequencedWorkerPool(1, "TestRLZTracker")),
34 request_context_getter_(new net::TestURLRequestContextGetter(
35 base::ThreadTaskRunnerHandle::Get())) {}
37 ~TestRLZTrackerDelegate() override { worker_pool_->Shutdown(); }
39 void set_brand(const char* brand) { brand_override_ = brand; }
41 void set_reactivation_brand(const char* reactivation_brand) {
42 // TODO(thakis): Reactivation doesn't exist on Mac yet.
43 reactivation_brand_override_ = reactivation_brand;
46 void SimulateOmniboxUsage() {
47 using std::swap;
48 base::Closure callback;
49 swap(callback, on_omnibox_search_callback_);
50 if (!callback.is_null())
51 callback.Run();
54 void SimulateHomepageUsage() {
55 using std::swap;
56 base::Closure callback;
57 swap(callback, on_homepage_search_callback_);
58 if (!callback.is_null())
59 callback.Run();
62 // RLZTrackerDelegate implementation.
63 void Cleanup() override {
64 on_omnibox_search_callback_.Reset();
65 on_homepage_search_callback_.Reset();
68 bool IsOnUIThread() override { return true; }
70 base::SequencedWorkerPool* GetBlockingPool() override {
71 return worker_pool_.get();
74 net::URLRequestContextGetter* GetRequestContext() override {
75 return request_context_getter_.get();
78 bool GetBrand(std::string* brand) override {
79 *brand = brand_override_;
80 return true;
83 bool IsBrandOrganic(const std::string& brand) override {
84 return brand.empty() || brand == "GGLS" || brand == "GGRS";
87 bool GetReactivationBrand(std::string* brand) override {
88 *brand = reactivation_brand_override_;
89 return true;
92 bool ShouldEnableZeroDelayForTesting() override { return true; }
94 bool GetLanguage(base::string16* language) override { return true; }
96 bool GetReferral(base::string16* referral) override { return true; }
98 bool ClearReferral() override { return true; }
100 void SetOmniboxSearchCallback(const base::Closure& callback) override {
101 DCHECK(!callback.is_null());
102 on_omnibox_search_callback_ = callback;
105 void SetHomepageSearchCallback(const base::Closure& callback) override {
106 DCHECK(!callback.is_null());
107 on_homepage_search_callback_ = callback;
110 private:
111 scoped_refptr<base::SequencedWorkerPool> worker_pool_;
112 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
114 std::string brand_override_;
115 std::string reactivation_brand_override_;
116 base::Closure on_omnibox_search_callback_;
117 base::Closure on_homepage_search_callback_;
119 DISALLOW_COPY_AND_ASSIGN(TestRLZTrackerDelegate);
122 // Dummy RLZ string for the access points.
123 const char kOmniboxRlzString[] = "test_omnibox";
124 const char kNewOmniboxRlzString[] = "new_omnibox";
125 #if !defined(OS_IOS)
126 const char kHomepageRlzString[] = "test_homepage";
127 const char kNewHomepageRlzString[] = "new_homepage";
128 const char kAppListRlzString[] = "test_applist";
129 const char kNewAppListRlzString[] = "new_applist";
130 #endif // !defined(OS_IOS)
132 // Some helper macros to test it a string contains/does not contain a substring.
134 AssertionResult CmpHelperSTRC(const char* str_expression,
135 const char* substr_expression,
136 const char* str,
137 const char* substr) {
138 if (nullptr != strstr(str, substr)) {
139 return AssertionSuccess();
142 return AssertionFailure() << "Expected: (" << substr_expression << ") in ("
143 << str_expression << "), actual: '"
144 << substr << "' not in '" << str << "'";
147 AssertionResult CmpHelperSTRNC(const char* str_expression,
148 const char* substr_expression,
149 const char* str,
150 const char* substr) {
151 if (nullptr == strstr(str, substr)) {
152 return AssertionSuccess();
155 return AssertionFailure() << "Expected: (" << substr_expression
156 << ") not in (" << str_expression << "), actual: '"
157 << substr << "' in '" << str << "'";
160 #define EXPECT_STR_CONTAINS(str, substr) \
161 EXPECT_PRED_FORMAT2(CmpHelperSTRC, str, substr)
163 #define EXPECT_STR_NOT_CONTAIN(str, substr) \
164 EXPECT_PRED_FORMAT2(CmpHelperSTRNC, str, substr)
166 } // namespace
168 // Test class for RLZ tracker. Makes some member functions public and
169 // overrides others to make it easier to test.
170 class TestRLZTracker : public RLZTracker {
171 public:
172 using RLZTracker::InitRlzDelayed;
173 using RLZTracker::DelayedInit;
175 TestRLZTracker() : assume_not_ui_thread_(true) { set_tracker(this); }
177 ~TestRLZTracker() override { set_tracker(nullptr); }
179 bool was_ping_sent_for_brand(const std::string& brand) const {
180 return pinged_brands_.count(brand) > 0;
183 void set_assume_not_ui_thread(bool assume_not_ui_thread) {
184 assume_not_ui_thread_ = assume_not_ui_thread;
187 private:
188 void ScheduleDelayedInit(base::TimeDelta delay) override {
189 // If the delay is 0, invoke the delayed init now. Otherwise,
190 // don't schedule anything, it will be manually called during tests.
191 if (delay == base::TimeDelta())
192 DelayedInit();
195 void ScheduleFinancialPing() override { PingNowImpl(); }
197 bool ScheduleRecordProductEvent(rlz_lib::Product product,
198 rlz_lib::AccessPoint point,
199 rlz_lib::Event event_id) override {
200 return !assume_not_ui_thread_;
203 bool ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point) override {
204 return !assume_not_ui_thread_;
207 bool ScheduleRecordFirstSearch(rlz_lib::AccessPoint point) override {
208 return !assume_not_ui_thread_;
211 #if defined(OS_CHROMEOS)
212 bool ScheduleClearRlzState() override { return !assume_not_ui_thread_; }
213 #endif
215 bool SendFinancialPing(const std::string& brand,
216 const base::string16& lang,
217 const base::string16& referral) override {
218 // Don't ping the server during tests, just pretend as if we did.
219 EXPECT_FALSE(brand.empty());
220 pinged_brands_.insert(brand);
222 // Set new access points RLZ string, like the actual server ping would have
223 // done.
224 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(),
225 kNewOmniboxRlzString);
226 #if !defined(OS_IOS)
227 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeHomePage(),
228 kNewHomepageRlzString);
229 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeAppList(),
230 kNewAppListRlzString);
231 #endif // !defined(OS_IOS)
232 return true;
235 std::set<std::string> pinged_brands_;
236 bool assume_not_ui_thread_;
238 DISALLOW_COPY_AND_ASSIGN(TestRLZTracker);
241 class RlzLibTest : public testing::Test {
242 protected:
243 void SetUp() override;
244 void TearDown() override;
246 void SetMainBrand(const char* brand);
247 void SetReactivationBrand(const char* brand);
249 void SimulateOmniboxUsage();
250 void SimulateHomepageUsage();
251 void SimulateAppListUsage();
252 void InvokeDelayedInit();
254 void ExpectEventRecorded(const char* event_name, bool expected);
255 void ExpectRlzPingSent(bool expected);
256 void ExpectReactivationRlzPingSent(bool expected);
258 base::MessageLoop message_loop_;
259 TestRLZTrackerDelegate* delegate_;
260 scoped_ptr<TestRLZTracker> tracker_;
261 RlzLibTestNoMachineStateHelper m_rlz_test_helper_;
264 void RlzLibTest::SetUp() {
265 testing::Test::SetUp();
266 m_rlz_test_helper_.SetUp();
268 delegate_ = new TestRLZTrackerDelegate;
269 tracker_.reset(new TestRLZTracker());
270 RLZTracker::SetRlzDelegate(make_scoped_ptr(delegate_));
272 // Make sure a non-organic brand code is set in the registry or the RLZTracker
273 // is pretty much a no-op.
274 SetMainBrand("TEST");
275 SetReactivationBrand("");
278 void RlzLibTest::TearDown() {
279 delegate_ = nullptr;
280 tracker_.reset();
281 testing::Test::TearDown();
282 m_rlz_test_helper_.TearDown();
285 void RlzLibTest::SetMainBrand(const char* brand) {
286 delegate_->set_brand(brand);
289 void RlzLibTest::SetReactivationBrand(const char* brand) {
290 delegate_->set_reactivation_brand(brand);
293 void RlzLibTest::SimulateOmniboxUsage() {
294 delegate_->SimulateOmniboxUsage();
297 void RlzLibTest::SimulateHomepageUsage() {
298 delegate_->SimulateHomepageUsage();
301 void RlzLibTest::SimulateAppListUsage() {
302 #if !defined(OS_IOS)
303 RLZTracker::RecordAppListSearch();
304 #endif // !defined(OS_IOS)
307 void RlzLibTest::InvokeDelayedInit() {
308 tracker_->DelayedInit();
311 void RlzLibTest::ExpectEventRecorded(const char* event_name, bool expected) {
312 char cgi[rlz_lib::kMaxCgiLength];
313 GetProductEventsAsCgi(rlz_lib::CHROME, cgi, arraysize(cgi));
314 if (expected) {
315 EXPECT_STR_CONTAINS(cgi, event_name);
316 } else {
317 EXPECT_STR_NOT_CONTAIN(cgi, event_name);
321 void RlzLibTest::ExpectRlzPingSent(bool expected) {
322 std::string brand;
323 delegate_->GetBrand(&brand);
324 EXPECT_EQ(expected, tracker_->was_ping_sent_for_brand(brand.c_str()));
327 void RlzLibTest::ExpectReactivationRlzPingSent(bool expected) {
328 std::string brand;
329 delegate_->GetReactivationBrand(&brand);
330 EXPECT_EQ(expected, tracker_->was_ping_sent_for_brand(brand.c_str()));
333 // The events that affect the different RLZ scenarios are the following:
335 // A: the user starts chrome for the first time
336 // B: the user stops chrome
337 // C: the user start a subsequent time
338 // D: the user stops chrome again
339 // I: the RLZTracker::DelayedInit() method is invoked
340 // X: the user performs a search using the omnibox
341 // Y: the user performs a search using the home page
342 // Z: the user performs a search using the app list
344 // The events A to D happen in chronological order, but the other events
345 // may happen at any point between A-B or C-D, in no particular order.
347 // The visible results of the scenarios on Win are:
349 // C1I event is recorded
350 // C2I event is recorded
351 // C7I event is recorded
352 // C1F event is recorded
353 // C2F event is recorded
354 // C7F event is recorded
355 // C1S event is recorded
356 // C2S event is recorded
357 // C7S event is recorded
358 // RLZ ping sent
360 // On Mac, C5 / C6 / C8 are sent instead of C1 / C2 / C7.
361 // On ChromeOS, CA / CB / CC are sent, respectively.
363 // On iOS, only the omnibox events are recorded, and the value send depends
364 // on the device form factor (phone or tablet).
366 // Variations on the above scenarios:
368 // - if the delay specified to InitRlzDelayed() is negative, then the RLZ
369 // ping should be sent out at the time of event X and not wait for I
371 // Also want to test that pre-warming the RLZ string cache works correctly.
373 #if defined(OS_WIN)
374 const char kOmniboxInstall[] = "C1I";
375 const char kOmniboxSetToGoogle[] = "C1S";
376 const char kOmniboxFirstSearch[] = "C1F";
378 const char kHomepageInstall[] = "C2I";
379 const char kHomepageSetToGoogle[] = "C2S";
380 const char kHomepageFirstSearch[] = "C2F";
382 const char kAppListInstall[] = "C7I";
383 const char kAppListSetToGoogle[] = "C7S";
384 const char kAppListFirstSearch[] = "C7F";
385 #elif defined(OS_IOS)
386 const char kOmniboxInstallPhone[] = "CDI";
387 const char kOmniboxSetToGooglePhone[] = "CDS";
388 const char kOmniboxFirstSearchPhone[] = "CDF";
390 const char kOmniboxInstallTablet[] = "C9I";
391 const char kOmniboxSetToGoogleTablet[] = "C9S";
392 const char kOmniboxFirstSearchTablet[] = "C9F";
393 #elif defined(OS_MACOSX)
394 const char kOmniboxInstall[] = "C5I";
395 const char kOmniboxSetToGoogle[] = "C5S";
396 const char kOmniboxFirstSearch[] = "C5F";
398 const char kHomepageInstall[] = "C6I";
399 const char kHomepageSetToGoogle[] = "C6S";
400 const char kHomepageFirstSearch[] = "C6F";
402 const char kAppListInstall[] = "C8I";
403 const char kAppListSetToGoogle[] = "C8S";
404 const char kAppListFirstSearch[] = "C8F";
405 #elif defined(OS_CHROMEOS)
406 const char kOmniboxInstall[] = "CAI";
407 const char kOmniboxSetToGoogle[] = "CAS";
408 const char kOmniboxFirstSearch[] = "CAF";
410 const char kHomepageInstall[] = "CBI";
411 const char kHomepageSetToGoogle[] = "CBS";
412 const char kHomepageFirstSearch[] = "CBF";
414 const char kAppListInstall[] = "CCI";
415 const char kAppListSetToGoogle[] = "CCS";
416 const char kAppListFirstSearch[] = "CCF";
417 #endif
419 const char* OmniboxInstall() {
420 #if defined(OS_IOS)
421 return ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET
422 ? kOmniboxInstallTablet
423 : kOmniboxInstallPhone;
424 #else
425 return kOmniboxInstall;
426 #endif
429 const char* OmniboxSetToGoogle() {
430 #if defined(OS_IOS)
431 return ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET
432 ? kOmniboxSetToGoogleTablet
433 : kOmniboxSetToGooglePhone;
434 #else
435 return kOmniboxSetToGoogle;
436 #endif
439 const char* OmniboxFirstSearch() {
440 #if defined(OS_IOS)
441 return ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET
442 ? kOmniboxFirstSearchTablet
443 : kOmniboxFirstSearchPhone;
444 #else
445 return kOmniboxFirstSearch;
446 #endif
449 const base::TimeDelta kDelay = base::TimeDelta::FromMilliseconds(20);
451 TEST_F(RlzLibTest, RecordProductEvent) {
452 RLZTracker::RecordProductEvent(rlz_lib::CHROME, RLZTracker::ChromeOmnibox(),
453 rlz_lib::FIRST_SEARCH);
455 ExpectEventRecorded(OmniboxFirstSearch(), true);
458 TEST_F(RlzLibTest, QuickStopAfterStart) {
459 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, true);
461 // Omnibox events.
462 ExpectEventRecorded(OmniboxInstall(), false);
463 ExpectEventRecorded(OmniboxSetToGoogle(), false);
464 ExpectEventRecorded(OmniboxFirstSearch(), false);
466 #if !defined(OS_IOS)
467 // Home page events.
468 ExpectEventRecorded(kHomepageInstall, false);
469 ExpectEventRecorded(kHomepageSetToGoogle, false);
470 ExpectEventRecorded(kHomepageFirstSearch, false);
472 // App list events.
473 ExpectEventRecorded(kAppListInstall, false);
474 ExpectEventRecorded(kAppListSetToGoogle, false);
475 ExpectEventRecorded(kAppListFirstSearch, false);
476 #endif // !defined(OS_IOS)
478 ExpectRlzPingSent(false);
481 TEST_F(RlzLibTest, DelayedInitOnly) {
482 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
483 InvokeDelayedInit();
485 // Omnibox events.
486 ExpectEventRecorded(OmniboxInstall(), true);
487 ExpectEventRecorded(OmniboxSetToGoogle(), true);
488 ExpectEventRecorded(OmniboxFirstSearch(), false);
490 #if !defined(OS_IOS)
491 // Home page events.
492 ExpectEventRecorded(kHomepageInstall, true);
493 ExpectEventRecorded(kHomepageSetToGoogle, true);
494 ExpectEventRecorded(kHomepageFirstSearch, false);
496 // App list events.
497 ExpectEventRecorded(kAppListInstall, true);
498 ExpectEventRecorded(kAppListSetToGoogle, true);
499 ExpectEventRecorded(kAppListFirstSearch, false);
500 #endif // !defined(OS_IOS)
502 ExpectRlzPingSent(true);
505 TEST_F(RlzLibTest, DelayedInitOnlyGoogleAsStartup) {
506 TestRLZTracker::InitRlzDelayed(true, false, kDelay, false, false, true);
507 InvokeDelayedInit();
509 // Omnibox events.
510 ExpectEventRecorded(OmniboxInstall(), true);
511 ExpectEventRecorded(OmniboxSetToGoogle(), false);
512 ExpectEventRecorded(OmniboxFirstSearch(), false);
514 #if !defined(OS_IOS)
515 // Home page events.
516 ExpectEventRecorded(kHomepageInstall, true);
517 ExpectEventRecorded(kHomepageSetToGoogle, true);
518 ExpectEventRecorded(kHomepageFirstSearch, true);
520 // App list events.
521 ExpectEventRecorded(kAppListInstall, true);
522 ExpectEventRecorded(kAppListSetToGoogle, false);
523 ExpectEventRecorded(kAppListFirstSearch, false);
524 #endif // !defined(OS_IOS)
526 ExpectRlzPingSent(true);
529 TEST_F(RlzLibTest, DelayedInitOnlyNoFirstRunNoRlzStrings) {
530 TestRLZTracker::InitRlzDelayed(false, false, kDelay, true, true, false);
531 InvokeDelayedInit();
533 // Omnibox events.
534 ExpectEventRecorded(OmniboxInstall(), true);
535 ExpectEventRecorded(OmniboxSetToGoogle(), true);
536 ExpectEventRecorded(OmniboxFirstSearch(), false);
538 #if !defined(OS_IOS)
539 // Home page events.
540 ExpectEventRecorded(kHomepageInstall, true);
541 ExpectEventRecorded(kHomepageSetToGoogle, true);
542 ExpectEventRecorded(kHomepageFirstSearch, false);
544 // App list events.
545 ExpectEventRecorded(kAppListInstall, true);
546 ExpectEventRecorded(kAppListSetToGoogle, true);
547 ExpectEventRecorded(kAppListFirstSearch, false);
548 #endif // !defined(OS_IOS)
550 ExpectRlzPingSent(true);
553 TEST_F(RlzLibTest, DelayedInitOnlyNoFirstRunNoRlzStringsGoogleAsStartup) {
554 TestRLZTracker::InitRlzDelayed(false, false, kDelay, false, false, true);
555 InvokeDelayedInit();
557 // Omnibox events.
558 ExpectEventRecorded(OmniboxInstall(), true);
559 ExpectEventRecorded(OmniboxSetToGoogle(), false);
560 ExpectEventRecorded(OmniboxFirstSearch(), false);
562 #if !defined(OS_IOS)
563 // Home page events.
564 ExpectEventRecorded(kHomepageInstall, true);
565 ExpectEventRecorded(kHomepageSetToGoogle, true);
566 ExpectEventRecorded(kHomepageFirstSearch, true);
568 // App list events.
569 ExpectEventRecorded(kAppListInstall, true);
570 ExpectEventRecorded(kAppListSetToGoogle, false);
571 ExpectEventRecorded(kAppListFirstSearch, false);
572 #endif // !defined(OS_IOS)
574 ExpectRlzPingSent(true);
577 TEST_F(RlzLibTest, DelayedInitOnlyNoFirstRun) {
578 // Set some dummy RLZ strings to simulate that we already ran before and
579 // performed a successful ping to the RLZ server.
580 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString);
581 #if !defined(OS_IOS)
582 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeHomePage(), kHomepageRlzString);
583 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeAppList(), kAppListRlzString);
584 #endif // !defined(OS_IOS)
586 TestRLZTracker::InitRlzDelayed(false, false, kDelay, true, true, true);
587 InvokeDelayedInit();
589 // Omnibox events.
590 ExpectEventRecorded(OmniboxInstall(), true);
591 ExpectEventRecorded(OmniboxSetToGoogle(), false);
592 ExpectEventRecorded(OmniboxFirstSearch(), false);
594 #if !defined(OS_IOS)
595 // Home page events.
596 ExpectEventRecorded(kHomepageInstall, true);
597 ExpectEventRecorded(kHomepageSetToGoogle, false);
598 ExpectEventRecorded(kHomepageFirstSearch, true);
600 // App list events.
601 ExpectEventRecorded(kAppListInstall, true);
602 ExpectEventRecorded(kAppListSetToGoogle, false);
603 ExpectEventRecorded(kAppListFirstSearch, false);
604 #endif // !defined(OS_IOS)
606 ExpectRlzPingSent(true);
609 TEST_F(RlzLibTest, DelayedInitOnlyNoGoogleDefaultSearchOrHomepageOrStartup) {
610 TestRLZTracker::InitRlzDelayed(true, false, kDelay, false, false, false);
611 InvokeDelayedInit();
613 // Omnibox events.
614 ExpectEventRecorded(OmniboxInstall(), true);
615 ExpectEventRecorded(OmniboxSetToGoogle(), false);
616 ExpectEventRecorded(OmniboxFirstSearch(), false);
618 #if !defined(OS_IOS)
619 // Home page events.
620 ExpectEventRecorded(kHomepageInstall, true);
621 ExpectEventRecorded(kHomepageSetToGoogle, false);
622 ExpectEventRecorded(kHomepageFirstSearch, false);
624 // App list events.
625 ExpectEventRecorded(kAppListInstall, true);
626 ExpectEventRecorded(kAppListSetToGoogle, false);
627 ExpectEventRecorded(kAppListFirstSearch, false);
628 #endif // !defined(OS_IOS)
630 ExpectRlzPingSent(true);
633 TEST_F(RlzLibTest, OmniboxUsageOnly) {
634 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
635 SimulateOmniboxUsage();
637 // Omnibox events.
638 ExpectEventRecorded(OmniboxInstall(), false);
639 ExpectEventRecorded(OmniboxSetToGoogle(), false);
640 ExpectEventRecorded(OmniboxFirstSearch(), true);
642 #if !defined(OS_IOS)
643 // Home page events.
644 ExpectEventRecorded(kHomepageInstall, false);
645 ExpectEventRecorded(kHomepageSetToGoogle, false);
646 ExpectEventRecorded(kHomepageFirstSearch, false);
648 // App list events.
649 ExpectEventRecorded(kAppListInstall, false);
650 ExpectEventRecorded(kAppListSetToGoogle, false);
651 ExpectEventRecorded(kAppListFirstSearch, false);
652 #endif // !defined(OS_IOS)
654 ExpectRlzPingSent(false);
657 TEST_F(RlzLibTest, HomepageUsageOnly) {
658 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
659 SimulateHomepageUsage();
661 // Omnibox events.
662 ExpectEventRecorded(OmniboxInstall(), false);
663 ExpectEventRecorded(OmniboxSetToGoogle(), false);
664 ExpectEventRecorded(OmniboxFirstSearch(), false);
666 #if !defined(OS_IOS)
667 // Home page events.
668 ExpectEventRecorded(kHomepageInstall, false);
669 ExpectEventRecorded(kHomepageSetToGoogle, false);
670 ExpectEventRecorded(kHomepageFirstSearch, true);
672 // App list events.
673 ExpectEventRecorded(kAppListInstall, false);
674 ExpectEventRecorded(kAppListSetToGoogle, false);
675 ExpectEventRecorded(kAppListFirstSearch, false);
676 #endif // !defined(OS_IOS)
678 ExpectRlzPingSent(false);
681 TEST_F(RlzLibTest, AppListUsageOnly) {
682 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
683 SimulateAppListUsage();
685 // Omnibox events.
686 ExpectEventRecorded(OmniboxInstall(), false);
687 ExpectEventRecorded(OmniboxSetToGoogle(), false);
688 ExpectEventRecorded(OmniboxFirstSearch(), false);
690 #if !defined(OS_IOS)
691 // Home page events.
692 ExpectEventRecorded(kHomepageInstall, false);
693 ExpectEventRecorded(kHomepageSetToGoogle, false);
694 ExpectEventRecorded(kHomepageFirstSearch, false);
696 // App list events.
697 ExpectEventRecorded(kAppListInstall, false);
698 ExpectEventRecorded(kAppListSetToGoogle, false);
699 ExpectEventRecorded(kAppListFirstSearch, true);
700 #endif // !defined(OS_IOS)
702 ExpectRlzPingSent(false);
705 TEST_F(RlzLibTest, UsageBeforeDelayedInit) {
706 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
707 SimulateOmniboxUsage();
708 SimulateHomepageUsage();
709 SimulateAppListUsage();
710 InvokeDelayedInit();
712 // Omnibox events.
713 ExpectEventRecorded(OmniboxInstall(), true);
714 ExpectEventRecorded(OmniboxSetToGoogle(), true);
715 ExpectEventRecorded(OmniboxFirstSearch(), true);
717 #if !defined(OS_IOS)
718 // Home page events.
719 ExpectEventRecorded(kHomepageInstall, true);
720 ExpectEventRecorded(kHomepageSetToGoogle, true);
721 ExpectEventRecorded(kHomepageFirstSearch, true);
723 // App list events.
724 ExpectEventRecorded(kAppListInstall, true);
725 ExpectEventRecorded(kAppListSetToGoogle, true);
726 ExpectEventRecorded(kAppListFirstSearch, true);
727 #endif // !defined(OS_IOS)
729 ExpectRlzPingSent(true);
732 TEST_F(RlzLibTest, UsageAfterDelayedInit) {
733 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
734 InvokeDelayedInit();
735 SimulateOmniboxUsage();
736 SimulateHomepageUsage();
737 SimulateAppListUsage();
739 // Omnibox events.
740 ExpectEventRecorded(OmniboxInstall(), true);
741 ExpectEventRecorded(OmniboxSetToGoogle(), true);
742 ExpectEventRecorded(OmniboxFirstSearch(), true);
744 #if !defined(OS_IOS)
745 // Home page events.
746 ExpectEventRecorded(kHomepageInstall, true);
747 ExpectEventRecorded(kHomepageSetToGoogle, true);
748 ExpectEventRecorded(kHomepageFirstSearch, true);
750 // App list events.
751 ExpectEventRecorded(kAppListInstall, true);
752 ExpectEventRecorded(kAppListSetToGoogle, true);
753 ExpectEventRecorded(kAppListFirstSearch, true);
754 #endif // !defined(OS_IOS)
756 ExpectRlzPingSent(true);
759 TEST_F(RlzLibTest, OmniboxUsageSendsPingWhenSendPingImmediately) {
760 TestRLZTracker::InitRlzDelayed(true, true, kDelay, true, true, false);
761 SimulateOmniboxUsage();
763 // Omnibox events.
764 ExpectEventRecorded(OmniboxInstall(), true);
765 ExpectEventRecorded(OmniboxSetToGoogle(), true);
766 ExpectEventRecorded(OmniboxFirstSearch(), true);
768 #if !defined(OS_IOS)
769 // Home page events.
770 ExpectEventRecorded(kHomepageInstall, true);
771 ExpectEventRecorded(kHomepageSetToGoogle, true);
772 ExpectEventRecorded(kHomepageFirstSearch, false);
774 // App list events.
775 ExpectEventRecorded(kAppListInstall, true);
776 ExpectEventRecorded(kAppListSetToGoogle, true);
777 ExpectEventRecorded(kAppListFirstSearch, false);
778 #endif // !defined(OS_IOS)
780 ExpectRlzPingSent(true);
783 TEST_F(RlzLibTest, HomepageUsageDoesNotSendPingWhenSendPingImmediately) {
784 TestRLZTracker::InitRlzDelayed(true, true, kDelay, true, true, false);
785 SimulateHomepageUsage();
787 // Omnibox events.
788 ExpectEventRecorded(OmniboxInstall(), false);
789 ExpectEventRecorded(OmniboxSetToGoogle(), false);
790 ExpectEventRecorded(OmniboxFirstSearch(), false);
792 #if !defined(OS_IOS)
793 // Home page events.
794 ExpectEventRecorded(kHomepageInstall, false);
795 ExpectEventRecorded(kHomepageSetToGoogle, false);
796 ExpectEventRecorded(kHomepageFirstSearch, true);
798 // App list events.
799 ExpectEventRecorded(kAppListInstall, false);
800 ExpectEventRecorded(kAppListSetToGoogle, false);
801 ExpectEventRecorded(kAppListFirstSearch, false);
802 #endif // !defined(OS_IOS)
804 ExpectRlzPingSent(false);
807 TEST_F(RlzLibTest, StartupUsageDoesNotSendPingWhenSendPingImmediately) {
808 TestRLZTracker::InitRlzDelayed(true, true, kDelay, true, false, true);
809 SimulateHomepageUsage();
811 // Omnibox events.
812 ExpectEventRecorded(OmniboxInstall(), false);
813 ExpectEventRecorded(OmniboxSetToGoogle(), false);
814 ExpectEventRecorded(OmniboxFirstSearch(), false);
816 #if !defined(OS_IOS)
817 // Home page events.
818 ExpectEventRecorded(kHomepageInstall, false);
819 ExpectEventRecorded(kHomepageSetToGoogle, false);
820 ExpectEventRecorded(kHomepageFirstSearch, true);
822 // App list events.
823 ExpectEventRecorded(kAppListInstall, false);
824 ExpectEventRecorded(kAppListSetToGoogle, false);
825 ExpectEventRecorded(kAppListFirstSearch, false);
826 #endif // !defined(OS_IOS)
828 ExpectRlzPingSent(false);
831 TEST_F(RlzLibTest, AppListUsageDoesNotSendPingWhenSendPingImmediately) {
832 TestRLZTracker::InitRlzDelayed(true, true, kDelay, true, false, false);
833 SimulateAppListUsage();
835 // Omnibox events.
836 ExpectEventRecorded(OmniboxInstall(), false);
837 ExpectEventRecorded(OmniboxSetToGoogle(), false);
838 ExpectEventRecorded(OmniboxFirstSearch(), false);
840 #if !defined(OS_IOS)
841 // Home page events.
842 ExpectEventRecorded(kHomepageInstall, false);
843 ExpectEventRecorded(kHomepageSetToGoogle, false);
844 ExpectEventRecorded(kHomepageFirstSearch, false);
846 // App list events.
847 ExpectEventRecorded(kAppListInstall, false);
848 ExpectEventRecorded(kAppListSetToGoogle, false);
849 ExpectEventRecorded(kAppListFirstSearch, true);
850 #endif // !defined(OS_IOS)
852 ExpectRlzPingSent(false);
855 TEST_F(RlzLibTest, GetAccessPointRlzOnIoThread) {
856 // Set dummy RLZ string.
857 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString);
859 base::string16 rlz;
861 tracker_->set_assume_not_ui_thread(true);
862 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz));
863 EXPECT_STREQ(kOmniboxRlzString, base::UTF16ToUTF8(rlz).c_str());
866 TEST_F(RlzLibTest, GetAccessPointRlzNotOnIoThread) {
867 // Set dummy RLZ string.
868 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString);
870 base::string16 rlz;
872 tracker_->set_assume_not_ui_thread(false);
873 EXPECT_FALSE(
874 RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz));
877 TEST_F(RlzLibTest, GetAccessPointRlzIsCached) {
878 // Set dummy RLZ string.
879 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString);
881 base::string16 rlz;
883 tracker_->set_assume_not_ui_thread(false);
884 EXPECT_FALSE(
885 RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz));
887 tracker_->set_assume_not_ui_thread(true);
888 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz));
889 EXPECT_STREQ(kOmniboxRlzString, base::UTF16ToUTF8(rlz).c_str());
891 tracker_->set_assume_not_ui_thread(false);
892 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz));
893 EXPECT_STREQ(kOmniboxRlzString, base::UTF16ToUTF8(rlz).c_str());
896 TEST_F(RlzLibTest, PingUpdatesRlzCache) {
897 // Set dummy RLZ string.
898 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString);
899 #if !defined(OS_IOS)
900 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeHomePage(), kHomepageRlzString);
901 rlz_lib::SetAccessPointRlz(RLZTracker::ChromeAppList(), kAppListRlzString);
902 #endif // !defined(OS_IOS)
904 base::string16 rlz;
906 // Prime the cache.
907 tracker_->set_assume_not_ui_thread(true);
909 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz));
910 EXPECT_STREQ(kOmniboxRlzString, base::UTF16ToUTF8(rlz).c_str());
911 #if !defined(OS_IOS)
912 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(
913 RLZTracker::ChromeHomePage(), &rlz));
914 EXPECT_STREQ(kHomepageRlzString, base::UTF16ToUTF8(rlz).c_str());
915 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeAppList(), &rlz));
916 EXPECT_STREQ(kAppListRlzString, base::UTF16ToUTF8(rlz).c_str());
917 #endif // !defined(OS_IOS)
919 // Make sure cache is valid.
920 tracker_->set_assume_not_ui_thread(false);
922 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz));
923 EXPECT_STREQ(kOmniboxRlzString, base::UTF16ToUTF8(rlz).c_str());
924 #if !defined(OS_IOS)
925 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(
926 RLZTracker::ChromeHomePage(), &rlz));
927 EXPECT_STREQ(kHomepageRlzString, base::UTF16ToUTF8(rlz).c_str());
928 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeAppList(), &rlz));
929 EXPECT_STREQ(kAppListRlzString, base::UTF16ToUTF8(rlz).c_str());
930 #endif // !defined(OS_IOS)
932 // Perform ping.
933 tracker_->set_assume_not_ui_thread(true);
934 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
935 InvokeDelayedInit();
936 ExpectRlzPingSent(true);
938 // Make sure cache is now updated.
939 tracker_->set_assume_not_ui_thread(false);
941 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz));
942 EXPECT_STREQ(kNewOmniboxRlzString, base::UTF16ToUTF8(rlz).c_str());
943 #if !defined(OS_IOS)
944 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(
945 RLZTracker::ChromeHomePage(), &rlz));
946 EXPECT_STREQ(kNewHomepageRlzString, base::UTF16ToUTF8(rlz).c_str());
947 EXPECT_TRUE(RLZTracker::GetAccessPointRlz(RLZTracker::ChromeAppList(), &rlz));
948 EXPECT_STREQ(kNewAppListRlzString, base::UTF16ToUTF8(rlz).c_str());
949 #endif // !defined(OS_IOS)
952 // TODO(thakis): Reactivation doesn't exist on Mac yet.
953 TEST_F(RlzLibTest, ReactivationNonOrganicNonOrganic) {
954 SetReactivationBrand("REAC");
956 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
957 InvokeDelayedInit();
959 ExpectRlzPingSent(true);
960 ExpectReactivationRlzPingSent(true);
963 TEST_F(RlzLibTest, ReactivationOrganicNonOrganic) {
964 SetMainBrand("GGLS");
965 SetReactivationBrand("REAC");
967 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
968 InvokeDelayedInit();
970 ExpectRlzPingSent(false);
971 ExpectReactivationRlzPingSent(true);
974 TEST_F(RlzLibTest, ReactivationNonOrganicOrganic) {
975 SetMainBrand("TEST");
976 SetReactivationBrand("GGLS");
978 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
979 InvokeDelayedInit();
981 ExpectRlzPingSent(true);
982 ExpectReactivationRlzPingSent(false);
985 TEST_F(RlzLibTest, ReactivationOrganicOrganic) {
986 SetMainBrand("GGLS");
987 SetReactivationBrand("GGRS");
989 TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
990 InvokeDelayedInit();
992 ExpectRlzPingSent(false);
993 ExpectReactivationRlzPingSent(false);
996 #if defined(OS_CHROMEOS)
997 TEST_F(RlzLibTest, ClearRlzState) {
998 RLZTracker::RecordProductEvent(rlz_lib::CHROME, RLZTracker::ChromeOmnibox(),
999 rlz_lib::FIRST_SEARCH);
1001 ExpectEventRecorded(OmniboxFirstSearch(), true);
1003 RLZTracker::ClearRlzState();
1005 ExpectEventRecorded(OmniboxFirstSearch(), false);
1007 #endif // defined(OS_CHROMEOS)
1009 } // namespace rlz