summaryrefslogtreecommitdiffstats
path: root/chrome/browser/rlz/rlz_unittest.cc
blob: 96816a4ee96637aaa957c69bb0423aa10e1af66d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/rlz/rlz.h"

#include "base/memory/scoped_ptr.h"
#include "base/stringprintf.h"
#include "base/path_service.h"
#include "base/test/test_reg_util_win.h"
#include "base/utf_string_conversions.h"
#include "base/win/registry.h"
#include "chrome/browser/autocomplete/autocomplete.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/env_vars.h"
#include "chrome/installer/util/browser_distribution.h"
#include "chrome/installer/util/google_update_constants.h"
#include "content/browser/tab_contents/navigation_entry.h"
#include "content/common/notification_details.h"
#include "content/common/notification_service.h"
#include "content/common/notification_source.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::win::RegKey;
using registry_util::RegistryOverrideManager;
using testing::AssertionResult;
using testing::AssertionSuccess;
using testing::AssertionFailure;

namespace {

// Registry path to overridden hive.
const wchar_t kRlzTempHkcu[] = L"rlz_hkcu";
const wchar_t kRlzTempHklm[] = L"rlz_hklm";

// Dummy RLZ string for the access points.
const char kOmniboxRlzString[] = "test_omnibox";
const char kHomepageRlzString[] = "test_homepage";

// Some helper macros to test it a string contains/does not contain a substring.

AssertionResult CmpHelperSTRC(const char* str_expression,
                              const char* substr_expression,
                              const char* str,
                              const char* substr) {
  if (NULL != strstr(str, substr)) {
    return AssertionSuccess();
  }

  return AssertionFailure() << "Expected: (" << substr_expression << ") in ("
                            << str_expression << "), actual: '"
                            << substr << "' not in '" << str << "'";
}

AssertionResult CmpHelperSTRNC(const char* str_expression,
                               const char* substr_expression,
                               const char* str,
                               const char* substr) {
  if (NULL == strstr(str, substr)) {
    return AssertionSuccess();
  }

  return AssertionFailure() << "Expected: (" << substr_expression
                            << ") not in (" << str_expression << "), actual: '"
                            << substr << "' in '" << str << "'";
}

#define EXPECT_STR_CONTAINS(str, substr) \
    EXPECT_PRED_FORMAT2(CmpHelperSTRC, str, substr)

#define EXPECT_STR_NOT_CONTAIN(str, substr) \
    EXPECT_PRED_FORMAT2(CmpHelperSTRNC, str, substr)

}  // namespace

// Test class for RLZ tracker. Makes some member functions public and
// overrides others to make it easier to test.
class TestRLZTracker : public RLZTracker {
 public:
  using RLZTracker::DelayedInit;
  using RLZTracker::Observe;
  using RLZTracker::RLZ_PAGETRANSITION_HOME_PAGE;

  TestRLZTracker() : pingnow_called_(false), assume_io_thread_(false) {
    set_tracker(this);
  }

  virtual ~TestRLZTracker() {
    set_tracker(NULL);
  }

  bool pingnow_called() const {
    return pingnow_called_;
  }

  bool assume_io_thread() const {
    return assume_io_thread_;
  }

  void set_assume_io_thread(bool assume_io_thread) {
    assume_io_thread_ = assume_io_thread;
  }

 private:
  virtual void ScheduleDelayedInit(int delay) OVERRIDE {
    // If the delay is 0, invoke the delayed init now. Otherwise,
    // don't schedule anything, it will be manually called during tests.
    if (delay == 0)
      DelayedInit();
  }

  virtual void ScheduleFinancialPing() OVERRIDE {
    PingNow(this);
  }

  virtual bool ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point) OVERRIDE {
    return !assume_io_thread_;
  }

  virtual bool SendFinancialPing(const std::wstring& brand,
                                 const std::wstring& lang,
                                 const std::wstring& referral,
                                 bool exclude_id) OVERRIDE {
    // Don't ping the server during tests.
    pingnow_called_ = true;
    return true;
  }

  bool pingnow_called_;
  bool assume_io_thread_;

  DISALLOW_COPY_AND_ASSIGN(TestRLZTracker);
};

class RlzLibTest : public testing::Test {
  virtual void SetUp() OVERRIDE;
  virtual void TearDown() OVERRIDE;

 protected:
  void SimulateOmniboxUsage();
  void SimulateHomepageUsage();
  void InvokeDelayedInit();

  void ExpectEventRecorded(const char* event_name, bool expected);
  void ExpectRlzPingSent(bool expected);

  TestRLZTracker tracker_;
  RegistryOverrideManager override_manager_;
};

