| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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/geolocation/gateway_data_provider_common.h" | |
| 6 | |
| 7 #include "base/scoped_ptr.h" | |
| 8 #include "base/string_util.h" | |
| 9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "testing/gmock/include/gmock/gmock.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 using testing::_; | |
| 15 using testing::AtLeast; | |
| 16 using testing::DoDefault; | |
| 17 using testing::Invoke; | |
| 18 using testing::Return; | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 class MockGatewayApi : public GatewayDataProviderCommon::GatewayApiInterface { | |
| 23 public: | |
| 24 MockGatewayApi() { | |
| 25 ON_CALL(*this, GetRouterData(_)) | |
| 26 .WillByDefault(Invoke(this, &MockGatewayApi::GetRouterDataInternal)); | |
| 27 } | |
| 28 | |
| 29 MOCK_METHOD1(GetRouterData, bool(GatewayData::RouterDataSet* data)); | |
| 30 | |
| 31 GatewayData::RouterDataSet data_out_; | |
| 32 | |
| 33 private: | |
| 34 bool GetRouterDataInternal(GatewayData::RouterDataSet* data) { | |
| 35 *data = data_out_; | |
| 36 return true; | |
| 37 } | |
| 38 }; | |
| 39 | |
| 40 // Stops the specified (nested) message loop when the listener is called back. | |
| 41 class MessageLoopQuitListener | |
| 42 : public GatewayDataProviderCommon::ListenerInterface { | |
| 43 public: | |
| 44 explicit MessageLoopQuitListener(MessageLoop* message_loop) | |
| 45 : message_loop_to_quit_(message_loop) { | |
| 46 CHECK(message_loop_to_quit_); | |
| 47 } | |
| 48 | |
| 49 // ListenerInterface | |
| 50 virtual void DeviceDataUpdateAvailable( | |
| 51 DeviceDataProvider<GatewayData>* provider) { | |
| 52 // Provider should call back on client's thread. | |
| 53 EXPECT_EQ(MessageLoop::current(), message_loop_to_quit_); | |
| 54 provider_ = provider; | |
| 55 message_loop_to_quit_->QuitNow(); | |
| 56 } | |
| 57 | |
| 58 MessageLoop* message_loop_to_quit_; | |
| 59 DeviceDataProvider<GatewayData>* provider_; | |
| 60 }; | |
| 61 | |
| 62 class MockGatewayPollingPolicy : public GatewayPollingPolicyInterface { | |
| 63 public: | |
| 64 MockGatewayPollingPolicy() { | |
| 65 ON_CALL(*this, PollingInterval()) | |
| 66 .WillByDefault(Return(1)); | |
| 67 ON_CALL(*this, NoRouterInterval()) | |
| 68 .WillByDefault(Return(1)); | |
| 69 } | |
| 70 | |
| 71 // GatewayPollingPolicyInterface | |
| 72 MOCK_METHOD0(PollingInterval, int()); | |
| 73 MOCK_METHOD0(NoRouterInterval, int()); | |
| 74 }; | |
| 75 | |
| 76 class GatewayDataProviderCommonWithMock : public GatewayDataProviderCommon { | |
| 77 public: | |
| 78 GatewayDataProviderCommonWithMock() | |
| 79 : new_gateway_api_(new MockGatewayApi), | |
| 80 new_polling_policy_(new MockGatewayPollingPolicy){ | |
| 81 } | |
| 82 | |
| 83 // GatewayDataProviderCommon | |
| 84 virtual GatewayApiInterface* NewGatewayApi() { | |
| 85 CHECK(new_gateway_api_ != NULL); | |
| 86 return new_gateway_api_.release(); | |
| 87 } | |
| 88 virtual GatewayPollingPolicyInterface* NewPollingPolicy() { | |
| 89 CHECK(new_polling_policy_ != NULL); | |
| 90 return new_polling_policy_.release(); | |
| 91 } | |
| 92 | |
| 93 scoped_ptr<MockGatewayApi> new_gateway_api_; | |
| 94 scoped_ptr<MockGatewayPollingPolicy> new_polling_policy_; | |
| 95 | |
| 96 DISALLOW_COPY_AND_ASSIGN(GatewayDataProviderCommonWithMock); | |
| 97 }; | |
| 98 | |
| 99 // Main test fixture. | |
| 100 class GeolocationGatewayDataProviderCommonTest : public testing::Test { | |
| 101 public: | |
| 102 GeolocationGatewayDataProviderCommonTest() | |
| 103 : quit_listener_(&main_message_loop_) { | |
| 104 } | |
| 105 | |
| 106 virtual void SetUp() { | |
| 107 provider_ = new GatewayDataProviderCommonWithMock; | |
| 108 gateway_api_ = provider_->new_gateway_api_.get(); | |
| 109 polling_policy_ = provider_->new_polling_policy_.get(); | |
| 110 provider_->AddListener(&quit_listener_); | |
| 111 } | |
| 112 virtual void TearDown() { | |
| 113 provider_->RemoveListener(&quit_listener_); | |
| 114 provider_->StopDataProvider(); | |
| 115 provider_ = NULL; | |
| 116 } | |
| 117 | |
| 118 protected: | |
| 119 MessageLoop main_message_loop_; | |
| 120 MessageLoopQuitListener quit_listener_; | |
| 121 scoped_refptr<GatewayDataProviderCommonWithMock> provider_; | |
| 122 MockGatewayApi* gateway_api_; | |
| 123 MockGatewayPollingPolicy* polling_policy_; | |
| 124 }; | |
| 125 | |
| 126 TEST_F(GeolocationGatewayDataProviderCommonTest, CreateDestroy) { | |
| 127 // Test fixture members were SetUp correctly. | |
| 128 EXPECT_EQ(&main_message_loop_, MessageLoop::current()); | |
| 129 EXPECT_TRUE(NULL != provider_.get()); | |
| 130 EXPECT_TRUE(NULL != gateway_api_); | |
| 131 } | |
| 132 | |
| 133 TEST_F(GeolocationGatewayDataProviderCommonTest, StartThread) { | |
| 134 EXPECT_CALL(*gateway_api_, GetRouterData(_)) | |
| 135 .Times(AtLeast(1)); | |
| 136 EXPECT_CALL(*polling_policy_, PollingInterval()) | |
| 137 .Times(AtLeast(1)); | |
| 138 EXPECT_TRUE(provider_->StartDataProvider()); | |
| 139 main_message_loop_.Run(); | |
| 140 SUCCEED(); | |
| 141 } | |
| 142 | |
| 143 TEST_F(GeolocationGatewayDataProviderCommonTest, ConnectToDifferentRouter){ | |
| 144 EXPECT_CALL(*polling_policy_, PollingInterval()) | |
| 145 .Times(AtLeast(1)); | |
| 146 EXPECT_CALL(*polling_policy_, NoRouterInterval()) | |
| 147 .Times(1); | |
| 148 EXPECT_CALL(*gateway_api_, GetRouterData(_)) | |
| 149 .WillOnce(Return(true)) // Connected to router. | |
| 150 .WillOnce(Return(false)) // Disconnected from router. | |
| 151 .WillRepeatedly(DoDefault()); // Connected to a different router. | |
| 152 | |
| 153 // Set MAC address for output. | |
| 154 RouterData single_router; | |
| 155 single_router.mac_address = ASCIIToUTF16("12-34-56-78-54-32"); | |
| 156 gateway_api_->data_out_.insert(single_router); | |
| 157 | |
| 158 provider_->StartDataProvider(); | |
| 159 main_message_loop_.Run(); | |
| 160 main_message_loop_.Run(); | |
| 161 } | |
| 162 | |
| 163 TEST_F(GeolocationGatewayDataProviderCommonTest, DoAnEmptyScan) { | |
| 164 EXPECT_CALL(*gateway_api_, GetRouterData(_)) | |
| 165 .Times(AtLeast(1)); | |
| 166 EXPECT_CALL(*polling_policy_, PollingInterval()) | |
| 167 .Times(AtLeast(1)); | |
| 168 EXPECT_TRUE(provider_->StartDataProvider()); | |
| 169 main_message_loop_.Run(); | |
| 170 GatewayData data; | |
| 171 EXPECT_TRUE(provider_->GetData(&data)); | |
| 172 EXPECT_EQ(0, static_cast<int>(data.router_data.size())); | |
| 173 } | |
| 174 | |
| 175 TEST_F(GeolocationGatewayDataProviderCommonTest, DoScanWithResults) { | |
| 176 EXPECT_CALL(*gateway_api_, GetRouterData(_)) | |
| 177 .Times(AtLeast(1)); | |
| 178 EXPECT_CALL(*polling_policy_, PollingInterval()) | |
| 179 .Times(AtLeast(1)); | |
| 180 RouterData single_router; | |
| 181 single_router.mac_address = ASCIIToUTF16("12-34-56-78-54-32"); | |
| 182 gateway_api_->data_out_.insert(single_router); | |
| 183 EXPECT_TRUE(provider_->StartDataProvider()); | |
| 184 main_message_loop_.Run(); | |
| 185 GatewayData data; | |
| 186 EXPECT_TRUE(provider_->GetData(&data)); | |
| 187 EXPECT_EQ(1, static_cast<int>(data.router_data.size())); | |
| 188 EXPECT_EQ(single_router.mac_address, data.router_data.begin()->mac_address); | |
| 189 } | |
| 190 | |
| 191 } // namespace | |
| OLD | NEW |