Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ppapi/tests/test_char_set.h" | 5 #include "ppapi/tests/test_char_set.h" |
| 6 | 6 |
| 7 #include "ppapi/c/dev/ppb_char_set_dev.h" | 7 #include "ppapi/c/dev/ppb_char_set_dev.h" |
| 8 #include "ppapi/c/trusted/ppb_char_set_trusted.h" | |
| 8 #include "ppapi/cpp/dev/memory_dev.h" | 9 #include "ppapi/cpp/dev/memory_dev.h" |
| 9 #include "ppapi/cpp/module.h" | 10 #include "ppapi/cpp/module.h" |
| 10 #include "ppapi/tests/testing_instance.h" | 11 #include "ppapi/tests/testing_instance.h" |
| 11 | 12 |
| 12 REGISTER_TEST_CASE(CharSet); | 13 REGISTER_TEST_CASE(CharSet); |
| 13 | 14 |
| 14 TestCharSet::TestCharSet(TestingInstance* instance) | 15 TestCharSet::TestCharSet(TestingInstance* instance) |
| 15 : TestCase(instance), | 16 : TestCase(instance), |
| 16 char_set_interface_(NULL) { | 17 char_set_interface_(NULL) { |
| 17 } | 18 } |
| 18 | 19 |
| 19 bool TestCharSet::Init() { | 20 bool TestCharSet::Init() { |
| 20 char_set_interface_ = static_cast<const PPB_CharSet_Dev*>( | 21 char_set_interface_ = static_cast<const PPB_CharSet_Dev*>( |
| 21 pp::Module::Get()->GetBrowserInterface(PPB_CHAR_SET_DEV_INTERFACE)); | 22 pp::Module::Get()->GetBrowserInterface(PPB_CHAR_SET_DEV_INTERFACE)); |
| 22 return !!char_set_interface_; | 23 char_set_trusted_interface_ = static_cast<const PPB_CharSet_Trusted*>( |
| 24 pp::Module::Get()->GetBrowserInterface(PPB_CHARSET_TRUSTED_INTERFACE)); | |
| 25 return char_set_interface_ && char_set_trusted_interface_; | |
| 23 } | 26 } |
| 24 | 27 |
| 25 void TestCharSet::RunTests(const std::string& filter) { | 28 void TestCharSet::RunTests(const std::string& filter) { |
| 29 RUN_TEST(UTF16ToCharSetDeprecated, filter); | |
| 26 RUN_TEST(UTF16ToCharSet, filter); | 30 RUN_TEST(UTF16ToCharSet, filter); |
| 31 RUN_TEST(CharSetToUTF16Deprecated, filter); | |
| 27 RUN_TEST(CharSetToUTF16, filter); | 32 RUN_TEST(CharSetToUTF16, filter); |
| 28 RUN_TEST(GetDefaultCharSet, filter); | 33 RUN_TEST(GetDefaultCharSet, filter); |
| 29 } | 34 } |
| 30 | 35 |
| 31 std::string TestCharSet::TestUTF16ToCharSet() { | 36 // TODO(brettw) remove this when the old interface is removed. |
| 37 std::string TestCharSet::TestUTF16ToCharSetDeprecated() { | |
| 32 // Empty string. | 38 // Empty string. |
| 33 std::vector<uint16_t> utf16; | 39 std::vector<uint16_t> utf16; |
| 34 utf16.push_back(0); | 40 utf16.push_back(0); |
| 35 uint32_t utf8result_len = 0; | 41 uint32_t utf8result_len = 0; |
| 36 pp::Memory_Dev memory; | 42 pp::Memory_Dev memory; |
| 37 char* utf8result = char_set_interface_->UTF16ToCharSet( | 43 char* utf8result = char_set_interface_->UTF16ToCharSet( |
| 38 instance_->pp_instance(), &utf16[0], 0, "latin1", | 44 instance_->pp_instance(), &utf16[0], 0, "latin1", |
| 39 PP_CHARSET_CONVERSIONERROR_SUBSTITUTE, &utf8result_len); | 45 PP_CHARSET_CONVERSIONERROR_SUBSTITUTE, &utf8result_len); |
| 40 ASSERT_TRUE(utf8result); | 46 ASSERT_TRUE(utf8result); |
| 41 ASSERT_TRUE(utf8result[0] == 0); | 47 ASSERT_TRUE(utf8result[0] == 0); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 // Invalid encoding name. | 101 // Invalid encoding name. |
| 96 utf8result = char_set_interface_->UTF16ToCharSet( | 102 utf8result = char_set_interface_->UTF16ToCharSet( |
| 97 instance_->pp_instance(), &utf16[0], static_cast<uint32_t>(utf16.size()), | 103 instance_->pp_instance(), &utf16[0], static_cast<uint32_t>(utf16.size()), |
| 98 "poopiepants", PP_CHARSET_CONVERSIONERROR_SUBSTITUTE, &utf8result_len); | 104 "poopiepants", PP_CHARSET_CONVERSIONERROR_SUBSTITUTE, &utf8result_len); |
| 99 ASSERT_TRUE(!utf8result); | 105 ASSERT_TRUE(!utf8result); |
| 100 ASSERT_TRUE(utf8result_len == 0); | 106 ASSERT_TRUE(utf8result_len == 0); |
| 101 | 107 |
| 102 PASS(); | 108 PASS(); |
| 103 } | 109 } |
| 104 | 110 |
| 105 std::string TestCharSet::TestCharSetToUTF16() { | 111 std::string TestCharSet::TestUTF16ToCharSet() { |
| 112 // Empty string. | |
| 113 std::vector<uint16_t> utf16; | |
| 114 utf16.push_back(0); | |
| 115 std::string output_buffer; | |
| 116 uint32_t utf8result_len = static_cast<uint32_t>(output_buffer.size()); | |
| 117 PP_Bool result = char_set_trusted_interface_->UTF16ToCharSet( | |
| 118 &utf16[0], 0, "latin1", PP_CHARSET_TRUSTED_CONVERSIONERROR_SUBSTITUTE, | |
| 119 &output_buffer[0], &utf8result_len); | |
| 120 ASSERT_TRUE(result == PP_TRUE); | |
| 121 ASSERT_TRUE(utf8result_len == 0); | |
| 122 | |
| 123 // No output buffer size returns length of string. | |
|
bbudge
2012/02/08 21:56:58
Null output buffer returns length of...
| |
| 124 utf16 = UTF8ToUTF16("hello"); | |
| 125 utf8result_len = 0; | |
| 126 result = char_set_trusted_interface_->UTF16ToCharSet( | |
| 127 &utf16[0], static_cast<uint32_t>(utf16.size()), "latin1", | |
| 128 PP_CHARSET_TRUSTED_CONVERSIONERROR_SUBSTITUTE, NULL, &utf8result_len); | |
| 129 ASSERT_TRUE(result == PP_TRUE); | |
| 130 ASSERT_TRUE(utf8result_len == 5); | |
| 131 | |
| 132 // Giving too small of a buffer just fills in that many items and gives us | |
| 133 // the desired size. | |
| 134 output_buffer.resize(100); | |
| 135 utf8result_len = 2; | |
| 136 output_buffer[utf8result_len] = '$'; // Barrier character. | |
| 137 result = char_set_trusted_interface_->UTF16ToCharSet( | |
| 138 &utf16[0], static_cast<uint32_t>(utf16.size()), "latin1", | |
| 139 PP_CHARSET_TRUSTED_CONVERSIONERROR_SUBSTITUTE, | |
| 140 &output_buffer[0], &utf8result_len); | |
| 141 ASSERT_TRUE(result == PP_TRUE); | |
| 142 ASSERT_TRUE(utf8result_len == 5); | |
| 143 ASSERT_TRUE(output_buffer[0] == 'h' && output_buffer[1] == 'e' && | |
| 144 output_buffer[2] == '$'); | |
| 145 | |
| 146 // Try round-tripping some English & Chinese from UTF-8 through UTF-16 | |
| 147 std::string utf8source("Hello, world. \xe4\xbd\xa0\xe5\xa5\xbd"); | |
| 148 utf16 = UTF8ToUTF16(utf8source); | |
| 149 output_buffer.resize(100); | |
| 150 utf8result_len = static_cast<uint32_t>(output_buffer.size()); | |
| 151 result = char_set_trusted_interface_->UTF16ToCharSet( | |
| 152 &utf16[0], static_cast<uint32_t>(utf16.size()), | |
| 153 "Utf-8", PP_CHARSET_TRUSTED_CONVERSIONERROR_FAIL, | |
| 154 &output_buffer[0], &utf8result_len); | |
| 155 ASSERT_TRUE(result == PP_TRUE); | |
| 156 output_buffer.resize(utf8result_len); | |
| 157 ASSERT_TRUE(utf8source == output_buffer); | |
| 158 | |
| 159 // Test an un-encodable character with various modes. | |
| 160 utf16 = UTF8ToUTF16("h\xe4\xbd\xa0i"); | |
| 161 | |
| 162 // Fail mode, size should get 0'ed on failure. | |
| 163 output_buffer.resize(100); | |
| 164 utf8result_len = static_cast<uint32_t>(output_buffer.size()); | |
| 165 result = char_set_trusted_interface_->UTF16ToCharSet( | |
| 166 &utf16[0], static_cast<uint32_t>(utf16.size()), | |
| 167 "latin1", PP_CHARSET_TRUSTED_CONVERSIONERROR_FAIL, | |
| 168 &output_buffer[0], &utf8result_len); | |
| 169 ASSERT_TRUE(result == PP_FALSE); | |
| 170 ASSERT_TRUE(utf8result_len == 0); | |
| 171 | |
| 172 // Skip mode. | |
| 173 output_buffer.resize(100); | |
| 174 utf8result_len = static_cast<uint32_t>(output_buffer.size()); | |
| 175 result = char_set_trusted_interface_->UTF16ToCharSet( | |
| 176 &utf16[0], static_cast<uint32_t>(utf16.size()), | |
| 177 "latin1", PP_CHARSET_TRUSTED_CONVERSIONERROR_SKIP, | |
| 178 &output_buffer[0], &utf8result_len); | |
| 179 ASSERT_TRUE(result == PP_TRUE); | |
| 180 ASSERT_TRUE(utf8result_len == 2); | |
| 181 ASSERT_TRUE(output_buffer[0] == 'h' && output_buffer[1] == 'i'); | |
| 182 | |
| 183 // Substitute mode. | |
| 184 output_buffer.resize(100); | |
| 185 utf8result_len = static_cast<uint32_t>(output_buffer.size()); | |
| 186 result = char_set_trusted_interface_->UTF16ToCharSet( | |
| 187 &utf16[0], static_cast<uint32_t>(utf16.size()), | |
| 188 "latin1", PP_CHARSET_TRUSTED_CONVERSIONERROR_SUBSTITUTE, | |
| 189 &output_buffer[0], &utf8result_len); | |
| 190 ASSERT_TRUE(utf8result_len == 3); | |
| 191 output_buffer.resize(utf8result_len); | |
| 192 ASSERT_TRUE(output_buffer == "h?i"); | |
| 193 | |
| 194 // Try some invalid input encoding. | |
| 195 output_buffer.resize(100); | |
| 196 utf8result_len = static_cast<uint32_t>(output_buffer.size()); | |
| 197 utf16.clear(); | |
| 198 utf16.push_back(0xD800); // High surrogate. | |
| 199 utf16.push_back('A'); // Not a low surrogate. | |
| 200 result = char_set_trusted_interface_->UTF16ToCharSet( | |
| 201 &utf16[0], static_cast<uint32_t>(utf16.size()), | |
| 202 "latin1", PP_CHARSET_TRUSTED_CONVERSIONERROR_SUBSTITUTE, | |
| 203 &output_buffer[0], &utf8result_len); | |
| 204 ASSERT_TRUE(utf8result_len == 2); | |
| 205 ASSERT_TRUE(output_buffer[0] == '?' && output_buffer[1] == 'A'); | |
| 206 | |
| 207 // Invalid encoding name. | |
| 208 output_buffer.resize(100); | |
| 209 utf8result_len = static_cast<uint32_t>(output_buffer.size()); | |
| 210 result = char_set_trusted_interface_->UTF16ToCharSet( | |
| 211 &utf16[0], static_cast<uint32_t>(utf16.size()), | |
| 212 "poopiepants", PP_CHARSET_TRUSTED_CONVERSIONERROR_SUBSTITUTE, | |
| 213 &output_buffer[0], &utf8result_len); | |
| 214 ASSERT_TRUE(result == PP_FALSE); | |
| 215 ASSERT_TRUE(utf8result_len == 0); | |
| 216 | |
| 217 PASS(); | |
| 218 } | |
| 219 | |
| 220 // TODO(brettw) remove this when the old interface is removed. | |
| 221 std::string TestCharSet::TestCharSetToUTF16Deprecated() { | |
| 106 pp::Memory_Dev memory; | 222 pp::Memory_Dev memory; |
| 107 | 223 |
| 108 // Empty string. | 224 // Empty string. |
| 109 uint32_t utf16result_len; | 225 uint32_t utf16result_len; |
| 110 uint16_t* utf16result = char_set_interface_->CharSetToUTF16( | 226 uint16_t* utf16result = char_set_interface_->CharSetToUTF16( |
| 111 instance_->pp_instance(), "", 0, "latin1", | 227 instance_->pp_instance(), "", 0, "latin1", |
| 112 PP_CHARSET_CONVERSIONERROR_FAIL, &utf16result_len); | 228 PP_CHARSET_CONVERSIONERROR_FAIL, &utf16result_len); |
| 113 ASSERT_TRUE(utf16result); | 229 ASSERT_TRUE(utf16result); |
| 114 ASSERT_TRUE(utf16result_len == 0); | 230 ASSERT_TRUE(utf16result_len == 0); |
| 115 ASSERT_TRUE(utf16result[0] == 0); | 231 ASSERT_TRUE(utf16result[0] == 0); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 159 utf16result = char_set_interface_->CharSetToUTF16( | 275 utf16result = char_set_interface_->CharSetToUTF16( |
| 160 instance_->pp_instance(), badutf8, 3, "poopiepants", | 276 instance_->pp_instance(), badutf8, 3, "poopiepants", |
| 161 PP_CHARSET_CONVERSIONERROR_SUBSTITUTE, &utf16result_len); | 277 PP_CHARSET_CONVERSIONERROR_SUBSTITUTE, &utf16result_len); |
| 162 ASSERT_TRUE(!utf16result); | 278 ASSERT_TRUE(!utf16result); |
| 163 ASSERT_TRUE(utf16result_len == 0); | 279 ASSERT_TRUE(utf16result_len == 0); |
| 164 memory.MemFree(utf16result); | 280 memory.MemFree(utf16result); |
| 165 | 281 |
| 166 PASS(); | 282 PASS(); |
| 167 } | 283 } |
| 168 | 284 |
| 285 std::string TestCharSet::TestCharSetToUTF16() { | |
| 286 std::vector<uint16_t> output_buffer; | |
| 287 output_buffer.resize(100); | |
| 288 | |
| 289 // Empty string. | |
| 290 output_buffer.resize(100); | |
| 291 uint32_t utf16result_len = static_cast<uint32_t>(output_buffer.size()); | |
| 292 PP_Bool result = char_set_trusted_interface_->CharSetToUTF16( | |
| 293 "", 0, "latin1", PP_CHARSET_TRUSTED_CONVERSIONERROR_FAIL, | |
| 294 &output_buffer[0], &utf16result_len); | |
| 295 ASSERT_TRUE(result); | |
| 296 ASSERT_TRUE(utf16result_len == 0); | |
| 297 ASSERT_TRUE(output_buffer[0] == 0); | |
| 298 | |
| 299 // Basic Latin1. | |
| 300 output_buffer.resize(100); | |
| 301 utf16result_len = static_cast<uint32_t>(output_buffer.size()); | |
| 302 char latin1[] = "H\xef"; | |
| 303 result = char_set_trusted_interface_->CharSetToUTF16( | |
| 304 latin1, 2, "latin1", PP_CHARSET_TRUSTED_CONVERSIONERROR_FAIL, | |
| 305 &output_buffer[0], &utf16result_len); | |
| 306 ASSERT_TRUE(result); | |
| 307 ASSERT_TRUE(utf16result_len == 2); | |
| 308 ASSERT_TRUE(output_buffer[0] == 'H' && output_buffer[1] == 0xef); | |
| 309 | |
| 310 // Invalid input encoding with FAIL. | |
| 311 output_buffer.resize(100); | |
| 312 utf16result_len = static_cast<uint32_t>(output_buffer.size()); | |
| 313 char badutf8[] = "A\xe4Z"; | |
| 314 result = char_set_trusted_interface_->CharSetToUTF16( | |
| 315 badutf8, 3, "utf8", PP_CHARSET_TRUSTED_CONVERSIONERROR_FAIL, | |
| 316 &output_buffer[0], &utf16result_len); | |
| 317 ASSERT_TRUE(!result); | |
| 318 ASSERT_TRUE(utf16result_len == 0); | |
| 319 | |
| 320 // Invalid input with SKIP. | |
| 321 output_buffer.resize(100); | |
| 322 utf16result_len = static_cast<uint32_t>(output_buffer.size()); | |
| 323 result = char_set_trusted_interface_->CharSetToUTF16( | |
| 324 badutf8, 3, "utf8", PP_CHARSET_TRUSTED_CONVERSIONERROR_SKIP, | |
| 325 &output_buffer[0], &utf16result_len); | |
| 326 ASSERT_TRUE(result); | |
| 327 ASSERT_TRUE(utf16result_len == 2); | |
| 328 ASSERT_TRUE(output_buffer[0] == 'A' && output_buffer[1] == 'Z'); | |
| 329 | |
| 330 // Invalid input with SUBSTITUTE. | |
| 331 output_buffer.resize(100); | |
| 332 utf16result_len = static_cast<uint32_t>(output_buffer.size()); | |
| 333 result = char_set_trusted_interface_->CharSetToUTF16( | |
| 334 badutf8, 3, "utf8", PP_CHARSET_TRUSTED_CONVERSIONERROR_SUBSTITUTE, | |
| 335 &output_buffer[0], &utf16result_len); | |
| 336 ASSERT_TRUE(result); | |
| 337 ASSERT_TRUE(utf16result_len == 3); | |
| 338 ASSERT_TRUE(output_buffer[0] == 'A' && output_buffer[1] == 0xFFFD && | |
| 339 output_buffer[2] == 'Z'); | |
| 340 | |
| 341 // Invalid encoding name. | |
| 342 output_buffer.resize(100); | |
| 343 utf16result_len = static_cast<uint32_t>(output_buffer.size()); | |
| 344 result = char_set_trusted_interface_->CharSetToUTF16( | |
| 345 badutf8, 3, "poopiepants", PP_CHARSET_TRUSTED_CONVERSIONERROR_SUBSTITUTE, | |
| 346 &output_buffer[0], &utf16result_len); | |
| 347 ASSERT_TRUE(!result); | |
| 348 ASSERT_TRUE(utf16result_len == 0); | |
| 349 | |
| 350 PASS(); | |
| 351 } | |
| 352 | |
| 169 std::string TestCharSet::TestGetDefaultCharSet() { | 353 std::string TestCharSet::TestGetDefaultCharSet() { |
| 170 // Test invalid instance. | 354 // Test invalid instance. |
| 171 pp::Var result(pp::Var::PassRef(), char_set_interface_->GetDefaultCharSet(0)); | 355 pp::Var result(pp::Var::PassRef(), char_set_interface_->GetDefaultCharSet(0)); |
| 172 ASSERT_TRUE(result.is_undefined()); | 356 ASSERT_TRUE(result.is_undefined()); |
| 173 | 357 |
| 174 // Just make sure the default char set is a nonempty string. | 358 // Just make sure the default char set is a nonempty string. |
| 175 result = pp::Var(pp::Var::PassRef(), | 359 result = pp::Var(pp::Var::PassRef(), |
| 176 char_set_interface_->GetDefaultCharSet(instance_->pp_instance())); | 360 char_set_interface_->GetDefaultCharSet(instance_->pp_instance())); |
| 177 ASSERT_TRUE(result.is_string()); | 361 ASSERT_TRUE(result.is_string()); |
| 178 ASSERT_FALSE(result.AsString().empty()); | 362 ASSERT_FALSE(result.AsString().empty()); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 189 | 373 |
| 190 std::vector<uint16_t> result_vector; | 374 std::vector<uint16_t> result_vector; |
| 191 if (!result) | 375 if (!result) |
| 192 return result_vector; | 376 return result_vector; |
| 193 | 377 |
| 194 result_vector.assign(result, &result[result_len]); | 378 result_vector.assign(result, &result[result_len]); |
| 195 pp::Memory_Dev memory; | 379 pp::Memory_Dev memory; |
| 196 memory.MemFree(result); | 380 memory.MemFree(result); |
| 197 return result_vector; | 381 return result_vector; |
| 198 } | 382 } |
| OLD | NEW |