void RlzLibTest::SetUp() {
  testing::Test::SetUp();

  // Before overriding HKLM for the tests, we need to set it up correctly
  // so that the rlz_lib calls work. This needs to be done before we do the
  // override.

  std::wstring temp_hklm_path = base::StringPrintf(
      L"%ls\\%ls",
      RegistryOverrideManager::kTempTestKeyPath,
      kRlzTempHklm);

  base::win::RegKey hklm;
  ASSERT_EQ(ERROR_SUCCESS, hklm.Create(HKEY_CURRENT_USER,
                                       temp_hklm_path.c_str(),
                                       KEY_READ));

  std::wstring temp_hkcu_path = base::StringPrintf(
      L"%ls\\%ls",
      RegistryOverrideManager::kTempTestKeyPath,
      kRlzTempHkcu);

  base::win::RegKey hkcu;
  ASSERT_EQ(ERROR_SUCCESS, hkcu.Create(HKEY_CURRENT_USER,
                                       temp_hkcu_path.c_str(),
                                       KEY_READ));

  rlz_lib::InitializeTempHivesForTesting(hklm, hkcu);

  // Its important to override HKLM before HKCU because of the registry
  // initialization performed above.
  override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE, kRlzTempHklm);
  override_manager_.OverrideRegistry(HKEY_CURRENT_USER, kRlzTempHkcu);

  // Make sure a non-organic brand code is set in the registry or the RLZTracker
  // is pretty much a no-op.
  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
  std::wstring reg_path = dist->GetStateKey();
  RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_SET_VALUE);
  ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(google_update::kRegRLZBrandField,
                                          L"TEST"));
}

void RlzLibTest::TearDown() {
  testing::Test::TearDown();
}

void RlzLibTest::SimulateOmniboxUsage() {
  tracker_.Observe(chrome::NOTIFICATION_OMNIBOX_OPENED_URL,
                   NotificationService::AllSources(),
                   Details<AutocompleteLog>(NULL));
}

void RlzLibTest::SimulateHomepageUsage() {
  NavigationEntry entry(NULL, 0, GURL(), GURL(), string16(),
                        TestRLZTracker::RLZ_PAGETRANSITION_HOME_PAGE);
  tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING,
                   NotificationService::AllSources(),
                   Details<NavigationEntry>(&entry));
}

void RlzLibTest::InvokeDelayedInit() {
  tracker_.DelayedInit();
}

void RlzLibTest::ExpectEventRecorded(const char* event_name, bool expected) {
  char cgi[rlz_lib::kMaxCgiLength];
  GetProductEventsAsCgi(rlz_lib::CHROME, cgi, arraysize(cgi));
  if (expected) {
    EXPECT_STR_CONTAINS(cgi, event_name);
  } else {
    EXPECT_STR_NOT_CONTAIN(cgi, event_name);
  }
}

void RlzLibTest::ExpectRlzPingSent(bool expected) {
  EXPECT_EQ(expected, tracker_.pingnow_called());
}

TEST_F(RlzLibTest, RecordProductEvent) {
  RLZTracker::RecordProductEvent(rlz_lib::CHROME, rlz_lib::CHROME_OMNIBOX,
                                 rlz_lib::FIRST_SEARCH);

  ExpectEventRecorded("C1F", true);
}

// The events that affect the different RLZ scenarios are the following:
//
//  A: the user starts chrome for the first time
//  B: the user stops chrome
//  C: the user start a subsequent time
//  D: the user stops chrome again
//  I: the RLZTracker::DelayedInit() method is invoked
//  X: the user performs a search using the omnibox
//  Y: the user performs a search using the home page
//
// The events A to D happen in chronological order, but the other events
// may happen at any point between A-B or C-D, in no particular order.
//
// The visible results of the scenarios are:
//
//  C1I event is recorded
//  C2I event is recorded
//  C1F event is recorded
//  C2F event is recorded
//  C1S event is recorded
//  C2S event is recorded
//  RLZ ping sent
//
// Variations on the above scenarios:
//
//  - if the delay specified to InitRlzDelayed() is negative, then the RLZ
//    ping should be sent out at the time of event X and not wait for I
//
// Also want to test that pre-warming the RLZ string cache works correctly.

TEST_F(RlzLibTest, QuickStopAfterStart) {
  RLZTracker::InitRlzDelayed(true, 20, true, true);

  // Omnibox events.
  ExpectEventRecorded("C1I", false);
  ExpectEventRecorded("C1S", false);
  ExpectEventRecorded("C1F", false);

  // Home page events.
  ExpectEventRecorded("C2I", false);
  ExpectEventRecorded("C2S", false);
  ExpectEventRecorded("C2F", false);

  ExpectRlzPingSent(false);
}

