license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
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" |
| 8 | #include "net/base/net_util.h" |
| 9 | #include "net/url_request/url_request_context.h" |
| 10 | #include "webkit/glue/dom_operations.h" |
| 11 | #include "webkit/glue/webview.h" |
| 12 | #include "webkit/glue/webframe.h" |
| 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(); |
| 45 | std::wstring file_wurl = ASCIIToWide(file_url.spec()); |
| 46 | test_shell_->LoadURL(file_wurl.c_str()); |
| 47 | test_shell_->WaitTestFinished(); |
| 48 | // Get all savable resource links for the page. |
| 49 | std::vector<GURL> resources_list; |
| 50 | std::vector<GURL> referrers_list; |
| 51 | std::vector<GURL> frames_list; |
| 52 | webkit_glue::SavableResourcesResult result(&resources_list, |
| 53 | &referrers_list, |
| 54 | &frames_list); |
| 55 | |
[email protected] | a4814671 | 2008-11-06 17:51:20 | [diff] [blame] | 56 | GURL main_page_gurl(WideToUTF8(file_wurl)); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 57 | ASSERT_TRUE(webkit_glue::GetAllSavableResourceLinksForCurrentPage( |
| 58 | test_shell_->webView(), main_page_gurl, &result)); |
| 59 | // Check all links of sub-resource |
| 60 | for (std::vector<GURL>::const_iterator cit = resources_list.begin(); |
| 61 | cit != resources_list.end(); ++cit) { |
| 62 | ASSERT_TRUE(expected_resources_set.find(*cit) != |
| 63 | expected_resources_set.end()); |
| 64 | } |
| 65 | // Check all links of frame. |
| 66 | for (std::vector<GURL>::const_iterator cit = frames_list.begin(); |
| 67 | cit != frames_list.end(); ++cit) { |
| 68 | ASSERT_TRUE(expected_resources_set.find(*cit) != |
| 69 | expected_resources_set.end()); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Test function GetAllSavableResourceLinksForCurrentPage with a web page |
| 74 | // which has valid savable resource links. |
| 75 | TEST_F(DomOperationsTests, GetSavableResourceLinksWithPageHasValidLinks) { |
| 76 | std::set<GURL> expected_resources_set; |
| 77 | // Set directory of test data. |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame^] | 78 | FilePath page_file_path = data_dir_.AppendASCII("dom_serializer"); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 79 | |
[email protected] | a4814671 | 2008-11-06 17:51:20 | [diff] [blame] | 80 | const char* expected_sub_resource_links[] = { |
| 81 | "file:///c:/yt/css/base_all-vfl36460.css", |
| 82 | "file:///c:/yt/js/base_all_with_bidi-vfl36451.js", |
| 83 | "file:///c:/yt/img/pixel-vfl73.gif" |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 84 | }; |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame^] | 85 | const char* expected_frame_links[] = { |
| 86 | "youtube_1.htm", |
| 87 | "youtube_2.htm" |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 88 | }; |
| 89 | // Add all expected links of sub-resource to expected set. |
[email protected] | a4814671 | 2008-11-06 17:51:20 | [diff] [blame] | 90 | for (size_t i = 0; i < arraysize(expected_sub_resource_links); ++i) |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 91 | expected_resources_set.insert(GURL(expected_sub_resource_links[i])); |
| 92 | // Add all expected links of frame to expected set. |
[email protected] | a4814671 | 2008-11-06 17:51:20 | [diff] [blame] | 93 | for (size_t i = 0; i < arraysize(expected_frame_links); ++i) { |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame^] | 94 | const FilePath expected_frame_url = |
| 95 | page_file_path.AppendASCII(expected_frame_links[i]); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 96 | expected_resources_set.insert( |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 97 | net::FilePathToFileURL(expected_frame_url)); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame^] | 100 | page_file_path = page_file_path.AppendASCII("youtube_1.htm"); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 101 | GetSavableResourceLinksForPage(page_file_path, expected_resources_set); |
| 102 | } |
| 103 | |
| 104 | // Test function GetAllSavableResourceLinksForCurrentPage with a web page |
| 105 | // which does not have valid savable resource links. |
| 106 | TEST_F(DomOperationsTests, GetSavableResourceLinksWithPageHasInvalidLinks) { |
| 107 | std::set<GURL> expected_resources_set; |
| 108 | // Set directory of test data. |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame^] | 109 | FilePath page_file_path = data_dir_.AppendASCII("dom_serializer"); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 110 | |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame^] | 111 | const char* expected_frame_links[] = { |
| 112 | "youtube_2.htm" |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 113 | }; |
| 114 | // Add all expected links of frame to expected set. |
[email protected] | a4814671 | 2008-11-06 17:51:20 | [diff] [blame] | 115 | for (size_t i = 0; i < arraysize(expected_frame_links); ++i) { |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame^] | 116 | FilePath expected_frame_url = |
| 117 | page_file_path.AppendASCII(expected_frame_links[i]); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 118 | expected_resources_set.insert( |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 119 | net::FilePathToFileURL(expected_frame_url)); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame^] | 122 | page_file_path = page_file_path.AppendASCII("youtube_2.htm"); |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 123 | GetSavableResourceLinksForPage(page_file_path, expected_resources_set); |
| 124 | } |
| 125 | |
| 126 | // Tests ParseIconSizes with various input. |
| 127 | TEST_F(DomOperationsTests, ParseIconSizes) { |
| 128 | struct TestData { |
| 129 | const std::wstring input; |
| 130 | const bool expected_result; |
| 131 | const bool is_any; |
[email protected] | a4814671 | 2008-11-06 17:51:20 | [diff] [blame] | 132 | const size_t expected_size_count; |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 133 | const int width1; |
| 134 | const int height1; |
| 135 | const int width2; |
| 136 | const int height2; |
| 137 | } data[] = { |
| 138 | // Bogus input cases. |
| 139 | { L"10", false, false, 0, 0, 0, 0, 0 }, |
| 140 | { L"10 10", false, false, 0, 0, 0, 0, 0 }, |
| 141 | { L"010", false, false, 0, 0, 0, 0, 0 }, |
| 142 | { L" 010 ", false, false, 0, 0, 0, 0, 0 }, |
| 143 | { L" 10x ", false, false, 0, 0, 0, 0, 0 }, |
| 144 | { L" x10 ", false, false, 0, 0, 0, 0, 0 }, |
| 145 | { L"any 10x10", false, false, 0, 0, 0, 0, 0 }, |
| 146 | { L"", false, false, 0, 0, 0, 0, 0 }, |
| 147 | { L"10ax11", false, false, 0, 0, 0, 0, 0 }, |
| 148 | |
| 149 | // Any. |
| 150 | { L"any", true, true, 0, 0, 0, 0, 0 }, |
| 151 | { L" any", true, true, 0, 0, 0, 0, 0 }, |
| 152 | { L" any ", true, true, 0, 0, 0, 0, 0 }, |
| 153 | |
| 154 | // Sizes. |
| 155 | { L"10x11", true, false, 1, 10, 11, 0, 0 }, |
| 156 | { L" 10x11 ", true, false, 1, 10, 11, 0, 0 }, |
| 157 | { L" 10x11 1x2", true, false, 2, 10, 11, 1, 2 }, |
| 158 | }; |
[email protected] | a4814671 | 2008-11-06 17:51:20 | [diff] [blame] | 159 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
initial.commit | f5b16fe | 2008-07-27 00:20:51 | [diff] [blame] | 160 | bool is_any; |
| 161 | std::vector<gfx::Size> sizes; |
| 162 | const bool result = |
| 163 | webkit_glue::ParseIconSizes(data[i].input, &sizes, &is_any); |
| 164 | ASSERT_EQ(result, data[i].expected_result); |
| 165 | if (result) { |
| 166 | ASSERT_EQ(data[i].is_any, is_any); |
| 167 | ASSERT_EQ(data[i].expected_size_count, sizes.size()); |
| 168 | if (sizes.size() > 0) { |
| 169 | ASSERT_EQ(data[i].width1, sizes[0].width()); |
| 170 | ASSERT_EQ(data[i].height1, sizes[0].height()); |
| 171 | } |
| 172 | if (sizes.size() > 1) { |
| 173 | ASSERT_EQ(data[i].width2, sizes[1].width()); |
| 174 | ASSERT_EQ(data[i].height2, sizes[1].height()); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 179 | |
[email protected] | a03c3ab | 2008-12-10 20:04:09 | [diff] [blame] | 180 | } // namespace |