| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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/geolocation/wifi_data_provider_win.h" | 5 #include "chrome/browser/geolocation/wifi_data_provider_win.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "chrome/browser/geolocation/wifi_data_provider_common.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" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 13 |
| 15 namespace { | 14 namespace { |
| 16 class MockWlanApi : public Win32WifiDataProvider::WlanApiInterface { | 15 class MockWlanApi : public Win32WifiDataProvider::WlanApiInterface { |
| 17 public: | 16 public: |
| 18 MockWlanApi() : calls_(0), bool_return_(true) { | 17 MockWlanApi() : calls_(0), bool_return_(true) { |
| 19 } | 18 } |
| 20 virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data) { | 19 virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data) { |
| 21 ++calls_; | 20 ++calls_; |
| 22 *data = data_out_; | 21 *data = data_out_; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 35 virtual int PollingInterval() { return 1; } | 34 virtual int PollingInterval() { return 1; } |
| 36 std::vector<bool> results_differed_; | 35 std::vector<bool> results_differed_; |
| 37 }; | 36 }; |
| 38 | 37 |
| 39 // Stops the specified (nested) message loop when the listener is called back. | 38 // Stops the specified (nested) message loop when the listener is called back. |
| 40 class MessageLoopQuitListener | 39 class MessageLoopQuitListener |
| 41 : public Win32WifiDataProvider::ListenerInterface { | 40 : public Win32WifiDataProvider::ListenerInterface { |
| 42 public: | 41 public: |
| 43 explicit MessageLoopQuitListener(MessageLoop* message_loop) | 42 explicit MessageLoopQuitListener(MessageLoop* message_loop) |
| 44 : message_loop_to_quit_(message_loop) { | 43 : message_loop_to_quit_(message_loop) { |
| 45 assert(message_loop_to_quit_ != NULL); | 44 DCHECK(message_loop_to_quit_ != NULL); |
| 46 } | 45 } |
| 47 // ListenerInterface | 46 // ListenerInterface |
| 48 virtual void DeviceDataUpdateAvailable( | 47 virtual void DeviceDataUpdateAvailable( |
| 49 DeviceDataProvider<WifiData>* provider) { | 48 DeviceDataProvider<WifiData>* provider) { |
| 50 // We expect the callback to come from the provider's internal worker | 49 // 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 | 50 // 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! | 51 // here is predicated on the need to cope with this scenario! |
| 53 EXPECT_NE(MessageLoop::current(), message_loop_to_quit_); | 52 EXPECT_NE(MessageLoop::current(), message_loop_to_quit_); |
| 54 provider_ = provider; | 53 provider_ = provider; |
| 55 // Can't call Quit() directly on another thread's message loop. | 54 // Can't call Quit() directly on another thread's message loop. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 67 Win32WifiDataProvider* provider = new Win32WifiDataProvider; | 66 Win32WifiDataProvider* provider = new Win32WifiDataProvider; |
| 68 *wlan_api_out = new MockWlanApi; | 67 *wlan_api_out = new MockWlanApi; |
| 69 provider->inject_mock_wlan_api(*wlan_api_out); // Takes ownership. | 68 provider->inject_mock_wlan_api(*wlan_api_out); // Takes ownership. |
| 70 provider->inject_mock_polling_policy(new MockPollingPolicy); // ditto | 69 provider->inject_mock_polling_policy(new MockPollingPolicy); // ditto |
| 71 return provider; | 70 return provider; |
| 72 } | 71 } |
| 73 virtual void SetUp() { | 72 virtual void SetUp() { |
| 74 provider_.reset(CreateWin32WifiDataProvider(&wlan_api_)); | 73 provider_.reset(CreateWin32WifiDataProvider(&wlan_api_)); |
| 75 } | 74 } |
| 76 virtual void TearDown() { | 75 virtual void TearDown() { |
| 77 provider_.reset(NULL); | 76 provider_.reset(); |
| 78 } | 77 } |
| 79 | 78 |
| 80 protected: | 79 protected: |
| 81 MessageLoop main_message_loop_; | 80 MessageLoop main_message_loop_; |
| 82 scoped_ptr<Win32WifiDataProvider> provider_; | 81 scoped_ptr<Win32WifiDataProvider> provider_; |
| 83 MockWlanApi* wlan_api_; | 82 MockWlanApi* wlan_api_; |
| 84 }; | 83 }; |
| 85 | 84 |
| 86 WifiDataProviderImplBase* CreateWin32WifiDataProviderStatic() { | 85 WifiDataProviderImplBase* CreateWin32WifiDataProviderStatic() { |
| 87 MockWlanApi* wlan_api; | 86 MockWlanApi* wlan_api; |
| 88 return Win32WifiDataProviderTest::CreateWin32WifiDataProvider(&wlan_api); | 87 return Win32WifiDataProviderTest::CreateWin32WifiDataProvider(&wlan_api); |
| 89 } | 88 } |
| 90 } // namespace | 89 } // namespace |
| 91 | 90 |
| 92 TEST_F(Win32WifiDataProviderTest, CreateDestroy) { | 91 TEST_F(Win32WifiDataProviderTest, CreateDestroy) { |
| 93 // Test fixture members were SetUp correctly. | 92 // Test fixture members were SetUp correctly. |
| 94 EXPECT_EQ(&main_message_loop_, MessageLoop::current()); | 93 EXPECT_EQ(&main_message_loop_, MessageLoop::current()); |
| 95 EXPECT_TRUE(NULL != provider_.get()); | 94 EXPECT_TRUE(NULL != provider_.get()); |
| 96 EXPECT_TRUE(NULL != wlan_api_); | 95 EXPECT_TRUE(NULL != wlan_api_); |
| 97 } | 96 } |
| 98 | 97 |
| 99 TEST_F(Win32WifiDataProviderTest, StartThread) { | 98 TEST_F(Win32WifiDataProviderTest, StartThread) { |
| 100 EXPECT_TRUE(provider_->StartDataProvider()); | 99 EXPECT_TRUE(provider_->StartDataProvider()); |
| 101 provider_.reset(NULL); // Stop()s the thread. | 100 provider_.reset(); // Stop()s the thread. |
| 102 SUCCEED(); | 101 SUCCEED(); |
| 103 } | 102 } |
| 104 | 103 |
| 105 TEST_F(Win32WifiDataProviderTest, DoAnEmptyScan) { | 104 TEST_F(Win32WifiDataProviderTest, DoAnEmptyScan) { |
| 106 MessageLoopQuitListener quit_listener(&main_message_loop_); | 105 MessageLoopQuitListener quit_listener(&main_message_loop_); |
| 107 provider_->AddListener(&quit_listener); | 106 provider_->AddListener(&quit_listener); |
| 108 EXPECT_TRUE(provider_->StartDataProvider()); | 107 EXPECT_TRUE(provider_->StartDataProvider()); |
| 109 main_message_loop_.Run(); | 108 main_message_loop_.Run(); |
| 110 // Check we had at least one call. The worker thread may have raced ahead | 109 // Check we had at least one call. The worker thread may have raced ahead |
| 111 // and made multiple calls. | 110 // and made multiple calls. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 140 } | 139 } |
| 141 | 140 |
| 142 TEST_F(Win32WifiDataProviderTest, StartThreadViaDeviceDataProvider) { | 141 TEST_F(Win32WifiDataProviderTest, StartThreadViaDeviceDataProvider) { |
| 143 MessageLoopQuitListener quit_listener(&main_message_loop_); | 142 MessageLoopQuitListener quit_listener(&main_message_loop_); |
| 144 DeviceDataProvider<WifiData>::SetFactory(CreateWin32WifiDataProviderStatic); | 143 DeviceDataProvider<WifiData>::SetFactory(CreateWin32WifiDataProviderStatic); |
| 145 DeviceDataProvider<WifiData>::Register(&quit_listener); | 144 DeviceDataProvider<WifiData>::Register(&quit_listener); |
| 146 main_message_loop_.Run(); | 145 main_message_loop_.Run(); |
| 147 DeviceDataProvider<WifiData>::Unregister(&quit_listener); | 146 DeviceDataProvider<WifiData>::Unregister(&quit_listener); |
| 148 DeviceDataProvider<WifiData>::ResetFactory(); | 147 DeviceDataProvider<WifiData>::ResetFactory(); |
| 149 } | 148 } |
| OLD | NEW |