| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/file_util.h" | 5 #include "base/bind.h" |
| 6 #include "base/path_service.h" | 6 #include "base/command_line.h" |
| 7 #include "base/string_util.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/utf_string_conversions.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" |
| 11 #include "content/shell/shell.h" |
| 12 #include "content/test/content_browser_test.h" |
| 13 #include "content/test/content_browser_test_utils.h" |
| 9 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
| 10 #include "net/url_request/url_request_context.h" | |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
| 12 #include "webkit/glue/dom_operations.h" | 15 #include "webkit/glue/dom_operations.h" |
| 13 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" | |
| 14 #include "webkit/tools/test_shell/test_shell_test.h" | |
| 15 | 16 |
| 16 namespace { | 17 namespace content { |
| 17 | 18 |
| 18 class DomOperationsTests : public TestShellTest { | 19 class DomOperationsTests : public ContentBrowserTest { |
| 19 public: | 20 public: |
| 21 virtual void SetUpCommandLine(CommandLine* command_line) { |
| 22 command_line->AppendSwitch(switches::kSingleProcess); |
| 23 } |
| 24 |
| 20 // Test function GetAllSavableResourceLinksForCurrentPage with a web page. | 25 // Test function GetAllSavableResourceLinksForCurrentPage with a web page. |
| 21 // We expect result of GetAllSavableResourceLinksForCurrentPage exactly | 26 // We expect result of GetAllSavableResourceLinksForCurrentPage exactly |
| 22 // matches expected_resources_set. | 27 // matches expected_resources_set. |
| 23 void GetSavableResourceLinksForPage(const base::FilePath& page_file_path, | 28 void GetSavableResourceLinksForPage( |
| 24 const std::set<GURL>& expected_resources_set); | 29 const base::FilePath& page_file_path, |
| 30 const std::set<GURL>& expected_resources_set) { |
| 31 // Convert local file path to file URL. |
| 32 GURL file_url = net::FilePathToFileURL(page_file_path); |
| 33 // Load the test file. |
| 34 NavigateToURL(shell(), file_url); |
| 25 | 35 |
| 26 protected: | 36 PostTaskToInProcessRendererAndWait( |
| 27 // testing::Test | 37 base::Bind(&DomOperationsTests::CheckResources, base::Unretained(this), |
| 28 virtual void SetUp() { | 38 page_file_path, expected_resources_set, file_url, |
| 29 TestShellTest::SetUp(); | 39 shell()->web_contents()->GetRoutingID())); |
| 30 } | 40 } |
| 31 | 41 |
| 32 virtual void TearDown() { | 42 void CheckResources(const base::FilePath& page_file_path, |
| 33 TestShellTest::TearDown(); | 43 const std::set<GURL>& expected_resources_set, |
| 44 const GURL& file_url, |
| 45 int render_view_id) { |
| 46 // Get all savable resource links for the page. |
| 47 std::vector<GURL> resources_list; |
| 48 std::vector<GURL> referrer_urls_list; |
| 49 std::vector<WebKit::WebReferrerPolicy> referrer_policies_list; |
| 50 std::vector<GURL> frames_list; |
| 51 webkit_glue::SavableResourcesResult result(&resources_list, |
| 52 &referrer_urls_list, |
| 53 &referrer_policies_list, |
| 54 &frames_list); |
| 55 |
| 56 const char* savable_schemes[] = { |
| 57 "http", |
| 58 "https", |
| 59 "file", |
| 60 NULL |
| 61 }; |
| 62 |
| 63 RenderView* render_view = RenderView::FromRoutingID(render_view_id); |
| 64 |
| 65 ASSERT_TRUE(webkit_glue::GetAllSavableResourceLinksForCurrentPage( |
| 66 render_view->GetWebView(), file_url, &result, savable_schemes)); |
| 67 // Check all links of sub-resource |
| 68 for (std::vector<GURL>::const_iterator cit = resources_list.begin(); |
| 69 cit != resources_list.end(); ++cit) { |
| 70 ASSERT_TRUE(expected_resources_set.find(*cit) != |
| 71 expected_resources_set.end()); |
| 72 } |
| 73 // Check all links of frame. |
| 74 for (std::vector<GURL>::const_iterator cit = frames_list.begin(); |
| 75 cit != frames_list.end(); ++cit) { |
| 76 ASSERT_TRUE(expected_resources_set.find(*cit) != |
| 77 expected_resources_set.end()); |
| 78 } |
| 34 } | 79 } |
| 35 }; | 80 }; |
| 36 | 81 |
| 37 | |
| 38 void DomOperationsTests::GetSavableResourceLinksForPage( | |
| 39 const base::FilePath& page_file_path, | |
| 40 const std::set<GURL>& expected_resources_set) { | |
| 41 // Convert local file path to file URL. | |
| 42 GURL file_url = net::FilePathToFileURL(page_file_path); | |
| 43 // Load the test file. | |
| 44 test_shell_->ResetTestController(); | |
| 45 test_shell_->LoadURL(file_url); | |
| 46 test_shell_->WaitTestFinished(); | |
| 47 // Get all savable resource links for the page. | |
| 48 std::vector<GURL> resources_list; | |
| 49 std::vector<GURL> referrer_urls_list; | |
| 50 std::vector<WebKit::WebReferrerPolicy> referrer_policies_list; | |
| 51 std::vector<GURL> frames_list; | |
| 52 webkit_glue::SavableResourcesResult result(&resources_list, | |
| 53 &referrer_urls_list, | |
| 54 &referrer_policies_list, | |
| 55 &frames_list); | |
| 56 | |
| 57 const char* savable_schemes[] = { | |
| 58 "http", | |
| 59 "https", | |
| 60 "file", | |
| 61 NULL | |
| 62 }; | |
| 63 | |
| 64 ASSERT_TRUE(webkit_glue::GetAllSavableResourceLinksForCurrentPage( | |
| 65 test_shell_->webView(), file_url, &result, savable_schemes)); | |
| 66 // Check all links of sub-resource | |
| 67 for (std::vector<GURL>::const_iterator cit = resources_list.begin(); | |
| 68 cit != resources_list.end(); ++cit) { | |
| 69 ASSERT_TRUE(expected_resources_set.find(*cit) != | |
| 70 expected_resources_set.end()); | |
| 71 } | |
| 72 // Check all links of frame. | |
| 73 for (std::vector<GURL>::const_iterator cit = frames_list.begin(); | |
| 74 cit != frames_list.end(); ++cit) { | |
| 75 ASSERT_TRUE(expected_resources_set.find(*cit) != | |
| 76 expected_resources_set.end()); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 // Test function GetAllSavableResourceLinksForCurrentPage with a web page | 82 // Test function GetAllSavableResourceLinksForCurrentPage with a web page |
| 81 // which has valid savable resource links. | 83 // which has valid savable resource links. |
| 82 TEST_F(DomOperationsTests, GetSavableResourceLinksWithPageHasValidLinks) { | 84 IN_PROC_BROWSER_TEST_F(DomOperationsTests, |
| 85 GetSavableResourceLinksWithPageHasValidLinks) { |
| 83 std::set<GURL> expected_resources_set; | 86 std::set<GURL> expected_resources_set; |
| 84 // Set directory of test data. | |
| 85 base::FilePath page_file_path = data_dir_.AppendASCII("dom_serializer"); | |
| 86 | 87 |
| 87 const char* expected_sub_resource_links[] = { | 88 const char* expected_sub_resource_links[] = { |
| 88 "file:///c:/yt/css/base_all-vfl36460.css", | 89 "file:///c:/yt/css/base_all-vfl36460.css", |
| 89 "file:///c:/yt/js/base_all_with_bidi-vfl36451.js", | 90 "file:///c:/yt/js/base_all_with_bidi-vfl36451.js", |
| 90 "file:///c:/yt/img/pixel-vfl73.gif" | 91 "file:///c:/yt/img/pixel-vfl73.gif" |
| 91 }; | 92 }; |
| 92 const char* expected_frame_links[] = { | 93 const char* expected_frame_links[] = { |
| 93 "youtube_1.htm", | 94 "youtube_1.htm", |
| 94 "youtube_2.htm" | 95 "youtube_2.htm" |
| 95 }; | 96 }; |
| 96 // Add all expected links of sub-resource to expected set. | 97 // Add all expected links of sub-resource to expected set. |
| 97 for (size_t i = 0; i < arraysize(expected_sub_resource_links); ++i) | 98 for (size_t i = 0; i < arraysize(expected_sub_resource_links); ++i) |
| 98 expected_resources_set.insert(GURL(expected_sub_resource_links[i])); | 99 expected_resources_set.insert(GURL(expected_sub_resource_links[i])); |
| 99 // Add all expected links of frame to expected set. | 100 // Add all expected links of frame to expected set. |
| 100 for (size_t i = 0; i < arraysize(expected_frame_links); ++i) { | 101 for (size_t i = 0; i < arraysize(expected_frame_links); ++i) { |
| 101 const base::FilePath expected_frame_url = | 102 const base::FilePath expected_frame_url = |
| 102 page_file_path.AppendASCII(expected_frame_links[i]); | 103 GetTestFilePath("dom_serializer", expected_frame_links[i]); |
| 103 expected_resources_set.insert( | 104 expected_resources_set.insert( |
| 104 net::FilePathToFileURL(expected_frame_url)); | 105 net::FilePathToFileURL(expected_frame_url)); |
| 105 } | 106 } |
| 106 | 107 |
| 107 page_file_path = page_file_path.AppendASCII("youtube_1.htm"); | 108 base::FilePath page_file_path = |
| 109 GetTestFilePath("dom_serializer", "youtube_1.htm"); |
| 108 GetSavableResourceLinksForPage(page_file_path, expected_resources_set); | 110 GetSavableResourceLinksForPage(page_file_path, expected_resources_set); |
| 109 } | 111 } |
| 110 | 112 |
| 111 // Test function GetAllSavableResourceLinksForCurrentPage with a web page | 113 // Test function GetAllSavableResourceLinksForCurrentPage with a web page |
| 112 // which does not have valid savable resource links. | 114 // which does not have valid savable resource links. |
| 113 TEST_F(DomOperationsTests, GetSavableResourceLinksWithPageHasInvalidLinks) { | 115 IN_PROC_BROWSER_TEST_F(DomOperationsTests, |
| 116 GetSavableResourceLinksWithPageHasInvalidLinks) { |
| 114 std::set<GURL> expected_resources_set; | 117 std::set<GURL> expected_resources_set; |
| 115 // Set directory of test data. | |
| 116 base::FilePath page_file_path = data_dir_.AppendASCII("dom_serializer"); | |
| 117 | 118 |
| 118 const char* expected_frame_links[] = { | 119 const char* expected_frame_links[] = { |
| 119 "youtube_2.htm" | 120 "youtube_2.htm" |
| 120 }; | 121 }; |
| 121 // Add all expected links of frame to expected set. | 122 // Add all expected links of frame to expected set. |
| 122 for (size_t i = 0; i < arraysize(expected_frame_links); ++i) { | 123 for (size_t i = 0; i < arraysize(expected_frame_links); ++i) { |
| 123 base::FilePath expected_frame_url = | 124 base::FilePath expected_frame_url = |
| 124 page_file_path.AppendASCII(expected_frame_links[i]); | 125 GetTestFilePath("dom_serializer", expected_frame_links[i]); |
| 125 expected_resources_set.insert( | 126 expected_resources_set.insert( |
| 126 net::FilePathToFileURL(expected_frame_url)); | 127 net::FilePathToFileURL(expected_frame_url)); |
| 127 } | 128 } |
| 128 | 129 |
| 129 page_file_path = page_file_path.AppendASCII("youtube_2.htm"); | 130 base::FilePath page_file_path = |
| 131 GetTestFilePath("dom_serializer", "youtube_2.htm"); |
| 130 GetSavableResourceLinksForPage(page_file_path, expected_resources_set); | 132 GetSavableResourceLinksForPage(page_file_path, expected_resources_set); |
| 131 } | 133 } |
| 132 | 134 |
| 133 } // namespace | 135 } // namespace content |
| OLD | NEW |