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