OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <utility> | |
5 #include <vector> | 6 #include <vector> |
6 | 7 |
7 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
8 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
9 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "base/string_util.h" | |
10 #include "base/time.h" | 12 #include "base/time.h" |
11 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
12 #include "base/values.h" | 14 #include "base/values.h" |
13 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/prefs/browser_prefs.h" | 16 #include "chrome/browser/prefs/browser_prefs.h" |
15 #include "chrome/browser/prefs/pref_service.h" | 17 #include "chrome/browser/prefs/pref_service.h" |
16 #include "chrome/browser/web_resource/notification_promo.h" | 18 #include "chrome/browser/web_resource/notification_promo.h" |
17 #include "chrome/browser/web_resource/promo_resource_service.h" | 19 #include "chrome/browser/web_resource/promo_resource_service.h" |
18 #include "chrome/common/chrome_notification_types.h" | 20 #include "chrome/common/chrome_notification_types.h" |
19 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
20 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
21 #include "chrome/test/base/testing_browser_process.h" | 23 #include "chrome/test/base/testing_browser_process.h" |
22 #include "chrome/test/base/testing_pref_service.h" | 24 #include "chrome/test/base/testing_pref_service.h" |
23 #include "content/public/browser/notification_registrar.h" | 25 #include "content/public/browser/notification_registrar.h" |
24 #include "content/public/browser/notification_service.h" | 26 #include "content/public/browser/notification_service.h" |
25 #include "net/url_request/test_url_fetcher_factory.h" | 27 #include "net/url_request/test_url_fetcher_factory.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
29 #include "unicode/smpdtfmt.h" | |
30 | |
31 namespace { | |
32 | |
33 const char* kDateFormat = "dd MMM yyyy HH:mm:ss zzz"; | |
Robert Sesek
2013/01/07 19:58:46
const char* -> const char kDateFormat[] removes a
Dan Beam
2013/01/07 20:13:23
Done.
| |
34 | |
35 std::pair<double, std::string> YearFromNow() { | |
36 UErrorCode status = U_ZERO_ERROR; | |
37 icu::SimpleDateFormat simple_formatter(icu::UnicodeString(kDateFormat), | |
38 icu::Locale("en_US"), | |
39 status); | |
40 DCHECK(U_SUCCESS(status)); | |
41 | |
42 const double year_from_now = | |
43 (base::Time::Now() + base::TimeDelta::FromDays(365)).ToTimeT(); | |
44 | |
45 icu::UnicodeString date_unicode_string; | |
46 simple_formatter.format(static_cast<UDate>(year_from_now * 1000), | |
47 date_unicode_string, | |
48 status); | |
49 DCHECK(U_SUCCESS(status)); | |
50 | |
51 std::string date_string; | |
52 UTF16ToUTF8(date_unicode_string.getBuffer(), | |
53 static_cast<size_t>(date_unicode_string.length()), | |
54 &date_string); | |
55 | |
56 return std::make_pair(year_from_now, date_string); | |
57 } | |
58 | |
59 } // namespace | |
27 | 60 |
28 class PromoResourceServiceTest : public testing::Test { | 61 class PromoResourceServiceTest : public testing::Test { |
29 public: | 62 public: |
30 // |promo_resource_service_| must be created after |local_state_|. | 63 // |promo_resource_service_| must be created after |local_state_|. |
31 PromoResourceServiceTest() | 64 PromoResourceServiceTest() |
32 : local_state_(TestingBrowserProcess::GetGlobal()), | 65 : local_state_(TestingBrowserProcess::GetGlobal()), |
33 promo_resource_service_(new PromoResourceService) {} | 66 promo_resource_service_(new PromoResourceService) {} |
34 | 67 |
35 protected: | 68 protected: |
36 ScopedTestingLocalState local_state_; | 69 ScopedTestingLocalState local_state_; |
(...skipping 10 matching lines...) Expand all Loading... | |
47 num_groups_(0), | 80 num_groups_(0), |
48 initial_segment_(0), | 81 initial_segment_(0), |
49 increment_(1), | 82 increment_(1), |
50 time_slice_(0), | 83 time_slice_(0), |
51 max_group_(0), | 84 max_group_(0), |
52 max_views_(0), | 85 max_views_(0), |
53 closed_(false) {} | 86 closed_(false) {} |
54 | 87 |
55 void Init(const std::string& json, | 88 void Init(const std::string& json, |
56 const std::string& promo_text, | 89 const std::string& promo_text, |
57 double start, double end, | 90 double start, |
58 int num_groups, int initial_segment, int increment, | 91 int num_groups, int initial_segment, int increment, |
59 int time_slice, int max_group, int max_views) { | 92 int time_slice, int max_group, int max_views) { |
60 Value* value(base::JSONReader::Read(json)); | 93 std::pair<double, std::string> year_from_now = YearFromNow(); |
94 std::vector<std::string> replacements; | |
95 replacements.push_back(year_from_now.second); | |
96 | |
97 std::string json_with_year( | |
98 ReplaceStringPlaceholders(json, replacements, NULL)); | |
99 Value* value(base::JSONReader::Read(json_with_year)); | |
61 ASSERT_TRUE(value); | 100 ASSERT_TRUE(value); |
101 | |
62 DictionaryValue* dict = NULL; | 102 DictionaryValue* dict = NULL; |
63 value->GetAsDictionary(&dict); | 103 value->GetAsDictionary(&dict); |
64 ASSERT_TRUE(dict); | 104 ASSERT_TRUE(dict); |
65 test_json_.reset(dict); | 105 test_json_.reset(dict); |
66 | 106 |
67 promo_type_ = NotificationPromo::NTP_NOTIFICATION_PROMO; | 107 promo_type_ = NotificationPromo::NTP_NOTIFICATION_PROMO; |
68 promo_text_ = promo_text; | 108 promo_text_ = promo_text; |
69 | 109 |
70 start_ = start; | 110 start_ = start; |
71 end_ = end; | 111 end_ = year_from_now.first; |
72 | 112 |
73 num_groups_ = num_groups; | 113 num_groups_ = num_groups; |
74 initial_segment_ = initial_segment; | 114 initial_segment_ = initial_segment; |
75 increment_ = increment; | 115 increment_ = increment; |
76 time_slice_ = time_slice; | 116 time_slice_ = time_slice; |
77 max_group_ = max_group; | 117 max_group_ = max_group; |
78 | 118 |
79 max_views_ = max_views; | 119 max_views_ = max_views; |
80 | 120 |
81 closed_ = false; | 121 closed_ = false; |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
312 | 352 |
313 int max_views_; | 353 int max_views_; |
314 | 354 |
315 bool closed_; | 355 bool closed_; |
316 }; | 356 }; |
317 | 357 |
318 // Test that everything gets parsed correctly, notifications are sent, | 358 // Test that everything gets parsed correctly, notifications are sent, |
319 // and CanShow() is handled correctly under variety of conditions. | 359 // and CanShow() is handled correctly under variety of conditions. |
320 // Additionally, test that the first string in |strings| is used if | 360 // Additionally, test that the first string in |strings| is used if |
321 // no payload.promo_short_message is specified in the JSON response. | 361 // no payload.promo_short_message is specified in the JSON response. |
322 | 362 TEST_F(PromoResourceServiceTest, NotificationPromoTest) { |
323 // hardcoded a date in Jan 2013. Srzly?? http://crbug.com/168561 | |
324 TEST_F(PromoResourceServiceTest, DISABLED_NotificationPromoTest) { | |
325 // Check that prefs are set correctly. | 363 // Check that prefs are set correctly. |
326 NotificationPromoTest promo_test; | 364 NotificationPromoTest promo_test; |
327 | 365 |
328 // Set up start and end dates and promo line in a Dictionary as if parsed | 366 // Set up start date and promo line in a Dictionary as if parsed from the |
329 // from the service. | 367 // service. date[0].end is replaced with a date 1 year in the future. |
330 promo_test.Init("{" | 368 promo_test.Init("{" |
331 " \"ntp_notification_promo\": [" | 369 " \"ntp_notification_promo\": [" |
332 " {" | 370 " {" |
333 " \"date\":" | 371 " \"date\":" |
334 " [" | 372 " [" |
335 " {" | 373 " {" |
336 " \"start\":\"3 Aug 1999 9:26:06 GMT\"," | 374 " \"start\":\"3 Aug 1999 9:26:06 GMT\"," |
337 " \"end\":\"7 Jan 2013 5:40:75 PST\"" | 375 " \"end\":\"$1\"" |
338 " }" | 376 " }" |
339 " ]," | 377 " ]," |
340 " \"strings\":" | 378 " \"strings\":" |
341 " {" | 379 " {" |
342 " \"NTP4_HOW_DO_YOU_FEEL_ABOUT_CHROME\":" | 380 " \"NTP4_HOW_DO_YOU_FEEL_ABOUT_CHROME\":" |
343 " \"What do you think of Chrome?\"" | 381 " \"What do you think of Chrome?\"" |
344 " }," | 382 " }," |
345 " \"grouping\":" | 383 " \"grouping\":" |
346 " {" | 384 " {" |
347 " \"buckets\":1000," | 385 " \"buckets\":1000," |
348 " \"segment\":200," | 386 " \"segment\":200," |
349 " \"increment\":100," | 387 " \"increment\":100," |
350 " \"increment_frequency\":3600," | 388 " \"increment_frequency\":3600," |
351 " \"increment_max\":400" | 389 " \"increment_max\":400" |
352 " }," | 390 " }," |
353 " \"payload\":" | 391 " \"payload\":" |
354 " {" | 392 " {" |
355 " \"days_active\":7," | 393 " \"days_active\":7," |
356 " \"install_age_days\":21" | 394 " \"install_age_days\":21" |
357 " }," | 395 " }," |
358 " \"max_views\":30" | 396 " \"max_views\":30" |
359 " }" | 397 " }" |
360 " ]" | 398 " ]" |
361 "}", | 399 "}", |
362 "What do you think of Chrome?", | 400 "What do you think of Chrome?", |
363 // The starting date is in 1999 to make tests pass | 401 // The starting date is in 1999 to make tests pass |
364 // on Android devices with incorrect or unset date/time. | 402 // on Android devices with incorrect or unset date/time. |
365 933672366, // unix epoch for 3 Aug 1999 9:26:06 GMT. | 403 933672366, // unix epoch for 3 Aug 1999 9:26:06 GMT. |
366 1357566075, // unix epoch for 7 Jan 2013 5:40:75 PST. | |
367 1000, 200, 100, 3600, 400, 30); | 404 1000, 200, 100, 3600, 400, 30); |
368 | 405 |
369 promo_test.InitPromoFromJson(true); | 406 promo_test.InitPromoFromJson(true); |
370 | 407 |
371 // Second time should not trigger a notification. | 408 // Second time should not trigger a notification. |
372 promo_test.InitPromoFromJson(false); | 409 promo_test.InitPromoFromJson(false); |
373 | 410 |
374 promo_test.TestInitFromPrefs(); | 411 promo_test.TestInitFromPrefs(); |
375 | 412 |
376 // Test various conditions of CanShow. | 413 // Test various conditions of CanShow. |
377 // TestGroup Has the side effect of setting us to a passing group. | 414 // TestGroup Has the side effect of setting us to a passing group. |
378 promo_test.TestGroup(); | 415 promo_test.TestGroup(); |
379 promo_test.TestViews(); | 416 promo_test.TestViews(); |
380 promo_test.TestClosed(); | 417 promo_test.TestClosed(); |
381 promo_test.TestPromoText(); | 418 promo_test.TestPromoText(); |
382 promo_test.TestTime(); | 419 promo_test.TestTime(); |
383 promo_test.TestIncrement(); | 420 promo_test.TestIncrement(); |
384 } | 421 } |
385 | 422 |
386 // Test that payload.promo_message_short is used if present. | 423 // Test that payload.promo_message_short is used if present. |
387 TEST_F(PromoResourceServiceTest, NotificationPromoCompatNoStringsTest) { | 424 TEST_F(PromoResourceServiceTest, NotificationPromoCompatNoStringsTest) { |
388 // Check that prefs are set correctly. | 425 // Check that prefs are set correctly. |
389 NotificationPromoTest promo_test; | 426 NotificationPromoTest promo_test; |
390 | 427 |
391 // Set up start and end dates and promo line in a Dictionary as if parsed | 428 // Set up start date and promo line in a Dictionary as if parsed from the |
392 // from the service. | 429 // service. date[0].end is replaced with a date 1 year in the future. |
393 promo_test.Init("{" | 430 promo_test.Init("{" |
394 " \"ntp_notification_promo\": [" | 431 " \"ntp_notification_promo\": [" |
395 " {" | 432 " {" |
396 " \"date\":" | 433 " \"date\":" |
397 " [" | 434 " [" |
398 " {" | 435 " {" |
399 " \"start\":\"3 Aug 1999 9:26:06 GMT\"," | 436 " \"start\":\"3 Aug 1999 9:26:06 GMT\"," |
400 " \"end\":\"7 Jan 2013 5:40:75 PST\"" | 437 " \"end\":\"$1\"" |
401 " }" | 438 " }" |
402 " ]," | 439 " ]," |
403 " \"grouping\":" | 440 " \"grouping\":" |
404 " {" | 441 " {" |
405 " \"buckets\":1000," | 442 " \"buckets\":1000," |
406 " \"segment\":200," | 443 " \"segment\":200," |
407 " \"increment\":100," | 444 " \"increment\":100," |
408 " \"increment_frequency\":3600," | 445 " \"increment_frequency\":3600," |
409 " \"increment_max\":400" | 446 " \"increment_max\":400" |
410 " }," | 447 " }," |
411 " \"payload\":" | 448 " \"payload\":" |
412 " {" | 449 " {" |
413 " \"promo_message_short\":" | 450 " \"promo_message_short\":" |
414 " \"What do you think of Chrome?\"," | 451 " \"What do you think of Chrome?\"," |
415 " \"days_active\":7," | 452 " \"days_active\":7," |
416 " \"install_age_days\":21" | 453 " \"install_age_days\":21" |
417 " }," | 454 " }," |
418 " \"max_views\":30" | 455 " \"max_views\":30" |
419 " }" | 456 " }" |
420 " ]" | 457 " ]" |
421 "}", | 458 "}", |
422 "What do you think of Chrome?", | 459 "What do you think of Chrome?", |
423 // The starting date is in 1999 to make tests pass | 460 // The starting date is in 1999 to make tests pass |
424 // on Android devices with incorrect or unset date/time. | 461 // on Android devices with incorrect or unset date/time. |
425 933672366, // unix epoch for 3 Aug 1999 9:26:06 GMT. | 462 933672366, // unix epoch for 3 Aug 1999 9:26:06 GMT. |
426 1357566075, // unix epoch for 7 Jan 2013 5:40:75 PST. | |
427 1000, 200, 100, 3600, 400, 30); | 463 1000, 200, 100, 3600, 400, 30); |
428 | 464 |
429 promo_test.InitPromoFromJson(true); | 465 promo_test.InitPromoFromJson(true); |
430 // Second time should not trigger a notification. | 466 // Second time should not trigger a notification. |
431 promo_test.InitPromoFromJson(false); | 467 promo_test.InitPromoFromJson(false); |
432 promo_test.TestInitFromPrefs(); | 468 promo_test.TestInitFromPrefs(); |
433 } | 469 } |
434 | 470 |
435 // Test that strings.|payload.promo_message_short| is used if present. | 471 // Test that strings.|payload.promo_message_short| is used if present. |
436 TEST_F(PromoResourceServiceTest, NotificationPromoCompatPayloadStringsTest) { | 472 TEST_F(PromoResourceServiceTest, NotificationPromoCompatPayloadStringsTest) { |
437 // Check that prefs are set correctly. | 473 // Check that prefs are set correctly. |
438 NotificationPromoTest promo_test; | 474 NotificationPromoTest promo_test; |
439 | 475 |
440 // Set up start and end dates and promo line in a Dictionary as if parsed | 476 // Set up start date and promo line in a Dictionary as if parsed from the |
441 // from the service. | 477 // service. date[0].end is replaced with a date 1 year in the future. |
442 promo_test.Init("{" | 478 promo_test.Init("{" |
443 " \"ntp_notification_promo\": [" | 479 " \"ntp_notification_promo\": [" |
444 " {" | 480 " {" |
445 " \"date\":" | 481 " \"date\":" |
446 " [" | 482 " [" |
447 " {" | 483 " {" |
448 " \"start\":\"3 Aug 1999 9:26:06 GMT\"," | 484 " \"start\":\"3 Aug 1999 9:26:06 GMT\"," |
449 " \"end\":\"7 Jan 2013 5:40:75 PST\"" | 485 " \"end\":\"$1\"" |
450 " }" | 486 " }" |
451 " ]," | 487 " ]," |
452 " \"grouping\":" | 488 " \"grouping\":" |
453 " {" | 489 " {" |
454 " \"buckets\":1000," | 490 " \"buckets\":1000," |
455 " \"segment\":200," | 491 " \"segment\":200," |
456 " \"increment\":100," | 492 " \"increment\":100," |
457 " \"increment_frequency\":3600," | 493 " \"increment_frequency\":3600," |
458 " \"increment_max\":400" | 494 " \"increment_max\":400" |
459 " }," | 495 " }," |
(...skipping 11 matching lines...) Expand all Loading... | |
471 " \"install_age_days\":21" | 507 " \"install_age_days\":21" |
472 " }," | 508 " }," |
473 " \"max_views\":30" | 509 " \"max_views\":30" |
474 " }" | 510 " }" |
475 " ]" | 511 " ]" |
476 "}", | 512 "}", |
477 "What do you think of Chrome?", | 513 "What do you think of Chrome?", |
478 // The starting date is in 1999 to make tests pass | 514 // The starting date is in 1999 to make tests pass |
479 // on Android devices with incorrect or unset date/time. | 515 // on Android devices with incorrect or unset date/time. |
480 933672366, // unix epoch for 3 Aug 1999 9:26:06 GMT. | 516 933672366, // unix epoch for 3 Aug 1999 9:26:06 GMT. |
481 1357566075, // unix epoch for 7 Jan 2013 5:40:75 PST. | |
482 1000, 200, 100, 3600, 400, 30); | 517 1000, 200, 100, 3600, 400, 30); |
483 | 518 |
484 promo_test.InitPromoFromJson(true); | 519 promo_test.InitPromoFromJson(true); |
485 // Second time should not trigger a notification. | 520 // Second time should not trigger a notification. |
486 promo_test.InitPromoFromJson(false); | 521 promo_test.InitPromoFromJson(false); |
487 promo_test.TestInitFromPrefs(); | 522 promo_test.TestInitFromPrefs(); |
488 } | 523 } |
489 | 524 |
490 TEST_F(PromoResourceServiceTest, PromoServerURLTest) { | 525 TEST_F(PromoResourceServiceTest, PromoServerURLTest) { |
491 GURL promo_server_url = NotificationPromo::PromoServerURL(); | 526 GURL promo_server_url = NotificationPromo::PromoServerURL(); |
492 EXPECT_FALSE(promo_server_url.is_empty()); | 527 EXPECT_FALSE(promo_server_url.is_empty()); |
493 EXPECT_TRUE(promo_server_url.is_valid()); | 528 EXPECT_TRUE(promo_server_url.is_valid()); |
494 EXPECT_TRUE(promo_server_url.SchemeIs(chrome::kHttpsScheme)); | 529 EXPECT_TRUE(promo_server_url.SchemeIs(chrome::kHttpsScheme)); |
495 // TODO(achuith): Test this better. | 530 // TODO(achuith): Test this better. |
496 } | 531 } |
OLD | NEW |