[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 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 "base/bind.h" |
| 6 | #include "base/command_line.h" |
| 7 | #include "base/files/file_path.h" |
avi | 1023d01 | 2015-12-25 02:39:14 | [diff] [blame] | 8 | #include "build/build_config.h" |
lukasza | 6af746b7 | 2015-09-18 23:37:22 | [diff] [blame] | 9 | #include "content/public/browser/render_frame_host.h" |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 10 | #include "content/public/browser/web_contents.h" |
| 11 | #include "content/public/common/content_switches.h" |
lukasza | 6af746b7 | 2015-09-18 23:37:22 | [diff] [blame] | 12 | #include "content/public/renderer/render_frame.h" |
[email protected] | 6e9def1 | 2014-03-27 20:23:28 | [diff] [blame] | 13 | #include "content/public/test/content_browser_test.h" |
| 14 | #include "content/public/test/content_browser_test_utils.h" |
[email protected] | 12a936d | 2013-05-15 04:55:49 | [diff] [blame] | 15 | #include "content/renderer/savable_resources.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 16 | #include "content/shell/browser/shell.h" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 17 | #include "net/base/filename_util.h" |
lukasza | fe911df | 2015-10-21 21:06:19 | [diff] [blame] | 18 | #include "testing/gmock/include/gmock/gmock.h" |
| 19 | #include "testing/gtest/include/gtest/gtest.h" |
lukasza | 87f4e2ab | 2015-11-04 16:03:03 | [diff] [blame] | 20 | #include "third_party/WebKit/public/web/WebFrame.h" |
lukasza | 6af746b7 | 2015-09-18 23:37:22 | [diff] [blame] | 21 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 22 | |
| 23 | namespace content { |
| 24 | |
[email protected] | 12a936d | 2013-05-15 04:55:49 | [diff] [blame] | 25 | class SavableResourcesTest : public ContentBrowserTest { |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 26 | public: |
lukasza | fe911df | 2015-10-21 21:06:19 | [diff] [blame] | 27 | using UrlVectorMatcher = testing::Matcher<std::vector<GURL>>; |
| 28 | |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 29 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 30 | command_line->AppendSwitch(switches::kSingleProcess); |
[email protected] | 0c2c0d1 | 2014-02-04 17:24:43 | [diff] [blame] | 31 | #if defined(OS_WIN) |
[email protected] | 5d97f76 | 2013-04-23 06:15:49 | [diff] [blame] | 32 | // Don't want to try to create a GPU process. |
[email protected] | 7af65ca | 2014-04-16 20:25:56 | [diff] [blame] | 33 | command_line->AppendSwitch(switches::kDisableGpu); |
[email protected] | 5d97f76 | 2013-04-23 06:15:49 | [diff] [blame] | 34 | #endif |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // Test function GetAllSavableResourceLinksForCurrentPage with a web page. |
| 38 | // We expect result of GetAllSavableResourceLinksForCurrentPage exactly |
| 39 | // matches expected_resources_set. |
| 40 | void GetSavableResourceLinksForPage( |
| 41 | const base::FilePath& page_file_path, |
lukasza | 87f4e2ab | 2015-11-04 16:03:03 | [diff] [blame] | 42 | const UrlVectorMatcher& expected_resources_matcher, |
| 43 | const UrlVectorMatcher& expected_subframe_urls_matcher) { |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 44 | // Convert local file path to file URL. |
| 45 | GURL file_url = net::FilePathToFileURL(page_file_path); |
| 46 | // Load the test file. |
| 47 | NavigateToURL(shell(), file_url); |
| 48 | |
lukasza | 6af746b7 | 2015-09-18 23:37:22 | [diff] [blame] | 49 | PostTaskToInProcessRendererAndWait(base::Bind( |
| 50 | &SavableResourcesTest::CheckResources, base::Unretained(this), |
lukasza | 87f4e2ab | 2015-11-04 16:03:03 | [diff] [blame] | 51 | page_file_path, expected_resources_matcher, |
| 52 | expected_subframe_urls_matcher, file_url, |
lukasza | 6af746b7 | 2015-09-18 23:37:22 | [diff] [blame] | 53 | shell()->web_contents()->GetMainFrame()->GetRoutingID())); |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void CheckResources(const base::FilePath& page_file_path, |
lukasza | fe911df | 2015-10-21 21:06:19 | [diff] [blame] | 57 | const UrlVectorMatcher& expected_resources_matcher, |
lukasza | 87f4e2ab | 2015-11-04 16:03:03 | [diff] [blame] | 58 | const UrlVectorMatcher& expected_subframe_urls_matcher, |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 59 | const GURL& file_url, |
lukasza | 6af746b7 | 2015-09-18 23:37:22 | [diff] [blame] | 60 | int render_frame_routing_id) { |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 61 | // Get all savable resource links for the page. |
| 62 | std::vector<GURL> resources_list; |
lukasza | 779a08f8 | 2015-11-04 18:27:04 | [diff] [blame] | 63 | std::vector<SavableSubframe> subframes; |
| 64 | SavableResourcesResult result(&resources_list, &subframes); |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 65 | |
lukasza | 6af746b7 | 2015-09-18 23:37:22 | [diff] [blame] | 66 | RenderFrame* render_frame = |
| 67 | RenderFrame::FromRoutingID(render_frame_routing_id); |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 68 | |
lukasza | 6af746b7 | 2015-09-18 23:37:22 | [diff] [blame] | 69 | ASSERT_TRUE(GetSavableResourceLinksForFrame( |
jam | e0dcd98 | 2017-01-11 03:13:45 | [diff] [blame] | 70 | render_frame->GetWebFrame(), &result)); |
samahto | 99fcc58 | 2015-06-15 17:13:27 | [diff] [blame] | 71 | |
lukasza | fe911df | 2015-10-21 21:06:19 | [diff] [blame] | 72 | EXPECT_THAT(resources_list, expected_resources_matcher); |
lukasza | 87f4e2ab | 2015-11-04 16:03:03 | [diff] [blame] | 73 | |
lukasza | 779a08f8 | 2015-11-04 18:27:04 | [diff] [blame] | 74 | std::vector<GURL> subframe_original_urls; |
| 75 | for (const SavableSubframe& subframe : subframes) { |
| 76 | subframe_original_urls.push_back(subframe.original_url); |
| 77 | } |
lukasza | 87f4e2ab | 2015-11-04 16:03:03 | [diff] [blame] | 78 | EXPECT_THAT(subframe_original_urls, expected_subframe_urls_matcher); |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 79 | } |
| 80 | }; |
| 81 | |
samahto | 99fcc58 | 2015-06-15 17:13:27 | [diff] [blame] | 82 | IN_PROC_BROWSER_TEST_F(SavableResourcesTest, |
| 83 | GetSavableResourceLinksWithPageHasValidStyleLink) { |
samahto | 99fcc58 | 2015-06-15 17:13:27 | [diff] [blame] | 84 | base::FilePath page_file_path = |
| 85 | GetTestFilePath("dom_serializer", "simple_linked_stylesheet.html"); |
lukasza | fe911df | 2015-10-21 21:06:19 | [diff] [blame] | 86 | |
| 87 | auto expected_subresources_matcher = testing::UnorderedElementsAre( |
| 88 | net::FilePathToFileURL(GetTestFilePath("dom_serializer", "style.css"))); |
| 89 | |
lukasza | 87f4e2ab | 2015-11-04 16:03:03 | [diff] [blame] | 90 | auto expected_subframe_urls_matcher = testing::IsEmpty(); |
| 91 | |
| 92 | GetSavableResourceLinksForPage(page_file_path, expected_subresources_matcher, |
| 93 | expected_subframe_urls_matcher); |
samahto | 99fcc58 | 2015-06-15 17:13:27 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 96 | // Test function GetAllSavableResourceLinksForCurrentPage with a web page |
| 97 | // which has valid savable resource links. |
[email protected] | 12a936d | 2013-05-15 04:55:49 | [diff] [blame] | 98 | IN_PROC_BROWSER_TEST_F(SavableResourcesTest, |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 99 | GetSavableResourceLinksWithPageHasValidLinks) { |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 100 | base::FilePath page_file_path = |
| 101 | GetTestFilePath("dom_serializer", "youtube_1.htm"); |
lukasza | fe911df | 2015-10-21 21:06:19 | [diff] [blame] | 102 | |
| 103 | auto expected_subresources_matcher = testing::UnorderedElementsAre( |
| 104 | GURL("file:///c:/yt/css/base_all-vfl36460.css"), |
| 105 | GURL("file:///c:/yt/js/base_all_with_bidi-vfl36451.js"), |
| 106 | GURL("file:///c:/yt/img/pixel-vfl73.gif")); |
| 107 | |
lukasza | 87f4e2ab | 2015-11-04 16:03:03 | [diff] [blame] | 108 | auto expected_subframe_urls_matcher = |
| 109 | testing::UnorderedElementsAre(net::FilePathToFileURL( |
| 110 | GetTestFilePath("dom_serializer", "youtube_2.htm"))); |
| 111 | |
| 112 | GetSavableResourceLinksForPage(page_file_path, expected_subresources_matcher, |
| 113 | expected_subframe_urls_matcher); |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // Test function GetAllSavableResourceLinksForCurrentPage with a web page |
| 117 | // which does not have valid savable resource links. |
[email protected] | 12a936d | 2013-05-15 04:55:49 | [diff] [blame] | 118 | IN_PROC_BROWSER_TEST_F(SavableResourcesTest, |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 119 | GetSavableResourceLinksWithPageHasInvalidLinks) { |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 120 | base::FilePath page_file_path = |
| 121 | GetTestFilePath("dom_serializer", "youtube_2.htm"); |
lukasza | fe911df | 2015-10-21 21:06:19 | [diff] [blame] | 122 | |
| 123 | auto expected_subresources_matcher = testing::IsEmpty(); |
lukasza | 87f4e2ab | 2015-11-04 16:03:03 | [diff] [blame] | 124 | |
| 125 | auto expected_subframe_urls_matcher = testing::IsEmpty(); |
| 126 | |
| 127 | GetSavableResourceLinksForPage(page_file_path, expected_subresources_matcher, |
| 128 | expected_subframe_urls_matcher); |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 129 | } |
| 130 | |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 131 | } // namespace content |