Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(110)

Side by Side Diff: chrome/browser/search/suggestion_source_unittest.cc

Issue 13375003: Fixing iframe jank in the local omnibox popup. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing comment. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
27 class TestableSuggestionSource : public SuggestionSource {
28 public:
29 using SuggestionSource::StartDataRequest;
30 using SuggestionSource::GetMimeType;
31 using SuggestionSource::ShouldServiceRequest;
32 using SuggestionSource::WillServiceRequest;
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
57 // have everything else happen on the IO thread. This setup is a hacky way to
58 // satisfy all 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 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 process_id,
90 render_view_id);
91 }
92 return request;
93 }
94
95 void StartDataRequest(const std::string& path_and_query) {
96 source()->StartDataRequest(path_and_query, false, callback_);
97 }
98
99 private:
100 virtual void SetUp() {
101 source_.reset(new TestableSuggestionSource());
102 callback_ = base::Bind(&SuggestionSourceTest::SaveResponse,
103 base::Unretained(this));
104 instant_io_context_ = new InstantIOContext;
105 InstantIOContext::SetUserDataOnIO(&resource_context_, instant_io_context_);
106 InstantIOContext::AddInstantProcessOnIO(instant_io_context_,
107 kInstantRendererPID);
108 response_ = NULL;
109 }
110
111 virtual void TearDown() {
112 source_.reset();
113 }
114
115 void SaveResponse(base::RefCountedMemory* data) {
116 response_ = data;
117 }
118
119 MessageLoop message_loop_;
120 content::TestBrowserThread ui_thread_;
121 content::TestBrowserThread io_thread_;
122
123 content::MockResourceContext resource_context_;
124 scoped_ptr<TestableSuggestionSource> source_;
125 content::URLDataSource::GotDataCallback callback_;
126 scoped_refptr<InstantIOContext> instant_io_context_;
127 scoped_refptr<base::RefCountedMemory> response_;
128 };
129
130 TEST_F(SuggestionSourceTest, ShouldServiceRequest) {
131 scoped_ptr<net::URLRequest> request;
132 request.reset(MockRequest("http://suggestion/loader.js",
133 true, kNonInstantRendererPID, 0));
134 EXPECT_FALSE(source()->ShouldServiceRequest(request.get()));
135 request.reset(MockRequest("chrome-search://bogus/loader.js",
136 true, kInstantRendererPID, 0));
137 EXPECT_FALSE(source()->ShouldServiceRequest(request.get()));
138 request.reset(MockRequest("chrome-search://suggestion/bogus.js",
139 true, kInstantRendererPID, 0));
140 EXPECT_FALSE(source()->ShouldServiceRequest(request.get()));
141 request.reset(MockRequest("chrome-search://suggestion/loader.js",
142 true, kInstantRendererPID, 0));
143 EXPECT_TRUE(source()->ShouldServiceRequest(request.get()));
144 request.reset(MockRequest("chrome-search://suggestion/loader.js?p=1&v=0",
145 true, kInstantRendererPID, 0));
146 EXPECT_TRUE(source()->ShouldServiceRequest(request.get()));
147 request.reset(MockRequest("chrome-search://suggestion/loader.js",
148 true, kNonInstantRendererPID, 0));
149 EXPECT_FALSE(source()->ShouldServiceRequest(request.get()));
150 }
151
152 TEST_F(SuggestionSourceTest, GetMimeType) {
153 // URLDataManagerBackend does not include / in path_and_query.
154 EXPECT_EQ("text/html", source()->GetMimeType("loader.html"));
155 EXPECT_EQ("application/javascript",
156 source()->GetMimeType("loader.js?p=1&v=0"));
157 EXPECT_EQ("", source()->GetMimeType("bogus.html"));
158 }
159
160 TEST_F(SuggestionSourceTest, StartDataRequest) {
161 StartDataRequest("bogus.html");
162 EXPECT_TRUE(response_string().empty());
163 StartDataRequest("loader.html?p=1&v=0");
164 EXPECT_FALSE(response_string().empty());
165 EXPECT_EQ(std::string::npos, response_string().find(kInstantOrigin));
166 StartDataRequest("loader.js");
167 EXPECT_TRUE(response_string().empty());
168 StartDataRequest("loader.js?p=1&v=0");
169 EXPECT_FALSE(response_string().empty());
170 EXPECT_NE(std::string::npos, response_string().find(kInstantOrigin));
171 StartDataRequest("loader.js?p=0&v=0");
172 EXPECT_FALSE(response_string().empty());
173 EXPECT_NE(std::string::npos, response_string().find(kNonInstantOrigin));
174 StartDataRequest("loader.js?p=42&v=0");
175 EXPECT_TRUE(response_string().empty());
176 }
177
178 TEST_F(SuggestionSourceTest, WillServiceRequest) {
179 scoped_ptr<net::URLRequest> request;
180 std::string path;
181 request.reset(MockRequest("http://suggestion/loader.js", true, 0, 1));
182 path = "loader.js";
183 source()->WillServiceRequest(request.get(), &path);
184 EXPECT_EQ(std::string("loader.js?p=0&v=1"), path);
185 request.reset(MockRequest("http://suggestion/loader.js?p=10&v=20",
186 true, 0, 1));
187 path = "loader.js?p=10&v=20";
188 source()->WillServiceRequest(request.get(), &path);
189 EXPECT_EQ(std::string("loader.js?p=0&v=1"), path);
190 request.reset(MockRequest("http://suggestion/loader.js?p=10&v=20",
191 false, 0, 1));
192 path = "loader.js?p=10&v=20";
193 source()->WillServiceRequest(request.get(), &path);
194 EXPECT_EQ(std::string("loader.js?"), path);
195 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698