Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 5 | #include <base/stl_util.h> |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 6 | #include <gtest/gtest.h> |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 7 | #include <mobile_provider.h> |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 8 | |
| 9 | #include "shill/manager.h" |
| 10 | #include "shill/mock_control.h" |
| 11 | #include "shill/mock_glib.h" |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 12 | #include "shill/mock_manager.h" |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 13 | #include "shill/mock_metrics.h" |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 14 | #include "shill/modem_info.h" |
| 15 | #include "shill/modem_manager.h" |
| 16 | |
| 17 | using testing::_; |
| 18 | using testing::Return; |
| 19 | using testing::Test; |
| 20 | |
| 21 | namespace shill { |
| 22 | |
| 23 | class ModemInfoTest : public Test { |
| 24 | public: |
| 25 | ModemInfoTest() |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 26 | : manager_(&control_interface_, &dispatcher_, &metrics_, &glib_), |
| 27 | modem_info_(&control_interface_, &dispatcher_, &metrics_, &manager_, |
| 28 | &glib_) {} |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 29 | |
| 30 | protected: |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 31 | static const char kTestMobileProviderDBPath[]; |
| 32 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 33 | MockGLib glib_; |
| 34 | MockControl control_interface_; |
| 35 | EventDispatcher dispatcher_; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 36 | MockMetrics metrics_; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 37 | MockManager manager_; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 38 | ModemInfo modem_info_; |
| 39 | }; |
| 40 | |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 41 | const char ModemInfoTest::kTestMobileProviderDBPath[] = |
| 42 | "provider_db_unittest.bfd"; |
| 43 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 44 | TEST_F(ModemInfoTest, StartStop) { |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 45 | const int kWatcher = 123; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 46 | EXPECT_EQ(0, modem_info_.modem_managers_.size()); |
| 47 | EXPECT_CALL(glib_, BusWatchName(_, _, _, _, _, _, _)) |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 48 | .WillOnce(Return(kWatcher)) |
| 49 | .WillOnce(Return(kWatcher + 1)) |
| 50 | .WillOnce(Return(kWatcher + 2)); |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 51 | modem_info_.provider_db_path_ = kTestMobileProviderDBPath; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 52 | modem_info_.Start(); |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 53 | EXPECT_EQ(3, modem_info_.modem_managers_.size()); |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 54 | EXPECT_TRUE(modem_info_.provider_db_); |
| 55 | EXPECT_TRUE(mobile_provider_lookup_by_name(modem_info_.provider_db_, "AT&T")); |
| 56 | EXPECT_FALSE(mobile_provider_lookup_by_name(modem_info_.provider_db_, "xyz")); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 57 | |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 58 | EXPECT_CALL(glib_, BusUnwatchName(kWatcher)).Times(1); |
| 59 | EXPECT_CALL(glib_, BusUnwatchName(kWatcher + 1)).Times(1); |
| 60 | EXPECT_CALL(glib_, BusUnwatchName(kWatcher + 2)).Times(1); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 61 | modem_info_.Stop(); |
| 62 | EXPECT_EQ(0, modem_info_.modem_managers_.size()); |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 63 | EXPECT_FALSE(modem_info_.provider_db_); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | TEST_F(ModemInfoTest, RegisterModemManager) { |
| 67 | const int kWatcher = 123; |
| 68 | static const char kService[] = "some.dbus.service"; |
| 69 | EXPECT_CALL(glib_, BusWatchName(_, _, _, _, _, _, _)) |
| 70 | .WillOnce(Return(kWatcher)); |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 71 | // Passes ownership of the database. |
| 72 | modem_info_.provider_db_ = mobile_provider_open_db(kTestMobileProviderDBPath); |
| 73 | EXPECT_TRUE(modem_info_.provider_db_); |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 74 | modem_info_.RegisterModemManager<ModemManagerClassic>( |
| 75 | kService, |
| 76 | "/dbus/service/path"); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 77 | ASSERT_EQ(1, modem_info_.modem_managers_.size()); |
| 78 | ModemManager *manager = modem_info_.modem_managers_[0]; |
| 79 | EXPECT_EQ(kService, manager->service_); |
| 80 | EXPECT_EQ(kWatcher, manager->watcher_id_); |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 81 | EXPECT_EQ(modem_info_.provider_db_, manager->provider_db_); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 82 | manager->watcher_id_ = 0; |
| 83 | } |
| 84 | |
| 85 | } // namespace shill |