Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <vector> | |
| 6 | |
| 7 #include "base/json/json_reader.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "base/string_number_conversions.h" | |
| 10 #include "base/time.h" | |
| 11 #include "base/utf_string_conversions.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/browser_process.h" | |
| 14 #include "chrome/browser/prefs/browser_prefs.h" | |
| 15 #include "chrome/browser/prefs/pref_service.h" | |
| 16 #include "chrome/browser/web_resource/notification_promo.h" | |
| 17 #include "chrome/browser/web_resource/promo_resource_service.h" | |
| 18 #include "chrome/common/chrome_notification_types.h" | |
| 19 #include "chrome/common/pref_names.h" | |
| 20 #include "chrome/test/base/testing_browser_process.h" | |
| 21 #include "chrome/test/base/testing_pref_service.h" | |
| 22 #include "chrome/test/base/testing_profile.h" | |
| 23 #include "content/public/browser/notification_registrar.h" | |
| 24 #include "content/public/browser/notification_service.h" | |
| 25 #include "testing/gtest/include/gtest/gtest.h" | |
| 26 | |
| 27 #include "chrome/browser/web_resource/notification_promo_mobile_ntp.h" | |
| 28 | |
| 29 class PromoResourceServiceMobileNtpTest : public testing::Test { | |
| 30 public: | |
| 31 PromoResourceServiceMobileNtpTest() | |
| 32 : local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)), | |
| 33 web_resource_service_(new PromoResourceService(&profile_)) { | |
| 34 } | |
| 35 | |
| 36 protected: | |
| 37 TestingProfile profile_; | |
| 38 ScopedTestingLocalState local_state_; | |
| 39 scoped_refptr<PromoResourceService> web_resource_service_; | |
| 40 MessageLoop loop_; | |
| 41 }; | |
| 42 | |
| 43 class NotificationPromoMobileNtpTest { | |
| 44 public: | |
| 45 explicit NotificationPromoMobileNtpTest(Profile* profile) | |
| 46 : profile_(profile), | |
| 47 prefs_(profile->GetPrefs()), | |
| 48 mobile_promo_(profile), | |
| 49 received_notification_(false) { | |
| 50 } | |
| 51 | |
| 52 void Init(const std::string& json, | |
| 53 const std::string& promo_text, | |
| 54 const std::string& promo_text_long, | |
| 55 const std::string& promo_action_type, | |
| 56 const std::string& promo_action_arg0, | |
| 57 const std::string& promo_action_arg1) { | |
| 58 Value* value(base::JSONReader::Read(json)); | |
| 59 ASSERT_TRUE(value); | |
| 60 DictionaryValue* dict = NULL; | |
| 61 value->GetAsDictionary(&dict); | |
| 62 ASSERT_TRUE(dict); | |
| 63 test_json_.reset(dict); | |
| 64 | |
| 65 promo_text_ = promo_text; | |
| 66 promo_text_long_ = promo_text_long; | |
| 67 promo_action_type_ = promo_action_type; | |
| 68 promo_action_args_.push_back(promo_action_arg0); | |
| 69 promo_action_args_.push_back(promo_action_arg1); | |
| 70 | |
| 71 received_notification_ = false; | |
| 72 } | |
| 73 | |
| 74 void InitPromoFromJson(bool should_receive_notification) { | |
| 75 const bool rv = mobile_promo_.InitFromJson(*test_json_); | |
| 76 EXPECT_TRUE(rv); | |
| 77 EXPECT_TRUE(mobile_promo_.valid()); | |
| 78 EXPECT_EQ(should_receive_notification, | |
| 79 mobile_promo_.notification_promo().new_notification()); | |
| 80 | |
| 81 // Test the fields. | |
| 82 TestNotification(); | |
| 83 } | |
| 84 | |
| 85 void TestNotification() { | |
| 86 // Check values. | |
| 87 EXPECT_TRUE(mobile_promo_.valid()); | |
| 88 EXPECT_EQ(mobile_promo_.text(), promo_text_); | |
| 89 EXPECT_EQ(mobile_promo_.text_long(), promo_text_long_); | |
| 90 EXPECT_EQ(mobile_promo_.action_type(), promo_action_type_); | |
| 91 EXPECT_TRUE(mobile_promo_.action_args() != NULL); | |
| 92 EXPECT_EQ(2u, promo_action_args_.size()); | |
| 93 EXPECT_EQ(mobile_promo_.action_args()->GetSize(), | |
| 94 promo_action_args_.size()); | |
| 95 for (std::size_t i = 0; i < promo_action_args_.size(); ++i) { | |
| 96 std::string value; | |
| 97 EXPECT_TRUE(mobile_promo_.action_args()->GetString(i, &value)); | |
| 98 EXPECT_EQ(value, promo_action_args_[i]); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 // Create a new NotificationPromo from prefs and compare to current | |
| 103 // notification. | |
| 104 void TestInitFromPrefs() { | |
| 105 NotificationPromoMobileNtp prefs_mobile_promo(profile_); | |
| 106 const bool rv = prefs_mobile_promo.InitFromPrefs(); | |
| 107 EXPECT_TRUE(rv); | |
| 108 EXPECT_TRUE(prefs_mobile_promo.valid()); | |
| 109 EXPECT_TRUE(mobile_promo_.valid()); | |
| 110 | |
| 111 EXPECT_EQ(prefs_mobile_promo.text(), | |
| 112 mobile_promo_.text()); | |
| 113 EXPECT_EQ(prefs_mobile_promo.text_long(), | |
| 114 mobile_promo_.text_long()); | |
| 115 EXPECT_EQ(prefs_mobile_promo.action_type(), | |
| 116 mobile_promo_.action_type()); | |
| 117 EXPECT_TRUE(mobile_promo_.action_args() != NULL); | |
| 118 EXPECT_EQ(prefs_mobile_promo.action_args()->GetSize(), | |
| 119 mobile_promo_.action_args()->GetSize()); | |
| 120 for (std::size_t i = 0; | |
| 121 i < prefs_mobile_promo.action_args()->GetSize(); | |
| 122 ++i) { | |
| 123 std::string promo_value; | |
| 124 std::string prefs_value; | |
| 125 EXPECT_TRUE( | |
| 126 prefs_mobile_promo.action_args()->GetString(i, &prefs_value)); | |
| 127 EXPECT_TRUE( | |
| 128 mobile_promo_.action_args()->GetString(i, &promo_value)); | |
| 129 EXPECT_EQ(promo_value, prefs_value); | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 private: | |
| 134 Profile* profile_; | |
| 135 PrefService* prefs_; | |
| 136 NotificationPromoMobileNtp mobile_promo_; | |
| 137 bool received_notification_; | |
| 138 scoped_ptr<DictionaryValue> test_json_; | |
| 139 | |
| 140 std::string promo_text_; | |
| 141 std::string promo_text_long_; | |
| 142 std::string promo_action_type_; | |
| 143 std::vector<std::string> promo_action_args_; | |
| 144 }; | |
| 145 | |
| 146 TEST_F(PromoResourceServiceMobileNtpTest, NotificationPromoMobileNtpTest) { | |
| 147 // Check that prefs are set correctly. | |
| 148 PrefService* prefs = profile_.GetPrefs(); | |
| 149 ASSERT_TRUE(prefs); | |
| 150 | |
| 151 NotificationPromoMobileNtpTest promo_test(&profile_); | |
| 152 | |
| 153 // Set up start and end dates and promo line in a Dictionary as if parsed | |
| 154 // from the service. | |
| 155 promo_test.Init( | |
| 156 "{" | |
| 157 " \"mobile_ntp_sync_promo\": [" | |
| 158 " {" | |
| 159 " \"date\":" | |
| 160 " [" | |
| 161 " {" | |
| 162 " \"start\":\"3 Aug 1999 9:26:06 GMT\"," | |
| 163 " \"end\":\"7 Jan 2013 5:40:75 PST\"" | |
| 164 " }" | |
| 165 " ]," | |
| 166 " \"strings\":" | |
| 167 " {" | |
| 168 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\":" | |
| 169 " \"Like Chrome? Go http://www.google.com/chrome/\"," | |
|
Dan Beam
2012/08/22 19:43:27
can you indent these so it's apparent they corresp
aruslan
2012/08/23 00:49:59
Done.
| |
| 170 " \"MOBILE_PROMO_CHROME_LONG_TEXT\":" | |
| 171 " \"It's simple. Go http://www.google.com/chrome/\"," | |
| 172 " \"MOBILE_PROMO_EMAIL_BODY\":\"This is the body.\"," | |
| 173 " \"XXX\":\"XXX value\"" | |
| 174 " }," | |
| 175 " \"payload\":" | |
| 176 " {" | |
| 177 " \"payload_format_version\":3," | |
| 178 " \"gplus_required\":false," | |
| 179 " \"promo_message_long\":" | |
| 180 " \"MOBILE_PROMO_CHROME_LONG_TEXT\"," | |
| 181 " \"promo_message_short\":" | |
| 182 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\"," | |
| 183 " \"promo_action_type\":\"ACTION_EMAIL\"," | |
| 184 " \"promo_action_args\":[\"MOBILE_PROMO_EMAIL_BODY\",\"XXX\"]" | |
| 185 " }," | |
| 186 " \"max_views\":30" | |
| 187 " }" | |
| 188 " ]" | |
| 189 "}", | |
| 190 "Like Chrome? Go http://www.google.com/chrome/", | |
| 191 "It\'s simple. Go http://www.google.com/chrome/", | |
| 192 "ACTION_EMAIL", "This is the body.", "XXX value"); | |
| 193 | |
| 194 promo_test.InitPromoFromJson(true); | |
| 195 | |
| 196 // Second time should not trigger a notification. | |
| 197 promo_test.InitPromoFromJson(false); | |
| 198 | |
| 199 promo_test.TestInitFromPrefs(); | |
| 200 } | |
| OLD | NEW |