1 // Copyright (c) 2012 The Chromium 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.
5 #include "chrome/browser/chromeos/mobile_config.h"
7 #include "base/time/time.h"
8 #include "testing/gtest/include/gtest/gtest.h"
12 const char kBadManifest
[] = "{\"version\": \"1\"}";
14 const char kGoodMobileConfig
[] =
16 " \"version\": \"1.0\","
18 " \"carrier (country)\" : {\n"
21 " \"id\": \"cr (country)\",\n"
22 " \"_comment\" : \"Based on SPN.\",\n"
25 " \"id\": \"Carrier (country)\",\n"
26 " \"_comment\" : \"Legacy ID when SPN is empty.\",\n"
29 " \"top_up_url\" : \"http://www.carrier.com/\",\n"
30 " \"show_portal_button\" : true,\n"
33 " \"deal_id\" : \"0\",\n"
34 " \"locales\" : [ \"en-US\", ],\n"
35 " \"expire_date\" : \"31/12/2099 0:0\",\n"
36 " \"notification_count\" : 1,\n"
37 " \"localized_content\" : {\n"
39 " \"notification_text\" : \"3G connectivity : Carrier.\",\n"
42 " \"notification_text\" : \"default_text.\",\n"
49 " \"initial_locales\" : {\n"
51 " \"setup_url\" : \"accounts.carrier.com\",\n"
56 const char kOldDealMobileConfig
[] =
58 " \"version\": \"1.0\","
60 " \"Carrier (country)\" : {\n"
61 " \"top_up_url\" : \"http://www.carrier.com/\",\n"
64 " \"deal_id\" : \"0\",\n"
65 " \"locales\" : [ \"en-US\", ],\n"
66 " \"expire_date\" : \"01/01/01 0:0\",\n"
67 " \"notification_count\" : 1,\n"
68 " \"localized_content\" : {\n"
70 " \"notification_text\" : \"3G connectivity : Carrier.\",\n"
73 " \"notification_text\" : \"default_text.\",\n"
82 const char kLocalMobileConfigNoDeals
[] =
84 " \"version\": \"1.0\","
85 " \"exclude_deals\": true,"
90 const char kLocalMobileConfig
[] =
92 " \"version\": \"1.0\","
94 " \"carrier (country)\" : {\n"
95 " \"exclude_deals\": true,"
96 " \"top_up_url\" : \"http://www.carrier-new-url.com/\",\n"
99 " \"deal_id\" : \"1\",\n"
100 " \"locales\" : [ \"en-GB\", ],\n"
101 " \"expire_date\" : \"31/12/2099 0:0\",\n"
102 " \"notification_count\" : 2,\n"
103 " \"localized_content\" : {\n"
105 " \"notification_text\" : \"3G connectivity : Carrier.\",\n"
108 " \"notification_text\" : \"default_text from local.\",\n"
115 " \"initial_locales\" : {\n"
117 " \"setup_url\" : \"accounts.carrier.com/localized/\",\n"
122 } // anonymous namespace
126 TEST(MobileConfigTest
, Basic
) {
127 MobileConfig
config(kGoodMobileConfig
, "en-US");
128 EXPECT_TRUE(config
.IsReady());
130 const MobileConfig::Carrier
* carrier
;
131 carrier
= config
.GetCarrier("Carrier (country)");
132 EXPECT_TRUE(carrier
!= NULL
);
133 carrier
= config
.GetCarrier("cr (country)");
134 EXPECT_TRUE(carrier
!= NULL
);
135 EXPECT_EQ("http://www.carrier.com/", carrier
->top_up_url());
136 EXPECT_EQ(true, carrier
->show_portal_button());
137 const MobileConfig::CarrierDeal
* deal
;
138 deal
= carrier
->GetDefaultDeal();
139 EXPECT_TRUE(deal
!= NULL
);
140 deal
= carrier
->GetDeal("0");
141 EXPECT_TRUE(deal
!= NULL
);
142 EXPECT_EQ("en-US", deal
->locales()[0]);
143 EXPECT_EQ(1, deal
->notification_count());
144 EXPECT_EQ("3G connectivity : Carrier.",
145 deal
->GetLocalizedString("en-US", "notification_text"));
146 EXPECT_EQ("default_text.",
147 deal
->GetLocalizedString("en", "notification_text"));
149 base::Time reference_time
;
150 base::Time::FromString("31/12/2099 0:00", &reference_time
);
151 EXPECT_EQ(reference_time
, deal
->expire_date());
153 const MobileConfig::LocaleConfig
* locale_config
;
154 locale_config
= config
.GetLocaleConfig();
155 EXPECT_TRUE(locale_config
!= NULL
);
156 EXPECT_EQ("accounts.carrier.com", locale_config
->setup_url());
158 // Check same manifest but with another initial locale.
159 MobileConfig
config_uk(kGoodMobileConfig
, "en-GB");
160 EXPECT_TRUE(config_uk
.IsReady());
161 locale_config
= config_uk
.GetLocaleConfig();
162 EXPECT_TRUE(locale_config
== NULL
);
165 TEST(MobileConfigTest
, OldDeal
) {
166 MobileConfig
config(kOldDealMobileConfig
, "en-US");
167 EXPECT_TRUE(config
.IsReady());
168 const MobileConfig::Carrier
* carrier
;
169 carrier
= config
.GetCarrier("Carrier (country)");
170 EXPECT_TRUE(carrier
!= NULL
);
171 // Check default value.
172 EXPECT_FALSE(carrier
->show_portal_button());
173 const MobileConfig::CarrierDeal
* deal
;
174 // TODO(nkostylev): Pass fixed time instead of relying on Time::Now().
175 deal
= carrier
->GetDefaultDeal();
176 EXPECT_TRUE(deal
== NULL
);
179 TEST(MobileConfigTest
, DealOtherLocale
) {
180 MobileConfig
config(kGoodMobileConfig
, "en-GB");
181 EXPECT_TRUE(config
.IsReady());
182 const MobileConfig::Carrier
* carrier
;
183 carrier
= config
.GetCarrier("Carrier (country)");
184 EXPECT_TRUE(carrier
!= NULL
);
185 const MobileConfig::CarrierDeal
* deal
;
186 deal
= carrier
->GetDefaultDeal();
187 EXPECT_TRUE(deal
== NULL
);
190 TEST(MobileConfigTest
, BadManifest
) {
191 MobileConfig
config(kBadManifest
, "en-US");
192 EXPECT_FALSE(config
.IsReady());
195 TEST(MobileConfigTest
, LocalConfigNoDeals
) {
196 MobileConfig
config(kGoodMobileConfig
, "en-US");
197 EXPECT_TRUE(config
.IsReady());
198 config
.LoadManifestFromString(kLocalMobileConfigNoDeals
);
199 EXPECT_TRUE(config
.IsReady());
200 const MobileConfig::Carrier
* carrier
;
201 carrier
= config
.GetCarrier("Carrier (country)");
202 EXPECT_TRUE(carrier
!= NULL
);
203 const MobileConfig::CarrierDeal
* deal
;
204 deal
= carrier
->GetDefaultDeal();
205 EXPECT_TRUE(deal
== NULL
);
206 deal
= carrier
->GetDeal("0");
207 EXPECT_TRUE(deal
== NULL
);
210 TEST(MobileConfigTest
, LocalConfig
) {
211 MobileConfig
config(kGoodMobileConfig
, "en-GB");
212 EXPECT_TRUE(config
.IsReady());
213 config
.LoadManifestFromString(kLocalMobileConfig
);
214 EXPECT_TRUE(config
.IsReady());
216 const MobileConfig::Carrier
* carrier
;
217 carrier
= config
.GetCarrier("Carrier (country)");
218 EXPECT_TRUE(carrier
!= NULL
);
219 EXPECT_EQ("http://www.carrier-new-url.com/", carrier
->top_up_url());
221 const MobileConfig::CarrierDeal
* deal
;
222 deal
= carrier
->GetDeal("0");
223 EXPECT_TRUE(deal
== NULL
);
224 deal
= carrier
->GetDefaultDeal();
225 EXPECT_TRUE(deal
!= NULL
);
226 deal
= carrier
->GetDeal("1");
227 EXPECT_TRUE(deal
!= NULL
);
228 EXPECT_EQ("en-GB", deal
->locales()[0]);
229 EXPECT_EQ(2, deal
->notification_count());
230 EXPECT_EQ("3G connectivity : Carrier.",
231 deal
->GetLocalizedString("en-GB", "notification_text"));
232 EXPECT_EQ("default_text from local.",
233 deal
->GetLocalizedString("en", "notification_text"));
234 base::Time reference_time
;
235 base::Time::FromString("31/12/2099 0:00", &reference_time
);
236 EXPECT_EQ(reference_time
, deal
->expire_date());
238 // Now reload same global/local config files but with proper initial locale.
239 MobileConfig
config_us(kGoodMobileConfig
, "en-US");
240 EXPECT_TRUE(config_us
.IsReady());
241 config_us
.LoadManifestFromString(kLocalMobileConfig
);
242 EXPECT_TRUE(config_us
.IsReady());
243 const MobileConfig::LocaleConfig
* locale_config
;
244 locale_config
= config_us
.GetLocaleConfig();
245 EXPECT_TRUE(locale_config
!= NULL
);
246 EXPECT_EQ("accounts.carrier.com/localized/", locale_config
->setup_url());
249 } // namespace chromeos