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