OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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/predictors/loading_data_collector.h" | |
6 | |
7 #include <iostream> | |
8 #include <memory> | |
9 #include <utility> | |
10 | |
11 #include "base/run_loop.h" | |
12 #include "base/test/histogram_tester.h" | |
13 #include "chrome/browser/history/history_service_factory.h" | |
14 #include "chrome/browser/predictors/loading_test_util.h" | |
15 #include "chrome/test/base/testing_profile.h" | |
16 #include "content/public/browser/resource_request_info.h" | |
17 #include "content/public/test/test_browser_thread_bundle.h" | |
18 #include "net/http/http_response_headers.h" | |
19 #include "net/url_request/url_request_context.h" | |
20 #include "net/url_request/url_request_job.h" | |
21 #include "net/url_request/url_request_test_util.h" | |
22 #include "testing/gmock/include/gmock/gmock.h" | |
23 #include "testing/gtest/include/gtest/gtest.h" | |
24 | |
25 using testing::StrictMock; | |
26 | |
27 namespace predictors { | |
28 | |
29 class LoadingDataCollectorTest : public testing::Test { | |
30 public: | |
31 LoadingDataCollectorTest() : thread_bundle_() {} | |
32 void SetUp() override { | |
33 url_request_context_.set_job_factory(&url_request_job_factory_); | |
Benoit L
2017/06/06 14:10:55
Actually, this code is a bit fragile.
Can you also
trevordixon
2017/06/07 09:14:27
OK. Not certain I did what you had in mind; have a
| |
34 } | |
35 | |
36 protected: | |
37 std::unique_ptr<net::URLRequest> CreateURLRequest( | |
38 const GURL& url, | |
39 net::RequestPriority priority, | |
40 content::ResourceType resource_type, | |
41 bool is_main_frame) { | |
42 std::unique_ptr<net::URLRequest> request = | |
43 url_request_context_.CreateRequest(url, priority, | |
44 &url_request_delegate_); | |
45 request->set_first_party_for_cookies(url); | |
46 content::ResourceRequestInfo::AllocateForTesting( | |
47 request.get(), resource_type, nullptr, -1, -1, -1, is_main_frame, false, | |
48 false, true, content::PREVIEWS_OFF); | |
49 request->Start(); | |
50 return request; | |
51 } | |
52 | |
53 content::TestBrowserThreadBundle thread_bundle_; | |
54 net::TestURLRequestContext url_request_context_; | |
55 MockURLRequestJobFactory url_request_job_factory_; | |
56 EmptyURLRequestDelegate url_request_delegate_; | |
57 }; | |
58 | |
59 TEST_F(LoadingDataCollectorTest, HandledResourceTypes) { | |
60 EXPECT_TRUE(LoadingDataCollector::IsHandledResourceType( | |
61 content::RESOURCE_TYPE_STYLESHEET, "bogus/mime-type")); | |
62 EXPECT_TRUE(LoadingDataCollector::IsHandledResourceType( | |
63 content::RESOURCE_TYPE_STYLESHEET, "")); | |
64 EXPECT_FALSE(LoadingDataCollector::IsHandledResourceType( | |
65 content::RESOURCE_TYPE_WORKER, "text/css")); | |
66 EXPECT_FALSE(LoadingDataCollector::IsHandledResourceType( | |
67 content::RESOURCE_TYPE_WORKER, "")); | |
68 EXPECT_TRUE(LoadingDataCollector::IsHandledResourceType( | |
69 content::RESOURCE_TYPE_PREFETCH, "text/css")); | |
70 EXPECT_FALSE(LoadingDataCollector::IsHandledResourceType( | |
71 content::RESOURCE_TYPE_PREFETCH, "bogus/mime-type")); | |
72 EXPECT_FALSE(LoadingDataCollector::IsHandledResourceType( | |
73 content::RESOURCE_TYPE_PREFETCH, "")); | |
74 EXPECT_TRUE(LoadingDataCollector::IsHandledResourceType( | |
75 content::RESOURCE_TYPE_PREFETCH, "application/font-woff")); | |
76 EXPECT_TRUE(LoadingDataCollector::IsHandledResourceType( | |
77 content::RESOURCE_TYPE_PREFETCH, "font/woff2")); | |
78 EXPECT_FALSE(LoadingDataCollector::IsHandledResourceType( | |
79 content::RESOURCE_TYPE_XHR, "")); | |
80 EXPECT_FALSE(LoadingDataCollector::IsHandledResourceType( | |
81 content::RESOURCE_TYPE_XHR, "bogus/mime-type")); | |
82 EXPECT_TRUE(LoadingDataCollector::IsHandledResourceType( | |
83 content::RESOURCE_TYPE_XHR, "application/javascript")); | |
84 } | |
85 | |
86 TEST_F(LoadingDataCollectorTest, ShouldRecordRequestMainFrame) { | |
87 std::unique_ptr<net::URLRequest> http_request = | |
88 CreateURLRequest(GURL("http://www.google.com"), net::MEDIUM, | |
89 content::RESOURCE_TYPE_IMAGE, true); | |
90 EXPECT_TRUE(LoadingDataCollector::ShouldRecordRequest( | |
91 http_request.get(), content::RESOURCE_TYPE_MAIN_FRAME)); | |
92 | |
93 std::unique_ptr<net::URLRequest> https_request = | |
94 CreateURLRequest(GURL("https://www.google.com"), net::MEDIUM, | |
95 content::RESOURCE_TYPE_IMAGE, true); | |
96 EXPECT_TRUE(LoadingDataCollector::ShouldRecordRequest( | |
97 https_request.get(), content::RESOURCE_TYPE_MAIN_FRAME)); | |
98 | |
99 std::unique_ptr<net::URLRequest> file_request = | |
100 CreateURLRequest(GURL("file://www.google.com"), net::MEDIUM, | |
101 content::RESOURCE_TYPE_IMAGE, true); | |
102 EXPECT_FALSE(LoadingDataCollector::ShouldRecordRequest( | |
103 file_request.get(), content::RESOURCE_TYPE_MAIN_FRAME)); | |
104 | |
105 std::unique_ptr<net::URLRequest> https_request_with_port = | |
106 CreateURLRequest(GURL("https://www.google.com:666"), net::MEDIUM, | |
107 content::RESOURCE_TYPE_IMAGE, true); | |
108 EXPECT_FALSE(LoadingDataCollector::ShouldRecordRequest( | |
109 https_request_with_port.get(), content::RESOURCE_TYPE_MAIN_FRAME)); | |
110 } | |
111 | |
112 TEST_F(LoadingDataCollectorTest, ShouldRecordRequestSubResource) { | |
113 std::unique_ptr<net::URLRequest> http_request = | |
114 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, | |
115 content::RESOURCE_TYPE_IMAGE, false); | |
116 EXPECT_FALSE(LoadingDataCollector::ShouldRecordRequest( | |
117 http_request.get(), content::RESOURCE_TYPE_IMAGE)); | |
118 | |
119 std::unique_ptr<net::URLRequest> https_request = | |
120 CreateURLRequest(GURL("https://www.google.com/cat.png"), net::MEDIUM, | |
121 content::RESOURCE_TYPE_IMAGE, false); | |
122 EXPECT_FALSE(LoadingDataCollector::ShouldRecordRequest( | |
123 https_request.get(), content::RESOURCE_TYPE_IMAGE)); | |
124 | |
125 std::unique_ptr<net::URLRequest> file_request = | |
126 CreateURLRequest(GURL("file://www.google.com/cat.png"), net::MEDIUM, | |
127 content::RESOURCE_TYPE_IMAGE, false); | |
128 EXPECT_FALSE(LoadingDataCollector::ShouldRecordRequest( | |
129 file_request.get(), content::RESOURCE_TYPE_IMAGE)); | |
130 | |
131 std::unique_ptr<net::URLRequest> https_request_with_port = | |
132 CreateURLRequest(GURL("https://www.google.com:666/cat.png"), net::MEDIUM, | |
133 content::RESOURCE_TYPE_IMAGE, false); | |
134 EXPECT_FALSE(LoadingDataCollector::ShouldRecordRequest( | |
135 https_request_with_port.get(), content::RESOURCE_TYPE_IMAGE)); | |
136 } | |
137 | |
138 TEST_F(LoadingDataCollectorTest, ShouldRecordResponseMainFrame) { | |
139 net::HttpResponseInfo response_info; | |
140 response_info.headers = MakeResponseHeaders(""); | |
141 url_request_job_factory_.set_response_info(response_info); | |
142 | |
143 std::unique_ptr<net::URLRequest> http_request = | |
144 CreateURLRequest(GURL("http://www.google.com"), net::MEDIUM, | |
145 content::RESOURCE_TYPE_MAIN_FRAME, true); | |
146 EXPECT_TRUE(LoadingDataCollector::ShouldRecordResponse(http_request.get())); | |
147 | |
148 std::unique_ptr<net::URLRequest> https_request = | |
149 CreateURLRequest(GURL("https://www.google.com"), net::MEDIUM, | |
150 content::RESOURCE_TYPE_MAIN_FRAME, true); | |
151 EXPECT_TRUE(LoadingDataCollector::ShouldRecordResponse(https_request.get())); | |
152 | |
153 std::unique_ptr<net::URLRequest> file_request = | |
154 CreateURLRequest(GURL("file://www.google.com"), net::MEDIUM, | |
155 content::RESOURCE_TYPE_MAIN_FRAME, true); | |
156 EXPECT_FALSE(LoadingDataCollector::ShouldRecordResponse(file_request.get())); | |
157 | |
158 std::unique_ptr<net::URLRequest> https_request_with_port = | |
159 CreateURLRequest(GURL("https://www.google.com:666"), net::MEDIUM, | |
160 content::RESOURCE_TYPE_MAIN_FRAME, true); | |
161 EXPECT_FALSE(LoadingDataCollector::ShouldRecordResponse( | |
162 https_request_with_port.get())); | |
163 } | |
164 | |
165 TEST_F(LoadingDataCollectorTest, ShouldRecordResponseSubresource) { | |
166 net::HttpResponseInfo response_info; | |
167 response_info.headers = | |
168 MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n"); | |
169 response_info.was_cached = true; | |
170 url_request_job_factory_.set_response_info(response_info); | |
171 | |
172 // Protocol. | |
173 std::unique_ptr<net::URLRequest> http_image_request = | |
174 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, | |
175 content::RESOURCE_TYPE_IMAGE, true); | |
176 EXPECT_TRUE( | |
177 LoadingDataCollector::ShouldRecordResponse(http_image_request.get())); | |
178 | |
179 std::unique_ptr<net::URLRequest> https_image_request = | |
180 CreateURLRequest(GURL("https://www.google.com/cat.png"), net::MEDIUM, | |
181 content::RESOURCE_TYPE_IMAGE, true); | |
182 EXPECT_TRUE( | |
183 LoadingDataCollector::ShouldRecordResponse(https_image_request.get())); | |
184 | |
185 std::unique_ptr<net::URLRequest> https_image_request_with_port = | |
186 CreateURLRequest(GURL("https://www.google.com:666/cat.png"), net::MEDIUM, | |
187 content::RESOURCE_TYPE_IMAGE, true); | |
188 EXPECT_FALSE(LoadingDataCollector::ShouldRecordResponse( | |
189 https_image_request_with_port.get())); | |
190 | |
191 std::unique_ptr<net::URLRequest> file_image_request = | |
192 CreateURLRequest(GURL("file://www.google.com/cat.png"), net::MEDIUM, | |
193 content::RESOURCE_TYPE_IMAGE, true); | |
194 EXPECT_FALSE( | |
195 LoadingDataCollector::ShouldRecordResponse(file_image_request.get())); | |
196 | |
197 // ResourceType. | |
198 std::unique_ptr<net::URLRequest> sub_frame_request = | |
199 CreateURLRequest(GURL("http://www.google.com/frame.html"), net::MEDIUM, | |
200 content::RESOURCE_TYPE_SUB_FRAME, true); | |
201 EXPECT_FALSE( | |
202 LoadingDataCollector::ShouldRecordResponse(sub_frame_request.get())); | |
203 | |
204 std::unique_ptr<net::URLRequest> font_request = | |
205 CreateURLRequest(GURL("http://www.google.com/comic-sans-ms.woff"), | |
206 net::MEDIUM, content::RESOURCE_TYPE_FONT_RESOURCE, true); | |
207 EXPECT_TRUE(LoadingDataCollector::ShouldRecordResponse(font_request.get())); | |
208 | |
209 // From MIME Type. | |
210 url_request_job_factory_.set_mime_type("image/png"); | |
211 std::unique_ptr<net::URLRequest> prefetch_image_request = | |
212 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, | |
213 content::RESOURCE_TYPE_PREFETCH, true); | |
214 EXPECT_TRUE( | |
215 LoadingDataCollector::ShouldRecordResponse(prefetch_image_request.get())); | |
216 | |
217 url_request_job_factory_.set_mime_type("image/my-wonderful-format"); | |
218 std::unique_ptr<net::URLRequest> prefetch_unknown_image_request = | |
219 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, | |
220 content::RESOURCE_TYPE_PREFETCH, true); | |
221 EXPECT_FALSE(LoadingDataCollector::ShouldRecordResponse( | |
222 prefetch_unknown_image_request.get())); | |
223 | |
224 url_request_job_factory_.set_mime_type("font/woff"); | |
225 std::unique_ptr<net::URLRequest> prefetch_font_request = | |
226 CreateURLRequest(GURL("http://www.google.com/comic-sans-ms.woff"), | |
227 net::MEDIUM, content::RESOURCE_TYPE_PREFETCH, true); | |
228 EXPECT_TRUE( | |
229 LoadingDataCollector::ShouldRecordResponse(prefetch_font_request.get())); | |
230 | |
231 url_request_job_factory_.set_mime_type("font/woff-woff"); | |
232 std::unique_ptr<net::URLRequest> prefetch_unknown_font_request = | |
233 CreateURLRequest(GURL("http://www.google.com/comic-sans-ms.woff"), | |
234 net::MEDIUM, content::RESOURCE_TYPE_PREFETCH, true); | |
235 EXPECT_FALSE(LoadingDataCollector::ShouldRecordResponse( | |
236 prefetch_unknown_font_request.get())); | |
237 | |
238 // Not main frame. | |
239 std::unique_ptr<net::URLRequest> font_request_sub_frame = CreateURLRequest( | |
240 GURL("http://www.google.com/comic-sans-ms.woff"), net::MEDIUM, | |
241 content::RESOURCE_TYPE_FONT_RESOURCE, false); | |
242 EXPECT_FALSE( | |
243 LoadingDataCollector::ShouldRecordResponse(font_request_sub_frame.get())); | |
244 } | |
245 | |
246 } // namespace predictors | |
OLD | NEW |