1 // Copyright 2013 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 "base/thread_task_runner_handle.h"
6 #include "chrome/browser/local_discovery/privet_http_asynchronous_factory.h"
7 #include "chrome/browser/local_discovery/privet_http_impl.h"
8 #include "chrome/browser/local_discovery/privet_notifications.h"
9 #include "net/url_request/test_url_fetcher_factory.h"
10 #include "net/url_request/url_request_test_util.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using testing::StrictMock
;
17 using ::testing::SaveArg
;
19 namespace local_discovery
{
23 const char kExampleDeviceName
[] = "test._privet._tcp.local";
24 const char kExampleDeviceHumanName
[] = "Test device";
25 const char kExampleDeviceDescription
[] = "Testing testing";
26 const char kExampleDeviceID
[] = "__test__id";
27 const char kDeviceInfoURL
[] = "http://1.2.3.4:8080/privet/info";
29 const char kInfoResponseUptime20
[] = "{\"uptime\": 20}";
30 const char kInfoResponseUptime3600
[] = "{\"uptime\": 3600}";
31 const char kInfoResponseNoUptime
[] = "{}";
33 class MockPrivetNotificationsListenerDeleagate
34 : public PrivetNotificationsListener::Delegate
{
36 MOCK_METHOD2(PrivetNotify
, void(int devices_active
, bool added
));
37 MOCK_METHOD0(PrivetRemoveNotification
, void());
40 class MockPrivetHttpFactory
: public PrivetHTTPAsynchronousFactory
{
42 class MockResolution
: public PrivetHTTPResolution
{
44 MockResolution(const std::string
& name
,
45 net::URLRequestContextGetter
* request_context
)
46 : name_(name
), request_context_(request_context
) {}
48 ~MockResolution() override
{}
50 void Start(const net::HostPortPair
& address
,
51 const ResultCallback
& callback
) override
{
52 callback
.Run(scoped_ptr
<PrivetHTTPClient
>(new PrivetHTTPClientImpl(
53 name_
, net::HostPortPair("1.2.3.4", 8080), request_context_
.get())));
56 void Start(const ResultCallback
& callback
) override
{
57 Start(net::HostPortPair(), callback
);
60 const std::string
& GetName() override
{ return name_
; }
64 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
67 explicit MockPrivetHttpFactory(net::URLRequestContextGetter
* request_context
)
68 : request_context_(request_context
) {
71 scoped_ptr
<PrivetHTTPResolution
> CreatePrivetHTTP(
72 const std::string
& name
) override
{
73 return scoped_ptr
<PrivetHTTPResolution
>(
74 new MockResolution(name
, request_context_
.get()));
78 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
81 class PrivetNotificationsListenerTest
: public ::testing::Test
{
83 PrivetNotificationsListenerTest()
84 : request_context_(new net::TestURLRequestContextGetter(
85 base::ThreadTaskRunnerHandle::Get())) {
86 notification_listener_
.reset(new PrivetNotificationsListener(
87 scoped_ptr
<PrivetHTTPAsynchronousFactory
>(
88 new MockPrivetHttpFactory(request_context_
.get())),
91 description_
.name
= kExampleDeviceHumanName
;
92 description_
.description
= kExampleDeviceDescription
;
95 virtual ~PrivetNotificationsListenerTest() {
98 bool SuccessfulResponseToInfo(const std::string
& response
) {
99 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
100 EXPECT_TRUE(fetcher
);
101 EXPECT_EQ(GURL(kDeviceInfoURL
), fetcher
->GetOriginalURL());
103 if (!fetcher
|| GURL(kDeviceInfoURL
) != fetcher
->GetOriginalURL())
106 fetcher
->SetResponseString(response
);
107 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
109 fetcher
->set_response_code(200);
110 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
115 StrictMock
<MockPrivetNotificationsListenerDeleagate
> mock_delegate_
;
116 scoped_ptr
<PrivetNotificationsListener
> notification_listener_
;
117 base::MessageLoop message_loop_
;
118 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_
;
119 net::TestURLFetcherFactory fetcher_factory_
;
120 DeviceDescription description_
;
123 TEST_F(PrivetNotificationsListenerTest
, DisappearReappearTest
) {
125 EXPECT_CALL(mock_delegate_
, PrivetNotify(
129 notification_listener_
->DeviceChanged(
134 SuccessfulResponseToInfo(kInfoResponseUptime20
);
136 EXPECT_CALL(mock_delegate_
, PrivetRemoveNotification());
138 notification_listener_
->DeviceRemoved(
141 notification_listener_
->DeviceChanged(
146 description_
.id
= kExampleDeviceID
;
148 notification_listener_
->DeviceChanged(
154 TEST_F(PrivetNotificationsListenerTest
, RegisterTest
) {
155 EXPECT_CALL(mock_delegate_
, PrivetNotify(
159 notification_listener_
->DeviceChanged(
164 SuccessfulResponseToInfo(kInfoResponseUptime20
);
166 EXPECT_CALL(mock_delegate_
, PrivetRemoveNotification());
168 description_
.id
= kExampleDeviceID
;
170 notification_listener_
->DeviceChanged(
176 TEST_F(PrivetNotificationsListenerTest
, HighUptimeTest
) {
177 notification_listener_
->DeviceChanged(
182 SuccessfulResponseToInfo(kInfoResponseUptime3600
);
184 description_
.id
= kExampleDeviceID
;
186 notification_listener_
->DeviceChanged(
192 TEST_F(PrivetNotificationsListenerTest
, HTTPErrorTest
) {
193 notification_listener_
->DeviceChanged(
198 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
200 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
202 fetcher
->set_response_code(200);
203 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
206 TEST_F(PrivetNotificationsListenerTest
, DictionaryErrorTest
) {
207 notification_listener_
->DeviceChanged(
212 SuccessfulResponseToInfo(kInfoResponseNoUptime
);
217 } // namespace local_discovery