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