• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
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 "net/proxy_resolution/network_delegate_error_observer.h"
6 
7 #include "base/functional/bind.h"
8 #include "base/functional/callback_helpers.h"
9 #include "base/location.h"
10 #include "base/run_loop.h"
11 #include "base/task/single_thread_task_runner.h"
12 #include "base/test/task_environment.h"
13 #include "base/threading/thread.h"
14 #include "net/base/net_errors.h"
15 #include "net/base/network_delegate_impl.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/abseil-cpp/absl/types/optional.h"
18 
19 namespace net {
20 
21 namespace {
22 
23 class TestNetworkDelegate : public NetworkDelegateImpl {
24  public:
25   TestNetworkDelegate() = default;
26   ~TestNetworkDelegate() override = default;
27 
got_pac_error() const28   bool got_pac_error() const { return got_pac_error_; }
29 
30  private:
31   // NetworkDelegate implementation.
OnPACScriptError(int line_number,const std::u16string & error)32   void OnPACScriptError(int line_number, const std::u16string& error) override {
33     got_pac_error_ = true;
34   }
35 
36   bool got_pac_error_ = false;
37 };
38 
39 // Check that the OnPACScriptError method can be called from an arbitrary
40 // thread.
TEST(NetworkDelegateErrorObserverTest,CallOnThread)41 TEST(NetworkDelegateErrorObserverTest, CallOnThread) {
42   base::test::TaskEnvironment task_environment;
43   base::Thread thread("test_thread");
44   thread.Start();
45   TestNetworkDelegate network_delegate;
46   NetworkDelegateErrorObserver observer(
47       &network_delegate,
48       base::SingleThreadTaskRunner::GetCurrentDefault().get());
49   thread.task_runner()->PostTask(
50       FROM_HERE,
51       base::BindOnce(&NetworkDelegateErrorObserver::OnPACScriptError,
52                      base::Unretained(&observer), 42, std::u16string()));
53   thread.Stop();
54   base::RunLoop().RunUntilIdle();
55   ASSERT_TRUE(network_delegate.got_pac_error());
56 }
57 
58 // Check that passing a NULL network delegate works.
TEST(NetworkDelegateErrorObserverTest,NoDelegate)59 TEST(NetworkDelegateErrorObserverTest, NoDelegate) {
60   base::test::TaskEnvironment task_environment;
61   base::Thread thread("test_thread");
62   thread.Start();
63   NetworkDelegateErrorObserver observer(
64       nullptr, base::SingleThreadTaskRunner::GetCurrentDefault().get());
65   thread.task_runner()->PostTask(
66       FROM_HERE,
67       base::BindOnce(&NetworkDelegateErrorObserver::OnPACScriptError,
68                      base::Unretained(&observer), 42, std::u16string()));
69   thread.Stop();
70   base::RunLoop().RunUntilIdle();
71   // Shouldn't have crashed until here...
72 }
73 
74 }  // namespace
75 
76 }  // namespace net
77