OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2009 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 #include <string> |
| 5 |
| 6 #include "base/file_util.h" |
| 7 #include "base/path_service.h" |
| 8 #include "base/string_util.h" |
| 9 #include "chrome/browser/net/url_request_mock_http_job.h" |
| 10 #include "chrome/browser/download/save_package.h" |
| 11 #include "chrome/common/chrome_paths.h" |
| 12 #include "chrome/test/automation/automation_messages.h" |
| 13 #include "chrome/test/automation/automation_proxy.h" |
| 14 #include "chrome/test/automation/browser_proxy.h" |
| 15 #include "chrome/test/automation/tab_proxy.h" |
| 16 #include "chrome/test/ui/ui_test.h" |
| 17 #include "net/url_request/url_request_unittest.h" |
| 18 #include "chrome/common/pref_names.h" |
| 19 |
| 20 const wchar_t* const kTestDir = L"encoding_tests"; |
| 21 |
| 22 class BrowserEncodingTest : public UITest { |
| 23 protected: |
| 24 BrowserEncodingTest() : UITest() {} |
| 25 |
| 26 // Make sure the content of the page are as expected |
| 27 // after override or auto-detect |
| 28 void CheckFile(const FilePath& generated_file, |
| 29 const FilePath& expected_result_file, |
| 30 bool check_equal) { |
| 31 FilePath expected_result_filepath = UITest::GetTestFilePath( |
| 32 kTestDir, expected_result_file.ToWStringHack()); |
| 33 |
| 34 ASSERT_TRUE(file_util::PathExists(expected_result_filepath)); |
| 35 WaitForGeneratedFileAndCheck(generated_file, |
| 36 expected_result_filepath, |
| 37 true, // We do care whether they are equal. |
| 38 check_equal, |
| 39 true); // Delete the generated file when done. |
| 40 } |
| 41 |
| 42 virtual void SetUp() { |
| 43 UITest::SetUp(); |
| 44 EXPECT_TRUE(file_util::CreateNewTempDirectory(L"", &save_dir_)); |
| 45 save_dir_ += FilePath::kSeparators[0]; |
| 46 } |
| 47 |
| 48 std::wstring save_dir_; |
| 49 }; |
| 50 |
| 51 // TODO(jnd): 1. Some encodings are missing here. It'll be added later. See |
| 52 // http://crbug.com/13306. |
| 53 // 2. Add more files with multiple encoding name variants for each canonical |
| 54 // encoding name). Webkit layout tests cover some, but testing in the UI test is |
| 55 // also necessary. |
| 56 TEST_F(BrowserEncodingTest, TestEncodingAliasMapping) { |
| 57 struct EncodingTestData { |
| 58 const wchar_t* file_name; |
| 59 const wchar_t* encoding_name; |
| 60 }; |
| 61 |
| 62 const EncodingTestData kEncodingTestDatas[] = { |
| 63 { L"Big5.html", L"Big5" }, |
| 64 { L"EUC-JP.html", L"EUC-JP" }, |
| 65 { L"gb18030.html", L"gb18030" }, |
| 66 { L"iso-8859-1.html", L"ISO-8859-1" }, |
| 67 { L"ISO-8859-2.html", L"ISO-8859-2" }, |
| 68 { L"ISO-8859-4.html", L"ISO-8859-4" }, |
| 69 { L"ISO-8859-5.html", L"ISO-8859-5" }, |
| 70 { L"ISO-8859-6.html", L"ISO-8859-6" }, |
| 71 { L"ISO-8859-7.html", L"ISO-8859-7" }, |
| 72 { L"ISO-8859-8.html", L"ISO-8859-8" }, |
| 73 { L"ISO-8859-13.html", L"ISO-8859-13" }, |
| 74 { L"ISO-8859-15.html", L"ISO-8859-15" }, |
| 75 { L"KOI8-R.html", L"KOI8-R" }, |
| 76 { L"KOI8-U.html", L"KOI8-U" }, |
| 77 { L"macintosh.html", L"macintosh" }, |
| 78 { L"Shift-JIS.html", L"Shift_JIS" }, |
| 79 { L"UTF-8.html", L"UTF-8" }, |
| 80 { L"UTF-16LE.html", L"UTF-16LE" }, |
| 81 { L"windows-874.html", L"windows-874" }, |
| 82 { L"windows-949.html", L"windows-949" }, |
| 83 { L"windows-1250.html", L"windows-1250" }, |
| 84 { L"windows-1251.html", L"windows-1251" }, |
| 85 { L"windows-1252.html", L"windows-1252" }, |
| 86 { L"windows-1253.html", L"windows-1253" }, |
| 87 { L"windows-1254.html", L"windows-1254" }, |
| 88 { L"windows-1255.html", L"windows-1255" }, |
| 89 { L"windows-1256.html", L"windows-1256" }, |
| 90 { L"windows-1257.html", L"windows-1257" }, |
| 91 { L"windows-1258.html", L"windows-1258" } |
| 92 }; |
| 93 const wchar_t* const kAliasTestDir = L"alias_mapping"; |
| 94 |
| 95 FilePath test_dir_path = FilePath::FromWStringHack(kTestDir); |
| 96 test_dir_path = test_dir_path.Append(kAliasTestDir); |
| 97 for (int i = 0; i < arraysize(kEncodingTestDatas); ++i) { |
| 98 FilePath test_file_path(test_dir_path); |
| 99 test_file_path = test_file_path.Append(kEncodingTestDatas[i].file_name); |
| 100 GURL url = |
| 101 URLRequestMockHTTPJob::GetMockUrl(test_file_path.ToWStringHack()); |
| 102 |
| 103 scoped_refptr<TabProxy> tab_proxy(GetActiveTab()); |
| 104 ASSERT_TRUE(tab_proxy.get()); |
| 105 ASSERT_TRUE(tab_proxy->NavigateToURL(url)); |
| 106 WaitUntilTabCount(1); |
| 107 |
| 108 std::wstring encoding; |
| 109 EXPECT_TRUE(tab_proxy->GetPageCurrentEncoding(&encoding)); |
| 110 EXPECT_EQ(encoding, kEncodingTestDatas[i].encoding_name); |
| 111 } |
| 112 } |
| 113 |
| 114 TEST_F(BrowserEncodingTest, TestOverrideEncoding) { |
| 115 const wchar_t* const kTestFileName = |
| 116 L"gb18030_with_iso88591_meta.html"; |
| 117 const wchar_t* const kExpectedFileName = |
| 118 L"expected_gb18030_saved_from_iso88591_meta.html"; |
| 119 const wchar_t* const kOverrideTestDir = L"user_override"; |
| 120 |
| 121 FilePath test_dir_path = FilePath::FromWStringHack(kTestDir); |
| 122 test_dir_path = test_dir_path.Append(kOverrideTestDir); |
| 123 test_dir_path = test_dir_path.Append(kTestFileName); |
| 124 GURL url = URLRequestMockHTTPJob::GetMockUrl(test_dir_path.ToWStringHack()); |
| 125 scoped_refptr<TabProxy> tab_proxy(GetActiveTab()); |
| 126 ASSERT_TRUE(tab_proxy.get()); |
| 127 ASSERT_TRUE(tab_proxy->NavigateToURL(url)); |
| 128 WaitUntilTabCount(1); |
| 129 |
| 130 // Get the encoding declared in the page. |
| 131 std::wstring encoding; |
| 132 EXPECT_TRUE(tab_proxy->GetPageCurrentEncoding(&encoding)); |
| 133 EXPECT_EQ(encoding, L"ISO-8859-1"); |
| 134 |
| 135 // Override the encoding to "gb18030". |
| 136 int64 last_nav_time = 0; |
| 137 EXPECT_TRUE(tab_proxy->GetLastNavigationTime(&last_nav_time)); |
| 138 EXPECT_TRUE(tab_proxy->OverrideEncoding(L"gb18030")); |
| 139 EXPECT_TRUE(tab_proxy->WaitForNavigation(last_nav_time)); |
| 140 |
| 141 // Re-get the encoding of page. It should be gb18030. |
| 142 EXPECT_TRUE(tab_proxy->GetPageCurrentEncoding(&encoding)); |
| 143 EXPECT_EQ(encoding, L"gb18030"); |
| 144 |
| 145 // Dump the page, the content of dump page should be identical to the |
| 146 // expected result file. |
| 147 std::wstring full_file_name = save_dir_ + kTestFileName; |
| 148 // We save the page as way of complete HTML file, which requires a directory |
| 149 // name to save sub resources in it. Although this test file does not have |
| 150 // sub resources, but the directory name is still required. |
| 151 std::wstring dir = save_dir_ + L"sub_resource_files"; |
| 152 EXPECT_TRUE(tab_proxy->SavePage(full_file_name, dir, |
| 153 SavePackage::SAVE_AS_COMPLETE_HTML)); |
| 154 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); |
| 155 ASSERT_TRUE(browser.get()); |
| 156 EXPECT_TRUE(WaitForDownloadShelfVisible(browser.get())); |
| 157 FilePath expected_file_name = |
| 158 FilePath::FromWStringHack(kOverrideTestDir); |
| 159 expected_file_name = expected_file_name.Append(kExpectedFileName); |
| 160 CheckFile(FilePath::FromWStringHack(full_file_name), |
| 161 expected_file_name, true); |
| 162 } |
| 163 |
| 164 // The following encodings are excluded from the auto-detection test because |
| 165 // it's a known issue that the current encoding detector does not detect them: |
| 166 // ISO-8859-4 |
| 167 // ISO-8859-13 |
| 168 // KOI8-U |
| 169 // macintosh |
| 170 // windows-874 |
| 171 // windows-1252 |
| 172 // windows-1253 |
| 173 // windows-1257 |
| 174 // windows-1258 |
| 175 |
| 176 // For Hebrew, the expected encoding value is ISO-8859-8-I. See |
| 177 // http://crbug.com/2927 for more details. |
| 178 TEST_F(BrowserEncodingTest, TestEncodingAutoDetect) { |
| 179 struct EncodingAutoDetectTestData { |
| 180 const wchar_t* test_file_name; // File name of test data. |
| 181 const wchar_t* expected_result; // File name of expected results. |
| 182 const wchar_t* expected_encoding; // expected encoding. |
| 183 }; |
| 184 const EncodingAutoDetectTestData kTestDatas[] = { |
| 185 { L"Big5_with_no_encoding_specified.html", |
| 186 L"expected_Big5_saved_from_no_encoding_specified.html", |
| 187 L"Big5" }, |
| 188 { L"gb18030_with_no_encoding_specified.html", |
| 189 L"expected_gb18030_saved_from_no_encoding_specified.html", |
| 190 L"gb18030" }, |
| 191 { L"iso-8859-1_with_no_encoding_specified.html", |
| 192 L"expected_iso-8859-1_saved_from_no_encoding_specified.html", |
| 193 L"ISO-8859-1" }, |
| 194 { L"ISO-8859-5_with_no_encoding_specified.html", |
| 195 L"expected_ISO-8859-5_saved_from_no_encoding_specified.html", |
| 196 L"ISO-8859-5" }, |
| 197 { L"ISO-8859-6_with_no_encoding_specified.html", |
| 198 L"expected_ISO-8859-6_saved_from_no_encoding_specified.html", |
| 199 L"ISO-8859-6" }, |
| 200 { L"ISO-8859-7_with_no_encoding_specified.html", |
| 201 L"expected_ISO-8859-7_saved_from_no_encoding_specified.html", |
| 202 L"ISO-8859-7" }, |
| 203 { L"ISO-8859-8_with_no_encoding_specified.html", |
| 204 L"expected_ISO-8859-8_saved_from_no_encoding_specified.html", |
| 205 L"ISO-8859-8-I" }, |
| 206 { L"KOI8-R_with_no_encoding_specified.html", |
| 207 L"expected_KOI8-R_saved_from_no_encoding_specified.html", |
| 208 L"KOI8-R" }, |
| 209 { L"Shift-JIS_with_no_encoding_specified.html", |
| 210 L"expected_Shift-JIS_saved_from_no_encoding_specified.html", |
| 211 L"Shift_JIS" }, |
| 212 { L"UTF-8_with_no_encoding_specified.html", |
| 213 L"expected_UTF-8_saved_from_no_encoding_specified.html", |
| 214 L"UTF-8" }, |
| 215 { L"windows-949_with_no_encoding_specified.html", |
| 216 L"expected_windows-949_saved_from_no_encoding_specified.html", |
| 217 L"windows-949" }, |
| 218 { L"windows-1251_with_no_encoding_specified.html", |
| 219 L"expected_windows-1251_saved_from_no_encoding_specified.html", |
| 220 L"windows-1251" }, |
| 221 { L"windows-1254_with_no_encoding_specified.html", |
| 222 L"expected_windows-1254_saved_from_no_encoding_specified.html", |
| 223 L"windows-1254" }, |
| 224 { L"windows-1255_with_no_encoding_specified.html", |
| 225 L"expected_windows-1255_saved_from_no_encoding_specified.html", |
| 226 L"windows-1255" }, |
| 227 { L"windows-1256_with_no_encoding_specified.html", |
| 228 L"expected_windows-1256_saved_from_no_encoding_specified.html", |
| 229 L"windows-1256" } |
| 230 }; |
| 231 const wchar_t* const kAutoDetectDir = L"auto_detect"; |
| 232 // Directory of the files of expected results. |
| 233 const wchar_t* const kExpectedResultDir = L"expected_results"; |
| 234 |
| 235 // Full path of saved file. full_file_name = save_dir_ + file_name[i]; |
| 236 std::wstring full_saved_file_name; |
| 237 // Sub resource directory of saved file. |
| 238 std::wstring tmp_save_dir(save_dir_); |
| 239 tmp_save_dir += L"sub_resource_files"; |
| 240 |
| 241 FilePath test_dir_path = FilePath::FromWStringHack(kTestDir); |
| 242 test_dir_path = test_dir_path.Append(kAutoDetectDir); |
| 243 |
| 244 for (int i = 0;i < arraysize(kTestDatas);i++) { |
| 245 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); |
| 246 ASSERT_TRUE(browser.get()); |
| 247 |
| 248 // Set the default charset to one of encodings not supported by the current |
| 249 // auto-detector (Please refer to the above comments) to make sure we |
| 250 // incorrectly decode the page. Now we use ISO-8859-4. |
| 251 browser->SetStringPreference(prefs::kDefaultCharset, L"ISO-8859-4"); |
| 252 FilePath test_file_path(test_dir_path); |
| 253 test_file_path = test_file_path.Append(kTestDatas[i].test_file_name); |
| 254 GURL url = |
| 255 URLRequestMockHTTPJob::GetMockUrl(test_file_path.ToWStringHack()); |
| 256 scoped_refptr<TabProxy> tab(GetActiveTab()); |
| 257 ASSERT_TRUE(tab.get()); |
| 258 ASSERT_TRUE(tab->NavigateToURL(url)); |
| 259 WaitUntilTabCount(1); |
| 260 |
| 261 // Disable auto detect if it is on. |
| 262 EXPECT_TRUE( |
| 263 browser->SetBooleanPreference(prefs::kWebKitUsesUniversalDetector, |
| 264 false)); |
| 265 EXPECT_TRUE(tab->Reload()); |
| 266 |
| 267 // Get the encoding used for the page, it must be the default charset we |
| 268 // just set. |
| 269 std::wstring encoding; |
| 270 EXPECT_TRUE(tab->GetPageCurrentEncoding(&encoding)); |
| 271 EXPECT_EQ(encoding, L"ISO-8859-4"); |
| 272 |
| 273 // Enable the encoding auto detection. |
| 274 EXPECT_TRUE(browser->SetBooleanPreference( |
| 275 prefs::kWebKitUsesUniversalDetector, true)); |
| 276 EXPECT_TRUE(tab->Reload()); |
| 277 |
| 278 // Re-get the encoding of page. It should return the real encoding now. |
| 279 bool encoding_auto_detect = false; |
| 280 EXPECT_TRUE( |
| 281 browser->GetBooleanPreference(prefs::kWebKitUsesUniversalDetector, |
| 282 &encoding_auto_detect)); |
| 283 EXPECT_TRUE(encoding_auto_detect); |
| 284 EXPECT_TRUE(tab->GetPageCurrentEncoding(&encoding)); |
| 285 EXPECT_EQ(encoding, kTestDatas[i].expected_encoding); |
| 286 |
| 287 // Dump the page, the content of dump page should be equal with our expect |
| 288 // result file. |
| 289 full_saved_file_name = save_dir_ + kTestDatas[i].test_file_name; |
| 290 // Full path of expect result file. |
| 291 FilePath expected_result_file_name = |
| 292 FilePath::FromWStringHack(kAutoDetectDir); |
| 293 expected_result_file_name = |
| 294 expected_result_file_name.Append(kExpectedResultDir); |
| 295 expected_result_file_name = |
| 296 expected_result_file_name.Append(kTestDatas[i].expected_result); |
| 297 EXPECT_TRUE(tab->SavePage(full_saved_file_name, tmp_save_dir, |
| 298 SavePackage::SAVE_AS_COMPLETE_HTML)); |
| 299 EXPECT_TRUE(WaitForDownloadShelfVisible(browser.get())); |
| 300 CheckFile(FilePath::FromWStringHack(full_saved_file_name), |
| 301 expected_result_file_name, |
| 302 true); |
| 303 } |
| 304 } |
| 305 |
OLD | NEW |