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.
5 #include "net/proxy/proxy_resolver_error_observer_mojo.h"
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"
20 class ErrorObserverClient
: public interfaces::ProxyResolverErrorObserver
{
26 explicit ErrorObserverClient(
27 mojo::InterfaceRequest
<interfaces::ProxyResolverErrorObserver
> request
);
29 EventWaiter
<Event
>& event_waiter() { return event_waiter_
; }
30 const std::vector
<std::pair
<int, base::string16
>>& errors() const {
35 void OnPacScriptError(int32_t line_number
,
36 const mojo::String
& error
) override
;
38 mojo::Binding
<interfaces::ProxyResolverErrorObserver
> binding_
;
39 EventWaiter
<Event
> event_waiter_
;
40 std::vector
<std::pair
<int, base::string16
>> errors_
;
42 DISALLOW_COPY_AND_ASSIGN(ErrorObserverClient
);
45 ErrorObserverClient::ErrorObserverClient(
46 mojo::InterfaceRequest
<interfaces::ProxyResolverErrorObserver
> request
)
47 : binding_(this, request
.Pass()) {
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
);
58 class ProxyResolverErrorObserverMojoTest
: public testing::Test
{
60 ProxyResolverErrorObserver
& error_observer() { return *error_observer_
; }
61 ErrorObserverClient
& client() { return *client_
; }
64 void SetUp() override
{
65 interfaces::ProxyResolverErrorObserverPtr error_observer_ptr
;
66 client_
.reset(new ErrorObserverClient(mojo::GetProxy(&error_observer_ptr
)));
68 ProxyResolverErrorObserverMojo::Create(error_observer_ptr
.Pass());
69 ASSERT_TRUE(error_observer_
);
72 scoped_ptr
<ErrorObserverClient
> client_
;
73 scoped_ptr
<ProxyResolverErrorObserver
> error_observer_
;
76 TEST_F(ProxyResolverErrorObserverMojoTest
, NullHandle
) {
77 EXPECT_FALSE(ProxyResolverErrorObserverMojo::Create(
78 interfaces::ProxyResolverErrorObserverPtr()));
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
);
90 TEST_F(ProxyResolverErrorObserverMojoTest
, ErrorReportedOnAnotherThread
) {
91 base::Thread
other_thread("error reporting thread");
92 base::string16
error(base::ASCIIToUTF16("error message"));
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
);