| Index: chrome/browser/search/suggestion_source_unittest.cc
|
| diff --git a/chrome/browser/search/suggestion_source_unittest.cc b/chrome/browser/search/suggestion_source_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..bca7985d503ce4cc8a854eecb2366caaa9a952ed
|
| --- /dev/null
|
| +++ b/chrome/browser/search/suggestion_source_unittest.cc
|
| @@ -0,0 +1,195 @@
|
| +// Copyright 2013 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/search/suggestion_source.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/memory/ref_counted_memory.h"
|
| +#include "base/memory/scoped_ptr.h"
|
| +#include "base/message_loop.h"
|
| +#include "chrome/browser/search/instant_io_context.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "content/public/browser/resource_request_info.h"
|
| +#include "content/public/test/mock_resource_context.h"
|
| +#include "content/public/test/test_browser_thread.h"
|
| +#include "googleurl/src/gurl.h"
|
| +#include "grit/browser_resources.h"
|
| +#include "net/url_request/url_request.h"
|
| +#include "net/url_request/url_request_context.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +const int kNonInstantRendererPID = 0;
|
| +const char kNonInstantOrigin[] = "http://evil";
|
| +const int kInstantRendererPID = 1;
|
| +const char kInstantOrigin[] = "chrome-search://instant";
|
| +
|
| +class TestableSuggestionSource : public SuggestionSource {
|
| + public:
|
| + using SuggestionSource::StartDataRequest;
|
| + using SuggestionSource::GetMimeType;
|
| + using SuggestionSource::ShouldServiceRequest;
|
| + using SuggestionSource::WillServiceRequest;
|
| +
|
| + protected:
|
| + // RenderViewHost is hard to mock in concert with everything else, so stub
|
| + // this method out for testing.
|
| + virtual bool GetOrigin(
|
| + int process_id,
|
| + int render_view_id,
|
| + std::string* origin) const OVERRIDE {
|
| + if (process_id == kInstantRendererPID) {
|
| + *origin = kInstantOrigin;
|
| + return true;
|
| + }
|
| + if (process_id == kNonInstantRendererPID) {
|
| + *origin = kNonInstantOrigin;
|
| + return true;
|
| + }
|
| + return false;
|
| + }
|
| +};
|
| +
|
| +class SuggestionSourceTest : public testing::Test {
|
| + public:
|
| + // net::URLRequest wants to be executed with a message loop that has TYPE_IO.
|
| + // InstantIOContext needs to be created on the UI thread and
|
| + // have everything else happen on the IO thread. This setup is a hacky way to
|
| + // satisfy all those constraints.
|
| + SuggestionSourceTest()
|
| + : message_loop_(base::MessageLoop::TYPE_IO),
|
| + ui_thread_(content::BrowserThread::UI, &message_loop_),
|
| + io_thread_(content::BrowserThread::IO, &message_loop_),
|
| + instant_io_context_(NULL),
|
| + response_(NULL) {
|
| + }
|
| +
|
| + TestableSuggestionSource* source() { return source_.get(); }
|
| +
|
| + std::string response_string() {
|
| + if (response_) {
|
| + return std::string(reinterpret_cast<const char*>(response_->front()),
|
| + response_->size());
|
| + }
|
| + return "";
|
| + }
|
| +
|
| + net::URLRequest* MockRequest(
|
| + const std::string& url,
|
| + bool allocate_info,
|
| + int process_id,
|
| + int render_view_id) {
|
| + net::URLRequest* request =
|
| + new net::URLRequest(GURL(url), NULL,
|
| + resource_context_.GetRequestContext());
|
| + if (allocate_info) {
|
| + content::ResourceRequestInfo::AllocateForTesting(request,
|
| + ResourceType::SUB_FRAME,
|
| + &resource_context_,
|
| + process_id,
|
| + render_view_id);
|
| + }
|
| + return request;
|
| + }
|
| +
|
| + void StartDataRequest(const std::string& path_and_query) {
|
| + source()->StartDataRequest(path_and_query, false, callback_);
|
| + }
|
| +
|
| + private:
|
| + virtual void SetUp() {
|
| + source_.reset(new TestableSuggestionSource());
|
| + callback_ = base::Bind(&SuggestionSourceTest::SaveResponse,
|
| + base::Unretained(this));
|
| + instant_io_context_ = new InstantIOContext;
|
| + InstantIOContext::SetUserDataOnIO(&resource_context_, instant_io_context_);
|
| + InstantIOContext::AddInstantProcessOnIO(instant_io_context_,
|
| + kInstantRendererPID);
|
| + response_ = NULL;
|
| + }
|
| +
|
| + virtual void TearDown() {
|
| + source_.reset();
|
| + }
|
| +
|
| + void SaveResponse(base::RefCountedMemory* data) {
|
| + response_ = data;
|
| + }
|
| +
|
| + MessageLoop message_loop_;
|
| + content::TestBrowserThread ui_thread_;
|
| + content::TestBrowserThread io_thread_;
|
| +
|
| + content::MockResourceContext resource_context_;
|
| + scoped_ptr<TestableSuggestionSource> source_;
|
| + content::URLDataSource::GotDataCallback callback_;
|
| + scoped_refptr<InstantIOContext> instant_io_context_;
|
| + scoped_refptr<base::RefCountedMemory> response_;
|
| +};
|
| +
|
| +TEST_F(SuggestionSourceTest, ShouldServiceRequest) {
|
| + scoped_ptr<net::URLRequest> request;
|
| + request.reset(MockRequest("http://suggestion/loader.js",
|
| + true, kNonInstantRendererPID, 0));
|
| + EXPECT_FALSE(source()->ShouldServiceRequest(request.get()));
|
| + request.reset(MockRequest("chrome-search://bogus/loader.js",
|
| + true, kInstantRendererPID, 0));
|
| + EXPECT_FALSE(source()->ShouldServiceRequest(request.get()));
|
| + request.reset(MockRequest("chrome-search://suggestion/bogus.js",
|
| + true, kInstantRendererPID, 0));
|
| + EXPECT_FALSE(source()->ShouldServiceRequest(request.get()));
|
| + request.reset(MockRequest("chrome-search://suggestion/loader.js",
|
| + true, kInstantRendererPID, 0));
|
| + EXPECT_TRUE(source()->ShouldServiceRequest(request.get()));
|
| + request.reset(MockRequest("chrome-search://suggestion/loader.js?p=1&v=0",
|
| + true, kInstantRendererPID, 0));
|
| + EXPECT_TRUE(source()->ShouldServiceRequest(request.get()));
|
| + request.reset(MockRequest("chrome-search://suggestion/loader.js",
|
| + true, kNonInstantRendererPID, 0));
|
| + EXPECT_FALSE(source()->ShouldServiceRequest(request.get()));
|
| +}
|
| +
|
| +TEST_F(SuggestionSourceTest, GetMimeType) {
|
| + // URLDataManagerBackend does not include / in path_and_query.
|
| + EXPECT_EQ("text/html", source()->GetMimeType("loader.html"));
|
| + EXPECT_EQ("application/javascript",
|
| + source()->GetMimeType("loader.js?p=1&v=0"));
|
| + EXPECT_EQ("", source()->GetMimeType("bogus.html"));
|
| +}
|
| +
|
| +TEST_F(SuggestionSourceTest, StartDataRequest) {
|
| + StartDataRequest("bogus.html");
|
| + EXPECT_TRUE(response_string().empty());
|
| + StartDataRequest("loader.html?p=1&v=0");
|
| + EXPECT_FALSE(response_string().empty());
|
| + EXPECT_EQ(std::string::npos, response_string().find(kInstantOrigin));
|
| + StartDataRequest("loader.js");
|
| + EXPECT_TRUE(response_string().empty());
|
| + StartDataRequest("loader.js?p=1&v=0");
|
| + EXPECT_FALSE(response_string().empty());
|
| + EXPECT_NE(std::string::npos, response_string().find(kInstantOrigin));
|
| + StartDataRequest("loader.js?p=0&v=0");
|
| + EXPECT_FALSE(response_string().empty());
|
| + EXPECT_NE(std::string::npos, response_string().find(kNonInstantOrigin));
|
| + StartDataRequest("loader.js?p=42&v=0");
|
| + EXPECT_TRUE(response_string().empty());
|
| +}
|
| +
|
| +TEST_F(SuggestionSourceTest, WillServiceRequest) {
|
| + scoped_ptr<net::URLRequest> request;
|
| + std::string path;
|
| + request.reset(MockRequest("http://suggestion/loader.js", true, 0, 1));
|
| + path = "loader.js";
|
| + source()->WillServiceRequest(request.get(), &path);
|
| + EXPECT_EQ(std::string("loader.js?p=0&v=1"), path);
|
| + request.reset(MockRequest("http://suggestion/loader.js?p=10&v=20",
|
| + true, 0, 1));
|
| + path = "loader.js?p=10&v=20";
|
| + source()->WillServiceRequest(request.get(), &path);
|
| + EXPECT_EQ(std::string("loader.js?p=0&v=1"), path);
|
| + request.reset(MockRequest("http://suggestion/loader.js?p=10&v=20",
|
| + false, 0, 1));
|
| + path = "loader.js?p=10&v=20";
|
| + source()->WillServiceRequest(request.get(), &path);
|
| + EXPECT_EQ(std::string("loader.js?"), path);
|
| +}
|
|
|