Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / local_discovery / privet_notifications_unittest.cc
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.
4
5 #include "chrome/browser/local_discovery/privet_http_asynchronous_factory.h"
6
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"
13
14 using testing::StrictMock;
15
16 using ::testing::_;
17 using ::testing::SaveArg;
18
19 namespace local_discovery {
20
21 namespace {
22
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";
28
29 const char kInfoResponseUptime20[] = "{\"uptime\": 20}";
30 const char kInfoResponseUptime3600[] = "{\"uptime\": 3600}";
31 const char kInfoResponseNoUptime[] = "{}";
32
33 class MockPrivetNotificationsListenerDeleagate
34     : public PrivetNotificationsListener::Delegate {
35  public:
36   MOCK_METHOD2(PrivetNotify, void(bool multiple, bool added));
37   MOCK_METHOD0(PrivetRemoveNotification, void());
38 };
39
40 class MockPrivetHttpFactory : public PrivetHTTPAsynchronousFactory {
41  public:
42   class MockResolution : public PrivetHTTPResolution {
43    public:
44     MockResolution(
45         const std::string& name,
46         net::URLRequestContextGetter* request_context,
47         const ResultCallback& callback)
48         : name_(name), request_context_(request_context), callback_(callback) {
49     }
50
51     ~MockResolution() override {}
52
53     void Start() override {
54       callback_.Run(scoped_ptr<PrivetHTTPClient>(new PrivetHTTPClientImpl(
55           name_, net::HostPortPair("1.2.3.4", 8080), request_context_.get())));
56     }
57
58     const std::string& GetName() override { return name_; }
59
60    private:
61     std::string name_;
62     scoped_refptr<net::URLRequestContextGetter> request_context_;
63     ResultCallback callback_;
64   };
65
66   explicit MockPrivetHttpFactory(net::URLRequestContextGetter* request_context)
67       : request_context_(request_context) {
68   }
69
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));
76   }
77
78  private:
79     scoped_refptr<net::URLRequestContextGetter> request_context_;
80 };
81
82 class PrivetNotificationsListenerTest : public ::testing::Test {
83  public:
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())),
89         &mock_delegate_));
90
91     description_.name = kExampleDeviceHumanName;
92     description_.description = kExampleDeviceDescription;
93   }
94
95   virtual ~PrivetNotificationsListenerTest() {
96   }
97
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());
102
103     if (!fetcher || GURL(kDeviceInfoURL) != fetcher->GetOriginalURL())
104       return false;
105
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;
112   }
113
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_;
121 };
122
123 TEST_F(PrivetNotificationsListenerTest, DisappearReappearTest) {
124
125   EXPECT_CALL(mock_delegate_, PrivetNotify(
126       false,
127       true));
128
129   notification_listener_->DeviceChanged(
130       true,
131       kExampleDeviceName,
132       description_);
133
134   SuccessfulResponseToInfo(kInfoResponseUptime20);
135
136   EXPECT_CALL(mock_delegate_, PrivetRemoveNotification());
137
138   notification_listener_->DeviceRemoved(
139       kExampleDeviceName);
140
141   notification_listener_->DeviceChanged(
142       true,
143       kExampleDeviceName,
144       description_);
145
146   description_.id = kExampleDeviceID;
147
148   notification_listener_->DeviceChanged(
149       true,
150       kExampleDeviceName,
151       description_);
152 }
153
154 TEST_F(PrivetNotificationsListenerTest, RegisterTest) {
155   EXPECT_CALL(mock_delegate_, PrivetNotify(
156       false,
157       true));
158
159   notification_listener_->DeviceChanged(
160       true,
161       kExampleDeviceName,
162       description_);
163
164   SuccessfulResponseToInfo(kInfoResponseUptime20);
165
166   EXPECT_CALL(mock_delegate_, PrivetRemoveNotification());
167
168   description_.id = kExampleDeviceID;
169
170   notification_listener_->DeviceChanged(
171       true,
172       kExampleDeviceName,
173       description_);
174 }
175
176 TEST_F(PrivetNotificationsListenerTest, HighUptimeTest) {
177   notification_listener_->DeviceChanged(
178       true,
179       kExampleDeviceName,
180       description_);
181
182   SuccessfulResponseToInfo(kInfoResponseUptime3600);
183
184   description_.id = kExampleDeviceID;
185
186   notification_listener_->DeviceChanged(
187       true,
188       kExampleDeviceName,
189       description_);
190 }
191
192 TEST_F(PrivetNotificationsListenerTest, HTTPErrorTest) {
193   notification_listener_->DeviceChanged(
194       true,
195       kExampleDeviceName,
196       description_);
197
198   net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
199
200   fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
201                                             net::OK));
202   fetcher->set_response_code(200);
203   fetcher->delegate()->OnURLFetchComplete(fetcher);
204 }
205
206 TEST_F(PrivetNotificationsListenerTest, DictionaryErrorTest) {
207   notification_listener_->DeviceChanged(
208       true,
209       kExampleDeviceName,
210       description_);
211
212   SuccessfulResponseToInfo(kInfoResponseNoUptime);
213 }
214
215 }  // namespace
216
217 }  // namespace local_discovery