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 "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(bool multiple, bool added));
37 MOCK_METHOD0(PrivetRemoveNotification, void());
40 class MockPrivetHttpFactory : public PrivetHTTPAsynchronousFactory {
42 class MockResolution : public PrivetHTTPResolution {
45 const std::string& name,
46 net::URLRequestContextGetter* request_context,
47 const ResultCallback& callback)
48 : name_(name), request_context_(request_context), callback_(callback) {
51 ~MockResolution() override {}
53 void Start() override {
54 callback_.Run(scoped_ptr<PrivetHTTPClient>(new PrivetHTTPClientImpl(
55 name_, net::HostPortPair("1.2.3.4", 8080), request_context_.get())));
58 const std::string& GetName() override { return name_; }
62 scoped_refptr<net::URLRequestContextGetter> request_context_;
63 ResultCallback callback_;
66 explicit MockPrivetHttpFactory(net::URLRequestContextGetter* request_context)
67 : request_context_(request_context) {
70 scoped_ptr<PrivetHTTPResolution> CreatePrivetHTTP(
71 const std::string& name,
72 const net::HostPortPair& address,
73 const ResultCallback& callback) override {
74 return scoped_ptr<PrivetHTTPResolution>(
75 new MockResolution(name, request_context_.get(), callback));
79 scoped_refptr<net::URLRequestContextGetter> request_context_;
82 class PrivetNotificationsListenerTest : public ::testing::Test {
84 PrivetNotificationsListenerTest() : request_context_(
85 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current())) {
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