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