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