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.
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"
24 #include "chrome/browser/web_resource/notification_promo_mobile_ntp.h"
26 class PromoResourceServiceMobileNtpTest
: public testing::Test
{
28 // |promo_resource_service_| must be created after |local_state_|.
29 PromoResourceServiceMobileNtpTest()
30 : local_state_(TestingBrowserProcess::GetGlobal()),
31 promo_resource_service_(new PromoResourceService
) {}
34 ScopedTestingLocalState local_state_
;
35 scoped_refptr
<PromoResourceService
> promo_resource_service_
;
36 base::MessageLoop loop_
;
39 class NotificationPromoMobileNtpTest
{
41 NotificationPromoMobileNtpTest() : received_notification_(false) {}
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 base::Value
* value(base::JSONReader::Read(json
));
51 base::DictionaryValue
* dict
= NULL
;
52 value
->GetAsDictionary(&dict
);
54 test_json_
.reset(dict
);
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
);
62 received_notification_
= false;
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());
75 void TestNotification() {
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
) {
87 EXPECT_TRUE(mobile_promo_
.action_args()->GetString(i
, &value
));
88 EXPECT_EQ(value
, promo_action_args_
[i
]);
92 // Create a new NotificationPromo from prefs and compare to current
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());
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();
112 std::string promo_value
;
113 std::string prefs_value
;
115 prefs_mobile_promo
.action_args()->GetString(i
, &prefs_value
));
117 mobile_promo_
.action_args()->GetString(i
, &promo_value
));
118 EXPECT_EQ(promo_value
, prefs_value
);
123 NotificationPromoMobileNtp mobile_promo_
;
124 bool received_notification_
;
125 scoped_ptr
<base::DictionaryValue
> test_json_
;
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_
;
133 TEST_F(PromoResourceServiceMobileNtpTest
, NotificationPromoMobileNtpTest
) {
134 NotificationPromoMobileNtpTest promo_test
;
136 // Set up start and end dates and promo line in a Dictionary as if parsed
140 " \"mobile_ntp_sync_promo\": ["
145 " \"start\":\"3 Aug 1999 9:26:06 GMT\","
146 " \"end\":\"7 Jan 2013 5:40:75 PST\""
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\""
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\"]"
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");
176 promo_test
.InitPromoFromJson(true);
178 // Second time should not trigger a notification.
179 promo_test
.InitPromoFromJson(false);
181 promo_test
.TestInitFromPrefs();