Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / google / google_search_counter_android_unittest.cc
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.
4
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"
20
21 namespace {
22
23 class MockSearchMetrics : public GoogleSearchMetrics {
24  public:
25   MOCK_CONST_METHOD2(RecordAndroidGoogleSearch,
26                      void(AccessPoint ap, bool prerender_enabled));
27 };
28
29 }  // namespace
30
31 class GoogleSearchCounterAndroidTest : public testing::Test {
32  protected:
33   GoogleSearchCounterAndroidTest();
34   virtual ~GoogleSearchCounterAndroidTest();
35
36   // testing::Test
37   virtual void SetUp();
38   virtual void TearDown();
39
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
46   // details.
47   void TestGoogleSearch(const std::string& url,
48                         bool is_omnibox,
49                         GoogleSearchMetrics::AccessPoint expected_metric,
50                         bool expected_prerender_enabled);
51
52  private:
53   void ExpectMetricsLogged(GoogleSearchMetrics::AccessPoint ap,
54                            bool prerender_enabled);
55
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 };
64
65 GoogleSearchCounterAndroidTest::GoogleSearchCounterAndroidTest()
66     : ui_thread_(content::BrowserThread::UI, &message_loop_),
67       profile_(new TestingProfile()),
68       search_counter_(new GoogleSearchCounterAndroid(profile_.get())),
69       mock_search_metrics_(NULL) {
70 }
71
72 GoogleSearchCounterAndroidTest::~GoogleSearchCounterAndroidTest() {
73 }
74
75 void GoogleSearchCounterAndroidTest::SetUp() {
76   // Keep a weak ptr to MockSearchMetrics so we can run expectations. The
77   // GoogleSearchCounter singleton will own and clean up MockSearchMetrics.
78   mock_search_metrics_ = new ::testing::StrictMock<MockSearchMetrics>;
79   GoogleSearchCounter::GetInstance()->SetSearchMetricsForTesting(
80       mock_search_metrics_);
81   prerender::PrerenderManager::SetMode(
82       prerender::PrerenderManager::PRERENDER_MODE_ENABLED);
83 }
84
85 void GoogleSearchCounterAndroidTest::TearDown() {
86   mock_search_metrics_ = NULL;
87 }
88
89 void GoogleSearchCounterAndroidTest::TestGoogleSearch(
90     const std::string& url,
91     bool is_omnibox,
92     GoogleSearchMetrics::AccessPoint expected_metric,
93     bool expected_prerender_enabled) {
94   content::LoadCommittedDetails details;
95   scoped_ptr<content::NavigationEntry> entry(
96       content::NavigationEntry::Create());
97   if (is_omnibox) {
98     entry->SetTransitionType(ui::PageTransitionFromInt(
99         ui::PAGE_TRANSITION_GENERATED |
100             ui::PAGE_TRANSITION_FROM_ADDRESS_BAR));
101   }
102   entry->SetURL(GURL(url));
103   details.entry = entry.get();
104
105   // Since the internal mocked metrics object is strict, if |expect_metrics| is
106   // false, the absence of this call to ExpectMetricsLogged will be noticed and
107   // cause the test to complain, as expected. We use this behaviour to test
108   // negative test cases (such as bad searches).
109   if (expected_metric != GoogleSearchMetrics::AP_BOUNDARY)
110     ExpectMetricsLogged(expected_metric, expected_prerender_enabled);
111
112   // For now we don't care about the notification source, but when we start
113   // listening for additional access points, we will have to pass in a valid
114   // controller.
115   search_counter_->Observe(
116       content::NOTIFICATION_NAV_ENTRY_COMMITTED,
117       content::Source<content::NavigationController>(NULL),
118       content::Details<content::LoadCommittedDetails>(&details));
119 }
120
121 void GoogleSearchCounterAndroidTest::ExpectMetricsLogged(
122     GoogleSearchMetrics::AccessPoint ap, bool prerender_enabled) {
123   EXPECT_CALL(*mock_search_metrics_,
124               RecordAndroidGoogleSearch(ap, prerender_enabled)).Times(1);
125 }
126
127 TEST_F(GoogleSearchCounterAndroidTest, EmptySearch) {
128   TestGoogleSearch(std::string(), false, GoogleSearchMetrics::AP_BOUNDARY,
129                    true);
130 }
131
132 TEST_F(GoogleSearchCounterAndroidTest, GoodOmniboxSearch) {
133   TestGoogleSearch("http://www.google.com/search?q=something", true,
134                    GoogleSearchMetrics::AP_OMNIBOX, true);
135 }
136
137 TEST_F(GoogleSearchCounterAndroidTest, BadOmniboxSearch) {
138   TestGoogleSearch("http://www.google.com/search?other=something", true,
139                    GoogleSearchMetrics::AP_BOUNDARY, true);
140 }
141
142 TEST_F(GoogleSearchCounterAndroidTest, EmptyOmniboxSearch) {
143   TestGoogleSearch(std::string(), true, GoogleSearchMetrics::AP_BOUNDARY, true);
144 }
145
146 TEST_F(GoogleSearchCounterAndroidTest, GoodOtherSearch) {
147   TestGoogleSearch("http://www.google.com/search?q=something", false,
148                    GoogleSearchMetrics::AP_OTHER, true);
149 }
150
151 TEST_F(GoogleSearchCounterAndroidTest, BadOtherSearch) {
152   TestGoogleSearch("http://www.google.com/search?other=something", false,
153                    GoogleSearchMetrics::AP_BOUNDARY, true);
154 }
155
156 TEST_F(GoogleSearchCounterAndroidTest, SearchAppSearch) {
157   TestGoogleSearch("http://www.google.com/webhp?source=search_app#q=something",
158                    false, GoogleSearchMetrics::AP_SEARCH_APP, true);
159 }
160
161 TEST_F(GoogleSearchCounterAndroidTest, SearchAppStart) {
162   // Starting the search app takes you to this URL, but it should not be
163   // considered an actual search event. Note that this URL is not considered an
164   // actual search because it has no query string parameter ("q").
165   TestGoogleSearch("http://www.google.com/webhp?source=search_app",
166                    false, GoogleSearchMetrics::AP_BOUNDARY, true);
167 }
168
169 TEST_F(GoogleSearchCounterAndroidTest, GoodOmniboxSearch_PrerenderDisabled) {
170   prerender::PrerenderManager::SetMode(
171       prerender::PrerenderManager::PRERENDER_MODE_DISABLED);
172   TestGoogleSearch("http://www.google.com/search?q=something", true,
173                    GoogleSearchMetrics::AP_OMNIBOX, false);
174 }