TEST_F(RlzLibTest, DelayedInitOnly) {
  RLZTracker::InitRlzDelayed(true, 20, true, true);
  InvokeDelayedInit();

  // Omnibox events.
  ExpectEventRecorded("C1I", true);
  ExpectEventRecorded("C1S", true);
  ExpectEventRecorded("C1F", false);

  // Home page events.
  ExpectEventRecorded("C2I", true);
  ExpectEventRecorded("C2S", true);
  ExpectEventRecorded("C2F", false);

  ExpectRlzPingSent(true);
}

TEST_F(RlzLibTest, DelayedInitOnlyNoFirstRunNoRlzStrings) {
  RLZTracker::InitRlzDelayed(false, 20, true, true);
  InvokeDelayedInit();

  // Omnibox events.
  ExpectEventRecorded("C1I", true);
  ExpectEventRecorded("C1S", true);
  ExpectEventRecorded("C1F", false);

  // Home page events.
  ExpectEventRecorded("C2I", true);
  ExpectEventRecorded("C2S", true);
  ExpectEventRecorded("C2F", false);

  ExpectRlzPingSent(true);
}

TEST_F(RlzLibTest, DelayedInitOnlyNoFirstRun) {
  // Set some dummy RLZ strings to simulate that we already ran before and
  // performed a successful ping to the RLZ server.
  rlz_lib::SetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, kOmniboxRlzString);
  rlz_lib::SetAccessPointRlz(rlz_lib::CHROME_HOME_PAGE, kHomepageRlzString);

  RLZTracker::InitRlzDelayed(false, 20, true, true);
  InvokeDelayedInit();

  // Omnibox events.
  ExpectEventRecorded("C1I", true);
  ExpectEventRecorded("C1S", false);
  ExpectEventRecorded("C1F", false);

  // Home page events.
  ExpectEventRecorded("C2I", true);
  ExpectEventRecorded("C2S", false);
  ExpectEventRecorded("C2F", false);

  ExpectRlzPingSent(true);
}

TEST_F(RlzLibTest, DelayedInitOnlyNoGoogleDefaultSearchOrHomepage) {
  RLZTracker::InitRlzDelayed(true, 20, false, false);
  InvokeDelayedInit();

  // Omnibox events.
  ExpectEventRecorded("C1I", true);
  ExpectEventRecorded("C1S", false);
  ExpectEventRecorded("C1F", false);

  // Home page events.
  ExpectEventRecorded("C2I", true);
  ExpectEventRecorded("C2S", false);
  ExpectEventRecorded("C2F", false);

  ExpectRlzPingSent(true);
}

TEST_F(RlzLibTest, OmniboxUsageOnly) {
  RLZTracker::InitRlzDelayed(true, 20, true, true);
  SimulateOmniboxUsage();

  // Omnibox events.
  ExpectEventRecorded("C1I", false);
  ExpectEventRecorded("C1S", false);
  ExpectEventRecorded("C1F", true);

  // Home page events.
  ExpectEventRecorded("C2I", false);
  ExpectEventRecorded("C2S", false);
  ExpectEventRecorded("C2F", false);

  ExpectRlzPingSent(false);
}

TEST_F(RlzLibTest, HomepageUsageOnly) {
  RLZTracker::InitRlzDelayed(true, 20, true, true);
  SimulateHomepageUsage();

  // Omnibox events.
  ExpectEventRecorded("C1I", false);
  ExpectEventRecorded("C1S", false);
  ExpectEventRecorded("C1F", false);

  // Home page events.
  ExpectEventRecorded("C2I", false);
  ExpectEventRecorded("C2S", false);
  ExpectEventRecorded("C2F", true);

  ExpectRlzPingSent(false);
}

TEST_F(RlzLibTest, UsageBeforeDelayedInit) {
  RLZTracker::InitRlzDelayed(true, 20, true, true);
  SimulateOmniboxUsage();
  SimulateHomepageUsage();
  InvokeDelayedInit();

  // Omnibox events.
  ExpectEventRecorded("C1I", true);
  ExpectEventRecorded("C1S", true);
  ExpectEventRecorded("C1F", true);

  // Home page events.
  ExpectEventRecorded("C2I", true);
  ExpectEventRecorded("C2S", true);
  ExpectEventRecorded("C2F", true);

  ExpectRlzPingSent(true);
}

