diff options
author | stevet@chromium.org <stevet@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-21 19:49:52 +0000 |
---|---|---|
committer | stevet@chromium.org <stevet@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-21 19:49:52 +0000 |
commit | ed666564e7c934367bf0e9fcbf86e18a97e0ddcb (patch) | |
tree | 111c050918307f71aedfd50a78eeb5b48d1ef12d /chrome/browser/google/google_search_counter_unittest.cc | |
parent | 4382c87cc29f5843ec6054e4d241bfe020c3dbc0 (diff) | |
download | chromium_src-ed666564e7c934367bf0e9fcbf86e18a97e0ddcb.zip chromium_src-ed666564e7c934367bf0e9fcbf86e18a97e0ddcb.tar.gz chromium_src-ed666564e7c934367bf0e9fcbf86e18a97e0ddcb.tar.bz2 |
Add a GoogleSearchCounter class to count Omnibox searches.
This listens to all committed naviations and analyzes them to see if they are a omnibox Google search. If so, this logs the appropriate enum value with GoogleSearchMetrics.
We plan on expanding this in the future to count searches from other access points to help understand the distribution of searches in Chrome.
BUG=116555
TEST=Do a search in Chrome using the omnibox with instant turned off. Check about:histograms and ensure that there is an entry for GoogleSearch.AccessPoint enum value 1.
Review URL: http://codereview.chromium.org/9655004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@128035 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/google/google_search_counter_unittest.cc')
-rw-r--r-- | chrome/browser/google/google_search_counter_unittest.cc | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/chrome/browser/google/google_search_counter_unittest.cc b/chrome/browser/google/google_search_counter_unittest.cc new file mode 100644 index 0000000..d462fcf --- /dev/null +++ b/chrome/browser/google/google_search_counter_unittest.cc @@ -0,0 +1,105 @@ +// Copyright (c) 2012 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/google/google_search_counter.h" +#include "chrome/browser/google/google_search_metrics.h" +#include "content/public/browser/navigation_controller.h" +#include "content/public/browser/navigation_details.h" +#include "content/public/browser/navigation_entry.h" +#include "content/public/browser/notification_service.h" +#include "content/public/browser/notification_types.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +class MockSearchMetrics : public GoogleSearchMetrics { + public: + MOCK_CONST_METHOD1(RecordGoogleSearch, + void(GoogleSearchMetrics::AccessPoint ap)); +}; + +} // namespace + +class GoogleSearchCounterTest : public testing::Test { + protected: + GoogleSearchCounterTest(); + virtual ~GoogleSearchCounterTest(); + + // testing::Test + virtual void SetUp(); + virtual void TearDown(); + + void TestOmniboxSearch(const std::string& url, bool expect_metrics); + + private: + void ExpectMetricsLogged(GoogleSearchMetrics::AccessPoint ap); + + // Weak ptr. Actual instance owned by GoogleSearchCounter. + ::testing::StrictMock<MockSearchMetrics>* mock_search_metrics_; +}; + +GoogleSearchCounterTest::GoogleSearchCounterTest() + : mock_search_metrics_(NULL) { +} + +GoogleSearchCounterTest::~GoogleSearchCounterTest() { +} + +void GoogleSearchCounterTest::SetUp() { + // Keep a weak ptr to MockSearchMetrics so we can run expectations. The + // GoogleSearchCounter singleton will own and clean up MockSearchMetrics. + mock_search_metrics_ = new ::testing::StrictMock<MockSearchMetrics>; + GoogleSearchCounter::GetInstance()->SetSearchMetricsForTesting( + mock_search_metrics_); +} + +void GoogleSearchCounterTest::TearDown() { + mock_search_metrics_ = NULL; +} + +void GoogleSearchCounterTest::TestOmniboxSearch(const std::string& url, + bool expect_metrics) { + content::LoadCommittedDetails details; + scoped_ptr<content::NavigationEntry> entry( + content::NavigationEntry::Create()); + entry->SetTransitionType(content::PAGE_TRANSITION_GENERATED); + entry->SetURL(GURL(url)); + details.entry = entry.get(); + + // Since the internal mocked metrics object is strict, if |expect_metrics| is + // false, the absence of this call to ExpectMetricsLogged will be noticed and + // cause the test to complain, as expected. We use this behaviour to test + // negative test cases (such as bad searches). + if (expect_metrics) + ExpectMetricsLogged(GoogleSearchMetrics::AP_OMNIBOX); + + // For now we don't care about the notification source, but when we start + // listening for additional access points, we will have to pass in a valid + // controller. + GoogleSearchCounter::GetInstance()->Observe( + content::NOTIFICATION_NAV_ENTRY_COMMITTED, + content::Source<content::NavigationController>(NULL), + content::Details<content::LoadCommittedDetails>(&details)); +} + +void GoogleSearchCounterTest::ExpectMetricsLogged( + GoogleSearchMetrics::AccessPoint ap) { + EXPECT_CALL(*mock_search_metrics_, RecordGoogleSearch(ap)).Times(1); +} + +TEST_F(GoogleSearchCounterTest, GoodOmniboxSearch) { + TestOmniboxSearch("http://www.google.com/search?q=something", true); +} + +TEST_F(GoogleSearchCounterTest, BadOmniboxSearch) { + TestOmniboxSearch("http://www.google.com/search?other=something", false); +} + +TEST_F(GoogleSearchCounterTest, EmptyOmniboxSearch) { + TestOmniboxSearch(std::string(), false); +} + +// TODO(stevet): Add a regression test to protect against the participar +// bad-flags handling case that asvitkine pointed out. |