Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / extensions / subscribe_page_action_browsertest.cc
blob1e9c7b62f6fb4ae37e4ca055548263488817d0a3
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 "chrome/browser/extensions/extension_browsertest.h"
6 #include "chrome/browser/ui/browser.h"
7 #include "chrome/browser/ui/tabs/tab_strip_model.h"
8 #include "chrome/common/url_constants.h"
9 #include "chrome/test/base/ui_test_utils.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/test/browser_test_utils.h"
12 #include "extensions/common/constants.h"
13 #include "extensions/common/extension.h"
15 using content::WebContents;
16 using extensions::Extension;
18 namespace {
20 const char kSubscribePage[] = "/subscribe.html";
21 const char kFeedPageMultiRel[] = "files/feeds/feed_multi_rel.html";
22 const char kValidFeedNoLinks[] = "files/feeds/feed_nolinks.xml";
23 const char kValidFeed0[] = "files/feeds/feed_script.xml";
24 const char kValidFeed1[] = "files/feeds/feed1.xml";
25 const char kValidFeed2[] = "files/feeds/feed2.xml";
26 const char kValidFeed3[] = "files/feeds/feed3.xml";
27 const char kValidFeed4[] = "files/feeds/feed4.xml";
28 const char kValidFeed5[] = "files/feeds/feed5.xml";
29 const char kValidFeed6[] = "files/feeds/feed6.xml";
30 const char kInvalidFeed1[] = "files/feeds/feed_invalid1.xml";
31 const char kInvalidFeed2[] = "files/feeds/feed_invalid2.xml";
32 // We need a triple encoded string to prove that we are not decoding twice in
33 // subscribe.js because one layer is also stripped off when subscribe.js passes
34 // it to the XMLHttpRequest object.
35 const char kFeedTripleEncoded[] = "files/feeds/url%25255Fdecoding.html";
37 static const char kScriptFeedTitle[] =
38 "window.domAutomationController.send("
39 " document.getElementById('title') ? "
40 " document.getElementById('title').textContent : "
41 " \"element 'title' not found\""
42 ");";
43 static const char kScriptAnchor[] =
44 "window.domAutomationController.send("
45 " document.getElementById('anchor_0') ? "
46 " document.getElementById('anchor_0').textContent : "
47 " \"element 'anchor_0' not found\""
48 ");";
49 static const char kScriptDesc[] =
50 "window.domAutomationController.send("
51 " document.getElementById('desc_0') ? "
52 " document.getElementById('desc_0').textContent : "
53 " \"element 'desc_0' not found\""
54 ");";
55 static const char kScriptError[] =
56 "window.domAutomationController.send("
57 " document.getElementById('error') ? "
58 " document.getElementById('error').textContent : "
59 " \"No error\""
60 ");";
62 GURL GetFeedUrl(net::SpawnedTestServer* server, const std::string& feed_page,
63 bool direct_url, std::string extension_id) {
64 GURL feed_url = server->GetURL(feed_page);
65 if (direct_url) {
66 // We navigate directly to the subscribe page for feeds where the feed
67 // sniffing won't work, in other words, as is the case for malformed feeds.
68 return GURL(std::string(extensions::kExtensionScheme) +
69 url::kStandardSchemeSeparator +
70 extension_id + std::string(kSubscribePage) + std::string("?") +
71 feed_url.spec() + std::string("&synchronous"));
72 } else {
73 // Navigate to the feed content (which will cause the extension to try to
74 // sniff the type and display the subscribe page in another tab.
75 return GURL(feed_url.spec());
79 bool ValidatePageElement(content::RenderFrameHost* frame,
80 const std::string& javascript,
81 const std::string& expected_value) {
82 std::string returned_value;
84 if (!content::ExecuteScriptAndExtractString(frame,
85 javascript,
86 &returned_value))
87 return false;
89 EXPECT_STREQ(expected_value.c_str(), returned_value.c_str());
90 return expected_value == returned_value;
93 // Navigates to a feed page and, if |sniff_xml_type| is set, wait for the
94 // extension to kick in, detect the feed and redirect to a feed preview page.
95 // |sniff_xml_type| is generally set to true if the feed is sniffable and false
96 // for invalid feeds.
97 void NavigateToFeedAndValidate(net::SpawnedTestServer* server,
98 const std::string& url,
99 Browser* browser,
100 std::string extension_id,
101 bool sniff_xml_type,
102 const std::string& expected_feed_title,
103 const std::string& expected_item_title,
104 const std::string& expected_item_desc,
105 const std::string& expected_error) {
106 if (sniff_xml_type) {
107 // TODO(finnur): Implement this is a non-flaky way.
110 // Navigate to the subscribe page directly.
111 ui_test_utils::NavigateToURL(browser,
112 GetFeedUrl(server, url, true, extension_id));
114 WebContents* tab = browser->tab_strip_model()->GetActiveWebContents();
115 content::RenderFrameHost* frame = content::FrameMatchingPredicate(
116 tab, base::Bind(&content::FrameIsChildOfMainFrame));
117 ASSERT_TRUE(ValidatePageElement(
118 tab->GetMainFrame(), kScriptFeedTitle, expected_feed_title));
119 ASSERT_TRUE(ValidatePageElement(frame, kScriptAnchor, expected_item_title));
120 ASSERT_TRUE(ValidatePageElement(frame, kScriptDesc, expected_item_desc));
121 ASSERT_TRUE(ValidatePageElement(frame, kScriptError, expected_error));
124 } // namespace
126 // Makes sure that the RSS detects RSS feed links, even when rel tag contains
127 // more than just "alternate".
128 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, RSSMultiRelLink) {
129 ASSERT_TRUE(test_server()->Start());
131 ASSERT_TRUE(LoadExtension(
132 test_data_dir_.AppendASCII("subscribe_page_action")));
134 ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(0));
136 // Navigate to the feed page.
137 GURL feed_url = test_server()->GetURL(kFeedPageMultiRel);
138 ui_test_utils::NavigateToURL(browser(), feed_url);
139 // We should now have one page action ready to go in the LocationBar.
140 ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1));
143 // This test is flaky on all platforms; see http://crbug.com/340354
144 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, DISABLED_RSSParseFeedValidFeed1) {
145 ASSERT_TRUE(test_server()->Start());
147 const Extension* extension = LoadExtension(
148 test_data_dir_.AppendASCII("subscribe_page_action"));
149 ASSERT_TRUE(extension);
150 std::string id = extension->id();
152 NavigateToFeedAndValidate(test_server(), kValidFeed1, browser(), id, true,
153 "Feed for MyFeedTitle",
154 "Title 1",
155 "Desc",
156 "No error");
159 // This test is flaky on all platforms; see http://crbug.com/340354
160 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, DISABLED_RSSParseFeedValidFeed2) {
161 ASSERT_TRUE(test_server()->Start());
163 const Extension* extension = LoadExtension(
164 test_data_dir_.AppendASCII("subscribe_page_action"));
165 ASSERT_TRUE(extension);
166 std::string id = extension->id();
168 NavigateToFeedAndValidate(test_server(), kValidFeed2, browser(), id, true,
169 "Feed for MyFeed2",
170 "My item title1",
171 "This is a summary.",
172 "No error");
175 // This test is flaky on all platforms; see http://crbug.com/340354
176 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, DISABLED_RSSParseFeedValidFeed3) {
177 ASSERT_TRUE(test_server()->Start());
179 const Extension* extension = LoadExtension(
180 test_data_dir_.AppendASCII("subscribe_page_action"));
181 ASSERT_TRUE(extension);
182 std::string id = extension->id();
184 NavigateToFeedAndValidate(test_server(), kValidFeed3, browser(), id, true,
185 "Feed for Google Code buglist rss feed",
186 "My dear title",
187 "My dear content",
188 "No error");
191 // This test is flaky on all platforms; see http://crbug.com/340354
192 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, DISABLED_RSSParseFeedValidFeed4) {
193 ASSERT_TRUE(test_server()->Start());
195 const Extension* extension = LoadExtension(
196 test_data_dir_.AppendASCII("subscribe_page_action"));
197 ASSERT_TRUE(extension);
198 std::string id = extension->id();
200 NavigateToFeedAndValidate(test_server(), kValidFeed4, browser(), id, true,
201 "Feed for Title chars <script> %23 stop",
202 "Title chars %23 stop",
203 "My dear content %23 stop",
204 "No error");
207 // This test is flaky on all platforms; see http://crbug.com/340354
208 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, DISABLED_RSSParseFeedValidFeed0) {
209 ASSERT_TRUE(test_server()->Start());
211 const Extension* extension = LoadExtension(
212 test_data_dir_.AppendASCII("subscribe_page_action"));
213 ASSERT_TRUE(extension);
214 std::string id = extension->id();
216 // Try a feed with a link with an onclick handler (before r27440 this would
217 // trigger a NOTREACHED).
218 NavigateToFeedAndValidate(test_server(), kValidFeed0, browser(), id, true,
219 "Feed for MyFeedTitle",
220 "Title 1",
221 "Desc VIDEO",
222 "No error");
225 // This test is flaky on all platforms; see http://crbug.com/340354
226 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, DISABLED_RSSParseFeedValidFeed5) {
227 ASSERT_TRUE(test_server()->Start());
229 const Extension* extension = LoadExtension(
230 test_data_dir_.AppendASCII("subscribe_page_action"));
231 ASSERT_TRUE(extension);
232 std::string id = extension->id();
234 // Feed with valid but mostly empty xml.
235 NavigateToFeedAndValidate(test_server(), kValidFeed5, browser(), id, true,
236 "Feed for Unknown feed name",
237 "element 'anchor_0' not found",
238 "element 'desc_0' not found",
239 "This feed contains no entries.");
242 // This test is flaky on all platforms; see http://crbug.com/340354
243 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, DISABLED_RSSParseFeedValidFeed6) {
244 ASSERT_TRUE(test_server()->Start());
246 const Extension* extension = LoadExtension(
247 test_data_dir_.AppendASCII("subscribe_page_action"));
248 ASSERT_TRUE(extension);
249 std::string id = extension->id();
251 // Feed that is technically invalid but still parseable.
252 NavigateToFeedAndValidate(test_server(), kValidFeed6, browser(), id, true,
253 "Feed for MyFeedTitle",
254 "Title 1",
255 "Desc",
256 "No error");
259 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, RSSParseFeedInvalidFeed1) {
260 ASSERT_TRUE(test_server()->Start());
262 const Extension* extension = LoadExtension(
263 test_data_dir_.AppendASCII("subscribe_page_action"));
264 ASSERT_TRUE(extension);
265 std::string id = extension->id();
267 // Try an empty feed.
268 NavigateToFeedAndValidate(test_server(), kInvalidFeed1, browser(), id, false,
269 "Feed for Unknown feed name",
270 "element 'anchor_0' not found",
271 "element 'desc_0' not found",
272 "This feed contains no entries.");
275 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, RSSParseFeedInvalidFeed2) {
276 ASSERT_TRUE(test_server()->Start());
278 const Extension* extension = LoadExtension(
279 test_data_dir_.AppendASCII("subscribe_page_action"));
280 ASSERT_TRUE(extension);
281 std::string id = extension->id();
283 // Try a garbage feed.
284 NavigateToFeedAndValidate(test_server(), kInvalidFeed2, browser(), id, false,
285 "Feed for Unknown feed name",
286 "element 'anchor_0' not found",
287 "element 'desc_0' not found",
288 "This feed contains no entries.");
291 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, RSSParseFeedInvalidFeed3) {
292 ASSERT_TRUE(test_server()->Start());
294 const Extension* extension = LoadExtension(
295 test_data_dir_.AppendASCII("subscribe_page_action"));
296 ASSERT_TRUE(extension);
297 std::string id = extension->id();
299 // Try a feed that doesn't exist.
300 NavigateToFeedAndValidate(test_server(), "foo.xml", browser(), id, false,
301 "Feed for Unknown feed name",
302 "element 'anchor_0' not found",
303 "element 'desc_0' not found",
304 "This feed contains no entries.");
307 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, RSSParseFeedInvalidFeed4) {
308 ASSERT_TRUE(test_server()->Start());
310 const Extension* extension = LoadExtension(
311 test_data_dir_.AppendASCII("subscribe_page_action"));
312 ASSERT_TRUE(extension);
313 std::string id = extension->id();
315 // subscribe.js shouldn't double-decode the URL passed in. Otherwise feed
316 // links such as http://search.twitter.com/search.atom?lang=en&q=%23chrome
317 // will result in no feed being downloaded because %23 gets decoded to # and
318 // therefore #chrome is not treated as part of the Twitter query. This test
319 // uses an underscore instead of a hash, but the principle is the same. If
320 // we start erroneously double decoding again, the path (and the feed) will
321 // become valid resulting in a failure for this test.
322 NavigateToFeedAndValidate(
323 test_server(), kFeedTripleEncoded, browser(), id, true,
324 "Feed for Unknown feed name",
325 "element 'anchor_0' not found",
326 "element 'desc_0' not found",
327 "This feed contains no entries.");
330 // This test is flaky on all platforms; see http://crbug.com/340354
331 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest,
332 DISABLED_RSSParseFeedValidFeedNoLinks) {
333 ASSERT_TRUE(test_server()->Start());
335 const Extension* extension = LoadExtension(
336 test_data_dir_.AppendASCII("subscribe_page_action"));
337 ASSERT_TRUE(extension);
338 std::string id = extension->id();
340 // Valid feed but containing no links.
341 NavigateToFeedAndValidate(
342 test_server(), kValidFeedNoLinks, browser(), id, true,
343 "Feed for MyFeedTitle",
344 "Title with no link",
345 "Desc",
346 "No error");