This source file includes following definitions.
- SetUp
- TearDown
- TestGoogleSearch
- ExpectMetricsLogged
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#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));
};
}
class GoogleSearchCounterTest : public testing::Test {
protected:
GoogleSearchCounterTest();
virtual ~GoogleSearchCounterTest();
virtual void SetUp();
virtual void TearDown();
void TestGoogleSearch(const std::string& url,
bool is_omnibox,
GoogleSearchMetrics::AccessPoint expected_metric);
private:
void ExpectMetricsLogged(GoogleSearchMetrics::AccessPoint ap);
::testing::StrictMock<MockSearchMetrics>* mock_search_metrics_;
};
GoogleSearchCounterTest::GoogleSearchCounterTest()
: mock_search_metrics_(NULL) {
}
GoogleSearchCounterTest::~GoogleSearchCounterTest() {
}
void GoogleSearchCounterTest::SetUp() {
mock_search_metrics_ = new ::testing::StrictMock<MockSearchMetrics>;
GoogleSearchCounter::GetInstance()->SetSearchMetricsForTesting(
mock_search_metrics_);
}
void GoogleSearchCounterTest::TearDown() {
mock_search_metrics_ = NULL;
}
void GoogleSearchCounterTest::TestGoogleSearch(
const std::string& url,
bool is_omnibox,
GoogleSearchMetrics::AccessPoint expected_metric) {
content::LoadCommittedDetails details;
scoped_ptr<content::NavigationEntry> entry(
content::NavigationEntry::Create());
if (is_omnibox)
entry->SetTransitionType(content::PAGE_TRANSITION_GENERATED);
entry->SetURL(GURL(url));
details.entry = entry.get();
if (expected_metric != GoogleSearchMetrics::AP_BOUNDARY)
ExpectMetricsLogged(expected_metric);
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, EmptySearch) {
TestGoogleSearch(std::string(), false, GoogleSearchMetrics::AP_BOUNDARY);
}
TEST_F(GoogleSearchCounterTest, GoodOmniboxSearch) {
TestGoogleSearch("http://www.google.com/search?q=something", true,
GoogleSearchMetrics::AP_OMNIBOX);
}
TEST_F(GoogleSearchCounterTest, BadOmniboxSearch) {
TestGoogleSearch("http://www.google.com/search?other=something", true,
GoogleSearchMetrics::AP_BOUNDARY);
}
TEST_F(GoogleSearchCounterTest, EmptyOmniboxSearch) {
TestGoogleSearch(std::string(), true, GoogleSearchMetrics::AP_BOUNDARY);
}
TEST_F(GoogleSearchCounterTest, GoodOtherSearch) {
TestGoogleSearch("http://www.google.com/search?q=something", false,
GoogleSearchMetrics::AP_OTHER);
}
TEST_F(GoogleSearchCounterTest, BadOtherSearch) {
TestGoogleSearch("http://www.google.com/search?other=something", false,
GoogleSearchMetrics::AP_BOUNDARY);
}
TEST_F(GoogleSearchCounterTest, SearchAppSearch) {
TestGoogleSearch("http://www.google.com/webhp?source=search_app#q=something",
false, GoogleSearchMetrics::AP_SEARCH_APP);
}
TEST_F(GoogleSearchCounterTest, SearchAppStart) {
TestGoogleSearch("http://www.google.com/webhp?source=search_app",
false, GoogleSearchMetrics::AP_BOUNDARY);
}