1 // Copyright 2014 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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/message_loop/message_loop.h"
7 #include "chrome/browser/google/google_search_counter.h"
8 #include "chrome/browser/google/google_search_counter_android.h"
9 #include "chrome/browser/prerender/prerender_manager.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "components/google/core/browser/google_search_metrics.h"
12 #include "content/public/browser/navigation_controller.h"
13 #include "content/public/browser/navigation_details.h"
14 #include "content/public/browser/navigation_entry.h"
15 #include "content/public/browser/notification_service.h"
16 #include "content/public/browser/notification_types.h"
17 #include "content/public/test/test_browser_thread.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
23 class MockSearchMetrics
: public GoogleSearchMetrics
{
25 MOCK_CONST_METHOD2(RecordAndroidGoogleSearch
,
26 void(AccessPoint ap
, bool prerender_enabled
));
31 class GoogleSearchCounterAndroidTest
: public testing::Test
{
33 GoogleSearchCounterAndroidTest();
34 ~GoogleSearchCounterAndroidTest() override
;
37 void SetUp() override
;
38 void TearDown() override
;
40 // Test if |url| is a Google search for specific types. When |is_omnibox| is
41 // true, this method will append Omnibox identifiers to the simulated URL
42 // navigation. If |expected_metric| is set and not AP_BOUNDARY, we'll also use
43 // the Search Metrics mock class to ensure that the type of metric recorded is
44 // correct. Note that when |expected_metric| is AP_BOUNDARY, we strictly
45 // forbid any metrics from being logged at all. See implementation below for
47 void TestGoogleSearch(const std::string
& url
,
49 GoogleSearchMetrics::AccessPoint expected_metric
,
50 bool expected_prerender_enabled
);
53 void ExpectMetricsLogged(GoogleSearchMetrics::AccessPoint ap
,
54 bool prerender_enabled
);
56 // Needed to pass PrerenderManager's DCHECKs.
57 base::MessageLoop message_loop_
;
58 content::TestBrowserThread ui_thread_
;
59 scoped_ptr
<TestingProfile
> profile_
;
60 scoped_ptr
<GoogleSearchCounterAndroid
> search_counter_
;
61 // Weak ptr. Actual instance owned by GoogleSearchCounter.
62 ::testing::StrictMock
<MockSearchMetrics
>* mock_search_metrics_
;
63 prerender::PrerenderManager::PrerenderManagerMode original_prerender_mode_
;
66 GoogleSearchCounterAndroidTest::GoogleSearchCounterAndroidTest()
67 : ui_thread_(content::BrowserThread::UI
, &message_loop_
),
68 profile_(new TestingProfile()),
69 search_counter_(new GoogleSearchCounterAndroid(profile_
.get())),
70 mock_search_metrics_(NULL
),
71 original_prerender_mode_(
72 prerender::PrerenderManager::PRERENDER_MODE_DISABLED
) {
75 GoogleSearchCounterAndroidTest::~GoogleSearchCounterAndroidTest() {
78 void GoogleSearchCounterAndroidTest::SetUp() {
79 // Keep a weak ptr to MockSearchMetrics so we can run expectations. The
80 // GoogleSearchCounter singleton will own and clean up MockSearchMetrics.
81 mock_search_metrics_
= new ::testing::StrictMock
<MockSearchMetrics
>;
82 GoogleSearchCounter::GetInstance()->SetSearchMetricsForTesting(
83 mock_search_metrics_
);
84 original_prerender_mode_
= prerender::PrerenderManager::GetMode();
85 prerender::PrerenderManager::SetMode(
86 prerender::PrerenderManager::PRERENDER_MODE_ENABLED
);
89 void GoogleSearchCounterAndroidTest::TearDown() {
90 mock_search_metrics_
= NULL
;
91 prerender::PrerenderManager::SetMode(original_prerender_mode_
);
94 void GoogleSearchCounterAndroidTest::TestGoogleSearch(
95 const std::string
& url
,
97 GoogleSearchMetrics::AccessPoint expected_metric
,
98 bool expected_prerender_enabled
) {
99 content::LoadCommittedDetails details
;
100 scoped_ptr
<content::NavigationEntry
> entry(
101 content::NavigationEntry::Create());
103 entry
->SetTransitionType(ui::PageTransitionFromInt(
104 ui::PAGE_TRANSITION_GENERATED
|
105 ui::PAGE_TRANSITION_FROM_ADDRESS_BAR
));
107 entry
->SetURL(GURL(url
));
108 details
.entry
= entry
.get();
110 // Since the internal mocked metrics object is strict, if |expect_metrics| is
111 // false, the absence of this call to ExpectMetricsLogged will be noticed and
112 // cause the test to complain, as expected. We use this behaviour to test
113 // negative test cases (such as bad searches).
114 if (expected_metric
!= GoogleSearchMetrics::AP_BOUNDARY
)
115 ExpectMetricsLogged(expected_metric
, expected_prerender_enabled
);
117 // For now we don't care about the notification source, but when we start
118 // listening for additional access points, we will have to pass in a valid
120 search_counter_
->Observe(
121 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
122 content::Source
<content::NavigationController
>(NULL
),
123 content::Details
<content::LoadCommittedDetails
>(&details
));
126 void GoogleSearchCounterAndroidTest::ExpectMetricsLogged(
127 GoogleSearchMetrics::AccessPoint ap
, bool prerender_enabled
) {
128 EXPECT_CALL(*mock_search_metrics_
,
129 RecordAndroidGoogleSearch(ap
, prerender_enabled
)).Times(1);
132 TEST_F(GoogleSearchCounterAndroidTest
, EmptySearch
) {
133 TestGoogleSearch(std::string(), false, GoogleSearchMetrics::AP_BOUNDARY
,
137 TEST_F(GoogleSearchCounterAndroidTest
, GoodOmniboxSearch
) {
138 TestGoogleSearch("http://www.google.com/search?q=something", true,
139 GoogleSearchMetrics::AP_OMNIBOX
, true);
142 TEST_F(GoogleSearchCounterAndroidTest
, BadOmniboxSearch
) {
143 TestGoogleSearch("http://www.google.com/search?other=something", true,
144 GoogleSearchMetrics::AP_BOUNDARY
, true);
147 TEST_F(GoogleSearchCounterAndroidTest
, EmptyOmniboxSearch
) {
148 TestGoogleSearch(std::string(), true, GoogleSearchMetrics::AP_BOUNDARY
, true);
151 TEST_F(GoogleSearchCounterAndroidTest
, GoodOtherSearch
) {
152 TestGoogleSearch("http://www.google.com/search?q=something", false,
153 GoogleSearchMetrics::AP_OTHER
, true);
156 TEST_F(GoogleSearchCounterAndroidTest
, BadOtherSearch
) {
157 TestGoogleSearch("http://www.google.com/search?other=something", false,
158 GoogleSearchMetrics::AP_BOUNDARY
, true);
161 TEST_F(GoogleSearchCounterAndroidTest
, SearchAppSearch
) {
162 TestGoogleSearch("http://www.google.com/webhp?source=search_app#q=something",
163 false, GoogleSearchMetrics::AP_SEARCH_APP
, true);
166 TEST_F(GoogleSearchCounterAndroidTest
, SearchAppStart
) {
167 // Starting the search app takes you to this URL, but it should not be
168 // considered an actual search event. Note that this URL is not considered an
169 // actual search because it has no query string parameter ("q").
170 TestGoogleSearch("http://www.google.com/webhp?source=search_app",
171 false, GoogleSearchMetrics::AP_BOUNDARY
, true);
174 TEST_F(GoogleSearchCounterAndroidTest
, GoodOmniboxSearch_PrerenderDisabled
) {
175 prerender::PrerenderManager::SetMode(
176 prerender::PrerenderManager::PRERENDER_MODE_DISABLED
);
177 TestGoogleSearch("http://www.google.com/search?q=something", true,
178 GoogleSearchMetrics::AP_OMNIBOX
, false);