| 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/wifi_data_provider_win.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/scoped_ptr.h" |
| 10 #include "base/string_util.h" |
| 11 #include "chrome/browser/geolocation/wifi_data_provider_common.h" |
| 12 #include "chrome/test/ui_test_utils.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace { |
| 16 class MockWlanApi : public Win32WifiDataProvider::WlanApiInterface { |
| 17 public: |
| 18 MockWlanApi() : calls_(0), bool_return_(true) { |
| 19 } |
| 20 virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data) { |
| 21 ++calls_; |
| 22 *data = data_out_; |
| 23 return bool_return_; |
| 24 } |
| 25 int calls_; |
| 26 bool bool_return_; |
| 27 WifiData::AccessPointDataSet data_out_; |
| 28 }; |
| 29 |
| 30 class MockPollingPolicy : public PollingPolicyInterface { |
| 31 public: |
| 32 virtual void UpdatePollingInterval(bool scan_results_differ) { |
| 33 results_differed_.push_back(scan_results_differ); |
| 34 } |
| 35 virtual int PollingInterval() { return 1; } |
| 36 std::vector<bool> results_differed_; |
| 37 }; |
| 38 |
| 39 // Stops the specified (nested) message loop when the listener is called back. |
| 40 class MessageLoopQuitListener |
| 41 : public Win32WifiDataProvider::ListenerInterface { |
| 42 public: |
| 43 explicit MessageLoopQuitListener(MessageLoop* message_loop) |
| 44 : message_loop_to_quit_(message_loop) { |
| 45 assert(message_loop_to_quit_ != NULL); |
| 46 } |
| 47 // ListenerInterface |
| 48 virtual void DeviceDataUpdateAvailable( |
| 49 DeviceDataProvider<WifiData>* provider) { |
| 50 // We expect the callback to come from the provider's internal worker |
| 51 // thread. This is not a strict requirement, just a lot of the complexity |
| 52 // here is predicated on the need to cope with this scenario! |
| 53 EXPECT_NE(MessageLoop::current(), message_loop_to_quit_); |
| 54 provider_ = provider; |
| 55 // Can't call Quit() directly on another thread's message loop. |
| 56 message_loop_to_quit_->PostTask(FROM_HERE, new MessageLoop::QuitTask); |
| 57 } |
| 58 MessageLoop* message_loop_to_quit_; |
| 59 DeviceDataProvider<WifiData>* provider_; |
| 60 }; |
| 61 |
| 62 // Main test fixture |
| 63 class Win32WifiDataProviderTest : public testing::Test { |
| 64 public: |
| 65 static Win32WifiDataProvider* CreateWin32WifiDataProvider( |
| 66 MockWlanApi** wlan_api_out) { |
| 67 Win32WifiDataProvider* provider = new Win32WifiDataProvider; |
| 68 *wlan_api_out = new MockWlanApi; |
| 69 provider->inject_mock_wlan_api(*wlan_api_out); // Takes ownership. |
| 70 provider->inject_mock_polling_policy(new MockPollingPolicy); // ditto |
| 71 return provider; |
| 72 } |
| 73 virtual void SetUp() { |
| 74 provider_.reset(CreateWin32WifiDataProvider(&wlan_api_)); |
| 75 } |
| 76 virtual void TearDown() { |
| 77 provider_.reset(NULL); |
| 78 } |
| 79 |
| 80 protected: |
| 81 MessageLoop main_message_loop_; |
| 82 scoped_ptr<Win32WifiDataProvider> provider_; |
| 83 MockWlanApi* wlan_api_; |
| 84 }; |
| 85 |
| 86 WifiDataProviderImplBase* CreateWin32WifiDataProviderStatic() { |
| 87 MockWlanApi* wlan_api; |
| 88 return Win32WifiDataProviderTest::CreateWin32WifiDataProvider(&wlan_api); |
| 89 } |
| 90 } // namespace |
| 91 |
| 92 TEST_F(Win32WifiDataProviderTest, CreateDestroy) { |
| 93 // Test fixture members were SetUp correctly. |
| 94 EXPECT_EQ(&main_message_loop_, MessageLoop::current()); |
| 95 EXPECT_TRUE(NULL != provider_.get()); |
| 96 EXPECT_TRUE(NULL != wlan_api_); |
| 97 } |
| 98 |
| 99 TEST_F(Win32WifiDataProviderTest, StartThread) { |
| 100 EXPECT_TRUE(provider_->StartDataProvider()); |
| 101 provider_.reset(NULL); // Stop()s the thread. |
| 102 SUCCEED(); |
| 103 } |
| 104 |
| 105 TEST_F(Win32WifiDataProviderTest, DoAnEmptyScan) { |
| 106 MessageLoopQuitListener quit_listener(&main_message_loop_); |
| 107 provider_->AddListener(&quit_listener); |
| 108 EXPECT_TRUE(provider_->StartDataProvider()); |
| 109 main_message_loop_.Run(); |
| 110 EXPECT_EQ(1, wlan_api_->calls_); |
| 111 WifiData data; |
| 112 EXPECT_TRUE(provider_->GetData(&data)); |
| 113 EXPECT_EQ(0, data.access_point_data.size()); |
| 114 } |
| 115 |
| 116 TEST_F(Win32WifiDataProviderTest, DoScanWithResults) { |
| 117 MessageLoopQuitListener quit_listener(&main_message_loop_); |
| 118 provider_->AddListener(&quit_listener); |
| 119 AccessPointData single_access_point; |
| 120 single_access_point.age = 1; |
| 121 single_access_point.channel = 2; |
| 122 single_access_point.mac_address = 3; |
| 123 single_access_point.radio_signal_strength = 4; |
| 124 single_access_point.signal_to_noise = 5; |
| 125 single_access_point.ssid = L"foossid"; |
| 126 wlan_api_->data_out_.insert(single_access_point); |
| 127 |
| 128 EXPECT_TRUE(provider_->StartDataProvider()); |
| 129 main_message_loop_.Run(); |
| 130 EXPECT_EQ(1, wlan_api_->calls_); |
| 131 WifiData data; |
| 132 EXPECT_TRUE(provider_->GetData(&data)); |
| 133 EXPECT_EQ(1, data.access_point_data.size()); |
| 134 EXPECT_EQ(single_access_point.age, data.access_point_data.begin()->age); |
| 135 EXPECT_EQ(single_access_point.ssid, data.access_point_data.begin()->ssid); |
| 136 } |
| 137 |
| 138 TEST_F(Win32WifiDataProviderTest, StartThreadViaDeviceDataProvider) { |
| 139 MessageLoopQuitListener quit_listener(&main_message_loop_); |
| 140 DeviceDataProvider<WifiData>::SetFactory(CreateWin32WifiDataProviderStatic); |
| 141 DeviceDataProvider<WifiData>::Register(&quit_listener); |
| 142 main_message_loop_.Run(); |
| 143 DeviceDataProvider<WifiData>::Unregister(&quit_listener); |
| 144 DeviceDataProvider<WifiData>::ResetFactory(); |
| 145 } |
| OLD | NEW |