blob: 2393f577d980b1756242277e2d1361aef62ae517 [file] [log] [blame]
Darin Petkovac635a82012-01-10 16:51:58 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkovb72cf402011-11-22 14:51:39 +01002// 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_service.h"
6
7#include <chromeos/dbus/service_constants.h>
8#include <gtest/gtest.h>
9
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050010#include "shill/cellular_capability.h"
Ben Chan3ecdf822012-08-06 12:29:23 -070011#include "shill/cellular_capability_cdma.h"
Darin Petkovb72cf402011-11-22 14:51:39 +010012#include "shill/mock_adaptors.h"
Thieu Le3426c8f2012-01-11 17:35:11 -080013#include "shill/mock_metrics.h"
Eric Shienbroodc7073302012-04-03 14:41:44 -040014#include "shill/mock_profile.h"
Ben Chan3ecdf822012-08-06 12:29:23 -070015#include "shill/nice_mock_control.h"
16#include "shill/proxy_factory.h"
Darin Petkovb72cf402011-11-22 14:51:39 +010017
Darin Petkov31332412012-01-28 01:50:02 +010018using std::string;
Darin Petkov9cb02682012-01-28 00:17:38 +010019using testing::_;
Eric Shienbroodc7073302012-04-03 14:41:44 -040020using testing::InSequence;
Darin Petkovb72cf402011-11-22 14:51:39 +010021using testing::NiceMock;
22
23namespace shill {
24
25class CellularServiceTest : public testing::Test {
26 public:
27 CellularServiceTest()
Darin Petkovac635a82012-01-10 16:51:58 +010028 : device_(new Cellular(&control_,
29 NULL,
Thieu Le3426c8f2012-01-11 17:35:11 -080030 &metrics_,
Darin Petkovac635a82012-01-10 16:51:58 +010031 NULL,
32 "usb0",
Darin Petkov31332412012-01-28 01:50:02 +010033 kAddress,
Darin Petkovac635a82012-01-10 16:51:58 +010034 3,
Ben Chan3ecdf822012-08-06 12:29:23 -070035 Cellular::kTypeCDMA,
Darin Petkovac635a82012-01-10 16:51:58 +010036 "",
37 "",
Jason Glasgowa585fc32012-06-06 11:04:09 -040038 "",
Ben Chan3ecdf822012-08-06 12:29:23 -070039 NULL,
40 ProxyFactory::GetInstance())),
Thieu Le3426c8f2012-01-11 17:35:11 -080041 service_(new CellularService(&control_, NULL, &metrics_, NULL,
42 device_)),
Darin Petkovb72cf402011-11-22 14:51:39 +010043 adaptor_(NULL) {}
44
45 virtual ~CellularServiceTest() {
46 adaptor_ = NULL;
47 }
48
49 virtual void SetUp() {
50 adaptor_ =
51 dynamic_cast<NiceMock<ServiceMockAdaptor> *>(service_->adaptor());
52 }
53
Ben Chan3ecdf822012-08-06 12:29:23 -070054 CellularCapabilityCDMA *GetCapabilityCDMA() {
55 return dynamic_cast<CellularCapabilityCDMA *>(device_->capability_.get());
Jason Glasgow82f9ab32012-04-04 14:27:19 -040056 }
57
Darin Petkovb72cf402011-11-22 14:51:39 +010058 protected:
Darin Petkov31332412012-01-28 01:50:02 +010059 static const char kAddress[];
60
Darin Petkovb72cf402011-11-22 14:51:39 +010061 NiceMockControl control_;
Thieu Le3426c8f2012-01-11 17:35:11 -080062 MockMetrics metrics_;
Darin Petkovac635a82012-01-10 16:51:58 +010063 CellularRefPtr device_;
Darin Petkovb72cf402011-11-22 14:51:39 +010064 CellularServiceRefPtr service_;
65 NiceMock<ServiceMockAdaptor> *adaptor_; // Owned by |service_|.
66};
67
Darin Petkov31332412012-01-28 01:50:02 +010068const char CellularServiceTest::kAddress[] = "000102030405";
69
Thieu Le9fdf1372012-05-14 15:20:48 -070070TEST_F(CellularServiceTest, Constructor) {
71 EXPECT_TRUE(service_->connectable());
72}
73
Darin Petkovb72cf402011-11-22 14:51:39 +010074TEST_F(CellularServiceTest, SetNetworkTechnology) {
75 EXPECT_CALL(*adaptor_, EmitStringChanged(flimflam::kNetworkTechnologyProperty,
76 flimflam::kNetworkTechnologyUmts));
77 EXPECT_TRUE(service_->network_technology().empty());
78 service_->SetNetworkTechnology(flimflam::kNetworkTechnologyUmts);
79 EXPECT_EQ(flimflam::kNetworkTechnologyUmts, service_->network_technology());
80 service_->SetNetworkTechnology(flimflam::kNetworkTechnologyUmts);
81}
82
83TEST_F(CellularServiceTest, SetRoamingState) {
84 EXPECT_CALL(*adaptor_, EmitStringChanged(flimflam::kRoamingStateProperty,
85 flimflam::kRoamingStateHome));
86 EXPECT_TRUE(service_->roaming_state().empty());
87 service_->SetRoamingState(flimflam::kRoamingStateHome);
88 EXPECT_EQ(flimflam::kRoamingStateHome, service_->roaming_state());
89 service_->SetRoamingState(flimflam::kRoamingStateHome);
90}
91
Darin Petkovac635a82012-01-10 16:51:58 +010092TEST_F(CellularServiceTest, FriendlyName) {
93 static const char kCarrier[] = "Cellular Carrier";
Ben Chan3ecdf822012-08-06 12:29:23 -070094 GetCapabilityCDMA()->carrier_ = kCarrier;
Thieu Le3426c8f2012-01-11 17:35:11 -080095 service_ = new CellularService(&control_, NULL, &metrics_, NULL, device_);
Darin Petkovac635a82012-01-10 16:51:58 +010096 EXPECT_EQ(kCarrier, service_->friendly_name());
97}
98
Darin Petkov31332412012-01-28 01:50:02 +010099TEST_F(CellularServiceTest, SetStorageIdentifier) {
Darin Petkovdd3e8662012-02-03 13:16:20 +0100100 EXPECT_EQ(string(flimflam::kTypeCellular) + "_" +
101 kAddress + "_" + service_->friendly_name(),
Darin Petkov31332412012-01-28 01:50:02 +0100102 service_->GetStorageIdentifier());
103 service_->SetStorageIdentifier("a b c");
104 EXPECT_EQ("a_b_c", service_->GetStorageIdentifier());
105}
106
Darin Petkov9cb02682012-01-28 00:17:38 +0100107TEST_F(CellularServiceTest, SetServingOperator) {
108 EXPECT_CALL(*adaptor_,
109 EmitStringmapChanged(flimflam::kServingOperatorProperty, _));
110 static const char kCode[] = "123456";
111 static const char kName[] = "Some Cellular Operator";
112 Cellular::Operator oper;
113 service_->SetServingOperator(oper);
114 oper.SetCode(kCode);
115 oper.SetName(kName);
116 service_->SetServingOperator(oper);
117 EXPECT_EQ(kCode, service_->serving_operator().GetCode());
118 EXPECT_EQ(kName, service_->serving_operator().GetName());
119 service_->SetServingOperator(oper);
120}
121
Darin Petkov381928f2012-02-02 23:00:12 +0100122TEST_F(CellularServiceTest, SetOLP) {
123 EXPECT_CALL(*adaptor_,
124 EmitStringmapChanged(flimflam::kPaymentPortalProperty, _));
125 static const char kURL[] = "payment.url";
126 static const char kMethod[] = "GET";
127 CellularService::OLP olp;
128 service_->SetOLP(olp);
129 olp.SetURL(kURL);
130 olp.SetMethod(kMethod);
131 service_->SetOLP(olp);
132 EXPECT_EQ(kURL, service_->olp().GetURL());
133 EXPECT_EQ(kMethod, service_->olp().GetMethod());
134 service_->SetOLP(olp);
135}
136
137TEST_F(CellularServiceTest, SetUsageURL) {
138 static const char kUsageURL[] = "usage.url";
139 EXPECT_CALL(*adaptor_, EmitStringChanged(flimflam::kUsageURLProperty,
140 kUsageURL));
141 EXPECT_TRUE(service_->usage_url().empty());
142 service_->SetUsageURL(kUsageURL);
143 EXPECT_EQ(kUsageURL, service_->usage_url());
144 service_->SetUsageURL(kUsageURL);
145}
146
Eric Shienbroodc7073302012-04-03 14:41:44 -0400147TEST_F(CellularServiceTest, SetApn) {
148 static const char kApn[] = "TheAPN";
149 static const char kUsername[] = "commander.data";
150 ProfileRefPtr profile(new NiceMock<MockProfile>(
151 &control_, reinterpret_cast<Manager *>(NULL)));
152 service_->set_profile(profile);
153 Error error;
154 Stringmap testapn;
155 testapn[flimflam::kApnProperty] = kApn;
156 testapn[flimflam::kApnUsernameProperty] = kUsername;
157 {
158 InSequence seq;
159 EXPECT_CALL(*adaptor_,
160 EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty,
161 _));
162 EXPECT_CALL(*adaptor_,
163 EmitStringmapChanged(flimflam::kCellularApnProperty, _));
164 }
165 service_->SetApn(testapn, &error);
166 EXPECT_TRUE(error.IsSuccess());
167 Stringmap resultapn = service_->GetApn(&error);
168 EXPECT_TRUE(error.IsSuccess());
169 EXPECT_EQ(2, resultapn.size());
170 Stringmap::const_iterator it = resultapn.find(flimflam::kApnProperty);
171 EXPECT_TRUE(it != resultapn.end() && it->second == kApn);
172 it = resultapn.find(flimflam::kApnUsernameProperty);
173 EXPECT_TRUE(it != resultapn.end() && it->second == kUsername);
174 EXPECT_FALSE(service_->GetUserSpecifiedApn() == NULL);
175}
176
177TEST_F(CellularServiceTest, ClearApn) {
178 static const char kApn[] = "TheAPN";
179 static const char kUsername[] = "commander.data";
180 ProfileRefPtr profile(new NiceMock<MockProfile>(
181 &control_, reinterpret_cast<Manager *>(NULL)));
182 service_->set_profile(profile);
183 Error error;
184 // Set up an APN to make sure that it later gets cleared.
185 Stringmap testapn;
186 testapn[flimflam::kApnProperty] = kApn;
187 testapn[flimflam::kApnUsernameProperty] = kUsername;
188 {
189 InSequence seq;
190 EXPECT_CALL(*adaptor_,
191 EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty,
192 _));
193 EXPECT_CALL(*adaptor_,
194 EmitStringmapChanged(flimflam::kCellularApnProperty, _));
195 }
196 service_->SetApn(testapn, &error);
197 Stringmap resultapn = service_->GetApn(&error);
198 ASSERT_TRUE(error.IsSuccess());
199 ASSERT_EQ(2, service_->GetApn(&error).size());
200
201 Stringmap emptyapn;
202 EXPECT_CALL(*adaptor_,
203 EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty,
204 _)).Times(0);
205 EXPECT_CALL(*adaptor_,
206 EmitStringmapChanged(flimflam::kCellularApnProperty, _)).Times(1);
207 service_->SetApn(emptyapn, &error);
208 EXPECT_TRUE(error.IsSuccess());
209 resultapn = service_->GetApn(&error);
210 EXPECT_TRUE(resultapn.empty());
211 EXPECT_TRUE(service_->GetUserSpecifiedApn() == NULL);
212}
213
214TEST_F(CellularServiceTest, LastGoodApn) {
215 static const char kApn[] = "TheAPN";
216 static const char kUsername[] = "commander.data";
217 ProfileRefPtr profile(new NiceMock<MockProfile>(
218 &control_, reinterpret_cast<Manager *>(NULL)));
219 service_->set_profile(profile);
220 Stringmap testapn;
221 testapn[flimflam::kApnProperty] = kApn;
222 testapn[flimflam::kApnUsernameProperty] = kUsername;
223 EXPECT_CALL(*adaptor_,
224 EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty, _));
225 service_->SetLastGoodApn(testapn);
226 Stringmap *resultapn = service_->GetLastGoodApn();
227 EXPECT_FALSE(resultapn == NULL);
228 EXPECT_EQ(2, resultapn->size());
229 Stringmap::const_iterator it = resultapn->find(flimflam::kApnProperty);
230 EXPECT_TRUE(it != resultapn->end() && it->second == kApn);
231 it = resultapn->find(flimflam::kApnUsernameProperty);
232 EXPECT_TRUE(it != resultapn->end() && it->second == kUsername);
233 // Now set the user-specified APN, and check that LastGoodApn got
234 // cleared.
235 Stringmap userapn;
236 userapn[flimflam::kApnProperty] = kApn;
237 userapn[flimflam::kApnUsernameProperty] = kUsername;
238 {
239 InSequence seq;
240 EXPECT_CALL(*adaptor_,
241 EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty,
242 _));
243 EXPECT_CALL(*adaptor_,
244 EmitStringmapChanged(flimflam::kCellularApnProperty, _));
245 }
246 Error error;
247 service_->SetApn(userapn, &error);
248 EXPECT_TRUE(service_->GetLastGoodApn() == NULL);
249}
250
Darin Petkovb72cf402011-11-22 14:51:39 +0100251} // namespace shill