OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 "base/json/json_reader.h" | |
6 #include "base/time.h" | |
7 #include "base/utf_string_conversions.h" | |
8 #include "base/values.h" | |
9 #include "chrome/browser/prefs/pref_service.h" | |
10 #include "chrome/browser/web_resource/web_resource_service.h" | |
11 #include "chrome/common/pref_names.h" | |
12 #include "chrome/test/testing_profile.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 typedef testing::Test WebResourceServiceTest; | |
16 | |
17 namespace { | |
18 | |
19 // From web_resource_service.cc | |
20 enum BuildType { | |
21 DEV_BUILD = 1, | |
22 BETA_BUILD = 1 << 1, | |
23 STABLE_BUILD = 1 << 2, | |
24 }; | |
25 | |
26 } // namespace | |
27 | |
28 // Verifies that custom dates read from a web resource server are written to | |
29 // the preferences file. | |
30 TEST_F(WebResourceServiceTest, UnpackLogoSignal) { | |
31 // Set up a testing profile and create a web resource service. | |
32 TestingProfile profile; | |
33 scoped_refptr<WebResourceService> web_resource_service( | |
34 new WebResourceService(&profile)); | |
35 | |
36 // Set up start and end dates in a Dictionary as if parsed from the service. | |
37 std::string json = "{ " | |
38 " \"topic\": {" | |
39 " \"answers\": [" | |
40 " {" | |
41 " \"name\": \"custom_logo_start\"," | |
42 " \"inproduct\": \"31/01/10 01:00 GMT\"" | |
43 " }," | |
44 " {" | |
45 " \"name\": \"custom_logo_end\"," | |
46 " \"inproduct\": \"31/01/12 01:00 GMT\"" | |
47 " }" | |
48 " ]" | |
49 " }" | |
50 "}"; | |
51 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( | |
52 base::JSONReader::Read(json, false))); | |
53 | |
54 // Check that prefs are set correctly. | |
55 web_resource_service->UnpackLogoSignal(*(test_json.get())); | |
56 PrefService* prefs = profile.GetPrefs(); | |
57 ASSERT_TRUE(prefs != NULL); | |
58 | |
59 double logo_start = | |
60 prefs->GetDouble(prefs::kNTPCustomLogoStart); | |
61 EXPECT_EQ(logo_start, 1264899600); // unix epoch for Jan 31 2010 0100 GMT. | |
62 double logo_end = | |
63 prefs->GetDouble(prefs::kNTPCustomLogoEnd); | |
64 EXPECT_EQ(logo_end, 1327971600); // unix epoch for Jan 31 2012 0100 GMT. | |
65 | |
66 // Change the start only and recheck. | |
67 json = "{ " | |
68 " \"topic\": {" | |
69 " \"answers\": [" | |
70 " {" | |
71 " \"name\": \"custom_logo_start\"," | |
72 " \"inproduct\": \"28/02/10 14:00 GMT\"" | |
73 " }," | |
74 " {" | |
75 " \"name\": \"custom_logo_end\"," | |
76 " \"inproduct\": \"31/01/12 01:00 GMT\"" | |
77 " }" | |
78 " ]" | |
79 " }" | |
80 "}"; | |
81 test_json->Clear(); | |
82 test_json.reset(static_cast<DictionaryValue*>( | |
83 base::JSONReader::Read(json, false))); | |
84 | |
85 // Check that prefs are set correctly. | |
86 web_resource_service->UnpackLogoSignal(*(test_json.get())); | |
87 | |
88 logo_start = prefs->GetDouble(prefs::kNTPCustomLogoStart); | |
89 EXPECT_EQ(logo_start, 1267365600); // date changes to Feb 28 2010 1400 GMT. | |
90 | |
91 // If no date is included in the prefs, reset custom logo dates to 0. | |
92 json = "{ " | |
93 " \"topic\": {" | |
94 " \"answers\": [" | |
95 " {" | |
96 " }" | |
97 " ]" | |
98 " }" | |
99 "}"; | |
100 test_json->Clear(); | |
101 test_json.reset(static_cast<DictionaryValue*>( | |
102 base::JSONReader::Read(json, false))); | |
103 | |
104 // Check that prefs are set correctly. | |
105 web_resource_service->UnpackLogoSignal(*(test_json.get())); | |
106 logo_start = prefs->GetDouble(prefs::kNTPCustomLogoStart); | |
107 EXPECT_EQ(logo_start, 0); // date value reset to 0; | |
108 logo_end = prefs->GetDouble(prefs::kNTPCustomLogoEnd); | |
109 EXPECT_EQ(logo_end, 0); // date value reset to 0; | |
110 } | |
111 | |
112 TEST_F(WebResourceServiceTest, UnpackPromoSignal) { | |
113 // Set up a testing profile and create a web resource service. | |
114 TestingProfile profile; | |
115 scoped_refptr<WebResourceService> web_resource_service( | |
116 new WebResourceService(&profile)); | |
117 | |
118 // Set up start and end dates and promo line in a Dictionary as if parsed | |
119 // from the service. | |
120 std::string json = "{ " | |
121 " \"topic\": {" | |
122 " \"answers\": [" | |
123 " {" | |
124 " \"name\": \"promo_start\"," | |
125 " \"question\": \"3:2\"," | |
126 " \"tooltip\": \"Eat more pie!\"," | |
127 " \"inproduct\": \"31/01/10 01:00 GMT\"" | |
128 " }," | |
129 " {" | |
130 " \"name\": \"promo_end\"," | |
131 " \"inproduct\": \"31/01/12 01:00 GMT\"" | |
132 " }" | |
133 " ]" | |
134 " }" | |
135 "}"; | |
136 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( | |
137 base::JSONReader::Read(json, false))); | |
138 | |
139 // Initialize a message loop for this to run on. | |
140 MessageLoop loop; | |
141 | |
142 // Check that prefs are set correctly. | |
143 web_resource_service->UnpackPromoSignal(*(test_json.get())); | |
144 PrefService* prefs = profile.GetPrefs(); | |
145 ASSERT_TRUE(prefs != NULL); | |
146 | |
147 std::string promo_line = prefs->GetString(prefs::kNTPPromoLine); | |
148 EXPECT_EQ(promo_line, "Eat more pie!"); | |
149 | |
150 int promo_group = prefs->GetInteger(prefs::kNTPPromoGroup); | |
151 EXPECT_GE(promo_group, 0); | |
152 EXPECT_LT(promo_group, 16); | |
153 | |
154 int promo_build_type = prefs->GetInteger(prefs::kNTPPromoBuild); | |
155 EXPECT_EQ(promo_build_type & DEV_BUILD, DEV_BUILD); | |
156 EXPECT_EQ(promo_build_type & BETA_BUILD, BETA_BUILD); | |
157 EXPECT_EQ(promo_build_type & STABLE_BUILD, 0); | |
158 | |
159 int promo_time_slice = prefs->GetInteger(prefs::kNTPPromoGroupTimeSlice); | |
160 EXPECT_EQ(promo_time_slice, 2); | |
161 | |
162 double promo_start = | |
163 prefs->GetDouble(prefs::kNTPPromoStart); | |
164 int64 actual_start = 1264899600 + // unix epoch for Jan 31 2010 0100 GMT. | |
165 promo_group * 2 * 60 * 60; | |
166 EXPECT_EQ(promo_start, actual_start); | |
167 | |
168 double promo_end = | |
169 prefs->GetDouble(prefs::kNTPPromoEnd); | |
170 EXPECT_EQ(promo_end, 1327971600); // unix epoch for Jan 31 2012 0100 GMT. | |
171 } | |
172 | |
173 | |
OLD | NEW |