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. 4 5 #include "chrome/browser/chromeos/mobile_config.h" 6 7 #include "base/time/time.h" 8 #include "testing/gtest/include/gtest/gtest.h" 9 10 namespace { 11 12 const char kBadManifest[] = "{\"version\": \"1\"}"; 13 14 const char kGoodMobileConfig[] = 15 "{" 16 " \"version\": \"1.0\"," 17 " \"carriers\" : {\n" 18 " \"carrier (country)\" : {\n" 19 " \"ids\" : [\n" 20 " {\n" 21 " \"id\": \"cr (country)\",\n" 22 " \"_comment\" : \"Based on SPN.\",\n" 23 " },\n" 24 " {\n" 25 " \"id\": \"Carrier (country)\",\n" 26 " \"_comment\" : \"Legacy ID when SPN is empty.\",\n" 27 " },\n" 28 " ],\n" 29 " \"top_up_url\" : \"http://www.carrier.com/\",\n" 30 " \"show_portal_button\" : true,\n" 31 " \"deals\" : [\n" 32 " {\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" 38 " \"en-US\" : {\n" 39 " \"notification_text\" : \"3G connectivity : Carrier.\",\n" 40 " },\n" 41 " \"default\" : {\n" 42 " \"notification_text\" : \"default_text.\",\n" 43 " },\n" 44 " },\n" 45 " },\n" 46 " ],\n" 47 " }," 48 " }," 49 " \"initial_locales\" : {\n" 50 " \"en-US\" : {\n" 51 " \"setup_url\" : \"accounts.carrier.com\",\n" 52 " }," 53 " }," 54 "}"; 55 56 const char kOldDealMobileConfig[] = 57 "{" 58 " \"version\": \"1.0\"," 59 " \"carriers\" : {\n" 60 " \"Carrier (country)\" : {\n" 61 " \"top_up_url\" : \"http://www.carrier.com/\",\n" 62 " \"deals\" : [\n" 63 " {\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" 69 " \"en-US\" : {\n" 70 " \"notification_text\" : \"3G connectivity : Carrier.\",\n" 71 " },\n" 72 " \"default\" : {\n" 73 " \"notification_text\" : \"default_text.\",\n" 74 " },\n" 75 " },\n" 76 " },\n" 77 " ],\n" 78 " }," 79 " }," 80 "}"; 81 82 const char kLocalMobileConfigNoDeals[] = 83 "{" 84 " \"version\": \"1.0\"," 85 " \"exclude_deals\": true," 86 " \"carriers\" : {\n" 87 " }," 88 "}"; 89 90 const char kLocalMobileConfig[] = 91 "{" 92 " \"version\": \"1.0\"," 93 " \"carriers\" : {\n" 94 " \"carrier (country)\" : {\n" 95 " \"exclude_deals\": true," 96 " \"top_up_url\" : \"http://www.carrier-new-url.com/\",\n" 97 " \"deals\" : [\n" 98 " {\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" 104 " \"en-GB\" : {\n" 105 " \"notification_text\" : \"3G connectivity : Carrier.\",\n" 106 " },\n" 107 " \"default\" : {\n" 108 " \"notification_text\" : \"default_text from local.\",\n" 109 " },\n" 110 " },\n" 111 " },\n" 112 " ],\n" 113 " }," 114 " }," 115 " \"initial_locales\" : {\n" 116 " \"en-US\" : {\n" 117 " \"setup_url\" : \"accounts.carrier.com/localized/\",\n" 118 " }," 119 " }," 120 "}"; 121 122 } // anonymous namespace 123 124 namespace chromeos { 125 126 TEST(MobileConfigTest, Basic) { 127 MobileConfig config(kGoodMobileConfig, "en-US"); 128 EXPECT_TRUE(config.IsReady()); 129 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")); 148 149 base::Time reference_time; 150 base::Time::FromString("31/12/2099 0:00", &reference_time); 151 EXPECT_EQ(reference_time, deal->expire_date()); 152 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()); 157 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); 163 } 164 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); 177 } 178 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); 188 } 189 190 TEST(MobileConfigTest, BadManifest) { 191 MobileConfig config(kBadManifest, "en-US"); 192 EXPECT_FALSE(config.IsReady()); 193 } 194 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); 208 } 209 210 TEST(MobileConfigTest, LocalConfig) { 211 MobileConfig config(kGoodMobileConfig, "en-GB"); 212 EXPECT_TRUE(config.IsReady()); 213 config.LoadManifestFromString(kLocalMobileConfig); 214 EXPECT_TRUE(config.IsReady()); 215 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()); 220 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()); 237 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()); 247 } 248 249 } // namespace chromeos 250