Fix crash in SpeechRecognizerImpl introduced in AudioParams refactor.
[chromium-blink-merge.git] / net / proxy / network_delegate_error_observer_unittest.cc
blob3d8db2376d1526c9ffe81f92734fc4d392b8e7e9
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/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/threading/thread.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/network_delegate_impl.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace net {
19 namespace {
21 class TestNetworkDelegate : public NetworkDelegateImpl {
22 public:
23 TestNetworkDelegate() : got_pac_error_(false) {}
24 ~TestNetworkDelegate() override {}
26 bool got_pac_error() const { return got_pac_error_; }
28 private:
29 // NetworkDelegate implementation.
30 int OnBeforeURLRequest(URLRequest* request,
31 const CompletionCallback& callback,
32 GURL* new_url) override {
33 return OK;
35 int OnBeforeSendHeaders(URLRequest* request,
36 const CompletionCallback& callback,
37 HttpRequestHeaders* headers) override {
38 return OK;
40 void OnSendHeaders(URLRequest* request,
41 const HttpRequestHeaders& headers) override {}
42 int OnHeadersReceived(
43 URLRequest* request,
44 const CompletionCallback& callback,
45 const HttpResponseHeaders* original_response_headers,
46 scoped_refptr<HttpResponseHeaders>* override_response_headers,
47 GURL* allowed_unsafe_redirect_url) override {
48 return OK;
50 void OnBeforeRedirect(URLRequest* request,
51 const GURL& new_location) override {}
52 void OnResponseStarted(URLRequest* request) override {}
53 void OnCompleted(URLRequest* request, bool started) override {}
54 void OnURLRequestDestroyed(URLRequest* request) override {}
56 void OnPACScriptError(int line_number, const base::string16& error) override {
57 got_pac_error_ = true;
59 AuthRequiredResponse OnAuthRequired(URLRequest* request,
60 const AuthChallengeInfo& auth_info,
61 const AuthCallback& callback,
62 AuthCredentials* credentials) override {
63 return AUTH_REQUIRED_RESPONSE_NO_ACTION;
65 bool OnCanGetCookies(const URLRequest& request,
66 const CookieList& cookie_list) override {
67 return true;
69 bool OnCanSetCookie(const URLRequest& request,
70 const std::string& cookie_line,
71 CookieOptions* options) override {
72 return true;
74 bool OnCanAccessFile(const URLRequest& request,
75 const base::FilePath& path) const override {
76 return true;
79 bool got_pac_error_;
82 // Check that the OnPACScriptError method can be called from an arbitrary
83 // thread.
84 TEST(NetworkDelegateErrorObserverTest, CallOnThread) {
85 base::Thread thread("test_thread");
86 thread.Start();
87 TestNetworkDelegate network_delegate;
88 NetworkDelegateErrorObserver observer(
89 &network_delegate, base::ThreadTaskRunnerHandle::Get().get());
90 thread.task_runner()->PostTask(
91 FROM_HERE, base::Bind(&NetworkDelegateErrorObserver::OnPACScriptError,
92 base::Unretained(&observer), 42, base::string16()));
93 thread.Stop();
94 base::MessageLoop::current()->RunUntilIdle();
95 ASSERT_TRUE(network_delegate.got_pac_error());
98 // Check that passing a NULL network delegate works.
99 TEST(NetworkDelegateErrorObserverTest, NoDelegate) {
100 base::Thread thread("test_thread");
101 thread.Start();
102 NetworkDelegateErrorObserver observer(
103 NULL, base::ThreadTaskRunnerHandle::Get().get());
104 thread.task_runner()->PostTask(
105 FROM_HERE, base::Bind(&NetworkDelegateErrorObserver::OnPACScriptError,
106 base::Unretained(&observer), 42, base::string16()));
107 thread.Stop();
108 base::MessageLoop::current()->RunUntilIdle();
109 // Shouldn't have crashed until here...
112 } // namespace
114 } // namespace net