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