blob: 7f3154c76aa140b9a6dc11ccefaaf428ed6757f1 [file] [log] [blame]
Darin Petkov887f2982011-07-14 16:10:17 -07001// 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 Petkov5c97ac52011-07-19 16:30:49 -07005#include <base/stl_util-inl.h>
Darin Petkov887f2982011-07-14 16:10:17 -07006#include <gtest/gtest.h>
Darin Petkove0a312e2011-07-20 13:45:28 -07007#include <mm/mm-modem.h>
Darin Petkov887f2982011-07-14 16:10:17 -07008
9#include "shill/manager.h"
10#include "shill/mock_control.h"
11#include "shill/mock_glib.h"
Chris Masone2ae797d2011-08-23 20:41:00 -070012#include "shill/mock_manager.h"
Darin Petkovc90fe522011-07-15 13:59:47 -070013#include "shill/mock_modem_manager_proxy.h"
Darin Petkove0a312e2011-07-20 13:45:28 -070014#include "shill/modem.h"
Darin Petkov887f2982011-07-14 16:10:17 -070015#include "shill/modem_manager.h"
Darin Petkovc90fe522011-07-15 13:59:47 -070016#include "shill/proxy_factory.h"
Darin Petkov887f2982011-07-14 16:10:17 -070017
Darin Petkovc90fe522011-07-15 13:59:47 -070018using std::string;
Darin Petkov5c97ac52011-07-19 16:30:49 -070019using std::vector;
Darin Petkov887f2982011-07-14 16:10:17 -070020using testing::_;
21using testing::Return;
22using testing::StrEq;
23using testing::Test;
24
25namespace shill {
26
27class 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 Petkovc90fe522011-07-15 13:59:47 -070036 &glib_),
Darin Petkov3b292332011-07-28 14:17:24 -070037 proxy_(new MockModemManagerProxy()),
38 proxy_factory_(this) {
Darin Petkovc90fe522011-07-15 13:59:47 -070039 ProxyFactory::set_factory(&proxy_factory_);
40 }
Darin Petkov887f2982011-07-14 16:10:17 -070041
Darin Petkove0a312e2011-07-20 13:45:28 -070042 virtual void TearDown();
Darin Petkov887f2982011-07-14 16:10:17 -070043
44 protected:
Darin Petkovc90fe522011-07-15 13:59:47 -070045 class TestProxyFactory : public ProxyFactory {
46 public:
Darin Petkov3b292332011-07-28 14:17:24 -070047 TestProxyFactory(ModemManagerTest *test) : test_(test) {}
Darin Petkov5c97ac52011-07-19 16:30:49 -070048
Darin Petkovc90fe522011-07-15 13:59:47 -070049 virtual ModemManagerProxyInterface *CreateModemManagerProxy(
50 ModemManager *manager,
51 const string &path,
52 const string &service) {
Darin Petkov3b292332011-07-28 14:17:24 -070053 return test_->proxy_.release();
Darin Petkovc90fe522011-07-15 13:59:47 -070054 }
55
56 private:
Darin Petkov3b292332011-07-28 14:17:24 -070057 ModemManagerTest *test_;
Darin Petkovc90fe522011-07-15 13:59:47 -070058 };
59
Darin Petkov887f2982011-07-14 16:10:17 -070060 static const char kService[];
61 static const char kPath[];
Darin Petkov5c97ac52011-07-19 16:30:49 -070062 static const char kOwner[];
63 static const char kModemPath[];
Darin Petkov887f2982011-07-14 16:10:17 -070064
65 MockGLib glib_;
66 MockControl control_interface_;
67 EventDispatcher dispatcher_;
Chris Masone2ae797d2011-08-23 20:41:00 -070068 MockManager manager_;
Darin Petkov887f2982011-07-14 16:10:17 -070069 ModemManager modem_manager_;
Darin Petkov3b292332011-07-28 14:17:24 -070070 scoped_ptr<MockModemManagerProxy> proxy_;
Darin Petkovc90fe522011-07-15 13:59:47 -070071 TestProxyFactory proxy_factory_;
Darin Petkov887f2982011-07-14 16:10:17 -070072};
73
Darin Petkov5c97ac52011-07-19 16:30:49 -070074const char ModemManagerTest::kService[] = "org.chromium.ModemManager";
75const char ModemManagerTest::kPath[] = "/org/chromium/ModemManager";
76const char ModemManagerTest::kOwner[] = ":1.17";
77const char ModemManagerTest::kModemPath[] = "/org/chromium/ModemManager/Gobi/0";
Darin Petkov887f2982011-07-14 16:10:17 -070078
Darin Petkove0a312e2011-07-20 13:45:28 -070079void ModemManagerTest::TearDown() {
80 modem_manager_.watcher_id_ = 0;
Darin Petkove0a312e2011-07-20 13:45:28 -070081 ProxyFactory::set_factory(NULL);
82}
83
Darin Petkov887f2982011-07-14 16:10:17 -070084TEST_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
99TEST_F(ModemManagerTest, Stop) {
100 const int kWatcher = 345;
Darin Petkov887f2982011-07-14 16:10:17 -0700101 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
109TEST_F(ModemManagerTest, Connect) {
110 EXPECT_EQ("", modem_manager_.owner_);
Darin Petkov3b292332011-07-28 14:17:24 -0700111 EXPECT_CALL(*proxy_, EnumerateDevices())
Darin Petkov5c97ac52011-07-19 16:30:49 -0700112 .WillOnce(Return(vector<DBus::Path>(1, kModemPath)));
Darin Petkov887f2982011-07-14 16:10:17 -0700113 modem_manager_.Connect(kOwner);
114 EXPECT_EQ(kOwner, modem_manager_.owner_);
Darin Petkov5c97ac52011-07-19 16:30:49 -0700115 EXPECT_EQ(1, modem_manager_.modems_.size());
Darin Petkova7b89492011-07-27 12:48:17 -0700116 ASSERT_TRUE(ContainsKey(modem_manager_.modems_, kModemPath));
117 EXPECT_FALSE(modem_manager_.modems_[kModemPath]->task_factory_.empty());
Darin Petkov887f2982011-07-14 16:10:17 -0700118}
119
120TEST_F(ModemManagerTest, Disconnect) {
Darin Petkov887f2982011-07-14 16:10:17 -0700121 modem_manager_.owner_ = kOwner;
122 modem_manager_.Disconnect();
123 EXPECT_EQ("", modem_manager_.owner_);
124}
125
126TEST_F(ModemManagerTest, OnAppear) {
127 EXPECT_EQ("", modem_manager_.owner_);
Darin Petkov3b292332011-07-28 14:17:24 -0700128 EXPECT_CALL(*proxy_, EnumerateDevices())
Darin Petkov5c97ac52011-07-19 16:30:49 -0700129 .WillOnce(Return(vector<DBus::Path>()));
130 ModemManager::OnAppear(NULL, kService, kOwner, &modem_manager_);
Darin Petkov887f2982011-07-14 16:10:17 -0700131 EXPECT_EQ(kOwner, modem_manager_.owner_);
132}
133
134TEST_F(ModemManagerTest, OnVanish) {
Darin Petkov887f2982011-07-14 16:10:17 -0700135 modem_manager_.owner_ = kOwner;
Darin Petkov5c97ac52011-07-19 16:30:49 -0700136 ModemManager::OnVanish(NULL, kService, &modem_manager_);
Darin Petkov887f2982011-07-14 16:10:17 -0700137 EXPECT_EQ("", modem_manager_.owner_);
138}
139
Darin Petkov5c97ac52011-07-19 16:30:49 -0700140TEST_F(ModemManagerTest, AddRemoveModem) {
141 modem_manager_.owner_ = kOwner;
142 modem_manager_.AddModem(kModemPath);
143 EXPECT_EQ(1, modem_manager_.modems_.size());
Darin Petkova7b89492011-07-27 12:48:17 -0700144 ASSERT_TRUE(ContainsKey(modem_manager_.modems_, kModemPath));
145 EXPECT_FALSE(modem_manager_.modems_[kModemPath]->task_factory_.empty());
Darin Petkove0a312e2011-07-20 13:45:28 -0700146
Darin Petkov5c97ac52011-07-19 16:30:49 -0700147 modem_manager_.RemoveModem(kModemPath);
148 EXPECT_EQ(0, modem_manager_.modems_.size());
149}
150
Darin Petkov887f2982011-07-14 16:10:17 -0700151} // namespace shill