Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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/net/chrome_network_delegate.h" | |
| 6 | |
| 7 #include "base/memory/ref_counted.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "chrome/browser/extensions/event_router_forwarder.h" | |
| 11 #include "chrome/browser/prefs/pref_member.h" | |
| 12 #include "net/url_request/url_request_test_util.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // Returns a raw pointer that the caller takes ownership of. | |
| 18 ChromeNetworkDelegate* CreateNetworkDelegate() { | |
|
eroman
2012/08/14 16:41:07
[optional]: return a scoped_ptr<ChromeNetworkDeleg
Jói
2012/08/14 22:17:10
Done.
| |
| 19 scoped_refptr<extensions::EventRouterForwarder> forwarder( | |
| 20 new extensions::EventRouterForwarder()); | |
| 21 BooleanPrefMember pref_member; | |
| 22 return new ChromeNetworkDelegate( | |
| 23 forwarder.get(), NULL, NULL, NULL, NULL, &pref_member, NULL); | |
|
eroman
2012/08/14 16:41:07
Is passing &pref_member safe? That variable will b
Jói
2012/08/14 22:17:10
It's safe for now but the approach you suggested i
| |
| 24 } | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 class ChromeNetworkDelegateTest : public testing::Test { | |
| 29 protected: | |
| 30 virtual void SetUp() OVERRIDE { | |
| 31 never_throttle_requests_original_value_ = | |
| 32 ChromeNetworkDelegate::g_never_throttle_requests_; | |
| 33 ChromeNetworkDelegate::g_never_throttle_requests_ = false; | |
| 34 } | |
| 35 | |
| 36 virtual void TearDown() OVERRIDE { | |
| 37 ChromeNetworkDelegate::g_never_throttle_requests_ = | |
| 38 never_throttle_requests_original_value_; | |
| 39 } | |
| 40 | |
| 41 // Implementation moved here for access to private bits. | |
| 42 void NeverThrottleLogicImpl() { | |
| 43 scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate()); | |
| 44 | |
| 45 TestURLRequestContext context; | |
| 46 TestURLRequest extension_request( | |
| 47 GURL("http://example.com/"), NULL, &context); | |
| 48 extension_request.set_first_party_for_cookies( | |
| 49 GURL("chrome-extension://abcdef/bingo.html")); | |
| 50 TestURLRequest web_page_request( | |
| 51 GURL("http://example.com/"), NULL, &context); | |
| 52 web_page_request.set_first_party_for_cookies( | |
| 53 GURL("http://example.com/helloworld.html")); | |
| 54 | |
| 55 ASSERT_TRUE(delegate->OnCanThrottleRequest(extension_request)); | |
| 56 ASSERT_FALSE(delegate->OnCanThrottleRequest(web_page_request)); | |
| 57 | |
| 58 delegate->NeverThrottleRequests(); | |
| 59 ASSERT_TRUE(ChromeNetworkDelegate::g_never_throttle_requests_); | |
| 60 ASSERT_FALSE(delegate->OnCanThrottleRequest(extension_request)); | |
| 61 ASSERT_FALSE(delegate->OnCanThrottleRequest(web_page_request)); | |
| 62 | |
| 63 // Verify that the flag applies to later instances of the | |
| 64 // ChromeNetworkDelegate. | |
| 65 // | |
| 66 // We test the side effects of the flag rather than just the flag | |
| 67 // itself (which we did above) to help ensure that a changed | |
| 68 // implementation would show the same behavior, i.e. all instances | |
| 69 // of ChromeNetworkDelegate after the flag is set obey the flag. | |
| 70 scoped_ptr<ChromeNetworkDelegate> second_delegate(CreateNetworkDelegate()); | |
| 71 ASSERT_FALSE(delegate->OnCanThrottleRequest(extension_request)); | |
| 72 ASSERT_FALSE(delegate->OnCanThrottleRequest(web_page_request)); | |
| 73 } | |
| 74 | |
| 75 private: | |
| 76 bool never_throttle_requests_original_value_; | |
| 77 MessageLoopForIO message_loop_; | |
| 78 }; | |
| 79 | |
| 80 TEST_F(ChromeNetworkDelegateTest, NeverThrottleLogic) { | |
| 81 NeverThrottleLogicImpl(); | |
| 82 } | |
| OLD | NEW |