Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: chrome/browser/rlz/rlz_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698