blob: c19f14c35abac7692222ad9125999e2d06ffc44a [file] [log] [blame]
Jason Glasgowef965562012-04-10 16:12:35 -04001// Copyright (c) 2012 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
5#include "shill/cellular_capability_universal.h"
6
7#include <base/bind.h>
8#include <chromeos/dbus/service_constants.h>
9#include <gtest/gtest.h>
10#include <mobile_provider.h>
11
12#include "shill/cellular.h"
13#include "shill/cellular_service.h"
14#include "shill/error.h"
15#include "shill/event_dispatcher.h"
16#include "shill/mock_adaptors.h"
17#include "shill/mock_glib.h"
18#include "shill/mock_manager.h"
19#include "shill/mock_metrics.h"
20#include "shill/mock_mm1_modem_modem3gpp_proxy.h"
21#include "shill/mock_mm1_modem_modemcdma_proxy.h"
22#include "shill/mock_mm1_modem_proxy.h"
23#include "shill/mock_mm1_modem_simple_proxy.h"
24#include "shill/mock_mm1_sim_proxy.h"
25#include "shill/mock_profile.h"
26#include "shill/mock_rtnl_handler.h"
27#include "shill/nice_mock_control.h"
28#include "shill/proxy_factory.h"
29
30using base::Bind;
31using base::Unretained;
32using std::string;
33using testing::InSequence;
34using testing::NiceMock;
35using testing::Return;
36using testing::_;
37
38namespace shill {
39
40MATCHER(IsSuccess, "") {
41 return arg.IsSuccess();
42}
43MATCHER(IsFailure, "") {
44 return arg.IsFailure();
45}
46
47class CellularCapabilityUniversalTest : public testing::Test {
48 public:
49 CellularCapabilityUniversalTest()
50 : manager_(&control_, &dispatcher_, &metrics_, &glib_),
51 cellular_(new Cellular(&control_,
52 &dispatcher_,
53 NULL,
54 &manager_,
55 "",
56 "",
57 0,
58 Cellular::kTypeUniversal,
59 "",
60 "",
61 NULL)),
62 modem_3gpp_proxy_(new mm1::MockModemModem3gppProxy()),
63 modem_cdma_proxy_(new mm1::MockModemModemCdmaProxy()),
64 modem_proxy_(new mm1::MockModemProxy()),
65 modem_simple_proxy_(new mm1::MockModemSimpleProxy()),
66 sim_proxy_(new mm1::MockSimProxy()),
67 proxy_factory_(this),
68 capability_(NULL),
69 device_adaptor_(NULL) {}
70
71 virtual ~CellularCapabilityUniversalTest() {
72 cellular_->service_ = NULL;
73 capability_ = NULL;
74 device_adaptor_ = NULL;
75 }
76
77 virtual void SetUp() {
78 capability_ = dynamic_cast<CellularCapabilityUniversal *>(
79 cellular_->capability_.get());
80 capability_->proxy_factory_ = &proxy_factory_;
81 device_adaptor_ =
82 dynamic_cast<NiceMock<DeviceMockAdaptor> *>(cellular_->adaptor());
83 }
84
85 virtual void TearDown() {
86 capability_->proxy_factory_ = NULL;
87 }
88
89 void InvokeEnable(bool enable, Error *error,
90 const ResultCallback &callback, int timeout) {
91 callback.Run(Error());
92 }
93 void InvokeEnableFail(bool enable, Error *error,
94 const ResultCallback &callback, int timeout) {
95 callback.Run(Error(Error::kOperationFailed));
96 }
97
98 MOCK_METHOD1(TestCallback, void(const Error &error));
99
100 protected:
101 static const char kImei[];
102
103 class TestProxyFactory : public ProxyFactory {
104 public:
105 explicit TestProxyFactory(CellularCapabilityUniversalTest *test) :
106 test_(test) {}
107
108 virtual mm1::ModemModem3gppProxyInterface *CreateMM1ModemModem3gppProxy(
109 const std::string &/* path */,
110 const std::string &/* service */) {
111 return test_->modem_3gpp_proxy_.release();
112 }
113
114 virtual mm1::ModemModemCdmaProxyInterface *CreateMM1ModemModemCdmaProxy(
115 const std::string &/* path */,
116 const std::string &/* service */) {
117 return test_->modem_cdma_proxy_.release();
118 }
119
120 virtual mm1::ModemProxyInterface *CreateMM1ModemProxy(
121 const std::string &/* path */,
122 const std::string &/* service */) {
123 return test_->modem_proxy_.release();
124 }
125
126 virtual mm1::ModemSimpleProxyInterface *CreateMM1ModemSimpleProxy(
127 const std::string &/* path */,
128 const std::string &/* service */) {
129 return test_->modem_simple_proxy_.release();
130 }
131
132 virtual mm1::SimProxyInterface *CreateSimProxy(
133 const std::string &/* path */,
134 const std::string &/* service */) {
135 return test_->sim_proxy_.release();
136 }
137
138 private:
139 CellularCapabilityUniversalTest *test_;
140 };
141
142 NiceMockControl control_;
143 EventDispatcher dispatcher_;
144 MockMetrics metrics_;
145 MockGLib glib_;
146 MockManager manager_;
147 CellularRefPtr cellular_;
148 scoped_ptr<mm1::MockModemModem3gppProxy> modem_3gpp_proxy_;
149 scoped_ptr<mm1::MockModemModemCdmaProxy> modem_cdma_proxy_;
150 scoped_ptr<mm1::MockModemProxy> modem_proxy_;
151 scoped_ptr<mm1::MockModemSimpleProxy> modem_simple_proxy_;
152 scoped_ptr<mm1::MockSimProxy> sim_proxy_;
153 TestProxyFactory proxy_factory_;
154 CellularCapabilityUniversal *capability_; // Owned by |cellular_|.
155 NiceMock<DeviceMockAdaptor> *device_adaptor_; // Owned by |cellular_|.
156};
157
158const char CellularCapabilityUniversalTest::kImei[] = "999911110000";
159
160TEST_F(CellularCapabilityUniversalTest, StartModem) {
161 EXPECT_CALL(*modem_proxy_,
162 Enable(true, _, _, CellularCapability::kTimeoutEnable))
163 .WillOnce(Invoke(this, &CellularCapabilityUniversalTest::InvokeEnable));
164 EXPECT_CALL(*modem_proxy_, AccessTechnologies())
165 .WillOnce(Return(MM_MODEM_ACCESS_TECHNOLOGY_LTE|
166 MM_MODEM_ACCESS_TECHNOLOGY_HSPA_PLUS));
167 EXPECT_CALL(*modem_3gpp_proxy_, EnabledFacilityLocks()).WillOnce(Return(0));
168 ::DBus::Struct< uint32_t, bool > quality;
169 quality._1 = 90;
170 quality._2 = true;
171 EXPECT_CALL(*modem_proxy_, SignalQuality()).WillOnce(Return(quality));
172 EXPECT_CALL(*modem_proxy_, Sim()).WillOnce(Return(""));
173 EXPECT_CALL(*modem_3gpp_proxy_, Imei()).WillOnce(Return(kImei));
174 EXPECT_CALL(*modem_3gpp_proxy_, RegistrationState())
175 .WillOnce(Return(MM_MODEM_3GPP_REGISTRATION_STATE_UNKNOWN));
176
177 // After setup we lose pointers to the proxies, so it is hard to set
178 // expectations.
179 SetUp();
180
181 Error error;
182 ResultCallback callback =
183 Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
184 capability_->StartModem(&error, callback);
185 EXPECT_TRUE(error.IsSuccess());
186}
187
188TEST_F(CellularCapabilityUniversalTest, StartModemFail) {
189 EXPECT_CALL(*modem_proxy_,
190 Enable(true, _, _, CellularCapability::kTimeoutEnable))
191 .WillOnce(
192 Invoke(this, &CellularCapabilityUniversalTest::InvokeEnableFail));
193 EXPECT_CALL(*this, TestCallback(IsFailure()));
194 ResultCallback callback =
195 Bind(&CellularCapabilityUniversalTest::TestCallback, Unretained(this));
196 SetUp();
197
198 Error error;
199 capability_->StartModem(&error, callback);
200 EXPECT_TRUE(error.IsSuccess());
201}
202
203} // namespace shill