Enables compositing support for webview.
[chromium-blink-merge.git] / net / proxy / network_delegate_error_observer_unittest.cc
blob8b2b51ceffdf1a28abd3c02b06dc2a10dd596a32
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.
5 #include "net/proxy/network_delegate_error_observer.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/message_loop_proxy.h"
10 #include "base/threading/thread.h"
11 #include "net/base/net_errors.h"
12 #include "net/base/network_delegate.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace net {
17 namespace {
19 class TestNetworkDelegate : public net::NetworkDelegate {
20 public:
21 TestNetworkDelegate() : got_pac_error_(false) {}
22 virtual ~TestNetworkDelegate() {}
24 bool got_pac_error() const { return got_pac_error_; }
26 private:
27 // net::NetworkDelegate implementation.
28 virtual int OnBeforeURLRequest(URLRequest* request,
29 const CompletionCallback& callback,
30 GURL* new_url) OVERRIDE {
31 return OK;
33 virtual int OnBeforeSendHeaders(URLRequest* request,
34 const CompletionCallback& callback,
35 HttpRequestHeaders* headers) OVERRIDE {
36 return OK;
38 virtual void OnSendHeaders(URLRequest* request,
39 const HttpRequestHeaders& headers) OVERRIDE {}
40 virtual int OnHeadersReceived(
41 URLRequest* request,
42 const CompletionCallback& callback,
43 const HttpResponseHeaders* original_response_headers,
44 scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE {
45 return net::OK;
47 virtual void OnBeforeRedirect(URLRequest* request,
48 const GURL& new_location) OVERRIDE {}
49 virtual void OnResponseStarted(URLRequest* request) OVERRIDE {}
50 virtual void OnRawBytesRead(const URLRequest& request,
51 int bytes_read) OVERRIDE {}
52 virtual void OnCompleted(URLRequest* request, bool started) OVERRIDE {}
53 virtual void OnURLRequestDestroyed(URLRequest* request) OVERRIDE {}
55 virtual void OnPACScriptError(int line_number,
56 const string16& error) OVERRIDE {
57 got_pac_error_ = true;
59 virtual AuthRequiredResponse OnAuthRequired(
60 URLRequest* request,
61 const AuthChallengeInfo& auth_info,
62 const AuthCallback& callback,
63 AuthCredentials* credentials) OVERRIDE {
64 return AUTH_REQUIRED_RESPONSE_NO_ACTION;
66 virtual bool OnCanGetCookies(const URLRequest& request,
67 const CookieList& cookie_list) OVERRIDE {
68 return true;
70 virtual bool OnCanSetCookie(const URLRequest& request,
71 const std::string& cookie_line,
72 CookieOptions* options) OVERRIDE {
73 return true;
75 virtual bool OnCanAccessFile(const net::URLRequest& request,
76 const FilePath& path) const OVERRIDE {
77 return true;
79 virtual bool OnCanThrottleRequest(const URLRequest& request) const OVERRIDE {
80 return false;
82 virtual int OnBeforeSocketStreamConnect(
83 SocketStream* stream,
84 const CompletionCallback& callback) OVERRIDE {
85 return OK;
87 virtual void OnRequestWaitStateChange(const net::URLRequest& request,
88 RequestWaitState state) OVERRIDE {
91 bool got_pac_error_;
94 } // namespace
96 // Check that the OnPACScriptError method can be called from an arbitrary
97 // thread.
98 TEST(NetworkDelegateErrorObserverTest, CallOnThread) {
99 base::Thread thread("test_thread");
100 thread.Start();
101 TestNetworkDelegate network_delegate;
102 NetworkDelegateErrorObserver
103 observer(&network_delegate,
104 base::MessageLoopProxy::current());
105 thread.message_loop()->PostTask(
106 FROM_HERE,
107 base::Bind(&NetworkDelegateErrorObserver::OnPACScriptError,
108 base::Unretained(&observer), 42, string16()));
109 thread.Stop();
110 MessageLoop::current()->RunUntilIdle();
111 ASSERT_TRUE(network_delegate.got_pac_error());
114 // Check that passing a NULL network delegate works.
115 TEST(NetworkDelegateErrorObserverTest, NoDelegate) {
116 base::Thread thread("test_thread");
117 thread.Start();
118 NetworkDelegateErrorObserver
119 observer(NULL, base::MessageLoopProxy::current());
120 thread.message_loop()->PostTask(
121 FROM_HERE,
122 base::Bind(&NetworkDelegateErrorObserver::OnPACScriptError,
123 base::Unretained(&observer), 42, string16()));
124 thread.Stop();
125 MessageLoop::current()->RunUntilIdle();
126 // Shouldn't have crashed until here...
129 } // namespace net