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