Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS 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 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 5 | #include <base/stl_util-inl.h> |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 6 | #include <gtest/gtest.h> |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 7 | #include <mm/mm-modem.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" |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 13 | #include "shill/mock_modem_manager_proxy.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 14 | #include "shill/modem.h" |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 15 | #include "shill/modem_manager.h" |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 16 | #include "shill/proxy_factory.h" |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 17 | |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 18 | using std::string; |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 19 | using std::vector; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 20 | using testing::_; |
| 21 | using testing::Return; |
| 22 | using testing::StrEq; |
| 23 | using testing::Test; |
| 24 | |
| 25 | namespace shill { |
| 26 | |
| 27 | class ModemManagerTest : public Test { |
| 28 | public: |
| 29 | ModemManagerTest() |
| 30 | : manager_(&control_interface_, &dispatcher_, &glib_), |
| 31 | modem_manager_(kService, |
| 32 | kPath, |
| 33 | &control_interface_, |
| 34 | &dispatcher_, |
| 35 | &manager_, |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 36 | &glib_), |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 37 | proxy_(new MockModemManagerProxy()), |
| 38 | proxy_factory_(this) { |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 39 | ProxyFactory::set_factory(&proxy_factory_); |
| 40 | } |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 41 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 42 | virtual void TearDown(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 43 | |
| 44 | protected: |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 45 | class TestProxyFactory : public ProxyFactory { |
| 46 | public: |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 47 | TestProxyFactory(ModemManagerTest *test) : test_(test) {} |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 48 | |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 49 | virtual ModemManagerProxyInterface *CreateModemManagerProxy( |
| 50 | ModemManager *manager, |
| 51 | const string &path, |
| 52 | const string &service) { |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 53 | return test_->proxy_.release(); |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | private: |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 57 | ModemManagerTest *test_; |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 60 | static const char kService[]; |
| 61 | static const char kPath[]; |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 62 | static const char kOwner[]; |
| 63 | static const char kModemPath[]; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 64 | |
| 65 | MockGLib glib_; |
| 66 | MockControl control_interface_; |
| 67 | EventDispatcher dispatcher_; |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 68 | MockManager manager_; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 69 | ModemManager modem_manager_; |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 70 | scoped_ptr<MockModemManagerProxy> proxy_; |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 71 | TestProxyFactory proxy_factory_; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 74 | const char ModemManagerTest::kService[] = "org.chromium.ModemManager"; |
| 75 | const char ModemManagerTest::kPath[] = "/org/chromium/ModemManager"; |
| 76 | const char ModemManagerTest::kOwner[] = ":1.17"; |
| 77 | const char ModemManagerTest::kModemPath[] = "/org/chromium/ModemManager/Gobi/0"; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 78 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 79 | void ModemManagerTest::TearDown() { |
| 80 | modem_manager_.watcher_id_ = 0; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 81 | ProxyFactory::set_factory(NULL); |
| 82 | } |
| 83 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 84 | TEST_F(ModemManagerTest, Start) { |
| 85 | const int kWatcher = 123; |
| 86 | EXPECT_CALL(glib_, BusWatchName(G_BUS_TYPE_SYSTEM, |
| 87 | StrEq(kService), |
| 88 | G_BUS_NAME_WATCHER_FLAGS_NONE, |
| 89 | ModemManager::OnAppear, |
| 90 | ModemManager::OnVanish, |
| 91 | &modem_manager_, |
| 92 | NULL)) |
| 93 | .WillOnce(Return(kWatcher)); |
| 94 | EXPECT_EQ(0, modem_manager_.watcher_id_); |
| 95 | modem_manager_.Start(); |
| 96 | EXPECT_EQ(kWatcher, modem_manager_.watcher_id_); |
| 97 | } |
| 98 | |
| 99 | TEST_F(ModemManagerTest, Stop) { |
| 100 | const int kWatcher = 345; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 101 | modem_manager_.watcher_id_ = kWatcher; |
| 102 | modem_manager_.owner_ = kOwner; |
| 103 | EXPECT_CALL(glib_, BusUnwatchName(kWatcher)).Times(1); |
| 104 | modem_manager_.Stop(); |
| 105 | EXPECT_EQ(0, modem_manager_.watcher_id_); |
| 106 | EXPECT_EQ("", modem_manager_.owner_); |
| 107 | } |
| 108 | |
| 109 | TEST_F(ModemManagerTest, Connect) { |
| 110 | EXPECT_EQ("", modem_manager_.owner_); |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 111 | EXPECT_CALL(*proxy_, EnumerateDevices()) |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 112 | .WillOnce(Return(vector<DBus::Path>(1, kModemPath))); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 113 | modem_manager_.Connect(kOwner); |
| 114 | EXPECT_EQ(kOwner, modem_manager_.owner_); |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 115 | EXPECT_EQ(1, modem_manager_.modems_.size()); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 116 | ASSERT_TRUE(ContainsKey(modem_manager_.modems_, kModemPath)); |
| 117 | EXPECT_FALSE(modem_manager_.modems_[kModemPath]->task_factory_.empty()); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | TEST_F(ModemManagerTest, Disconnect) { |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 121 | modem_manager_.owner_ = kOwner; |
| 122 | modem_manager_.Disconnect(); |
| 123 | EXPECT_EQ("", modem_manager_.owner_); |
| 124 | } |
| 125 | |
| 126 | TEST_F(ModemManagerTest, OnAppear) { |
| 127 | EXPECT_EQ("", modem_manager_.owner_); |
Darin Petkov | 3b29233 | 2011-07-28 14:17:24 -0700 | [diff] [blame] | 128 | EXPECT_CALL(*proxy_, EnumerateDevices()) |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 129 | .WillOnce(Return(vector<DBus::Path>())); |
| 130 | ModemManager::OnAppear(NULL, kService, kOwner, &modem_manager_); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 131 | EXPECT_EQ(kOwner, modem_manager_.owner_); |
| 132 | } |
| 133 | |
| 134 | TEST_F(ModemManagerTest, OnVanish) { |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 135 | modem_manager_.owner_ = kOwner; |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 136 | ModemManager::OnVanish(NULL, kService, &modem_manager_); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 137 | EXPECT_EQ("", modem_manager_.owner_); |
| 138 | } |
| 139 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 140 | TEST_F(ModemManagerTest, AddRemoveModem) { |
| 141 | modem_manager_.owner_ = kOwner; |
| 142 | modem_manager_.AddModem(kModemPath); |
| 143 | EXPECT_EQ(1, modem_manager_.modems_.size()); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 144 | ASSERT_TRUE(ContainsKey(modem_manager_.modems_, kModemPath)); |
| 145 | EXPECT_FALSE(modem_manager_.modems_[kModemPath]->task_factory_.empty()); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 146 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 147 | modem_manager_.RemoveModem(kModemPath); |
| 148 | EXPECT_EQ(0, modem_manager_.modems_.size()); |
| 149 | } |
| 150 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 151 | } // namespace shill |