[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" |
| 8 | #include "content/public/browser/web_contents.h" |
| 9 | #include "content/public/common/content_switches.h" |
| 10 | #include "content/public/renderer/render_view.h" |
[email protected] | 12a936d | 2013-05-15 04:55:49 | [diff] [blame] | 11 | #include "content/renderer/savable_resources.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 12 | #include "content/shell/browser/shell.h" |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 13 | #include "content/test/content_browser_test.h" |
| 14 | #include "content/test/content_browser_test_utils.h" |
| 15 | #include "net/base/net_util.h" |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 16 | |
| 17 | namespace content { |
| 18 | |
[email protected] | 12a936d | 2013-05-15 04:55:49 | [diff] [blame] | 19 | class SavableResourcesTest : public ContentBrowserTest { |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 20 | public: |
[email protected] | 4209190 | 2013-05-02 02:24:12 | [diff] [blame] | 21 | virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 22 | command_line->AppendSwitch(switches::kSingleProcess); |
[email protected] | 0c2c0d1 | 2014-02-04 17:24:43 | [diff] [blame^] | 23 | #if defined(OS_WIN) |
[email protected] | 5d97f76 | 2013-04-23 06:15:49 | [diff] [blame] | 24 | // Don't want to try to create a GPU process. |
| 25 | command_line->AppendSwitch(switches::kDisableAcceleratedCompositing); |
| 26 | #endif |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | // Test function GetAllSavableResourceLinksForCurrentPage with a web page. |
| 30 | // We expect result of GetAllSavableResourceLinksForCurrentPage exactly |
| 31 | // matches expected_resources_set. |
| 32 | void GetSavableResourceLinksForPage( |
| 33 | const base::FilePath& page_file_path, |
| 34 | const std::set<GURL>& expected_resources_set) { |
| 35 | // Convert local file path to file URL. |
| 36 | GURL file_url = net::FilePathToFileURL(page_file_path); |
| 37 | // Load the test file. |
| 38 | NavigateToURL(shell(), file_url); |
| 39 | |
| 40 | PostTaskToInProcessRendererAndWait( |
[email protected] | 12a936d | 2013-05-15 04:55:49 | [diff] [blame] | 41 | base::Bind(&SavableResourcesTest::CheckResources, |
| 42 | base::Unretained(this), |
| 43 | page_file_path, |
| 44 | expected_resources_set, |
| 45 | file_url, |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 46 | shell()->web_contents()->GetRoutingID())); |
| 47 | } |
| 48 | |
| 49 | void CheckResources(const base::FilePath& page_file_path, |
| 50 | const std::set<GURL>& expected_resources_set, |
| 51 | const GURL& file_url, |
| 52 | int render_view_id) { |
| 53 | // Get all savable resource links for the page. |
| 54 | std::vector<GURL> resources_list; |
| 55 | std::vector<GURL> referrer_urls_list; |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 56 | std::vector<blink::WebReferrerPolicy> referrer_policies_list; |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 57 | std::vector<GURL> frames_list; |
[email protected] | 12a936d | 2013-05-15 04:55:49 | [diff] [blame] | 58 | SavableResourcesResult result(&resources_list, |
| 59 | &referrer_urls_list, |
| 60 | &referrer_policies_list, |
| 61 | &frames_list); |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 62 | |
| 63 | const char* savable_schemes[] = { |
| 64 | "http", |
| 65 | "https", |
| 66 | "file", |
| 67 | NULL |
| 68 | }; |
| 69 | |
| 70 | RenderView* render_view = RenderView::FromRoutingID(render_view_id); |
| 71 | |
[email protected] | 12a936d | 2013-05-15 04:55:49 | [diff] [blame] | 72 | ASSERT_TRUE(GetAllSavableResourceLinksForCurrentPage( |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 73 | render_view->GetWebView(), file_url, &result, savable_schemes)); |
| 74 | // Check all links of sub-resource |
| 75 | for (std::vector<GURL>::const_iterator cit = resources_list.begin(); |
| 76 | cit != resources_list.end(); ++cit) { |
| 77 | ASSERT_TRUE(expected_resources_set.find(*cit) != |
| 78 | expected_resources_set.end()); |
| 79 | } |
| 80 | // Check all links of frame. |
| 81 | for (std::vector<GURL>::const_iterator cit = frames_list.begin(); |
| 82 | cit != frames_list.end(); ++cit) { |
| 83 | ASSERT_TRUE(expected_resources_set.find(*cit) != |
| 84 | expected_resources_set.end()); |
| 85 | } |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | // Test function GetAllSavableResourceLinksForCurrentPage with a web page |
| 90 | // which has valid savable resource links. |
[email protected] | 12a936d | 2013-05-15 04:55:49 | [diff] [blame] | 91 | IN_PROC_BROWSER_TEST_F(SavableResourcesTest, |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 92 | GetSavableResourceLinksWithPageHasValidLinks) { |
| 93 | std::set<GURL> expected_resources_set; |
| 94 | |
| 95 | const char* expected_sub_resource_links[] = { |
| 96 | "file:///c:/yt/css/base_all-vfl36460.css", |
| 97 | "file:///c:/yt/js/base_all_with_bidi-vfl36451.js", |
| 98 | "file:///c:/yt/img/pixel-vfl73.gif" |
| 99 | }; |
| 100 | const char* expected_frame_links[] = { |
| 101 | "youtube_1.htm", |
| 102 | "youtube_2.htm" |
| 103 | }; |
| 104 | // Add all expected links of sub-resource to expected set. |
| 105 | for (size_t i = 0; i < arraysize(expected_sub_resource_links); ++i) |
| 106 | expected_resources_set.insert(GURL(expected_sub_resource_links[i])); |
| 107 | // Add all expected links of frame to expected set. |
| 108 | for (size_t i = 0; i < arraysize(expected_frame_links); ++i) { |
| 109 | const base::FilePath expected_frame_url = |
| 110 | GetTestFilePath("dom_serializer", expected_frame_links[i]); |
| 111 | expected_resources_set.insert( |
| 112 | net::FilePathToFileURL(expected_frame_url)); |
| 113 | } |
| 114 | |
| 115 | base::FilePath page_file_path = |
| 116 | GetTestFilePath("dom_serializer", "youtube_1.htm"); |
| 117 | GetSavableResourceLinksForPage(page_file_path, expected_resources_set); |
| 118 | } |
| 119 | |
| 120 | // Test function GetAllSavableResourceLinksForCurrentPage with a web page |
| 121 | // which does not have valid savable resource links. |
[email protected] | 12a936d | 2013-05-15 04:55:49 | [diff] [blame] | 122 | IN_PROC_BROWSER_TEST_F(SavableResourcesTest, |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 123 | GetSavableResourceLinksWithPageHasInvalidLinks) { |
| 124 | std::set<GURL> expected_resources_set; |
| 125 | |
| 126 | const char* expected_frame_links[] = { |
| 127 | "youtube_2.htm" |
| 128 | }; |
| 129 | // Add all expected links of frame to expected set. |
| 130 | for (size_t i = 0; i < arraysize(expected_frame_links); ++i) { |
| 131 | base::FilePath expected_frame_url = |
| 132 | GetTestFilePath("dom_serializer", expected_frame_links[i]); |
| 133 | expected_resources_set.insert( |
| 134 | net::FilePathToFileURL(expected_frame_url)); |
| 135 | } |
| 136 | |
| 137 | base::FilePath page_file_path = |
| 138 | GetTestFilePath("dom_serializer", "youtube_2.htm"); |
| 139 | GetSavableResourceLinksForPage(page_file_path, expected_resources_set); |
| 140 | } |
| 141 | |
[email protected] | c1978abe | 2013-04-23 03:08:12 | [diff] [blame] | 142 | } // namespace content |