[email protected] | c2d98651 | 2012-05-12 00:22:46 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 4 | |
| 5 | #include "base/file_util.h" |
| 6 | #include "base/path_service.h" |
| 7 | #include "base/string_util.h" |
[email protected] | be1ce6a7 | 2010-08-03 14:35:22 | [diff] [blame] | 8 | #include "base/utf_string_conversions.h" |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 9 | #include "net/base/net_util.h" |
| 10 | #include "net/url_request/url_request_context.h" |
[email protected] | c1d9cdc | 2011-01-17 06:50:01 | [diff] [blame] | 11 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 12 | #include "webkit/glue/dom_operations.h" |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 13 | #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" |
| 14 | #include "webkit/tools/test_shell/test_shell_test.h" |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | class DomOperationsTests : public TestShellTest { |
| 19 | public: |
| 20 | // Test function GetAllSavableResourceLinksForCurrentPage with a web page. |
| 21 | // We expect result of GetAllSavableResourceLinksForCurrentPage exactly |
| 22 | // matches expected_resources_set. |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 23 | void GetSavableResourceLinksForPage(const FilePath& page_file_path, |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 24 | const std::set<GURL>& expected_resources_set); |
| 25 | |
| 26 | protected: |
| 27 | // testing::Test |
| 28 | virtual void SetUp() { |
| 29 | TestShellTest::SetUp(); |
| 30 | } |
| 31 | |
| 32 | virtual void TearDown() { |
| 33 | TestShellTest::TearDown(); |
| 34 | } |
| 35 | }; |
| 36 | |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 37 | |
| 38 | void DomOperationsTests::GetSavableResourceLinksForPage( |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 39 | const FilePath& page_file_path, |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 40 | const std::set<GURL>& expected_resources_set) { |
| 41 | // Convert local file path to file URL. |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 42 | GURL file_url = net::FilePathToFileURL(page_file_path); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 43 | // Load the test file. |
| 44 | test_shell_->ResetTestController(); |
[email protected] | 3d968937 | 2009-09-10 04:29:17 | [diff] [blame] | 45 | test_shell_->LoadURL(file_url); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 46 | test_shell_->WaitTestFinished(); |
| 47 | // Get all savable resource links for the page. |
| 48 | std::vector<GURL> resources_list; |
[email protected] | c2d98651 | 2012-05-12 00:22:46 | [diff] [blame] | 49 | std::vector<GURL> referrer_urls_list; |
| 50 | std::vector<WebKit::WebReferrerPolicy> referrer_policies_list; |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 51 | std::vector<GURL> frames_list; |
| 52 | webkit_glue::SavableResourcesResult result(&resources_list, |
[email protected] | c2d98651 | 2012-05-12 00:22:46 | [diff] [blame] | 53 | &referrer_urls_list, |
| 54 | &referrer_policies_list, |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 55 | &frames_list); |
| 56 | |
[email protected] | dbeb395 | 2009-10-13 18:01:18 | [diff] [blame] | 57 | const char* savable_schemes[] = { |
| 58 | "http", |
| 59 | "https", |
| 60 | "file", |
| 61 | NULL |
| 62 | }; |
| 63 | |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 64 | ASSERT_TRUE(webkit_glue::GetAllSavableResourceLinksForCurrentPage( |
[email protected] | dbeb395 | 2009-10-13 18:01:18 | [diff] [blame] | 65 | test_shell_->webView(), file_url, &result, savable_schemes)); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 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 |
| 81 | // which has valid savable resource links. |
| 82 | TEST_F(DomOperationsTests, GetSavableResourceLinksWithPageHasValidLinks) { |
| 83 | std::set<GURL> expected_resources_set; |
| 84 | // Set directory of test data. |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 85 | FilePath page_file_path = data_dir_.AppendASCII("dom_serializer"); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 86 | |
[email protected] | a4814671 | 2008-11-06 17:51:20 | [diff] [blame] | 87 | const char* expected_sub_resource_links[] = { |
| 88 | "file:///c:/yt/css/base_all-vfl36460.css", |
| 89 | "file:///c:/yt/js/base_all_with_bidi-vfl36451.js", |
| 90 | "file:///c:/yt/img/pixel-vfl73.gif" |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 91 | }; |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 92 | const char* expected_frame_links[] = { |
| 93 | "youtube_1.htm", |
| 94 | "youtube_2.htm" |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 95 | }; |
| 96 | // Add all expected links of sub-resource to expected set. |
[email protected] | a4814671 | 2008-11-06 17:51:20 | [diff] [blame] | 97 | for (size_t i = 0; i < arraysize(expected_sub_resource_links); ++i) |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 98 | expected_resources_set.insert(GURL(expected_sub_resource_links[i])); |
| 99 | // Add all expected links of frame to expected set. |
[email protected] | a4814671 | 2008-11-06 17:51:20 | [diff] [blame] | 100 | for (size_t i = 0; i < arraysize(expected_frame_links); ++i) { |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 101 | const FilePath expected_frame_url = |
| 102 | page_file_path.AppendASCII(expected_frame_links[i]); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 103 | expected_resources_set.insert( |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 104 | net::FilePathToFileURL(expected_frame_url)); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 105 | } |
| 106 | |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 107 | page_file_path = page_file_path.AppendASCII("youtube_1.htm"); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 108 | GetSavableResourceLinksForPage(page_file_path, expected_resources_set); |
| 109 | } |
| 110 | |
| 111 | // Test function GetAllSavableResourceLinksForCurrentPage with a web page |
| 112 | // which does not have valid savable resource links. |
| 113 | TEST_F(DomOperationsTests, GetSavableResourceLinksWithPageHasInvalidLinks) { |
| 114 | std::set<GURL> expected_resources_set; |
| 115 | // Set directory of test data. |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 116 | FilePath page_file_path = data_dir_.AppendASCII("dom_serializer"); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 117 | |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 118 | const char* expected_frame_links[] = { |
| 119 | "youtube_2.htm" |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 120 | }; |
| 121 | // Add all expected links of frame to expected set. |
[email protected] | a4814671 | 2008-11-06 17:51:20 | [diff] [blame] | 122 | for (size_t i = 0; i < arraysize(expected_frame_links); ++i) { |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 123 | FilePath expected_frame_url = |
| 124 | page_file_path.AppendASCII(expected_frame_links[i]); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 125 | expected_resources_set.insert( |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 126 | net::FilePathToFileURL(expected_frame_url)); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 129 | page_file_path = page_file_path.AppendASCII("youtube_2.htm"); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 130 | GetSavableResourceLinksForPage(page_file_path, expected_resources_set); |
| 131 | } |
| 132 | |
[email protected] | a03c3ab | 2008-12-10 20:04:09 | [diff] [blame] | 133 | } // namespace |