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 "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/extensions/apps_promo.h" 10 #include "chrome/browser/prefs/browser_prefs.h" 11 #include "chrome/browser/prefs/pref_service.h" 12 #include "chrome/browser/web_resource/promo_resource_service.h" 13 #include "chrome/common/pref_names.h" 14 #include "chrome/test/testing_browser_process.h" 15 #include "chrome/test/testing_pref_service.h" 16 #include "chrome/test/testing_profile.h" 17 #include "testing/gtest/include/gtest/gtest.h" 18 19 typedef testing::Test PromoResourceServiceTest; 20 21 // Verifies that custom dates read from a web resource server are written to 22 // the preferences file. 23 TEST_F(PromoResourceServiceTest, UnpackLogoSignal) { 24 // Set up a testing profile and create a promo resource service. 25 TestingProfile profile; 26 TestingPrefService local_state; 27 TestingBrowserProcess* testing_browser_process = 28 static_cast<TestingBrowserProcess*>(g_browser_process); 29 testing_browser_process->SetPrefService(&local_state); 30 browser::RegisterLocalState(&local_state); 31 32 scoped_refptr<PromoResourceService> web_resource_service( 33 new PromoResourceService(&profile)); 34 35 // Set up start and end dates in a Dictionary as if parsed from the service. 36 std::string json = "{ " 37 " \"topic\": {" 38 " \"answers\": [" 39 " {" 40 " \"name\": \"custom_logo_start\"," 41 " \"inproduct\": \"31/01/10 01:00 GMT\"" 42 " }," 43 " {" 44 " \"name\": \"custom_logo_end\"," 45 " \"inproduct\": \"31/01/12 01:00 GMT\"" 46 " }" 47 " ]" 48 " }" 49 "}"; 50 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( 51 base::JSONReader::Read(json, false))); 52 53 // Check that prefs are set correctly. 54 web_resource_service->UnpackLogoSignal(*(test_json.get())); 55 PrefService* prefs = profile.GetPrefs(); 56 ASSERT_TRUE(prefs != NULL); 57 58 double logo_start = 59 prefs->GetDouble(prefs::kNTPCustomLogoStart); 60 EXPECT_EQ(logo_start, 1264899600); // unix epoch for Jan 31 2010 0100 GMT. 61 double logo_end = 62 prefs->GetDouble(prefs::kNTPCustomLogoEnd); 63 EXPECT_EQ(logo_end, 1327971600); // unix epoch for Jan 31 2012 0100 GMT. 64 65 // Change the start only and recheck. 66 json = "{ " 67 " \"topic\": {" 68 " \"answers\": [" 69 " {" 70 " \"name\": \"custom_logo_start\"," 71 " \"inproduct\": \"28/02/10 14:00 GMT\"" 72 " }," 73 " {" 74 " \"name\": \"custom_logo_end\"," 75 " \"inproduct\": \"31/01/12 01:00 GMT\"" 76 " }" 77 " ]" 78 " }" 79 "}"; 80 test_json->Clear(); 81 test_json.reset(static_cast<DictionaryValue*>( 82 base::JSONReader::Read(json, false))); 83 84 // Check that prefs are set correctly. 85 web_resource_service->UnpackLogoSignal(*(test_json.get())); 86 87 logo_start = prefs->GetDouble(prefs::kNTPCustomLogoStart); 88 EXPECT_EQ(logo_start, 1267365600); // date changes to Feb 28 2010 1400 GMT. 89 90 // If no date is included in the prefs, reset custom logo dates to 0. 91 json = "{ " 92 " \"topic\": {" 93 " \"answers\": [" 94 " {" 95 " }" 96 " ]" 97 " }" 98 "}"; 99 test_json->Clear(); 100 test_json.reset(static_cast<DictionaryValue*>( 101 base::JSONReader::Read(json, false))); 102 103 // Check that prefs are set correctly. 104 web_resource_service->UnpackLogoSignal(*(test_json.get())); 105 logo_start = prefs->GetDouble(prefs::kNTPCustomLogoStart); 106 EXPECT_EQ(logo_start, 0); // date value reset to 0; 107 logo_end = prefs->GetDouble(prefs::kNTPCustomLogoEnd); 108 EXPECT_EQ(logo_end, 0); // date value reset to 0; 109 110 testing_browser_process->SetPrefService(NULL); 111 } 112 113 TEST_F(PromoResourceServiceTest, UnpackPromoSignal) { 114 // Set up a testing profile and create a promo resource service. 115 TestingProfile profile; 116 TestingPrefService local_state; 117 TestingBrowserProcess* testing_browser_process = 118 static_cast<TestingBrowserProcess*>(g_browser_process); 119 testing_browser_process->SetPrefService(&local_state); 120 browser::RegisterLocalState(&local_state); 121 122 scoped_refptr<PromoResourceService> web_resource_service( 123 new PromoResourceService(&profile)); 124 125 // Set up start and end dates and promo line in a Dictionary as if parsed 126 // from the service. 127 std::string json = "{ " 128 " \"topic\": {" 129 " \"answers\": [" 130 " {" 131 " \"name\": \"promo_start\"," 132 " \"question\": \"3:2\"," 133 " \"tooltip\": \"Eat more pie!\"," 134 " \"inproduct\": \"31/01/10 01:00 GMT\"" 135 " }," 136 " {" 137 " \"name\": \"promo_end\"," 138 " \"inproduct\": \"31/01/12 01:00 GMT\"" 139 " }" 140 " ]" 141 " }" 142 "}"; 143 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( 144 base::JSONReader::Read(json, false))); 145 146 // Initialize a message loop for this to run on. 147 MessageLoop loop; 148 149 // Check that prefs are set correctly. 150 web_resource_service->UnpackPromoSignal(*(test_json.get())); 151 PrefService* prefs = profile.GetPrefs(); 152 ASSERT_TRUE(prefs != NULL); 153 154 std::string promo_line = prefs->GetString(prefs::kNTPPromoLine); 155 EXPECT_EQ(promo_line, "Eat more pie!"); 156 157 int promo_group = prefs->GetInteger(prefs::kNTPPromoGroup); 158 EXPECT_GE(promo_group, 0); 159 EXPECT_LT(promo_group, 16); 160 161 int promo_build_type = prefs->GetInteger(prefs::kNTPPromoBuild); 162 EXPECT_EQ(promo_build_type & PromoResourceService::DEV_BUILD, 163 PromoResourceService::DEV_BUILD); 164 EXPECT_EQ(promo_build_type & PromoResourceService::BETA_BUILD, 165 PromoResourceService::BETA_BUILD); 166 EXPECT_EQ(promo_build_type & PromoResourceService::STABLE_BUILD, 0); 167 168 int promo_time_slice = prefs->GetInteger(prefs::kNTPPromoGroupTimeSlice); 169 EXPECT_EQ(promo_time_slice, 2); 170 171 double promo_start = 172 prefs->GetDouble(prefs::kNTPPromoStart); 173 int64 actual_start = 1264899600 + // unix epoch for Jan 31 2010 0100 GMT. 174 promo_group * 2 * 60 * 60; 175 EXPECT_EQ(promo_start, actual_start); 176 177 double promo_end = 178 prefs->GetDouble(prefs::kNTPPromoEnd); 179 EXPECT_EQ(promo_end, 1327971600); // unix epoch for Jan 31 2012 0100 GMT. 180 181 testing_browser_process->SetPrefService(NULL); 182 } 183 184 TEST_F(PromoResourceServiceTest, UnpackWebStoreSignal) { 185 // Set up a testing profile and create a promo resource service. 186 TestingProfile profile; 187 TestingPrefService local_state; 188 TestingBrowserProcess* testing_browser_process = 189 static_cast<TestingBrowserProcess*>(g_browser_process); 190 testing_browser_process->SetPrefService(&local_state); 191 192 browser::RegisterLocalState(&local_state); 193 194 scoped_refptr<PromoResourceService> web_resource_service( 195 new PromoResourceService(&profile)); 196 197 web_resource_service->set_channel("dev"); 198 199 // Set up start and end dates and promo line in a Dictionary as if parsed 200 // from the service. 201 std::string json = "{ " 202 " \"topic\": {" 203 " \"answers\": [" 204 " {" 205 " \"answer_id\": \"341252\"," 206 " \"name\": \"webstore_promo:15\"," 207 " \"question\": \"The header!\"," 208 " \"inproduct_target\": \"The button label!\"," 209 " \"inproduct\": \"http://link.com\"," 210 " \"tooltip\": \"No thanks, hide this.\"" 211 " }" 212 " ]" 213 " }" 214 "}"; 215 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( 216 base::JSONReader::Read(json, false))); 217 218 // Initialize a message loop for this to run on. 219 MessageLoop loop; 220 221 // Check that prefs are set correctly. 222 web_resource_service->UnpackWebStoreSignal(*(test_json.get())); 223 PrefService* prefs = profile.GetPrefs(); 224 ASSERT_TRUE(prefs != NULL); 225 226 EXPECT_EQ("341252", AppsPromo::GetPromoId()); 227 EXPECT_EQ("The header!", AppsPromo::GetPromoHeaderText()); 228 EXPECT_EQ("The button label!", AppsPromo::GetPromoButtonText()); 229 EXPECT_EQ(GURL("http://link.com"), AppsPromo::GetPromoLink()); 230 EXPECT_EQ("No thanks, hide this.", AppsPromo::GetPromoExpireText()); 231 232 testing_browser_process->SetPrefService(NULL); 233 } 234 235 TEST_F(PromoResourceServiceTest, IsBuildTargeted) { 236 // canary 237 EXPECT_FALSE(PromoResourceService::IsBuildTargeted("canary", 1)); 238 EXPECT_FALSE(PromoResourceService::IsBuildTargeted("canary", 3)); 239 EXPECT_FALSE(PromoResourceService::IsBuildTargeted("canary", 7)); 240 EXPECT_TRUE(PromoResourceService::IsBuildTargeted("canary", 15)); 241 EXPECT_TRUE(PromoResourceService::IsBuildTargeted("canary", 8)); 242 EXPECT_TRUE(PromoResourceService::IsBuildTargeted("canary", 11)); 243 244 // dev 245 EXPECT_TRUE(PromoResourceService::IsBuildTargeted("dev", 1)); 246 EXPECT_TRUE(PromoResourceService::IsBuildTargeted("dev", 3)); 247 EXPECT_TRUE(PromoResourceService::IsBuildTargeted("dev", 7)); 248 EXPECT_TRUE(PromoResourceService::IsBuildTargeted("dev", 15)); 249 EXPECT_FALSE(PromoResourceService::IsBuildTargeted("dev", 8)); 250 EXPECT_TRUE(PromoResourceService::IsBuildTargeted("dev", 11)); 251 252 // beta 253 EXPECT_FALSE(PromoResourceService::IsBuildTargeted("beta", 1)); 254 EXPECT_TRUE(PromoResourceService::IsBuildTargeted("beta", 3)); 255 EXPECT_TRUE(PromoResourceService::IsBuildTargeted("beta", 7)); 256 EXPECT_TRUE(PromoResourceService::IsBuildTargeted("beta", 15)); 257 EXPECT_FALSE(PromoResourceService::IsBuildTargeted("beta", 8)); 258 EXPECT_TRUE(PromoResourceService::IsBuildTargeted("beta", 11)); 259 260 // stable 261 EXPECT_FALSE(PromoResourceService::IsBuildTargeted("", 1)); 262 EXPECT_FALSE(PromoResourceService::IsBuildTargeted("", 3)); 263 EXPECT_TRUE(PromoResourceService::IsBuildTargeted("", 7)); 264 EXPECT_TRUE(PromoResourceService::IsBuildTargeted("", 15)); 265 EXPECT_FALSE(PromoResourceService::IsBuildTargeted("", 8)); 266 EXPECT_FALSE(PromoResourceService::IsBuildTargeted("", 11)); 267 EXPECT_TRUE(PromoResourceService::IsBuildTargeted("", 12)); 268 } 269