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/mojo_proxy_resolver_v8_tracing_bindings.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/threading/thread.h"
13 #include "net/test/event_waiter.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 class MojoProxyResolverV8TracingBindingsTest
: public testing::Test
{
24 MojoProxyResolverV8TracingBindingsTest() : weak_factory_(this) {}
26 void SetUp() override
{
27 bindings_
.reset(new MojoProxyResolverV8TracingBindings
<
28 MojoProxyResolverV8TracingBindingsTest
>(weak_factory_
.GetWeakPtr()));
31 void Alert(const mojo::String
& message
) {
32 alerts_
.push_back(message
.To
<std::string
>());
33 waiter_
.NotifyEvent(EVENT_ALERT
);
36 void OnError(int32_t line_number
, const mojo::String
& message
) {
37 errors_
.push_back(std::make_pair(line_number
, message
.To
<std::string
>()));
38 waiter_
.NotifyEvent(EVENT_ERROR
);
41 void ResolveDns(interfaces::HostResolverRequestInfoPtr request_info
,
42 interfaces::HostResolverRequestClientPtr client
) {}
44 void SendAlertAndError() {
45 bindings_
->Alert(base::ASCIIToUTF16("alert"));
46 bindings_
->OnError(-1, base::ASCIIToUTF16("error"));
50 scoped_ptr
<MojoProxyResolverV8TracingBindings
<
51 MojoProxyResolverV8TracingBindingsTest
>> bindings_
;
53 EventWaiter
<Event
> waiter_
;
55 std::vector
<std::string
> alerts_
;
56 std::vector
<std::pair
<int, std::string
>> errors_
;
58 base::WeakPtrFactory
<MojoProxyResolverV8TracingBindingsTest
> weak_factory_
;
61 TEST_F(MojoProxyResolverV8TracingBindingsTest
, Basic
) {
64 EXPECT_TRUE(bindings_
->GetHostResolver());
65 EXPECT_FALSE(bindings_
->GetBoundNetLog().net_log());
67 ASSERT_EQ(1u, alerts_
.size());
68 EXPECT_EQ("alert", alerts_
[0]);
69 ASSERT_EQ(1u, errors_
.size());
70 EXPECT_EQ(-1, errors_
[0].first
);
71 EXPECT_EQ("error", errors_
[0].second
);
74 TEST_F(MojoProxyResolverV8TracingBindingsTest
, CalledFromOtherThread
) {
75 base::Thread
thread("alert and error thread");
77 thread
.message_loop()->PostTask(
79 base::Bind(&MojoProxyResolverV8TracingBindingsTest::SendAlertAndError
,
80 base::Unretained(this)));
82 waiter_
.WaitForEvent(EVENT_ERROR
);
84 ASSERT_EQ(1u, alerts_
.size());
85 EXPECT_EQ("alert", alerts_
[0]);
86 ASSERT_EQ(1u, errors_
.size());
87 EXPECT_EQ(-1, errors_
[0].first
);
88 EXPECT_EQ("error", errors_
[0].second
);