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