| OLD | NEW |
| (Empty) |
| 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 "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" | |
| 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(int devices_active, 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(const std::string& name, | |
| 45 net::URLRequestContextGetter* request_context) | |
| 46 : name_(name), request_context_(request_context) {} | |
| 47 | |
| 48 ~MockResolution() override {} | |
| 49 | |
| 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()))); | |
| 54 } | |
| 55 | |
| 56 void Start(const ResultCallback& callback) override { | |
| 57 Start(net::HostPortPair(), callback); | |
| 58 } | |
| 59 | |
| 60 const std::string& GetName() override { return name_; } | |
| 61 | |
| 62 private: | |
| 63 std::string name_; | |
| 64 scoped_refptr<net::URLRequestContextGetter> request_context_; | |
| 65 }; | |
| 66 | |
| 67 explicit MockPrivetHttpFactory(net::URLRequestContextGetter* request_context) | |
| 68 : request_context_(request_context) { | |
| 69 } | |
| 70 | |
| 71 scoped_ptr<PrivetHTTPResolution> CreatePrivetHTTP( | |
| 72 const std::string& name) override { | |
| 73 return scoped_ptr<PrivetHTTPResolution>( | |
| 74 new MockResolution(name, request_context_.get())); | |
| 75 } | |
| 76 | |
| 77 private: | |
| 78 scoped_refptr<net::URLRequestContextGetter> request_context_; | |
| 79 }; | |
| 80 | |
| 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_)); | |
| 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 1, | |
| 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 1, | |
| 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 | |
| OLD | NEW |