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/location.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/threading/thread.h"
15 #include "mojo/common/common_type_converters.h"
16 #include "net/test/event_waiter.h"
17 #include "testing/gtest/include/gtest/gtest.h"
22 class ErrorObserverClient
: public interfaces::ProxyResolverErrorObserver
{
28 explicit ErrorObserverClient(
29 mojo::InterfaceRequest
<interfaces::ProxyResolverErrorObserver
> request
);
31 EventWaiter
<Event
>& event_waiter() { return event_waiter_
; }
32 const std::vector
<std::pair
<int, base::string16
>>& errors() const {
37 void OnPacScriptError(int32_t line_number
,
38 const mojo::String
& error
) override
;
40 mojo::Binding
<interfaces::ProxyResolverErrorObserver
> binding_
;
41 EventWaiter
<Event
> event_waiter_
;
42 std::vector
<std::pair
<int, base::string16
>> errors_
;
44 DISALLOW_COPY_AND_ASSIGN(ErrorObserverClient
);
47 ErrorObserverClient::ErrorObserverClient(
48 mojo::InterfaceRequest
<interfaces::ProxyResolverErrorObserver
> request
)
49 : binding_(this, request
.Pass()) {
52 void ErrorObserverClient::OnPacScriptError(int32_t line_number
,
53 const mojo::String
& error
) {
54 errors_
.push_back(std::make_pair(line_number
, error
.To
<base::string16
>()));
55 event_waiter_
.NotifyEvent(ERROR_RECEIVED
);
60 class ProxyResolverErrorObserverMojoTest
: public testing::Test
{
62 ProxyResolverErrorObserver
& error_observer() { return *error_observer_
; }
63 ErrorObserverClient
& client() { return *client_
; }
66 void SetUp() override
{
67 interfaces::ProxyResolverErrorObserverPtr error_observer_ptr
;
68 client_
.reset(new ErrorObserverClient(mojo::GetProxy(&error_observer_ptr
)));
70 ProxyResolverErrorObserverMojo::Create(error_observer_ptr
.Pass());
71 ASSERT_TRUE(error_observer_
);
74 scoped_ptr
<ErrorObserverClient
> client_
;
75 scoped_ptr
<ProxyResolverErrorObserver
> error_observer_
;
78 TEST_F(ProxyResolverErrorObserverMojoTest
, NullHandle
) {
79 EXPECT_FALSE(ProxyResolverErrorObserverMojo::Create(
80 interfaces::ProxyResolverErrorObserverPtr()));
83 TEST_F(ProxyResolverErrorObserverMojoTest
, ErrorReportedOnMainThread
) {
84 base::string16
error(base::ASCIIToUTF16("error message"));
85 error_observer().OnPACScriptError(123, error
);
86 client().event_waiter().WaitForEvent(ErrorObserverClient::ERROR_RECEIVED
);
87 ASSERT_EQ(1u, client().errors().size());
88 EXPECT_EQ(123, client().errors()[0].first
);
89 EXPECT_EQ(error
, client().errors()[0].second
);
92 TEST_F(ProxyResolverErrorObserverMojoTest
, ErrorReportedOnAnotherThread
) {
93 base::Thread
other_thread("error reporting thread");
94 base::string16
error(base::ASCIIToUTF16("error message"));
96 other_thread
.task_runner()->PostTask(
97 FROM_HERE
, base::Bind(&ProxyResolverErrorObserver::OnPACScriptError
,
98 base::Unretained(&error_observer()), 123, error
));
99 client().event_waiter().WaitForEvent(ErrorObserverClient::ERROR_RECEIVED
);
100 ASSERT_EQ(1u, client().errors().size());
101 EXPECT_EQ(123, client().errors()[0].first
);
102 EXPECT_EQ(error
, client().errors()[0].second
);