blob: 3834ff3071f63773220cb20c468d15fbf240f80b [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"
Jason Glasgow82f9ab32012-04-04 14:27:19 -040011#include "shill/cellular_capability_gsm.h"
Darin Petkovb72cf402011-11-22 14:51:39 +010012#include "shill/nice_mock_control.h"
13#include "shill/mock_adaptors.h"
Thieu Le3426c8f2012-01-11 17:35:11 -080014#include "shill/mock_metrics.h"
Eric Shienbroodc7073302012-04-03 14:41:44 -040015#include "shill/mock_profile.h"
Darin Petkovb72cf402011-11-22 14:51:39 +010016
Darin Petkov31332412012-01-28 01:50:02 +010017using std::string;
Darin Petkov9cb02682012-01-28 00:17:38 +010018using testing::_;
Eric Shienbroodc7073302012-04-03 14:41:44 -040019using testing::InSequence;
Darin Petkovb72cf402011-11-22 14:51:39 +010020using testing::NiceMock;
21
22namespace shill {
23
24class CellularServiceTest : public testing::Test {
25 public:
26 CellularServiceTest()
Darin Petkovac635a82012-01-10 16:51:58 +010027 : device_(new Cellular(&control_,
28 NULL,
Thieu Le3426c8f2012-01-11 17:35:11 -080029 &metrics_,
Darin Petkovac635a82012-01-10 16:51:58 +010030 NULL,
31 "usb0",
Darin Petkov31332412012-01-28 01:50:02 +010032 kAddress,
Darin Petkovac635a82012-01-10 16:51:58 +010033 3,
34 Cellular::kTypeGSM,
35 "",
36 "",
Jason Glasgowa585fc32012-06-06 11:04:09 -040037 "",
Darin Petkovac635a82012-01-10 16:51:58 +010038 NULL)),
Thieu Le3426c8f2012-01-11 17:35:11 -080039 service_(new CellularService(&control_, NULL, &metrics_, NULL,
40 device_)),
Darin Petkovb72cf402011-11-22 14:51:39 +010041 adaptor_(NULL) {}
42
43 virtual ~CellularServiceTest() {
44 adaptor_ = NULL;
45 }
46
47 virtual void SetUp() {
48 adaptor_ =
49 dynamic_cast<NiceMock<ServiceMockAdaptor> *>(service_->adaptor());
50 }
51
Jason Glasgow82f9ab32012-04-04 14:27:19 -040052 CellularCapabilityGSM *GetCapabilityGSM() {
53 return dynamic_cast<CellularCapabilityGSM *>(device_->capability_.get());
54 }
55
Darin Petkovb72cf402011-11-22 14:51:39 +010056 protected:
Darin Petkov31332412012-01-28 01:50:02 +010057 static const char kAddress[];
58
Darin Petkovb72cf402011-11-22 14:51:39 +010059 NiceMockControl control_;
Thieu Le3426c8f2012-01-11 17:35:11 -080060 MockMetrics metrics_;
Darin Petkovac635a82012-01-10 16:51:58 +010061 CellularRefPtr device_;
Darin Petkovb72cf402011-11-22 14:51:39 +010062 CellularServiceRefPtr service_;
63 NiceMock<ServiceMockAdaptor> *adaptor_; // Owned by |service_|.
64};
65
Darin Petkov31332412012-01-28 01:50:02 +010066const char CellularServiceTest::kAddress[] = "000102030405";
67
Thieu Le9fdf1372012-05-14 15:20:48 -070068TEST_F(CellularServiceTest, Constructor) {
69 EXPECT_TRUE(service_->connectable());
70}
71
Darin Petkovb72cf402011-11-22 14:51:39 +010072TEST_F(CellularServiceTest, SetNetworkTechnology) {
73 EXPECT_CALL(*adaptor_, EmitStringChanged(flimflam::kNetworkTechnologyProperty,
74 flimflam::kNetworkTechnologyUmts));
75 EXPECT_TRUE(service_->network_technology().empty());
76 service_->SetNetworkTechnology(flimflam::kNetworkTechnologyUmts);
77 EXPECT_EQ(flimflam::kNetworkTechnologyUmts, service_->network_technology());
78 service_->SetNetworkTechnology(flimflam::kNetworkTechnologyUmts);
79}
80
81TEST_F(CellularServiceTest, SetRoamingState) {
82 EXPECT_CALL(*adaptor_, EmitStringChanged(flimflam::kRoamingStateProperty,
83 flimflam::kRoamingStateHome));
84 EXPECT_TRUE(service_->roaming_state().empty());
85 service_->SetRoamingState(flimflam::kRoamingStateHome);
86 EXPECT_EQ(flimflam::kRoamingStateHome, service_->roaming_state());
87 service_->SetRoamingState(flimflam::kRoamingStateHome);
88}
89
Darin Petkovac635a82012-01-10 16:51:58 +010090TEST_F(CellularServiceTest, FriendlyName) {
91 static const char kCarrier[] = "Cellular Carrier";
Jason Glasgow82f9ab32012-04-04 14:27:19 -040092 GetCapabilityGSM()->carrier_ = kCarrier;
Thieu Le3426c8f2012-01-11 17:35:11 -080093 service_ = new CellularService(&control_, NULL, &metrics_, NULL, device_);
Darin Petkovac635a82012-01-10 16:51:58 +010094 EXPECT_EQ(kCarrier, service_->friendly_name());
95}
96
Darin Petkov31332412012-01-28 01:50:02 +010097TEST_F(CellularServiceTest, SetStorageIdentifier) {
Darin Petkovdd3e8662012-02-03 13:16:20 +010098 EXPECT_EQ(string(flimflam::kTypeCellular) + "_" +
99 kAddress + "_" + service_->friendly_name(),
Darin Petkov31332412012-01-28 01:50:02 +0100100 service_->GetStorageIdentifier());
101 service_->SetStorageIdentifier("a b c");
102 EXPECT_EQ("a_b_c", service_->GetStorageIdentifier());
103}
104
Darin Petkov9cb02682012-01-28 00:17:38 +0100105TEST_F(CellularServiceTest, SetServingOperator) {
106 EXPECT_CALL(*adaptor_,
107 EmitStringmapChanged(flimflam::kServingOperatorProperty, _));
108 static const char kCode[] = "123456";
109 static const char kName[] = "Some Cellular Operator";
110 Cellular::Operator oper;
111 service_->SetServingOperator(oper);
112 oper.SetCode(kCode);
113 oper.SetName(kName);
114 service_->SetServingOperator(oper);
115 EXPECT_EQ(kCode, service_->serving_operator().GetCode());
116 EXPECT_EQ(kName, service_->serving_operator().GetName());
117 service_->SetServingOperator(oper);
118}
119
Darin Petkov381928f2012-02-02 23:00:12 +0100120TEST_F(CellularServiceTest, SetOLP) {
121 EXPECT_CALL(*adaptor_,
122 EmitStringmapChanged(flimflam::kPaymentPortalProperty, _));
123 static const char kURL[] = "payment.url";
124 static const char kMethod[] = "GET";
125 CellularService::OLP olp;
126 service_->SetOLP(olp);
127 olp.SetURL(kURL);
128 olp.SetMethod(kMethod);
129 service_->SetOLP(olp);
130 EXPECT_EQ(kURL, service_->olp().GetURL());
131 EXPECT_EQ(kMethod, service_->olp().GetMethod());
132 service_->SetOLP(olp);
133}
134
135TEST_F(CellularServiceTest, SetUsageURL) {
136 static const char kUsageURL[] = "usage.url";
137 EXPECT_CALL(*adaptor_, EmitStringChanged(flimflam::kUsageURLProperty,
138 kUsageURL));
139 EXPECT_TRUE(service_->usage_url().empty());
140 service_->SetUsageURL(kUsageURL);
141 EXPECT_EQ(kUsageURL, service_->usage_url());
142 service_->SetUsageURL(kUsageURL);
143}
144
Eric Shienbroodc7073302012-04-03 14:41:44 -0400145TEST_F(CellularServiceTest, SetApn) {
146 static const char kApn[] = "TheAPN";
147 static const char kUsername[] = "commander.data";
148 ProfileRefPtr profile(new NiceMock<MockProfile>(
149 &control_, reinterpret_cast<Manager *>(NULL)));
150 service_->set_profile(profile);
151 Error error;
152 Stringmap testapn;
153 testapn[flimflam::kApnProperty] = kApn;
154 testapn[flimflam::kApnUsernameProperty] = kUsername;
155 {
156 InSequence seq;
157 EXPECT_CALL(*adaptor_,
158 EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty,
159 _));
160 EXPECT_CALL(*adaptor_,
161 EmitStringmapChanged(flimflam::kCellularApnProperty, _));
162 }
163 service_->SetApn(testapn, &error);
164 EXPECT_TRUE(error.IsSuccess());
165 Stringmap resultapn = service_->GetApn(&error);
166 EXPECT_TRUE(error.IsSuccess());
167 EXPECT_EQ(2, resultapn.size());
168 Stringmap::const_iterator it = resultapn.find(flimflam::kApnProperty);
169 EXPECT_TRUE(it != resultapn.end() && it->second == kApn);
170 it = resultapn.find(flimflam::kApnUsernameProperty);
171 EXPECT_TRUE(it != resultapn.end() && it->second == kUsername);
172 EXPECT_FALSE(service_->GetUserSpecifiedApn() == NULL);
173}
174
175TEST_F(CellularServiceTest, ClearApn) {
176 static const char kApn[] = "TheAPN";
177 static const char kUsername[] = "commander.data";
178 ProfileRefPtr profile(new NiceMock<MockProfile>(
179 &control_, reinterpret_cast<Manager *>(NULL)));
180 service_->set_profile(profile);
181 Error error;
182 // Set up an APN to make sure that it later gets cleared.
183 Stringmap testapn;
184 testapn[flimflam::kApnProperty] = kApn;
185 testapn[flimflam::kApnUsernameProperty] = kUsername;
186 {
187 InSequence seq;
188 EXPECT_CALL(*adaptor_,
189 EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty,
190 _));
191 EXPECT_CALL(*adaptor_,
192 EmitStringmapChanged(flimflam::kCellularApnProperty, _));
193 }
194 service_->SetApn(testapn, &error);
195 Stringmap resultapn = service_->GetApn(&error);
196 ASSERT_TRUE(error.IsSuccess());
197 ASSERT_EQ(2, service_->GetApn(&error).size());
198
199 Stringmap emptyapn;
200 EXPECT_CALL(*adaptor_,
201 EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty,
202 _)).Times(0);
203 EXPECT_CALL(*adaptor_,
204 EmitStringmapChanged(flimflam::kCellularApnProperty, _)).Times(1);
205 service_->SetApn(emptyapn, &error);
206 EXPECT_TRUE(error.IsSuccess());
207 resultapn = service_->GetApn(&error);
208 EXPECT_TRUE(resultapn.empty());
209 EXPECT_TRUE(service_->GetUserSpecifiedApn() == NULL);
210}
211
212TEST_F(CellularServiceTest, LastGoodApn) {
213 static const char kApn[] = "TheAPN";
214 static const char kUsername[] = "commander.data";
215 ProfileRefPtr profile(new NiceMock<MockProfile>(
216 &control_, reinterpret_cast<Manager *>(NULL)));
217 service_->set_profile(profile);
218 Stringmap testapn;
219 testapn[flimflam::kApnProperty] = kApn;
220 testapn[flimflam::kApnUsernameProperty] = kUsername;
221 EXPECT_CALL(*adaptor_,
222 EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty, _));
223 service_->SetLastGoodApn(testapn);
224 Stringmap *resultapn = service_->GetLastGoodApn();
225 EXPECT_FALSE(resultapn == NULL);
226 EXPECT_EQ(2, resultapn->size());
227 Stringmap::const_iterator it = resultapn->find(flimflam::kApnProperty);
228 EXPECT_TRUE(it != resultapn->end() && it->second == kApn);
229 it = resultapn->find(flimflam::kApnUsernameProperty);
230 EXPECT_TRUE(it != resultapn->end() && it->second == kUsername);
231 // Now set the user-specified APN, and check that LastGoodApn got
232 // cleared.
233 Stringmap userapn;
234 userapn[flimflam::kApnProperty] = kApn;
235 userapn[flimflam::kApnUsernameProperty] = kUsername;
236 {
237 InSequence seq;
238 EXPECT_CALL(*adaptor_,
239 EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty,
240 _));
241 EXPECT_CALL(*adaptor_,
242 EmitStringmapChanged(flimflam::kCellularApnProperty, _));
243 }
244 Error error;
245 service_->SetApn(userapn, &error);
246 EXPECT_TRUE(service_->GetLastGoodApn() == NULL);
247}
248
Darin Petkovb72cf402011-11-22 14:51:39 +0100249} // namespace shill