TEST_F(RlzLibTest, OmniboxUsageAfterDelayedInit) {
  RLZTracker::InitRlzDelayed(true, 20, true, true);
  InvokeDelayedInit();
  SimulateOmniboxUsage();
  SimulateHomepageUsage();

  // Omnibox events.
  ExpectEventRecorded("C1I", true);
  ExpectEventRecorded("C1S", true);
  ExpectEventRecorded("C1F", true);

  // Home page events.
  ExpectEventRecorded("C2I", true);
  ExpectEventRecorded("C2S", true);
  ExpectEventRecorded("C2F", true);

  ExpectRlzPingSent(true);
}

TEST_F(RlzLibTest, OmniboxUsageSendsPingWhenDelayNegative) {
  RLZTracker::InitRlzDelayed(true, -20, true, true);
  SimulateOmniboxUsage();

  // Omnibox events.
  ExpectEventRecorded("C1I", true);
  ExpectEventRecorded("C1S", true);
  ExpectEventRecorded("C1F", true);

  // Home page events.
  ExpectEventRecorded("C2I", true);
  ExpectEventRecorded("C2S", true);
  ExpectEventRecorded("C2F", false);

  ExpectRlzPingSent(true);
}

TEST_F(RlzLibTest, HomepageUsageDoesNotSendPingWhenDelayNegative) {
  RLZTracker::InitRlzDelayed(true, -20, true, true);
  SimulateHomepageUsage();

  // Omnibox events.
  ExpectEventRecorded("C1I", false);
  ExpectEventRecorded("C1S", false);
  ExpectEventRecorded("C1F", false);

  // Home page events.
  ExpectEventRecorded("C2I", false);
  ExpectEventRecorded("C2S", false);
  ExpectEventRecorded("C2F", true);

  ExpectRlzPingSent(false);
}

TEST_F(RlzLibTest, GetAccessPointRlzOnIoThread) {
  // Set dummy RLZ string.
  rlz_lib::SetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, kOmniboxRlzString);

  std::wstring rlz;

  tracker_.set_assume_io_thread(true);
  EXPECT_TRUE(RLZTracker::GetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, &rlz));
  EXPECT_STREQ(kOmniboxRlzString, WideToUTF8(rlz).c_str());
}

TEST_F(RlzLibTest, GetAccessPointRlzNotOnIoThread) {
  // Set dummy RLZ string.
  rlz_lib::SetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, kOmniboxRlzString);

  std::wstring rlz;

  tracker_.set_assume_io_thread(false);
  EXPECT_FALSE(RLZTracker::GetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, &rlz));
}

TEST_F(RlzLibTest, GetAccessPointRlzIsCached) {
  // Set dummy RLZ string.
  rlz_lib::SetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, kOmniboxRlzString);

  std::wstring rlz;

  tracker_.set_assume_io_thread(false);
  EXPECT_FALSE(RLZTracker::GetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, &rlz));

  tracker_.set_assume_io_thread(true);
  EXPECT_TRUE(RLZTracker::GetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, &rlz));
  EXPECT_STREQ(kOmniboxRlzString, WideToUTF8(rlz).c_str());

  tracker_.set_assume_io_thread(false);
  EXPECT_TRUE(RLZTracker::GetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, &rlz));
  EXPECT_STREQ(kOmniboxRlzString, WideToUTF8(rlz).c_str());
}

TEST_F(RlzLibTest, PingInvalidatesRlzCache) {
  // Set dummy RLZ string.
  rlz_lib::SetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, kOmniboxRlzString);

  std::wstring rlz;

  // Prime the cache.
  tracker_.set_assume_io_thread(true);
  EXPECT_TRUE(RLZTracker::GetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, &rlz));
  EXPECT_STREQ(kOmniboxRlzString, WideToUTF8(rlz).c_str());

  // Make sure cache is valid.
  tracker_.set_assume_io_thread(false);
  EXPECT_TRUE(RLZTracker::GetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, &rlz));
  EXPECT_STREQ(kOmniboxRlzString, WideToUTF8(rlz).c_str());

  // Perform ping.
  RLZTracker::InitRlzDelayed(true, 20, true, true);
  InvokeDelayedInit();
  ExpectRlzPingSent(true);

  // Make sure cache is now invalid.
  EXPECT_FALSE(RLZTracker::GetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, &rlz));
}

TEST_F(RlzLibTest, ObserveHandlesBadArgs) {
  NavigationEntry entry(NULL, 0, GURL(), GURL(), string16(),
                        PageTransition::LINK);
  tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING,
                   NotificationService::AllSources(),
                   Details<NavigationEntry>(NULL));
  tracker_.Observe(content::NOTIFICATION_NAV_ENTRY_PENDING,
                   NotificationService::AllSources(),
                   Details<NavigationEntry>(&entry));
}