OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 <algorithm> |
| 6 |
| 7 #include "content/public/test/browser_test_utils.h" |
| 8 #include "content/public/test/content_browser_test.h" |
| 9 #include "content/public/test/content_browser_test_utils.h" |
| 10 #include "content/shell/browser/shell.h" |
| 11 #include "net/dns/host_resolver_proc.h" |
| 12 #include "net/dns/mock_host_resolver.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 class MockHostResolverProc : public net::HostResolverProc { |
| 18 public: |
| 19 MockHostResolverProc() : net::HostResolverProc(nullptr) {} |
| 20 |
| 21 int Resolve(const std::string& host, |
| 22 net::AddressFamily address_family, |
| 23 net::HostResolverFlags flags, |
| 24 net::AddressList* addresses, |
| 25 int* os_error) override { |
| 26 requests_.push_back(host); |
| 27 return 0; |
| 28 } |
| 29 |
| 30 size_t num_requests() { return requests_.size(); } |
| 31 |
| 32 bool host_requested(std::string host) { |
| 33 return std::find(requests_.begin(), requests_.end(), host) != |
| 34 requests_.end(); |
| 35 } |
| 36 |
| 37 private: |
| 38 ~MockHostResolverProc() override {} |
| 39 |
| 40 std::vector<std::string> requests_; |
| 41 }; |
| 42 |
| 43 class ResourceHintsBrowserTest : public ContentBrowserTest { |
| 44 public: |
| 45 ResourceHintsBrowserTest() |
| 46 : mock_host_resolver_proc_(new MockHostResolverProc), |
| 47 scoped_host_resolver_proc_(nullptr) {} |
| 48 |
| 49 protected: |
| 50 scoped_refptr<MockHostResolverProc> mock_host_resolver_proc_; |
| 51 std::unique_ptr<net::ScopedDefaultHostResolverProc> |
| 52 scoped_host_resolver_proc_; |
| 53 }; |
| 54 |
| 55 IN_PROC_BROWSER_TEST_F(ResourceHintsBrowserTest, DnsPrefetch) { |
| 56 scoped_host_resolver_proc_.reset( |
| 57 new net::ScopedDefaultHostResolverProc(mock_host_resolver_proc_.get())); |
| 58 ASSERT_TRUE(embedded_test_server()->Start()); |
| 59 |
| 60 EXPECT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL( |
| 61 "/resource_hints/dns_prefetch.html"))); |
| 62 EXPECT_TRUE( |
| 63 NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"))); |
| 64 |
| 65 EXPECT_EQ(mock_host_resolver_proc_->num_requests(), 1u); |
| 66 EXPECT_TRUE(mock_host_resolver_proc_->host_requested("chromium.org")); |
| 67 scoped_host_resolver_proc_.reset(); |
| 68 } |
| 69 |
| 70 IN_PROC_BROWSER_TEST_F(ResourceHintsBrowserTest, DnsPrefetchMany) { |
| 71 scoped_host_resolver_proc_.reset( |
| 72 new net::ScopedDefaultHostResolverProc(mock_host_resolver_proc_.get())); |
| 73 ASSERT_TRUE(embedded_test_server()->Start()); |
| 74 |
| 75 EXPECT_TRUE( |
| 76 NavigateToURL(shell(), embedded_test_server()->GetURL( |
| 77 "/resource_hints/dns_prefetch_many.html"))); |
| 78 EXPECT_TRUE( |
| 79 NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"))); |
| 80 |
| 81 EXPECT_EQ(mock_host_resolver_proc_->num_requests(), 4u); |
| 82 EXPECT_TRUE(mock_host_resolver_proc_->host_requested("chromium1.org")); |
| 83 EXPECT_TRUE(mock_host_resolver_proc_->host_requested("chromium2.org")); |
| 84 EXPECT_TRUE(mock_host_resolver_proc_->host_requested("chromium3.org")); |
| 85 EXPECT_TRUE(mock_host_resolver_proc_->host_requested("chromium4.org")); |
| 86 scoped_host_resolver_proc_.reset(); |
| 87 } |
| 88 |
| 89 } // namespace content |
OLD | NEW |