OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 "chrome/browser/search/suggestion_source.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/ref_counted_memory.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "chrome/browser/search/instant_io_context.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/resource_request_info.h" |
| 14 #include "content/public/test/mock_resource_context.h" |
| 15 #include "content/public/test/test_browser_thread.h" |
| 16 #include "googleurl/src/gurl.h" |
| 17 #include "grit/browser_resources.h" |
| 18 #include "net/url_request/url_request.h" |
| 19 #include "net/url_request/url_request_context.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 const int kNonInstantRendererPID = 0; |
| 23 const char kNonInstantOrigin[] = "http://evil"; |
| 24 const int kInstantRendererPID = 1; |
| 25 const char kInstantOrigin[] = "chrome-search://instant"; |
| 26 const int kInvalidRendererPID = 42; |
| 27 |
| 28 class TestableSuggestionSource : public SuggestionSource { |
| 29 public: |
| 30 using SuggestionSource::StartDataRequest; |
| 31 using SuggestionSource::GetMimeType; |
| 32 using SuggestionSource::ShouldServiceRequest; |
| 33 |
| 34 protected: |
| 35 // RenderViewHost is hard to mock in concert with everything else, so stub |
| 36 // this method out for testing. |
| 37 virtual bool GetOrigin( |
| 38 int process_id, |
| 39 int render_view_id, |
| 40 std::string* origin) const OVERRIDE { |
| 41 if (process_id == kInstantRendererPID) { |
| 42 *origin = kInstantOrigin; |
| 43 return true; |
| 44 } |
| 45 if (process_id == kNonInstantRendererPID) { |
| 46 *origin = kNonInstantOrigin; |
| 47 return true; |
| 48 } |
| 49 return false; |
| 50 } |
| 51 }; |
| 52 |
| 53 class SuggestionSourceTest : public testing::Test { |
| 54 public: |
| 55 // net::URLRequest wants to be executed with a message loop that has TYPE_IO. |
| 56 // InstantIOContext needs to be created on the UI thread and have everything |
| 57 // else happen on the IO thread. This setup is a hacky way to satisfy all |
| 58 // those constraints. |
| 59 SuggestionSourceTest() |
| 60 : message_loop_(base::MessageLoop::TYPE_IO), |
| 61 ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 62 io_thread_(content::BrowserThread::IO, &message_loop_), |
| 63 instant_io_context_(NULL), |
| 64 response_(NULL) { |
| 65 } |
| 66 |
| 67 TestableSuggestionSource* source() { return source_.get(); } |
| 68 |
| 69 std::string response_string() { |
| 70 if (response_) { |
| 71 return std::string(reinterpret_cast<const char*>(response_->front()), |
| 72 response_->size()); |
| 73 } |
| 74 return ""; |
| 75 } |
| 76 |
| 77 net::URLRequest* MockRequest( |
| 78 const std::string& url, |
| 79 bool allocate_info, |
| 80 int render_process_id, |
| 81 int render_view_id) { |
| 82 net::URLRequest* request = |
| 83 new net::URLRequest(GURL(url), NULL, |
| 84 resource_context_.GetRequestContext()); |
| 85 if (allocate_info) { |
| 86 content::ResourceRequestInfo::AllocateForTesting(request, |
| 87 ResourceType::SUB_FRAME, |
| 88 &resource_context_, |
| 89 render_process_id, |
| 90 render_view_id); |
| 91 } |
| 92 return request; |
| 93 } |
| 94 |
| 95 void StartDataRequest( |
| 96 const std::string& path_and_query, |
| 97 int render_process_id, |
| 98 int render_view_id) { |
| 99 source()->StartDataRequest(path_and_query, render_process_id, |
| 100 render_view_id, callback_); |
| 101 } |
| 102 |
| 103 private: |
| 104 virtual void SetUp() { |
| 105 source_.reset(new TestableSuggestionSource()); |
| 106 callback_ = base::Bind(&SuggestionSourceTest::SaveResponse, |
| 107 base::Unretained(this)); |
| 108 instant_io_context_ = new InstantIOContext; |
| 109 InstantIOContext::SetUserDataOnIO(&resource_context_, instant_io_context_); |
| 110 InstantIOContext::AddInstantProcessOnIO(instant_io_context_, |
| 111 kInstantRendererPID); |
| 112 response_ = NULL; |
| 113 } |
| 114 |
| 115 virtual void TearDown() { |
| 116 source_.reset(); |
| 117 } |
| 118 |
| 119 void SaveResponse(base::RefCountedMemory* data) { |
| 120 response_ = data; |
| 121 } |
| 122 |
| 123 MessageLoop message_loop_; |
| 124 content::TestBrowserThread ui_thread_; |
| 125 content::TestBrowserThread io_thread_; |
| 126 |
| 127 content::MockResourceContext resource_context_; |
| 128 scoped_ptr<TestableSuggestionSource> source_; |
| 129 content::URLDataSource::GotDataCallback callback_; |
| 130 scoped_refptr<InstantIOContext> instant_io_context_; |
| 131 scoped_refptr<base::RefCountedMemory> response_; |
| 132 }; |
| 133 |
| 134 TEST_F(SuggestionSourceTest, ShouldServiceRequest) { |
| 135 scoped_ptr<net::URLRequest> request; |
| 136 request.reset(MockRequest("http://suggestion/loader.js", |
| 137 true, kNonInstantRendererPID, 0)); |
| 138 EXPECT_FALSE(source()->ShouldServiceRequest(request.get())); |
| 139 request.reset(MockRequest("chrome-search://bogus/loader.js", |
| 140 true, kInstantRendererPID, 0)); |
| 141 EXPECT_FALSE(source()->ShouldServiceRequest(request.get())); |
| 142 request.reset(MockRequest("chrome-search://suggestion/bogus.js", |
| 143 true, kInstantRendererPID, 0)); |
| 144 EXPECT_FALSE(source()->ShouldServiceRequest(request.get())); |
| 145 request.reset(MockRequest("chrome-search://suggestion/loader.js", |
| 146 true, kInstantRendererPID, 0)); |
| 147 EXPECT_TRUE(source()->ShouldServiceRequest(request.get())); |
| 148 request.reset(MockRequest("chrome-search://suggestion/loader.js", |
| 149 true, kNonInstantRendererPID, 0)); |
| 150 EXPECT_FALSE(source()->ShouldServiceRequest(request.get())); |
| 151 request.reset(MockRequest("chrome-search://suggestion/loader.js", |
| 152 true, kInvalidRendererPID, 0)); |
| 153 EXPECT_FALSE(source()->ShouldServiceRequest(request.get())); |
| 154 } |
| 155 |
| 156 TEST_F(SuggestionSourceTest, GetMimeType) { |
| 157 // URLDataManagerBackend does not include / in path_and_query. |
| 158 EXPECT_EQ("text/html", source()->GetMimeType("loader.html")); |
| 159 EXPECT_EQ("application/javascript", source()->GetMimeType("loader.js")); |
| 160 EXPECT_EQ("", source()->GetMimeType("bogus.html")); |
| 161 } |
| 162 |
| 163 TEST_F(SuggestionSourceTest, StartDataRequest) { |
| 164 StartDataRequest("bogus.html", kInstantRendererPID, 0); |
| 165 EXPECT_TRUE(response_string().empty()); |
| 166 StartDataRequest("bogus.html", kNonInstantRendererPID, 0); |
| 167 EXPECT_TRUE(response_string().empty()); |
| 168 StartDataRequest("loader.html", kInstantRendererPID, 0); |
| 169 EXPECT_FALSE(response_string().empty()); |
| 170 StartDataRequest("loader.js", kInstantRendererPID, 0); |
| 171 EXPECT_FALSE(response_string().empty()); |
| 172 EXPECT_NE(std::string::npos, response_string().find(kInstantOrigin)); |
| 173 StartDataRequest("loader.js", kNonInstantRendererPID, 0); |
| 174 EXPECT_FALSE(response_string().empty()); |
| 175 EXPECT_NE(std::string::npos, response_string().find(kNonInstantOrigin)); |
| 176 StartDataRequest("loader.js", kInvalidRendererPID, 0); |
| 177 EXPECT_TRUE(response_string().empty()); |
| 178 } |
OLD | NEW |