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