Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / local_discovery / privet_notifications_unittest.cc
blobb4c437007380d9ab18fc6ac46927ae220e7ee611
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;
16 using ::testing::_;
17 using ::testing::SaveArg;
19 namespace local_discovery {
21 namespace {
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 {
35 public:
36 MOCK_METHOD2(PrivetNotify, void(int devices_active, bool added));
37 MOCK_METHOD0(PrivetRemoveNotification, void());
40 class MockPrivetHttpFactory : public PrivetHTTPAsynchronousFactory {
41 public:
42 class MockResolution : public PrivetHTTPResolution {
43 public:
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_; }
62 private:
63 std::string 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()));
77 private:
78 scoped_refptr<net::URLRequestContextGetter> request_context_;
81 class PrivetNotificationsListenerTest : public ::testing::Test {
82 public:
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())),
89 &mock_delegate_));
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())
104 return false;
106 fetcher->SetResponseString(response);
107 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
108 net::OK));
109 fetcher->set_response_code(200);
110 fetcher->delegate()->OnURLFetchComplete(fetcher);
111 return true;
114 protected:
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(
127 true));
129 notification_listener_->DeviceChanged(
130 true,
131 kExampleDeviceName,
132 description_);
134 SuccessfulResponseToInfo(kInfoResponseUptime20);
136 EXPECT_CALL(mock_delegate_, PrivetRemoveNotification());
138 notification_listener_->DeviceRemoved(
139 kExampleDeviceName);
141 notification_listener_->DeviceChanged(
142 true,
143 kExampleDeviceName,
144 description_);
146 description_.id = kExampleDeviceID;
148 notification_listener_->DeviceChanged(
149 true,
150 kExampleDeviceName,
151 description_);
154 TEST_F(PrivetNotificationsListenerTest, RegisterTest) {
155 EXPECT_CALL(mock_delegate_, PrivetNotify(
157 true));
159 notification_listener_->DeviceChanged(
160 true,
161 kExampleDeviceName,
162 description_);
164 SuccessfulResponseToInfo(kInfoResponseUptime20);
166 EXPECT_CALL(mock_delegate_, PrivetRemoveNotification());
168 description_.id = kExampleDeviceID;
170 notification_listener_->DeviceChanged(
171 true,
172 kExampleDeviceName,
173 description_);
176 TEST_F(PrivetNotificationsListenerTest, HighUptimeTest) {
177 notification_listener_->DeviceChanged(
178 true,
179 kExampleDeviceName,
180 description_);
182 SuccessfulResponseToInfo(kInfoResponseUptime3600);
184 description_.id = kExampleDeviceID;
186 notification_listener_->DeviceChanged(
187 true,
188 kExampleDeviceName,
189 description_);
192 TEST_F(PrivetNotificationsListenerTest, HTTPErrorTest) {
193 notification_listener_->DeviceChanged(
194 true,
195 kExampleDeviceName,
196 description_);
198 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
200 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
201 net::OK));
202 fetcher->set_response_code(200);
203 fetcher->delegate()->OnURLFetchComplete(fetcher);
206 TEST_F(PrivetNotificationsListenerTest, DictionaryErrorTest) {
207 notification_listener_->DeviceChanged(
208 true,
209 kExampleDeviceName,
210 description_);
212 SuccessfulResponseToInfo(kInfoResponseNoUptime);
215 } // namespace
217 } // namespace local_discovery