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