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