1// Copyright (c) 2011 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/customization_document.h"
6
7#include "base/time.h"
8#include "chrome/browser/chromeos/mock_system_access.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11namespace {
12
13const char kGoodStartupManifest[] =
14    "{"
15    "  \"version\": \"1.0\","
16    "  \"initial_locale\" : \"en-US\","
17    "  \"initial_timezone\" : \"US/Pacific\","
18    "  \"keyboard_layout\" : \"xkb:us::eng\","
19    "  \"registration_url\" : \"http://www.google.com\","
20    "  \"setup_content\" : {"
21    "    \"en-US\" : {"
22    "      \"help_page\" : \"file:///opt/oem/help/en-US/help.html\","
23    "      \"eula_page\" : \"file:///opt/oem/eula/en-US/eula.html\","
24    "    },"
25    "    \"ru-RU\" : {"
26    "      \"help_page\" : \"file:///opt/oem/help/ru-RU/help.html\","
27    "      \"eula_page\" : \"file:///opt/oem/eula/ru-RU/eula.html\","
28    "    },"
29    "    \"default\" : {"
30    "      \"help_page\" : \"file:///opt/oem/help/en/help.html\","
31    "      \"eula_page\" : \"file:///opt/oem/eula/en/eula.html\","
32    "    },"
33    "  },"
34    "  \"hwid_map\" : ["
35    "    {"
36    "      \"hwid_mask\": \"ZGA*34\","
37    "      \"initial_locale\" : \"ja\","
38    "      \"initial_timezone\" : \"Asia/Tokyo\","
39    "      \"keyboard_layout\" : \"mozc-jp\","
40    "    },"
41    "    {"
42    "      \"hwid_mask\": \"Mario 1?3*\","
43    "      \"initial_locale\" : \"ru-RU\","
44    "      \"initial_timezone\" : \"Europe/Moscow\","
45    "      \"keyboard_layout\" : \"xkb:ru::rus\","
46    "    },"
47    "  ],"
48    "}";
49
50const char kBadManifest[] = "{\"version\": \"1\"}";
51
52const char kGoodServicesManifest[] =
53    "{"
54    "  \"version\": \"1.0\","
55    "  \"app_content\" : {"
56    "    \"en-US\" : {"
57    "      \"initial_start_page\": \"http://mario/promo\","
58    "      \"support_page\": \"http://mario/us\","
59    "    },"
60    "    \"ru-RU\" : {"
61    "      \"initial_start_page\": \"http://mario/ru/promo\","
62    "      \"support_page\": \"http://mario/ru\","
63    "    },"
64    "    \"default\" : {"
65    "      \"initial_start_page\": \"http://mario/global/promo\","
66    "      \"support_page\": \"http://mario/global\","
67    "    },"
68    "  },"
69    "  \"carrier_deals\" : {"
70    "    \"Carrier (country)\" : {"
71    "      \"deal_locale\" : \"en-US\","
72    "      \"top_up_url\" : \"http://www.carrier.com/\","
73    "      \"notification_count\" : 1,\n"
74    "      \"expire_date\" : \"31/12/12 0:00\","
75    "      \"localized_content\" : {"
76    "        \"en-US\" : {"
77    "          \"notification_text\" : \"3G connectivity : Carrier.\","
78    "        },"
79    "        \"default\" : {"
80    "          \"notification_text\" : \"default_text.\","
81    "        },"
82    "      },"
83    "    },"
84    "  },"
85    "}";
86
87const char kOldDealServicesManifest[] =
88    "{"
89    "  \"version\": \"1.0\","
90    "  \"carrier_deals\" : {"
91    "    \"Carrier (country)\" : {"
92    "      \"deal_locale\" : \"en-US\","
93    "      \"top_up_url\" : \"http://www.carrier.com/\","
94    "      \"notification_count\" : 1,"
95    "      \"expire_date\" : \"01/01/01 0:00\","
96    "      \"localized_content\" : {"
97    "        \"en-US\" : {"
98    "          \"notification_text\" : \"en-US text.\","
99    "        },"
100    "        \"default\" : {"
101    "          \"notification_text\" : \"default_text.\","
102    "        },"
103    "      },"
104    "    },"
105    "  },"
106    "}";
107
108}  // anonymous namespace
109
110namespace chromeos {
111
112using ::testing::_;
113using ::testing::DoAll;
114using ::testing::NotNull;
115using ::testing::Return;
116using ::testing::SetArgumentPointee;
117
118TEST(StartupCustomizationDocumentTest, Basic) {
119  MockSystemAccess mock_system_access;
120  EXPECT_CALL(mock_system_access, GetMachineStatistic(_, NotNull()))
121      .WillRepeatedly(Return(false));
122  EXPECT_CALL(mock_system_access,
123      GetMachineStatistic(std::string("hwid"), NotNull()))
124          .WillOnce(DoAll(SetArgumentPointee<1>(std::string("Mario 12345")),
125                          Return(true)));
126  StartupCustomizationDocument customization(&mock_system_access,
127                                             kGoodStartupManifest);
128  EXPECT_EQ("ru-RU", customization.initial_locale());
129  EXPECT_EQ("Europe/Moscow", customization.initial_timezone());
130  EXPECT_EQ("xkb:ru::rus", customization.keyboard_layout());
131  EXPECT_EQ("http://www.google.com", customization.registration_url());
132
133  EXPECT_EQ("file:///opt/oem/help/en-US/help.html",
134            customization.GetHelpPage("en-US"));
135  EXPECT_EQ("file:///opt/oem/help/ru-RU/help.html",
136            customization.GetHelpPage("ru-RU"));
137  EXPECT_EQ("file:///opt/oem/help/en/help.html",
138            customization.GetHelpPage("ja"));
139
140  EXPECT_EQ("file:///opt/oem/eula/en-US/eula.html",
141            customization.GetEULAPage("en-US"));
142  EXPECT_EQ("file:///opt/oem/eula/ru-RU/eula.html",
143            customization.GetEULAPage("ru-RU"));
144  EXPECT_EQ("file:///opt/oem/eula/en/eula.html",
145            customization.GetEULAPage("ja"));
146}
147
148TEST(StartupCustomizationDocumentTest, VPD) {
149  MockSystemAccess mock_system_access;
150  EXPECT_CALL(mock_system_access,
151      GetMachineStatistic(std::string("hwid"), NotNull()))
152          .WillOnce(DoAll(SetArgumentPointee<1>(std::string("Mario 12345")),
153                          Return(true)));
154  EXPECT_CALL(mock_system_access,
155      GetMachineStatistic(std::string("initial_locale"), NotNull()))
156          .WillOnce(DoAll(SetArgumentPointee<1>(std::string("ja")),
157                          Return(true)));
158  EXPECT_CALL(mock_system_access,
159      GetMachineStatistic(std::string("initial_timezone"), NotNull()))
160          .WillOnce(DoAll(SetArgumentPointee<1>(std::string("Asia/Tokyo")),
161                          Return(true)));
162  EXPECT_CALL(mock_system_access,
163      GetMachineStatistic(std::string("keyboard_layout"), NotNull()))
164          .WillOnce(DoAll(SetArgumentPointee<1>(std::string("mozc-jp")),
165                          Return(true)));
166  StartupCustomizationDocument customization(&mock_system_access,
167                                             kGoodStartupManifest);
168  EXPECT_TRUE(customization.IsReady());
169  EXPECT_EQ("ja", customization.initial_locale());
170  EXPECT_EQ("Asia/Tokyo", customization.initial_timezone());
171  EXPECT_EQ("mozc-jp", customization.keyboard_layout());
172}
173
174TEST(StartupCustomizationDocumentTest, BadManifest) {
175  MockSystemAccess mock_system_access;
176  StartupCustomizationDocument customization(&mock_system_access, kBadManifest);
177  EXPECT_FALSE(customization.IsReady());
178}
179
180TEST(ServicesCustomizationDocumentTest, Basic) {
181  ServicesCustomizationDocument customization(kGoodServicesManifest, "en-US");
182  EXPECT_TRUE(customization.IsReady());
183
184  EXPECT_EQ("http://mario/promo",
185            customization.GetInitialStartPage("en-US"));
186  EXPECT_EQ("http://mario/ru/promo",
187            customization.GetInitialStartPage("ru-RU"));
188  EXPECT_EQ("http://mario/global/promo",
189            customization.GetInitialStartPage("ja"));
190
191  EXPECT_EQ("http://mario/us", customization.GetSupportPage("en-US"));
192  EXPECT_EQ("http://mario/ru", customization.GetSupportPage("ru-RU"));
193  EXPECT_EQ("http://mario/global", customization.GetSupportPage("ja"));
194
195  const ServicesCustomizationDocument::CarrierDeal* deal;
196  deal = customization.GetCarrierDeal("Carrier (country)", true);
197  EXPECT_TRUE(deal != NULL);
198  EXPECT_EQ("en-US", deal->deal_locale);
199  EXPECT_EQ("http://www.carrier.com/", deal->top_up_url);
200  EXPECT_EQ(1, deal->notification_count);
201  EXPECT_EQ("3G connectivity : Carrier.",
202            deal->GetLocalizedString("en-US", "notification_text"));
203  EXPECT_EQ("default_text.",
204            deal->GetLocalizedString("en", "notification_text"));
205
206  base::Time reference_time;
207  base::Time::FromString(L"31/12/12 0:00", &reference_time);
208  EXPECT_EQ(reference_time, deal->expire_date);
209}
210
211TEST(ServicesCustomizationDocumentTest, OldDeal) {
212  ServicesCustomizationDocument customization(kOldDealServicesManifest,
213                                              "en-US");
214  EXPECT_TRUE(customization.IsReady());
215
216  const ServicesCustomizationDocument::CarrierDeal* deal;
217  // TODO(nkostylev): Pass fixed time instead of relying on Time::Now().
218  deal = customization.GetCarrierDeal("Carrier (country)", true);
219  EXPECT_TRUE(deal == NULL);
220}
221
222TEST(ServicesCustomizationDocumentTest, DealOtherLocale) {
223  ServicesCustomizationDocument customization(kGoodServicesManifest,
224                                              "en-GB");
225  EXPECT_TRUE(customization.IsReady());
226
227  const ServicesCustomizationDocument::CarrierDeal* deal;
228  deal = customization.GetCarrierDeal("Carrier (country)", true);
229  EXPECT_TRUE(deal == NULL);
230}
231
232TEST(ServicesCustomizationDocumentTest, BadManifest) {
233  ServicesCustomizationDocument customization(kBadManifest, "en-US");
234  EXPECT_FALSE(customization.IsReady());
235}
236
237TEST(ServicesCustomizationDocumentTest, NoDealRestrictions) {
238  ServicesCustomizationDocument customization_oth_locale(kGoodServicesManifest,
239                                                         "en-GB");
240  EXPECT_TRUE(customization_oth_locale.IsReady());
241  const ServicesCustomizationDocument::CarrierDeal* deal;
242  deal = customization_oth_locale.GetCarrierDeal("Carrier (country)", false);
243  EXPECT_TRUE(deal != NULL);
244
245  ServicesCustomizationDocument customization_old_deal(kOldDealServicesManifest,
246                                                       "en-US");
247  EXPECT_TRUE(customization_old_deal.IsReady());
248  deal = customization_old_deal.GetCarrierDeal("Carrier (country)", false);
249  EXPECT_TRUE(deal != NULL);
250}
251
252}  // namespace chromeos
253