OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "net/proxy/proxy_resolver_error_observer_mojo.h" |
| 6 |
| 7 #include <utility> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/threading/thread.h" |
| 13 #include "mojo/common/common_type_converters.h" |
| 14 #include "net/test/event_waiter.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace net { |
| 18 namespace { |
| 19 |
| 20 class ErrorObserverClient : public interfaces::ProxyResolverErrorObserver { |
| 21 public: |
| 22 enum Event { |
| 23 ERROR_RECEIVED, |
| 24 }; |
| 25 |
| 26 explicit ErrorObserverClient( |
| 27 mojo::InterfaceRequest<interfaces::ProxyResolverErrorObserver> request); |
| 28 |
| 29 EventWaiter<Event>& event_waiter() { return event_waiter_; } |
| 30 const std::vector<std::pair<int, base::string16>>& errors() const { |
| 31 return errors_; |
| 32 } |
| 33 |
| 34 private: |
| 35 void OnPacScriptError(int32_t line_number, |
| 36 const mojo::String& error) override; |
| 37 |
| 38 mojo::Binding<interfaces::ProxyResolverErrorObserver> binding_; |
| 39 EventWaiter<Event> event_waiter_; |
| 40 std::vector<std::pair<int, base::string16>> errors_; |
| 41 |
| 42 DISALLOW_COPY_AND_ASSIGN(ErrorObserverClient); |
| 43 }; |
| 44 |
| 45 ErrorObserverClient::ErrorObserverClient( |
| 46 mojo::InterfaceRequest<interfaces::ProxyResolverErrorObserver> request) |
| 47 : binding_(this, request.Pass()) { |
| 48 } |
| 49 |
| 50 void ErrorObserverClient::OnPacScriptError(int32_t line_number, |
| 51 const mojo::String& error) { |
| 52 errors_.push_back(std::make_pair(line_number, error.To<base::string16>())); |
| 53 event_waiter_.NotifyEvent(ERROR_RECEIVED); |
| 54 } |
| 55 |
| 56 } // namespace |
| 57 |
| 58 class ProxyResolverErrorObserverMojoTest : public testing::Test { |
| 59 public: |
| 60 ProxyResolverErrorObserver& error_observer() { return *error_observer_; } |
| 61 ErrorObserverClient& client() { return *client_; } |
| 62 |
| 63 private: |
| 64 void SetUp() override { |
| 65 interfaces::ProxyResolverErrorObserverPtr error_observer_ptr; |
| 66 client_.reset(new ErrorObserverClient(mojo::GetProxy(&error_observer_ptr))); |
| 67 error_observer_ = |
| 68 ProxyResolverErrorObserverMojo::Create(error_observer_ptr.Pass()); |
| 69 ASSERT_TRUE(error_observer_); |
| 70 } |
| 71 |
| 72 scoped_ptr<ErrorObserverClient> client_; |
| 73 scoped_ptr<ProxyResolverErrorObserver> error_observer_; |
| 74 }; |
| 75 |
| 76 TEST_F(ProxyResolverErrorObserverMojoTest, NullHandle) { |
| 77 EXPECT_FALSE(ProxyResolverErrorObserverMojo::Create( |
| 78 interfaces::ProxyResolverErrorObserverPtr())); |
| 79 } |
| 80 |
| 81 TEST_F(ProxyResolverErrorObserverMojoTest, ErrorReportedOnMainThread) { |
| 82 base::string16 error(base::ASCIIToUTF16("error message")); |
| 83 error_observer().OnPACScriptError(123, error); |
| 84 client().event_waiter().WaitForEvent(ErrorObserverClient::ERROR_RECEIVED); |
| 85 ASSERT_EQ(1u, client().errors().size()); |
| 86 EXPECT_EQ(123, client().errors()[0].first); |
| 87 EXPECT_EQ(error, client().errors()[0].second); |
| 88 } |
| 89 |
| 90 TEST_F(ProxyResolverErrorObserverMojoTest, ErrorReportedOnAnotherThread) { |
| 91 base::Thread other_thread("error reporting thread"); |
| 92 base::string16 error(base::ASCIIToUTF16("error message")); |
| 93 other_thread.Start(); |
| 94 other_thread.message_loop()->PostTask( |
| 95 FROM_HERE, base::Bind(&ProxyResolverErrorObserver::OnPACScriptError, |
| 96 base::Unretained(&error_observer()), 123, error)); |
| 97 client().event_waiter().WaitForEvent(ErrorObserverClient::ERROR_RECEIVED); |
| 98 ASSERT_EQ(1u, client().errors().size()); |
| 99 EXPECT_EQ(123, client().errors()[0].first); |
| 100 EXPECT_EQ(error, client().errors()[0].second); |
| 101 } |
| 102 |
| 103 } // namespace net |
OLD | NEW |