rlz_unittest.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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.
4
5#include "chrome/browser/rlz/rlz.h"
6
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/autocomplete/autocomplete_input.h"
12#include "chrome/browser/chrome_notification_types.h"
13#include "chrome/browser/google/google_brand.h"
14#include "chrome/browser/omnibox/omnibox_log.h"
15#include "chrome/browser/profiles/profile.h"
16#include "chrome/installer/util/browser_distribution.h"
17#include "chrome/installer/util/google_update_constants.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"
24
25#if defined(OS_WIN)
26#include "base/win/registry.h"
27#endif
28
29using content::NavigationEntry;
30using testing::AssertionResult;
31using testing::AssertionSuccess;
32using testing::AssertionFailure;
33
34#if defined(OS_WIN)
35using base::win::RegKey;
36#endif
37
38namespace {
39
40// Dummy RLZ string for the access points.
41const char kOmniboxRlzString[] = "test_omnibox";
42const char kHomepageRlzString[] = "test_homepage";
43const char kAppListRlzString[] = "test_applist";
44const char kNewOmniboxRlzString[] = "new_omnibox";
45const char kNewHomepageRlzString[] = "new_homepage";
46const char kNewAppListRlzString[] = "new_applist";
47
48// Some helper macros to test it a string contains/does not contain a substring.
49
50AssertionResult CmpHelperSTRC(const char* str_expression,
51                              const char* substr_expression,
52                              const char* str,
53                              const char* substr) {
54  if (NULL != strstr(str, substr)) {
55    return AssertionSuccess();
56  }
57
58  return AssertionFailure() << "Expected: (" << substr_expression << ") in ("
59                            << str_expression << "), actual: '"
60                            << substr << "' not in '" << str << "'";
61}
62
63AssertionResult CmpHelperSTRNC(const char* str_expression,
64                               const char* substr_expression,
65                               const char* str,
66                               const char* substr) {
67  if (NULL == strstr(str, substr)) {
68    return AssertionSuccess();
69  }
70
71  return AssertionFailure() << "Expected: (" << substr_expression
72                            << ") not in (" << str_expression << "), actual: '"
73                            << substr << "' in '" << str << "'";
74}
75
76#define EXPECT_STR_CONTAINS(str, substr) \
77    EXPECT_PRED_FORMAT2(CmpHelperSTRC, str, substr)
78
79#define EXPECT_STR_NOT_CONTAIN(str, substr) \
80    EXPECT_PRED_FORMAT2(CmpHelperSTRNC, str, substr)
81
82}  // namespace
83
84// Test class for RLZ tracker. Makes some member functions public and
85// overrides others to make it easier to test.
86class TestRLZTracker : public RLZTracker {
87 public:
88  using RLZTracker::InitRlzDelayed;
89  using RLZTracker::DelayedInit;
90  using RLZTracker::Observe;
91
92  TestRLZTracker() : assume_not_ui_thread_(true) {
93    set_tracker(this);
94  }
95
96  virtual ~TestRLZTracker() {
97    set_tracker(NULL);
98  }
99
100  bool was_ping_sent_for_brand(const std::string& brand) const {
101    return pinged_brands_.count(brand) > 0;
102  }
103
104  void set_assume_not_ui_thread(bool assume_not_ui_thread) {
105    assume_not_ui_thread_ = assume_not_ui_thread;
106  }
107
108 private:
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())
113      DelayedInit();
114  }
115
116  virtual void ScheduleFinancialPing() OVERRIDE {
117    PingNowImpl();
118  }
119
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_;
124  }
125
126  virtual bool ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point) OVERRIDE {
127    return !assume_not_ui_thread_;
128  }
129
130  virtual bool ScheduleRecordFirstSearch(rlz_lib::AccessPoint point) OVERRIDE {
131    return !assume_not_ui_thread_;
132  }
133
134#if defined(OS_CHROMEOS)
135  virtual bool ScheduleClearRlzState() OVERRIDE {
136    return !assume_not_ui_thread_;
137  }
138#endif
139
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);
146
147    // Set new access points RLZ string, like the actual server ping would have
148    // done.
149    rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(),
150                               kNewOmniboxRlzString);
151    rlz_lib::SetAccessPointRlz(RLZTracker::ChromeHomePage(),
152                               kNewHomepageRlzString);
153    rlz_lib::SetAccessPointRlz(RLZTracker::ChromeAppList(),
154                               kNewAppListRlzString);
155    return true;
156  }
157
158  std::set<std::string> pinged_brands_;
159  bool assume_not_ui_thread_;
160
161  DISALLOW_COPY_AND_ASSIGN(TestRLZTracker);
162};
163
164class RlzLibTest : public RlzLibTestNoMachineState {
165 protected:
166  virtual void SetUp() OVERRIDE;
167
168  void SetMainBrand(const char* brand);
169  void SetReactivationBrand(const char* brand);
170#if defined(OS_WIN)
171  void SetRegistryBrandValue(const wchar_t* name, const char* brand);
172#endif
173
174  void SimulateOmniboxUsage();
175  void SimulateHomepageUsage();
176  void SimulateAppListUsage();
177  void InvokeDelayedInit();
178
179  void ExpectEventRecorded(const char* event_name, bool expected);
180  void ExpectRlzPingSent(bool expected);
181  void ExpectReactivationRlzPingSent(bool expected);
182
183  TestRLZTracker tracker_;
184#if defined(OS_POSIX)
185  scoped_ptr<google_brand::BrandForTesting> brand_override_;
186#endif
187};
188
189void RlzLibTest::SetUp() {
190  RlzLibTestNoMachineState::SetUp();
191
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("");
196}
197
198void RlzLibTest::SetMainBrand(const char* brand) {
199#if defined(OS_WIN)
200  SetRegistryBrandValue(google_update::kRegRLZBrandField, brand);
201#elif defined(OS_POSIX)
202  brand_override_.reset(new google_brand::BrandForTesting(brand));
203#endif
204  std::string check_brand;
205  google_brand::GetBrand(&check_brand);
206  EXPECT_EQ(brand, check_brand);
207}
208
209void RlzLibTest::SetReactivationBrand(const char* brand) {
210  // TODO(thakis): Reactivation doesn't exist on Mac yet.
211#if defined(OS_WIN)
212  SetRegistryBrandValue(google_update::kRegRLZReactivationBrandField, brand);
213  std::string check_brand;
214  google_brand::GetReactivationBrand(&check_brand);
215  EXPECT_EQ(brand, check_brand);
216#endif
217}
218
219#if defined(OS_WIN)
220void RlzLibTest::SetRegistryBrandValue(const wchar_t* name,
221                                       const char* brand) {
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);
225  if (*brand == 0) {
226    LONG result = key.DeleteValue(name);
227    ASSERT_TRUE(ERROR_SUCCESS == result || ERROR_FILE_NOT_FOUND == result);
228  } else {
229    base::string16 brand16 = base::ASCIIToUTF16(brand);
230    ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(name, brand16.c_str()));
231  }
232}
233#endif
234
235void 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, AutocompleteInput::INVALID,
241                   true, 0, false, -1,
242                   AutocompleteInput::INVALID_SPEC,
243                   base::TimeDelta::FromSeconds(0), 0,
244                   base::TimeDelta::FromSeconds(0),
245                   AutocompleteResult());
246
247  tracker_.Observe(chrome::NOTIFICATION_OMNIBOX_OPENED_URL,
248                   content::NotificationService::AllSources(),
249                   content::Details<OmniboxLog>(&dummy));
250}
251
252void RlzLibTest::SimulateHomepageUsage() {
253  scoped_ptr<NavigationEntry> entry(NavigationEntry::Create());
254  entry->SetPageID(0);
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()));
259}
260
261void RlzLibTest::SimulateAppListUsage() {
262  RLZTracker::RecordAppListSearch();
263}
264
265void RlzLibTest::InvokeDelayedInit() {
266  tracker_.DelayedInit();
267}
268
269void RlzLibTest::ExpectEventRecorded(const char* event_name, bool expected) {
270  char cgi[rlz_lib::kMaxCgiLength];
271  GetProductEventsAsCgi(rlz_lib::CHROME, cgi, arraysize(cgi));
272  if (expected) {
273    EXPECT_STR_CONTAINS(cgi, event_name);
274  } else {
275    EXPECT_STR_NOT_CONTAIN(cgi, event_name);
276  }
277}
278
279void RlzLibTest::ExpectRlzPingSent(bool expected) {
280  std::string brand;
281  google_brand::GetBrand(&brand);
282  EXPECT_EQ(expected, tracker_.was_ping_sent_for_brand(brand.c_str()));
283}
284
285void RlzLibTest::ExpectReactivationRlzPingSent(bool expected) {
286  std::string brand;
287  google_brand::GetReactivationBrand(&brand);
288  EXPECT_EQ(expected, tracker_.was_ping_sent_for_brand(brand.c_str()));
289}
290
291// The events that affect the different RLZ scenarios are the following:
292//
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
301//
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.
304//
305// The visible results of the scenarios on Win are:
306//
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
316//  RLZ ping sent
317//
318//  On Mac, C5 / C6 / C8 are sent instead of C1 / C2 / C7.
319//  On ChromeOS, CA / CB / CC are sent, respectively.
320//
321// Variations on the above scenarios:
322//
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
325//
326// Also want to test that pre-warming the RLZ string cache works correctly.
327
328#if defined(OS_WIN)
329const char kOmniboxInstall[] = "C1I";
330const char kOmniboxSetToGoogle[] = "C1S";
331const char kOmniboxFirstSearch[] = "C1F";
332
333const char kHomepageInstall[] = "C2I";
334const char kHomepageSetToGoogle[] = "C2S";
335const char kHomepageFirstSeach[] = "C2F";
336
337const char kAppListInstall[] = "C7I";
338const char kAppListSetToGoogle[] = "C7S";
339const char kAppListFirstSearch[] = "C7F";
340#elif defined(OS_MACOSX)
341const char kOmniboxInstall[] = "C5I";
342const char kOmniboxSetToGoogle[] = "C5S";
343const char kOmniboxFirstSearch[] = "C5F";
344
345const char kHomepageInstall[] = "C6I";
346const char kHomepageSetToGoogle[] = "C6S";
347const char kHomepageFirstSeach[] = "C6F";
348
349const char kAppListInstall[] = "C8I";
350const char kAppListSetToGoogle[] = "C8S";
351const char kAppListFirstSearch[] = "C8F";
352#elif defined(OS_CHROMEOS)
353const char kOmniboxInstall[] = "CAI";
354const char kOmniboxSetToGoogle[] = "CAS";
355const char kOmniboxFirstSearch[] = "CAF";
356
357const char kHomepageInstall[] = "CBI";
358const char kHomepageSetToGoogle[] = "CBS";
359const char kHomepageFirstSeach[] = "CBF";
360
361const char kAppListInstall[] = "CCI";
362const char kAppListSetToGoogle[] = "CCS";
363const char kAppListFirstSearch[] = "CCF";
364#endif
365
366const base::TimeDelta kDelay = base::TimeDelta::FromMilliseconds(20);
367
368TEST_F(RlzLibTest, RecordProductEvent) {
369  RLZTracker::RecordProductEvent(rlz_lib::CHROME, RLZTracker::ChromeOmnibox(),
370                                 rlz_lib::FIRST_SEARCH);
371
372  ExpectEventRecorded(kOmniboxFirstSearch, true);
373}
374
375TEST_F(RlzLibTest, QuickStopAfterStart) {
376  TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, true);
377
378  // Omnibox events.
379  ExpectEventRecorded(kOmniboxInstall, false);
380  ExpectEventRecorded(kOmniboxSetToGoogle, false);
381  ExpectEventRecorded(kOmniboxFirstSearch, false);
382
383  // Home page events.
384  ExpectEventRecorded(kHomepageInstall, false);
385  ExpectEventRecorded(kHomepageSetToGoogle, false);
386  ExpectEventRecorded(kHomepageFirstSeach, false);
387
388  // App list events.
389  ExpectEventRecorded(kAppListInstall, false);
390  ExpectEventRecorded(kAppListSetToGoogle, false);
391  ExpectEventRecorded(kAppListFirstSearch, false);
392
393  ExpectRlzPingSent(false);
394}
395
396TEST_F(RlzLibTest, DelayedInitOnly) {
397  TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
398  InvokeDelayedInit();
399
400  // Omnibox events.
401  ExpectEventRecorded(kOmniboxInstall, true);
402  ExpectEventRecorded(kOmniboxSetToGoogle, true);
403  ExpectEventRecorded(kOmniboxFirstSearch, false);
404
405  // Home page events.
406  ExpectEventRecorded(kHomepageInstall, true);
407  ExpectEventRecorded(kHomepageSetToGoogle, true);
408  ExpectEventRecorded(kHomepageFirstSeach, false);
409
410  // App list events.
411  ExpectEventRecorded(kAppListInstall, true);
412  ExpectEventRecorded(kAppListSetToGoogle, true);
413  ExpectEventRecorded(kAppListFirstSearch, false);
414
415  ExpectRlzPingSent(true);
416}
417
418TEST_F(RlzLibTest, DelayedInitOnlyGoogleAsStartup) {
419  TestRLZTracker::InitRlzDelayed(true, false, kDelay, false, false, true);
420  InvokeDelayedInit();
421
422  // Omnibox events.
423  ExpectEventRecorded(kOmniboxInstall, true);
424  ExpectEventRecorded(kOmniboxSetToGoogle, false);
425  ExpectEventRecorded(kOmniboxFirstSearch, false);
426
427  // Home page events.
428  ExpectEventRecorded(kHomepageInstall, true);
429  ExpectEventRecorded(kHomepageSetToGoogle, true);
430  ExpectEventRecorded(kHomepageFirstSeach, true);
431
432  // App list events.
433  ExpectEventRecorded(kAppListInstall, true);
434  ExpectEventRecorded(kAppListSetToGoogle, false);
435  ExpectEventRecorded(kAppListFirstSearch, false);
436
437  ExpectRlzPingSent(true);
438}
439
440TEST_F(RlzLibTest, DelayedInitOnlyNoFirstRunNoRlzStrings) {
441  TestRLZTracker::InitRlzDelayed(false, false, kDelay, true, true, false);
442  InvokeDelayedInit();
443
444  // Omnibox events.
445  ExpectEventRecorded(kOmniboxInstall, true);
446  ExpectEventRecorded(kOmniboxSetToGoogle, true);
447  ExpectEventRecorded(kOmniboxFirstSearch, false);
448
449  // Home page events.
450  ExpectEventRecorded(kHomepageInstall, true);
451  ExpectEventRecorded(kHomepageSetToGoogle, true);
452  ExpectEventRecorded(kHomepageFirstSeach, false);
453
454  // App list events.
455  ExpectEventRecorded(kAppListInstall, true);
456  ExpectEventRecorded(kAppListSetToGoogle, true);
457  ExpectEventRecorded(kAppListFirstSearch, false);
458
459  ExpectRlzPingSent(true);
460}
461
462TEST_F(RlzLibTest, DelayedInitOnlyNoFirstRunNoRlzStringsGoogleAsStartup) {
463  TestRLZTracker::InitRlzDelayed(false, false, kDelay, false, false, true);
464  InvokeDelayedInit();
465
466  // Omnibox events.
467  ExpectEventRecorded(kOmniboxInstall, true);
468  ExpectEventRecorded(kOmniboxSetToGoogle, false);
469  ExpectEventRecorded(kOmniboxFirstSearch, false);
470
471  // Home page events.
472  ExpectEventRecorded(kHomepageInstall, true);
473  ExpectEventRecorded(kHomepageSetToGoogle, true);
474  ExpectEventRecorded(kHomepageFirstSeach, true);
475
476  // App list events.
477  ExpectEventRecorded(kAppListInstall, true);
478  ExpectEventRecorded(kAppListSetToGoogle, false);
479  ExpectEventRecorded(kAppListFirstSearch, false);
480
481  ExpectRlzPingSent(true);
482}
483
484TEST_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);
490
491  TestRLZTracker::InitRlzDelayed(false, false, kDelay, true, true, true);
492  InvokeDelayedInit();
493
494  // Omnibox events.
495  ExpectEventRecorded(kOmniboxInstall, true);
496  ExpectEventRecorded(kOmniboxSetToGoogle, false);
497  ExpectEventRecorded(kOmniboxFirstSearch, false);
498
499  // Home page events.
500  ExpectEventRecorded(kHomepageInstall, true);
501  ExpectEventRecorded(kHomepageSetToGoogle, false);
502  ExpectEventRecorded(kHomepageFirstSeach, true);
503
504  // App list events.
505  ExpectEventRecorded(kAppListInstall, true);
506  ExpectEventRecorded(kAppListSetToGoogle, false);
507  ExpectEventRecorded(kAppListFirstSearch, false);
508
509  ExpectRlzPingSent(true);
510}
511
512TEST_F(RlzLibTest, DelayedInitOnlyNoGoogleDefaultSearchOrHomepageOrStartup) {
513  TestRLZTracker::InitRlzDelayed(true, false, kDelay, false, false, false);
514  InvokeDelayedInit();
515
516  // Omnibox events.
517  ExpectEventRecorded(kOmniboxInstall, true);
518  ExpectEventRecorded(kOmniboxSetToGoogle, false);
519  ExpectEventRecorded(kOmniboxFirstSearch, false);
520
521  // Home page events.
522  ExpectEventRecorded(kHomepageInstall, true);
523  ExpectEventRecorded(kHomepageSetToGoogle, false);
524  ExpectEventRecorded(kHomepageFirstSeach, false);
525
526  // App list events.
527  ExpectEventRecorded(kAppListInstall, true);
528  ExpectEventRecorded(kAppListSetToGoogle, false);
529  ExpectEventRecorded(kAppListFirstSearch, false);
530
531  ExpectRlzPingSent(true);
532}
533
534TEST_F(RlzLibTest, OmniboxUsageOnly) {
535  TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
536  SimulateOmniboxUsage();
537
538  // Omnibox events.
539  ExpectEventRecorded(kOmniboxInstall, false);
540  ExpectEventRecorded(kOmniboxSetToGoogle, false);
541  ExpectEventRecorded(kOmniboxFirstSearch, true);
542
543  // Home page events.
544  ExpectEventRecorded(kHomepageInstall, false);
545  ExpectEventRecorded(kHomepageSetToGoogle, false);
546  ExpectEventRecorded(kHomepageFirstSeach, false);
547
548  // App list events.
549  ExpectEventRecorded(kAppListInstall, false);
550  ExpectEventRecorded(kAppListSetToGoogle, false);
551  ExpectEventRecorded(kAppListFirstSearch, false);
552
553  ExpectRlzPingSent(false);
554}
555
556TEST_F(RlzLibTest, HomepageUsageOnly) {
557  TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
558  SimulateHomepageUsage();
559
560  // Omnibox events.
561  ExpectEventRecorded(kOmniboxInstall, false);
562  ExpectEventRecorded(kOmniboxSetToGoogle, false);
563  ExpectEventRecorded(kOmniboxFirstSearch, false);
564
565  // Home page events.
566  ExpectEventRecorded(kHomepageInstall, false);
567  ExpectEventRecorded(kHomepageSetToGoogle, false);
568  ExpectEventRecorded(kHomepageFirstSeach, true);
569
570  // App list events.
571  ExpectEventRecorded(kAppListInstall, false);
572  ExpectEventRecorded(kAppListSetToGoogle, false);
573  ExpectEventRecorded(kAppListFirstSearch, false);
574
575  ExpectRlzPingSent(false);
576}
577
578TEST_F(RlzLibTest, AppListUsageOnly) {
579  TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
580  SimulateAppListUsage();
581
582  // Omnibox events.
583  ExpectEventRecorded(kOmniboxInstall, false);
584  ExpectEventRecorded(kOmniboxSetToGoogle, false);
585  ExpectEventRecorded(kOmniboxFirstSearch, false);
586
587  // Home page events.
588  ExpectEventRecorded(kHomepageInstall, false);
589  ExpectEventRecorded(kHomepageSetToGoogle, false);
590  ExpectEventRecorded(kHomepageFirstSeach, false);
591
592  // App list events.
593  ExpectEventRecorded(kAppListInstall, false);
594  ExpectEventRecorded(kAppListSetToGoogle, false);
595  ExpectEventRecorded(kAppListFirstSearch, true);
596
597  ExpectRlzPingSent(false);
598}
599
600TEST_F(RlzLibTest, UsageBeforeDelayedInit) {
601  TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
602  SimulateOmniboxUsage();
603  SimulateHomepageUsage();
604  SimulateAppListUsage();
605  InvokeDelayedInit();
606
607  // Omnibox events.
608  ExpectEventRecorded(kOmniboxInstall, true);
609  ExpectEventRecorded(kOmniboxSetToGoogle, true);
610  ExpectEventRecorded(kOmniboxFirstSearch, true);
611
612  // Home page events.
613  ExpectEventRecorded(kHomepageInstall, true);
614  ExpectEventRecorded(kHomepageSetToGoogle, true);
615  ExpectEventRecorded(kHomepageFirstSeach, true);
616
617  // App list events.
618  ExpectEventRecorded(kAppListInstall, true);
619  ExpectEventRecorded(kAppListSetToGoogle, true);
620  ExpectEventRecorded(kAppListFirstSearch, true);
621
622  ExpectRlzPingSent(true);
623}
624
625TEST_F(RlzLibTest, UsageAfterDelayedInit) {
626  TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
627  InvokeDelayedInit();
628  SimulateOmniboxUsage();
629  SimulateHomepageUsage();
630  SimulateAppListUsage();
631
632  // Omnibox events.
633  ExpectEventRecorded(kOmniboxInstall, true);
634  ExpectEventRecorded(kOmniboxSetToGoogle, true);
635  ExpectEventRecorded(kOmniboxFirstSearch, true);
636
637  // Home page events.
638  ExpectEventRecorded(kHomepageInstall, true);
639  ExpectEventRecorded(kHomepageSetToGoogle, true);
640  ExpectEventRecorded(kHomepageFirstSeach, true);
641
642  // App list events.
643  ExpectEventRecorded(kAppListInstall, true);
644  ExpectEventRecorded(kAppListSetToGoogle, true);
645  ExpectEventRecorded(kAppListFirstSearch, true);
646
647  ExpectRlzPingSent(true);
648}
649
650TEST_F(RlzLibTest, OmniboxUsageSendsPingWhenSendPingImmediately) {
651  TestRLZTracker::InitRlzDelayed(true, true, kDelay, true, true, false);
652  SimulateOmniboxUsage();
653
654  // Omnibox events.
655  ExpectEventRecorded(kOmniboxInstall, true);
656  ExpectEventRecorded(kOmniboxSetToGoogle, true);
657  ExpectEventRecorded(kOmniboxFirstSearch, true);
658
659  // Home page events.
660  ExpectEventRecorded(kHomepageInstall, true);
661  ExpectEventRecorded(kHomepageSetToGoogle, true);
662  ExpectEventRecorded(kHomepageFirstSeach, false);
663
664  // App list events.
665  ExpectEventRecorded(kAppListInstall, true);
666  ExpectEventRecorded(kAppListSetToGoogle, true);
667  ExpectEventRecorded(kAppListFirstSearch, false);
668
669  ExpectRlzPingSent(true);
670}
671
672TEST_F(RlzLibTest, HomepageUsageDoesNotSendPingWhenSendPingImmediately) {
673  TestRLZTracker::InitRlzDelayed(true, true, kDelay, true, true, false);
674  SimulateHomepageUsage();
675
676  // Omnibox events.
677  ExpectEventRecorded(kOmniboxInstall, false);
678  ExpectEventRecorded(kOmniboxSetToGoogle, false);
679  ExpectEventRecorded(kOmniboxFirstSearch, false);
680
681  // Home page events.
682  ExpectEventRecorded(kHomepageInstall, false);
683  ExpectEventRecorded(kHomepageSetToGoogle, false);
684  ExpectEventRecorded(kHomepageFirstSeach, true);
685
686  // App list events.
687  ExpectEventRecorded(kAppListInstall, false);
688  ExpectEventRecorded(kAppListSetToGoogle, false);
689  ExpectEventRecorded(kAppListFirstSearch, false);
690
691  ExpectRlzPingSent(false);
692}
693
694TEST_F(RlzLibTest, StartupUsageDoesNotSendPingWhenSendPingImmediately) {
695  TestRLZTracker::InitRlzDelayed(true, true, kDelay, true, false, true);
696  SimulateHomepageUsage();
697
698  // Omnibox events.
699  ExpectEventRecorded(kOmniboxInstall, false);
700  ExpectEventRecorded(kOmniboxSetToGoogle, false);
701  ExpectEventRecorded(kOmniboxFirstSearch, false);
702
703  // Home page events.
704  ExpectEventRecorded(kHomepageInstall, false);
705  ExpectEventRecorded(kHomepageSetToGoogle, false);
706  ExpectEventRecorded(kHomepageFirstSeach, true);
707
708  // App list events.
709  ExpectEventRecorded(kAppListInstall, false);
710  ExpectEventRecorded(kAppListSetToGoogle, false);
711  ExpectEventRecorded(kAppListFirstSearch, false);
712
713  ExpectRlzPingSent(false);
714}
715
716TEST_F(RlzLibTest, AppListUsageDoesNotSendPingWhenSendPingImmediately) {
717  TestRLZTracker::InitRlzDelayed(true, true, kDelay, true, false, false);
718  SimulateAppListUsage();
719
720  // Omnibox events.
721  ExpectEventRecorded(kOmniboxInstall, false);
722  ExpectEventRecorded(kOmniboxSetToGoogle, false);
723  ExpectEventRecorded(kOmniboxFirstSearch, false);
724
725  // Home page events.
726  ExpectEventRecorded(kHomepageInstall, false);
727  ExpectEventRecorded(kHomepageSetToGoogle, false);
728  ExpectEventRecorded(kHomepageFirstSeach, false);
729
730  // App list events.
731  ExpectEventRecorded(kAppListInstall, false);
732  ExpectEventRecorded(kAppListSetToGoogle, false);
733  ExpectEventRecorded(kAppListFirstSearch, true);
734
735  ExpectRlzPingSent(false);
736}
737
738TEST_F(RlzLibTest, GetAccessPointRlzOnIoThread) {
739  // Set dummy RLZ string.
740  rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString);
741
742  base::string16 rlz;
743
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());
747}
748
749TEST_F(RlzLibTest, GetAccessPointRlzNotOnIoThread) {
750  // Set dummy RLZ string.
751  rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString);
752
753  base::string16 rlz;
754
755  tracker_.set_assume_not_ui_thread(false);
756  EXPECT_FALSE(
757      RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz));
758}
759
760TEST_F(RlzLibTest, GetAccessPointRlzIsCached) {
761  // Set dummy RLZ string.
762  rlz_lib::SetAccessPointRlz(RLZTracker::ChromeOmnibox(), kOmniboxRlzString);
763
764  base::string16 rlz;
765
766  tracker_.set_assume_not_ui_thread(false);
767  EXPECT_FALSE(
768      RLZTracker::GetAccessPointRlz(RLZTracker::ChromeOmnibox(), &rlz));
769
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());
773
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());
777}
778
779TEST_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);
784
785  base::string16 rlz;
786
787  // Prime the cache.
788  tracker_.set_assume_not_ui_thread(true);
789
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());
797
798  // Make sure cache is valid.
799  tracker_.set_assume_not_ui_thread(false);
800
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());
808
809  // Perform ping.
810  tracker_.set_assume_not_ui_thread(true);
811  TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
812  InvokeDelayedInit();
813  ExpectRlzPingSent(true);
814
815  // Make sure cache is now updated.
816  tracker_.set_assume_not_ui_thread(false);
817
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());
825}
826
827TEST_F(RlzLibTest, ObserveHandlesBadArgs) {
828  scoped_ptr<NavigationEntry> entry(NavigationEntry::Create());
829  entry->SetPageID(0);
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()));
837}
838
839// TODO(thakis): Reactivation doesn't exist on Mac yet.
840#if defined(OS_WIN)
841TEST_F(RlzLibTest, ReactivationNonOrganicNonOrganic) {
842  SetReactivationBrand("REAC");
843
844  TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
845  InvokeDelayedInit();
846
847  ExpectRlzPingSent(true);
848  ExpectReactivationRlzPingSent(true);
849}
850
851TEST_F(RlzLibTest, ReactivationOrganicNonOrganic) {
852  SetMainBrand("GGLS");
853  SetReactivationBrand("REAC");
854
855  TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
856  InvokeDelayedInit();
857
858  ExpectRlzPingSent(false);
859  ExpectReactivationRlzPingSent(true);
860}
861
862TEST_F(RlzLibTest, ReactivationNonOrganicOrganic) {
863  SetMainBrand("TEST");
864  SetReactivationBrand("GGLS");
865
866  TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
867  InvokeDelayedInit();
868
869  ExpectRlzPingSent(true);
870  ExpectReactivationRlzPingSent(false);
871}
872
873TEST_F(RlzLibTest, ReactivationOrganicOrganic) {
874  SetMainBrand("GGLS");
875  SetReactivationBrand("GGRS");
876
877  TestRLZTracker::InitRlzDelayed(true, false, kDelay, true, true, false);
878  InvokeDelayedInit();
879
880  ExpectRlzPingSent(false);
881  ExpectReactivationRlzPingSent(false);
882}
883#endif  // defined(OS_WIN)
884
885#if defined(OS_CHROMEOS)
886TEST_F(RlzLibTest, ClearRlzState) {
887  RLZTracker::RecordProductEvent(rlz_lib::CHROME, RLZTracker::ChromeOmnibox(),
888                                 rlz_lib::FIRST_SEARCH);
889
890  ExpectEventRecorded(kOmniboxFirstSearch, true);
891
892  RLZTracker::ClearRlzState();
893
894  ExpectEventRecorded(kOmniboxFirstSearch, false);
895}
896#endif  // defined(OS_CHROMEOS)
897