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