Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: chrome/browser/web_resource/promo_resource_service_unittest.cc

Issue 10860025: Remove promotion-type-specific JSON handling from NotificationPromo (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Indent in contructor. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <vector> 5 #include <vector>
6 6
7 #include "base/json/json_reader.h" 7 #include "base/json/json_reader.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/string_number_conversions.h" 9 #include "base/string_number_conversions.h"
10 #include "base/time.h" 10 #include "base/time.h"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 increment_(1), 53 increment_(1),
54 time_slice_(0), 54 time_slice_(0),
55 max_group_(0), 55 max_group_(0),
56 max_views_(0), 56 max_views_(0),
57 closed_(false), 57 closed_(false),
58 gplus_required_(false) { 58 gplus_required_(false) {
59 } 59 }
60 60
61 void Init(const std::string& json, 61 void Init(const std::string& json,
62 const std::string& promo_text, 62 const std::string& promo_text,
63 #if defined(OS_ANDROID)
64 const std::string& promo_text_long,
65 const std::string& promo_action_type,
66 const std::string& promo_action_arg0,
67 const std::string& promo_action_arg1,
68 #endif // defined(OS_ANDROID)
69 double start, double end, 63 double start, double end,
70 int num_groups, int initial_segment, int increment, 64 int num_groups, int initial_segment, int increment,
71 int time_slice, int max_group, int max_views, 65 int time_slice, int max_group, int max_views,
72 bool gplus_required) { 66 bool gplus_required) {
73 Value* value(base::JSONReader::Read(json)); 67 Value* value(base::JSONReader::Read(json));
74 ASSERT_TRUE(value); 68 ASSERT_TRUE(value);
75 DictionaryValue* dict = NULL; 69 DictionaryValue* dict = NULL;
76 value->GetAsDictionary(&dict); 70 value->GetAsDictionary(&dict);
77 ASSERT_TRUE(dict); 71 ASSERT_TRUE(dict);
78 test_json_.reset(dict); 72 test_json_.reset(dict);
79 73
80 promo_type_ = 74 promo_type_ = NotificationPromo::NTP_NOTIFICATION_PROMO;
81 #if !defined(OS_ANDROID)
82 NotificationPromo::NTP_NOTIFICATION_PROMO;
83 #else
84 NotificationPromo::MOBILE_NTP_SYNC_PROMO;
85 #endif
86
87 promo_text_ = promo_text; 75 promo_text_ = promo_text;
88 76
89 #if defined(OS_ANDROID)
90 promo_text_long_ = promo_text_long;
91 promo_action_type_ = promo_action_type;
92 promo_action_args_.push_back(promo_action_arg0);
93 promo_action_args_.push_back(promo_action_arg1);
94 #endif // defined(OS_ANDROID)
95
96 start_ = start; 77 start_ = start;
97 end_ = end; 78 end_ = end;
98 79
99 num_groups_ = num_groups; 80 num_groups_ = num_groups;
100 initial_segment_ = initial_segment; 81 initial_segment_ = initial_segment;
101 increment_ = increment; 82 increment_ = increment;
102 time_slice_ = time_slice; 83 time_slice_ = time_slice;
103 max_group_ = max_group; 84 max_group_ = max_group;
104 85
105 max_views_ = max_views; 86 max_views_ = max_views;
(...skipping 10 matching lines...) Expand all
116 notification_promo_.new_notification()); 97 notification_promo_.new_notification());
117 98
118 // Test the fields. 99 // Test the fields.
119 TestNotification(); 100 TestNotification();
120 } 101 }
121 102
122 void TestNotification() { 103 void TestNotification() {
123 // Check values. 104 // Check values.
124 EXPECT_EQ(notification_promo_.promo_text_, promo_text_); 105 EXPECT_EQ(notification_promo_.promo_text_, promo_text_);
125 106
126 #if defined(OS_ANDROID)
127 EXPECT_EQ(notification_promo_.promo_text_long_, promo_text_long_);
128 EXPECT_EQ(notification_promo_.promo_action_type_, promo_action_type_);
129 EXPECT_TRUE(notification_promo_.promo_action_args_.get() != NULL);
130 EXPECT_EQ(std::size_t(2), promo_action_args_.size());
131 EXPECT_EQ(notification_promo_.promo_action_args_->GetSize(),
132 promo_action_args_.size());
133 for (std::size_t i = 0; i < promo_action_args_.size(); ++i) {
134 std::string value;
135 EXPECT_TRUE(notification_promo_.promo_action_args_->GetString(i, &value));
136 EXPECT_EQ(value, promo_action_args_[i]);
137 }
138 #endif // defined(OS_ANDROID)
139
140 EXPECT_EQ(notification_promo_.start_, start_); 107 EXPECT_EQ(notification_promo_.start_, start_);
141 EXPECT_EQ(notification_promo_.end_, end_); 108 EXPECT_EQ(notification_promo_.end_, end_);
142 109
143 EXPECT_EQ(notification_promo_.num_groups_, num_groups_); 110 EXPECT_EQ(notification_promo_.num_groups_, num_groups_);
144 EXPECT_EQ(notification_promo_.initial_segment_, initial_segment_); 111 EXPECT_EQ(notification_promo_.initial_segment_, initial_segment_);
145 EXPECT_EQ(notification_promo_.increment_, increment_); 112 EXPECT_EQ(notification_promo_.increment_, increment_);
146 EXPECT_EQ(notification_promo_.time_slice_, time_slice_); 113 EXPECT_EQ(notification_promo_.time_slice_, time_slice_);
147 EXPECT_EQ(notification_promo_.max_group_, max_group_); 114 EXPECT_EQ(notification_promo_.max_group_, max_group_);
148 115
149 EXPECT_EQ(notification_promo_.max_views_, max_views_); 116 EXPECT_EQ(notification_promo_.max_views_, max_views_);
(...skipping 12 matching lines...) Expand all
162 // Create a new NotificationPromo from prefs and compare to current 129 // Create a new NotificationPromo from prefs and compare to current
163 // notification. 130 // notification.
164 void TestInitFromPrefs() { 131 void TestInitFromPrefs() {
165 NotificationPromo prefs_notification_promo(profile_); 132 NotificationPromo prefs_notification_promo(profile_);
166 prefs_notification_promo.InitFromPrefs(promo_type_); 133 prefs_notification_promo.InitFromPrefs(promo_type_);
167 134
168 EXPECT_EQ(notification_promo_.prefs_, 135 EXPECT_EQ(notification_promo_.prefs_,
169 prefs_notification_promo.prefs_); 136 prefs_notification_promo.prefs_);
170 EXPECT_EQ(notification_promo_.promo_text_, 137 EXPECT_EQ(notification_promo_.promo_text_,
171 prefs_notification_promo.promo_text_); 138 prefs_notification_promo.promo_text_);
172 #if defined(OS_ANDROID)
173 EXPECT_EQ(notification_promo_.promo_text_long_,
174 prefs_notification_promo.promo_text_long_);
175 EXPECT_EQ(notification_promo_.promo_action_type_,
176 prefs_notification_promo.promo_action_type_);
177 EXPECT_TRUE(prefs_notification_promo.promo_action_args_.get() != NULL);
178 EXPECT_EQ(notification_promo_.promo_action_args_->GetSize(),
179 prefs_notification_promo.promo_action_args_->GetSize());
180 for (std::size_t i = 0;
181 i < notification_promo_.promo_action_args_->GetSize();
182 ++i) {
183 std::string promo_value;
184 std::string prefs_value;
185 EXPECT_TRUE(
186 notification_promo_.promo_action_args_->GetString(i, &promo_value));
187 EXPECT_TRUE(
188 prefs_notification_promo.promo_action_args_->GetString(
189 i, &prefs_value));
190 EXPECT_EQ(promo_value, prefs_value);
191 }
192 #endif // defined(OS_ANDROID)
193 EXPECT_EQ(notification_promo_.start_, 139 EXPECT_EQ(notification_promo_.start_,
194 prefs_notification_promo.start_); 140 prefs_notification_promo.start_);
195 EXPECT_EQ(notification_promo_.end_, 141 EXPECT_EQ(notification_promo_.end_,
196 prefs_notification_promo.end_); 142 prefs_notification_promo.end_);
197 EXPECT_EQ(notification_promo_.num_groups_, 143 EXPECT_EQ(notification_promo_.num_groups_,
198 prefs_notification_promo.num_groups_); 144 prefs_notification_promo.num_groups_);
199 EXPECT_EQ(notification_promo_.initial_segment_, 145 EXPECT_EQ(notification_promo_.initial_segment_,
200 prefs_notification_promo.initial_segment_); 146 prefs_notification_promo.initial_segment_);
201 EXPECT_EQ(notification_promo_.increment_, 147 EXPECT_EQ(notification_promo_.increment_,
202 prefs_notification_promo.increment_); 148 prefs_notification_promo.increment_);
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 326
381 private: 327 private:
382 Profile* profile_; 328 Profile* profile_;
383 PrefService* prefs_; 329 PrefService* prefs_;
384 NotificationPromo notification_promo_; 330 NotificationPromo notification_promo_;
385 bool received_notification_; 331 bool received_notification_;
386 scoped_ptr<DictionaryValue> test_json_; 332 scoped_ptr<DictionaryValue> test_json_;
387 333
388 NotificationPromo::PromoType promo_type_; 334 NotificationPromo::PromoType promo_type_;
389 std::string promo_text_; 335 std::string promo_text_;
390 #if defined(OS_ANDROID)
391 std::string promo_text_long_;
392 std::string promo_action_type_;
393 std::vector<std::string> promo_action_args_;
394 #endif // defined(OS_ANDROID)
395 336
396 double start_; 337 double start_;
397 double end_; 338 double end_;
398 339
399 int num_groups_; 340 int num_groups_;
400 int initial_segment_; 341 int initial_segment_;
401 int increment_; 342 int increment_;
402 int time_slice_; 343 int time_slice_;
403 int max_group_; 344 int max_group_;
404 345
405 int max_views_; 346 int max_views_;
406 347
407 bool closed_; 348 bool closed_;
408 349
409 bool gplus_required_; 350 bool gplus_required_;
410 }; 351 };
411 352
353 // Test that everything gets parsed correctly, notifications are sent,
354 // and CanShow() is handled correctly under variety of conditions.
355 // Additionally, test that the first string in |strings| is used if
356 // no payload.promo_short_message is specified in the JSON response.
412 TEST_F(PromoResourceServiceTest, NotificationPromoTest) { 357 TEST_F(PromoResourceServiceTest, NotificationPromoTest) {
413 // Check that prefs are set correctly. 358 // Check that prefs are set correctly.
414 PrefService* prefs = profile_.GetPrefs(); 359 PrefService* prefs = profile_.GetPrefs();
415 ASSERT_TRUE(prefs != NULL); 360 ASSERT_TRUE(prefs != NULL);
416 361
417 NotificationPromoTest promo_test(&profile_); 362 NotificationPromoTest promo_test(&profile_);
418 363
419 // Set up start and end dates and promo line in a Dictionary as if parsed 364 // Set up start and end dates and promo line in a Dictionary as if parsed
420 // from the service. 365 // from the service.
421 #if !defined(OS_ANDROID)
422 promo_test.Init("{" 366 promo_test.Init("{"
423 " \"ntp_notification_promo\": [" 367 " \"ntp_notification_promo\": ["
424 " {" 368 " {"
425 " \"date\":" 369 " \"date\":"
426 " [" 370 " ["
427 " {" 371 " {"
428 " \"start\":\"15 Jan 2012 10:50:85 PST\"," 372 " \"start\":\"3 Aug 1999 9:26:06 GMT\","
429 " \"end\":\"7 Jan 2013 5:40:75 PST\"" 373 " \"end\":\"7 Jan 2013 5:40:75 PST\""
430 " }" 374 " }"
431 " ]," 375 " ],"
432 " \"strings\":" 376 " \"strings\":"
433 " {" 377 " {"
434 " \"NTP4_HOW_DO_YOU_FEEL_ABOUT_CHROME\":" 378 " \"NTP4_HOW_DO_YOU_FEEL_ABOUT_CHROME\":"
435 " \"What do you think of Chrome?\"" 379 " \"What do you think of Chrome?\""
436 " }," 380 " },"
437 " \"grouping\":" 381 " \"grouping\":"
438 " {" 382 " {"
439 " \"buckets\":1000," 383 " \"buckets\":1000,"
440 " \"segment\":200," 384 " \"segment\":200,"
441 " \"increment\":100," 385 " \"increment\":100,"
442 " \"increment_frequency\":3600," 386 " \"increment_frequency\":3600,"
443 " \"increment_max\":400" 387 " \"increment_max\":400"
444 " }," 388 " },"
445 " \"payload\":" 389 " \"payload\":"
446 " {" 390 " {"
447 " \"days_active\":7," 391 " \"days_active\":7,"
448 " \"install_age_days\":21," 392 " \"install_age_days\":21,"
449 " \"gplus_required\":false" 393 " \"gplus_required\":false"
450 " }," 394 " },"
451 " \"max_views\":30" 395 " \"max_views\":30"
452 " }" 396 " }"
453 " ]" 397 " ]"
454 "}", 398 "}",
455 "What do you think of Chrome?", 399 "What do you think of Chrome?",
456 1326653485, // unix epoch for 15 Jan 2012 10:50:85 PST. 400 // The starting date is in 1999 to make tests pass
457 1357566075, // unix epoch for 7 Jan 2013 5:40:75 PST. 401 // on Android devices with incorrect or unset date/time.
402 933672366, // unix epoch for 3 Aug 1999 9:26:06 GMT.
403 1357566075, // unix epoch for 7 Jan 2013 5:40:75 PST.
458 1000, 200, 100, 3600, 400, 30, false); 404 1000, 200, 100, 3600, 400, 30, false);
459 #else
460 promo_test.Init(
461 "{"
462 " \"mobile_ntp_sync_promo\": ["
463 " {"
464 " \"date\":"
465 " ["
466 " {"
467 " \"start\":\"3 Aug 1999 9:26:06 GMT\","
468 " \"end\":\"7 Jan 2013 5:40:75 PST\""
469 " }"
470 " ],"
471 " \"strings\":"
472 " {"
473 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\":"
474 " \"Like Chrome? Go http://www.google.com/chrome/\","
475 " \"MOBILE_PROMO_CHROME_LONG_TEXT\":"
476 " \"It\'s simple. Go http://www.google.com/chrome/\","
477 " \"MOBILE_PROMO_EMAIL_BODY\":\"This is the body.\","
478 " \"XXX_VALUE\":\"XXX value\""
479 " },"
480 " \"grouping\":"
481 " {"
482 " \"buckets\":1000,"
483 " \"segment\":200,"
484 " \"increment\":100,"
485 " \"increment_frequency\":3600,"
486 " \"increment_max\":400"
487 " },"
488 " \"payload\":"
489 " {"
490 " \"payload_format_version\":3,"
491 " \"gplus_required\":false,"
492 " \"promo_message_long\":"
493 " \"MOBILE_PROMO_CHROME_LONG_TEXT\","
494 " \"promo_message_short\":"
495 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\","
496 " \"promo_action_type\":\"ACTION_EMAIL\","
497 " \"promo_action_args\":[\"MOBILE_PROMO_EMAIL_BODY\",\"XXX\"],"
498 " \"XXX\":\"XXX_VALUE\""
499 " },"
500 " \"max_views\":30"
501 " }"
502 " ]"
503 "}",
504 "Like Chrome? Go http://www.google.com/chrome/",
505 "It\'s simple. Go http://www.google.com/chrome/",
506 "ACTION_EMAIL", "This is the body.", "XXX value",
507 // We hardcode the unix epoch time to make sure our parsing works.
508 // The starting date is in 1999 to make tests pass on Android devices
509 // with incorrect or unset date/time.
510 933672366, // unix epoch for 3 Aug 1999 9:26:06 GMT.
511 1357566075, // unix epoch for 7 Jan 2013 5:40:75 PST.
512 1000, 200, 100, 3600, 400, 30, false);
513 #endif // !defined(OS_ANDROID)
514 405
515 promo_test.InitPromoFromJson(true); 406 promo_test.InitPromoFromJson(true);
516 407
517 // Second time should not trigger a notification. 408 // Second time should not trigger a notification.
518 promo_test.InitPromoFromJson(false); 409 promo_test.InitPromoFromJson(false);
519 410
520 promo_test.TestInitFromPrefs(); 411 promo_test.TestInitFromPrefs();
521 412
522 // Test various conditions of CanShow. 413 // Test various conditions of CanShow.
523 // 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.
524 promo_test.TestGroup(); 415 promo_test.TestGroup();
525 promo_test.TestViews(); 416 promo_test.TestViews();
526 promo_test.TestClosed(); 417 promo_test.TestClosed();
527 promo_test.TestPromoText(); 418 promo_test.TestPromoText();
528 promo_test.TestTime(); 419 promo_test.TestTime();
529 promo_test.TestIncrement(); 420 promo_test.TestIncrement();
530 promo_test.TestGplus(); 421 promo_test.TestGplus();
531 } 422 }
532 423
424 // Test that payload.promo_message_short is used if present.
425 TEST_F(PromoResourceServiceTest, NotificationPromoCompatNoStringsTest) {
426 // Check that prefs are set correctly.
427 PrefService* prefs = profile_.GetPrefs();
428 ASSERT_TRUE(prefs != NULL);
429
430 NotificationPromoTest promo_test(&profile_);
431
432 // Set up start and end dates and promo line in a Dictionary as if parsed
433 // from the service.
434 promo_test.Init("{"
435 " \"ntp_notification_promo\": ["
436 " {"
437 " \"date\":"
438 " ["
439 " {"
440 " \"start\":\"3 Aug 1999 9:26:06 GMT\","
441 " \"end\":\"7 Jan 2013 5:40:75 PST\""
442 " }"
443 " ],"
444 " \"grouping\":"
445 " {"
446 " \"buckets\":1000,"
447 " \"segment\":200,"
448 " \"increment\":100,"
449 " \"increment_frequency\":3600,"
450 " \"increment_max\":400"
451 " },"
452 " \"payload\":"
453 " {"
454 " \"promo_message_short\":"
455 " \"What do you think of Chrome?\","
456 " \"days_active\":7,"
457 " \"install_age_days\":21,"
458 " \"gplus_required\":false"
459 " },"
460 " \"max_views\":30"
461 " }"
462 " ]"
463 "}",
464 "What do you think of Chrome?",
465 // The starting date is in 1999 to make tests pass
466 // on Android devices with incorrect or unset date/time.
467 933672366, // unix epoch for 3 Aug 1999 9:26:06 GMT.
468 1357566075, // unix epoch for 7 Jan 2013 5:40:75 PST.
469 1000, 200, 100, 3600, 400, 30, false);
470
471 promo_test.InitPromoFromJson(true);
472 // Second time should not trigger a notification.
473 promo_test.InitPromoFromJson(false);
474 promo_test.TestInitFromPrefs();
475 }
476
477 // Test that strings.|payload.promo_message_short| is used if present.
478 TEST_F(PromoResourceServiceTest, NotificationPromoCompatPayloadStringsTest) {
479 // Check that prefs are set correctly.
480 PrefService* prefs = profile_.GetPrefs();
481 ASSERT_TRUE(prefs != NULL);
482
483 NotificationPromoTest promo_test(&profile_);
484
485 // Set up start and end dates and promo line in a Dictionary as if parsed
486 // from the service.
487 promo_test.Init("{"
488 " \"ntp_notification_promo\": ["
489 " {"
490 " \"date\":"
491 " ["
492 " {"
493 " \"start\":\"3 Aug 1999 9:26:06 GMT\","
494 " \"end\":\"7 Jan 2013 5:40:75 PST\""
495 " }"
496 " ],"
497 " \"grouping\":"
498 " {"
499 " \"buckets\":1000,"
500 " \"segment\":200,"
501 " \"increment\":100,"
502 " \"increment_frequency\":3600,"
503 " \"increment_max\":400"
504 " },"
505 " \"strings\":"
506 " {"
507 " \"bogus\":\"string\","
508 " \"GOOD_STRING\":"
509 " \"What do you think of Chrome?\""
510 " },"
511 " \"payload\":"
512 " {"
513 " \"promo_message_short\":"
514 " \"GOOD_STRING\","
515 " \"days_active\":7,"
516 " \"install_age_days\":21,"
517 " \"gplus_required\":false"
518 " },"
519 " \"max_views\":30"
520 " }"
521 " ]"
522 "}",
523 "What do you think of Chrome?",
524 // The starting date is in 1999 to make tests pass
525 // on Android devices with incorrect or unset date/time.
526 933672366, // unix epoch for 3 Aug 1999 9:26:06 GMT.
527 1357566075, // unix epoch for 7 Jan 2013 5:40:75 PST.
528 1000, 200, 100, 3600, 400, 30, false);
529
530 promo_test.InitPromoFromJson(true);
531 // Second time should not trigger a notification.
532 promo_test.InitPromoFromJson(false);
533 promo_test.TestInitFromPrefs();
534 }
535
533 TEST_F(PromoResourceServiceTest, PromoServerURLTest) { 536 TEST_F(PromoResourceServiceTest, PromoServerURLTest) {
534 GURL promo_server_url = NotificationPromo::PromoServerURL(); 537 GURL promo_server_url = NotificationPromo::PromoServerURL();
535 EXPECT_FALSE(promo_server_url.is_empty()); 538 EXPECT_FALSE(promo_server_url.is_empty());
536 EXPECT_TRUE(promo_server_url.SchemeIs("https")); 539 EXPECT_TRUE(promo_server_url.SchemeIs("https"));
537 // TODO(achuith): Test this better. 540 // TODO(achuith): Test this better.
538 } 541 }
OLDNEW
« no previous file with comments | « chrome/browser/web_resource/promo_resource_service_mobile_ntp_unittest.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698