| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/printing/cloud_print/privet_notifications.h" | 5 #include "chrome/browser/printing/cloud_print/privet_notifications.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 } | 62 } |
| 63 | 63 |
| 64 const std::string& GetName() override { return name_; } | 64 const std::string& GetName() override { return name_; } |
| 65 | 65 |
| 66 private: | 66 private: |
| 67 std::string name_; | 67 std::string name_; |
| 68 scoped_refptr<net::URLRequestContextGetter> request_context_; | 68 scoped_refptr<net::URLRequestContextGetter> request_context_; |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 explicit MockPrivetHttpFactory(net::URLRequestContextGetter* request_context) | 71 explicit MockPrivetHttpFactory(net::URLRequestContextGetter* request_context) |
| 72 : request_context_(request_context) { | 72 : request_context_(request_context) {} |
| 73 } | |
| 74 | 73 |
| 75 std::unique_ptr<PrivetHTTPResolution> CreatePrivetHTTP( | 74 std::unique_ptr<PrivetHTTPResolution> CreatePrivetHTTP( |
| 76 const std::string& name) override { | 75 const std::string& name) override { |
| 77 return base::WrapUnique(new MockResolution(name, request_context_.get())); | 76 return base::WrapUnique(new MockResolution(name, request_context_.get())); |
| 78 } | 77 } |
| 79 | 78 |
| 80 private: | 79 private: |
| 81 scoped_refptr<net::URLRequestContextGetter> request_context_; | 80 scoped_refptr<net::URLRequestContextGetter> request_context_; |
| 82 }; | 81 }; |
| 83 | 82 |
| 84 class PrivetNotificationsListenerTest : public ::testing::Test { | 83 class PrivetNotificationsListenerTest : public ::testing::Test { |
| 85 public: | 84 public: |
| 86 PrivetNotificationsListenerTest() | 85 PrivetNotificationsListenerTest() |
| 87 : request_context_(new net::TestURLRequestContextGetter( | 86 : request_context_(new net::TestURLRequestContextGetter( |
| 88 base::ThreadTaskRunnerHandle::Get())) { | 87 base::ThreadTaskRunnerHandle::Get())) { |
| 89 notification_listener_.reset(new PrivetNotificationsListener( | 88 notification_listener_.reset(new PrivetNotificationsListener( |
| 90 std::unique_ptr<PrivetHTTPAsynchronousFactory>( | 89 std::unique_ptr<PrivetHTTPAsynchronousFactory>( |
| 91 new MockPrivetHttpFactory(request_context_.get())), | 90 new MockPrivetHttpFactory(request_context_.get())), |
| 92 &mock_delegate_)); | 91 &mock_delegate_)); |
| 93 | 92 |
| 94 description_.name = kExampleDeviceHumanName; | 93 description_.name = kExampleDeviceHumanName; |
| 95 description_.description = kExampleDeviceDescription; | 94 description_.description = kExampleDeviceDescription; |
| 96 } | 95 } |
| 97 | 96 |
| 98 virtual ~PrivetNotificationsListenerTest() { | 97 virtual ~PrivetNotificationsListenerTest() {} |
| 99 } | |
| 100 | 98 |
| 101 bool SuccessfulResponseToInfo(const std::string& response) { | 99 bool SuccessfulResponseToInfo(const std::string& response) { |
| 102 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | 100 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); |
| 103 EXPECT_TRUE(fetcher); | |
| 104 EXPECT_EQ(GURL(kDeviceInfoURL), fetcher->GetOriginalURL()); | |
| 105 | |
| 106 if (!fetcher || GURL(kDeviceInfoURL) != fetcher->GetOriginalURL()) | 101 if (!fetcher || GURL(kDeviceInfoURL) != fetcher->GetOriginalURL()) |
| 107 return false; | 102 return false; |
| 108 | 103 |
| 109 fetcher->SetResponseString(response); | 104 fetcher->SetResponseString(response); |
| 110 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, | 105 fetcher->set_status( |
| 111 net::OK)); | 106 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); |
| 112 fetcher->set_response_code(200); | 107 fetcher->set_response_code(200); |
| 113 fetcher->delegate()->OnURLFetchComplete(fetcher); | 108 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 114 return true; | 109 return true; |
| 115 } | 110 } |
| 116 | 111 |
| 117 protected: | 112 protected: |
| 118 StrictMock<MockPrivetNotificationsListenerDeleagate> mock_delegate_; | 113 StrictMock<MockPrivetNotificationsListenerDeleagate> mock_delegate_; |
| 119 std::unique_ptr<PrivetNotificationsListener> notification_listener_; | 114 std::unique_ptr<PrivetNotificationsListener> notification_listener_; |
| 120 base::MessageLoop message_loop_; | 115 base::MessageLoop message_loop_; |
| 121 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 116 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| 122 net::TestURLFetcherFactory fetcher_factory_; | 117 net::TestURLFetcherFactory fetcher_factory_; |
| 123 DeviceDescription description_; | 118 DeviceDescription description_; |
| 124 }; | 119 }; |
| 125 | 120 |
| 126 TEST_F(PrivetNotificationsListenerTest, DisappearReappearTest) { | 121 TEST_F(PrivetNotificationsListenerTest, DisappearReappearTest) { |
| 127 EXPECT_CALL(mock_delegate_, PrivetNotify( | 122 EXPECT_CALL(mock_delegate_, PrivetNotify(1, true)); |
| 128 1, | 123 notification_listener_->DeviceChanged(kExampleDeviceName, description_); |
| 129 true)); | 124 EXPECT_TRUE(SuccessfulResponseToInfo(kInfoResponseUptime20)); |
| 130 | |
| 131 notification_listener_->DeviceChanged( | |
| 132 kExampleDeviceName, | |
| 133 description_); | |
| 134 | |
| 135 SuccessfulResponseToInfo(kInfoResponseUptime20); | |
| 136 | 125 |
| 137 EXPECT_CALL(mock_delegate_, PrivetRemoveNotification()); | 126 EXPECT_CALL(mock_delegate_, PrivetRemoveNotification()); |
| 138 | 127 notification_listener_->DeviceRemoved(kExampleDeviceName); |
| 139 notification_listener_->DeviceRemoved( | 128 notification_listener_->DeviceChanged(kExampleDeviceName, description_); |
| 140 kExampleDeviceName); | |
| 141 | |
| 142 notification_listener_->DeviceChanged( | |
| 143 kExampleDeviceName, | |
| 144 description_); | |
| 145 | |
| 146 description_.id = kExampleDeviceID; | 129 description_.id = kExampleDeviceID; |
| 147 | 130 notification_listener_->DeviceChanged(kExampleDeviceName, description_); |
| 148 notification_listener_->DeviceChanged( | |
| 149 kExampleDeviceName, | |
| 150 description_); | |
| 151 } | 131 } |
| 152 | 132 |
| 153 TEST_F(PrivetNotificationsListenerTest, RegisterTest) { | 133 TEST_F(PrivetNotificationsListenerTest, RegisterTest) { |
| 154 EXPECT_CALL(mock_delegate_, PrivetNotify( | 134 EXPECT_CALL(mock_delegate_, PrivetNotify(1, true)); |
| 155 1, | 135 notification_listener_->DeviceChanged(kExampleDeviceName, description_); |
| 156 true)); | 136 EXPECT_TRUE(SuccessfulResponseToInfo(kInfoResponseUptime20)); |
| 157 | |
| 158 notification_listener_->DeviceChanged( | |
| 159 kExampleDeviceName, | |
| 160 description_); | |
| 161 | |
| 162 SuccessfulResponseToInfo(kInfoResponseUptime20); | |
| 163 | 137 |
| 164 EXPECT_CALL(mock_delegate_, PrivetRemoveNotification()); | 138 EXPECT_CALL(mock_delegate_, PrivetRemoveNotification()); |
| 139 description_.id = kExampleDeviceID; |
| 140 notification_listener_->DeviceChanged(kExampleDeviceName, description_); |
| 141 } |
| 165 | 142 |
| 166 description_.id = kExampleDeviceID; | 143 TEST_F(PrivetNotificationsListenerTest, RepeatedNotification) { |
| 144 EXPECT_CALL(mock_delegate_, PrivetNotify(1, true)); |
| 145 notification_listener_->DeviceChanged(kExampleDeviceName, description_); |
| 146 EXPECT_TRUE(SuccessfulResponseToInfo(kInfoResponseUptime20)); |
| 167 | 147 |
| 168 notification_listener_->DeviceChanged( | 148 EXPECT_CALL(mock_delegate_, PrivetNotify(_, _)).Times(0); |
| 169 kExampleDeviceName, | 149 notification_listener_->DeviceChanged(kExampleDeviceName, description_); |
| 170 description_); | 150 |
| 151 EXPECT_CALL(mock_delegate_, PrivetRemoveNotification()); |
| 152 notification_listener_->DeviceRemoved(kExampleDeviceName); |
| 153 |
| 154 EXPECT_CALL(mock_delegate_, PrivetNotify(_, _)).Times(0); |
| 155 notification_listener_->DeviceChanged(kExampleDeviceName, description_); |
| 156 |
| 157 EXPECT_CALL(mock_delegate_, PrivetRemoveNotification()).Times(0); |
| 158 notification_listener_->DeviceRemoved(kExampleDeviceName); |
| 171 } | 159 } |
| 172 | 160 |
| 173 TEST_F(PrivetNotificationsListenerTest, HighUptimeTest) { | 161 TEST_F(PrivetNotificationsListenerTest, HighUptimeTest) { |
| 174 notification_listener_->DeviceChanged( | 162 notification_listener_->DeviceChanged(kExampleDeviceName, description_); |
| 175 kExampleDeviceName, | 163 EXPECT_TRUE(SuccessfulResponseToInfo(kInfoResponseUptime3600)); |
| 176 description_); | |
| 177 | |
| 178 SuccessfulResponseToInfo(kInfoResponseUptime3600); | |
| 179 | |
| 180 description_.id = kExampleDeviceID; | 164 description_.id = kExampleDeviceID; |
| 181 | 165 notification_listener_->DeviceChanged(kExampleDeviceName, description_); |
| 182 notification_listener_->DeviceChanged( | |
| 183 kExampleDeviceName, | |
| 184 description_); | |
| 185 } | 166 } |
| 186 | 167 |
| 187 TEST_F(PrivetNotificationsListenerTest, HTTPErrorTest) { | 168 TEST_F(PrivetNotificationsListenerTest, HTTPErrorTest) { |
| 188 notification_listener_->DeviceChanged( | 169 notification_listener_->DeviceChanged(kExampleDeviceName, description_); |
| 189 kExampleDeviceName, | |
| 190 description_); | |
| 191 | |
| 192 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | 170 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); |
| 193 | 171 fetcher->set_status( |
| 194 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, | 172 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); |
| 195 net::OK)); | |
| 196 fetcher->set_response_code(200); | 173 fetcher->set_response_code(200); |
| 197 fetcher->delegate()->OnURLFetchComplete(fetcher); | 174 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 198 } | 175 } |
| 199 | 176 |
| 200 TEST_F(PrivetNotificationsListenerTest, DictionaryErrorTest) { | 177 TEST_F(PrivetNotificationsListenerTest, DictionaryErrorTest) { |
| 201 notification_listener_->DeviceChanged( | 178 notification_listener_->DeviceChanged(kExampleDeviceName, description_); |
| 202 kExampleDeviceName, | |
| 203 description_); | |
| 204 | |
| 205 SuccessfulResponseToInfo(kInfoResponseNoUptime); | 179 SuccessfulResponseToInfo(kInfoResponseNoUptime); |
| 206 } | 180 } |
| 207 | 181 |
| 208 } // namespace | 182 } // namespace |
| 209 | 183 |
| 210 } // namespace cloud_print | 184 } // namespace cloud_print |
| OLD | NEW |