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