| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/cpp/dev/memory_dev.h" |
| 8 #include "ppapi/cpp/module.h" | 9 #include "ppapi/cpp/module.h" |
| 9 #include "ppapi/tests/testing_instance.h" | 10 #include "ppapi/tests/testing_instance.h" |
| 10 | 11 |
| 11 REGISTER_TEST_CASE(CharSet); | 12 REGISTER_TEST_CASE(CharSet); |
| 12 | 13 |
| 13 TestCharSet::TestCharSet(TestingInstance* instance) | 14 TestCharSet::TestCharSet(TestingInstance* instance) |
| 14 : TestCase(instance), | 15 : TestCase(instance), |
| 15 char_set_interface_(NULL) { | 16 char_set_interface_(NULL) { |
| 16 } | 17 } |
| 17 | 18 |
| 18 bool TestCharSet::Init() { | 19 bool TestCharSet::Init() { |
| 19 char_set_interface_ = static_cast<PPB_CharSet_Dev const*>( | 20 char_set_interface_ = static_cast<PPB_CharSet_Dev const*>( |
| 20 pp::Module::Get()->GetBrowserInterface(PPB_CHAR_SET_DEV_INTERFACE)); | 21 pp::Module::Get()->GetBrowserInterface(PPB_CHAR_SET_DEV_INTERFACE)); |
| 21 return !!char_set_interface_; | 22 return !!char_set_interface_; |
| 22 } | 23 } |
| 23 | 24 |
| 24 void TestCharSet::RunTest() { | 25 void TestCharSet::RunTest() { |
| 25 RUN_TEST(UTF16ToCharSet); | 26 RUN_TEST(UTF16ToCharSet); |
| 26 RUN_TEST(CharSetToUTF16); | 27 RUN_TEST(CharSetToUTF16); |
| 27 RUN_TEST(GetDefaultCharSet); | 28 RUN_TEST(GetDefaultCharSet); |
| 28 } | 29 } |
| 29 | 30 |
| 30 std::string TestCharSet::TestUTF16ToCharSet() { | 31 std::string TestCharSet::TestUTF16ToCharSet() { |
| 31 // Empty string. | 32 // Empty string. |
| 32 std::vector<uint16_t> utf16; | 33 std::vector<uint16_t> utf16; |
| 33 utf16.push_back(0); | 34 utf16.push_back(0); |
| 34 uint32_t utf8result_len = 0; | 35 uint32_t utf8result_len = 0; |
| 36 pp::Memory_Dev memory; |
| 35 char* utf8result = char_set_interface_->UTF16ToCharSet( | 37 char* utf8result = char_set_interface_->UTF16ToCharSet( |
| 36 instance_->pp_instance(), &utf16[0], 0, "latin1", | 38 instance_->pp_instance(), &utf16[0], 0, "latin1", |
| 37 PP_CHARSET_CONVERSIONERROR_SUBSTITUTE, &utf8result_len); | 39 PP_CHARSET_CONVERSIONERROR_SUBSTITUTE, &utf8result_len); |
| 38 ASSERT_TRUE(utf8result); | 40 ASSERT_TRUE(utf8result); |
| 39 ASSERT_TRUE(utf8result[0] == 0); | 41 ASSERT_TRUE(utf8result[0] == 0); |
| 40 ASSERT_TRUE(utf8result_len == 0); | 42 ASSERT_TRUE(utf8result_len == 0); |
| 41 pp::Module::Get()->core()->MemFree(utf8result); | 43 memory.MemFree(utf8result); |
| 42 | 44 |
| 43 // Try round-tripping some English & Chinese from UTF-8 through UTF-16 | 45 // Try round-tripping some English & Chinese from UTF-8 through UTF-16 |
| 44 std::string utf8source("Hello, world. \xe4\xbd\xa0\xe5\xa5\xbd"); | 46 std::string utf8source("Hello, world. \xe4\xbd\xa0\xe5\xa5\xbd"); |
| 45 utf16 = UTF8ToUTF16(utf8source); | 47 utf16 = UTF8ToUTF16(utf8source); |
| 46 utf8result = char_set_interface_->UTF16ToCharSet( | 48 utf8result = char_set_interface_->UTF16ToCharSet( |
| 47 instance_->pp_instance(), &utf16[0], static_cast<uint32_t>(utf16.size()), | 49 instance_->pp_instance(), &utf16[0], static_cast<uint32_t>(utf16.size()), |
| 48 "Utf-8", PP_CHARSET_CONVERSIONERROR_FAIL, &utf8result_len); | 50 "Utf-8", PP_CHARSET_CONVERSIONERROR_FAIL, &utf8result_len); |
| 49 ASSERT_TRUE(utf8source == std::string(utf8result, utf8result_len)); | 51 ASSERT_TRUE(utf8source == std::string(utf8result, utf8result_len)); |
| 50 pp::Module::Get()->core()->MemFree(utf8result); | 52 memory.MemFree(utf8result); |
| 51 | 53 |
| 52 // Test an un-encodable character with various modes. | 54 // Test an un-encodable character with various modes. |
| 53 utf16 = UTF8ToUTF16("h\xe4\xbd\xa0i"); | 55 utf16 = UTF8ToUTF16("h\xe4\xbd\xa0i"); |
| 54 | 56 |
| 55 // Fail mode. | 57 // Fail mode. |
| 56 utf8result_len = 1234; // Test that this gets 0'ed on failure. | 58 utf8result_len = 1234; // Test that this gets 0'ed on failure. |
| 57 utf8result = char_set_interface_->UTF16ToCharSet( | 59 utf8result = char_set_interface_->UTF16ToCharSet( |
| 58 instance_->pp_instance(), &utf16[0], static_cast<uint32_t>(utf16.size()), | 60 instance_->pp_instance(), &utf16[0], static_cast<uint32_t>(utf16.size()), |
| 59 "latin1", PP_CHARSET_CONVERSIONERROR_FAIL, &utf8result_len); | 61 "latin1", PP_CHARSET_CONVERSIONERROR_FAIL, &utf8result_len); |
| 60 ASSERT_TRUE(utf8result_len == 0); | 62 ASSERT_TRUE(utf8result_len == 0); |
| 61 ASSERT_TRUE(utf8result == NULL); | 63 ASSERT_TRUE(utf8result == NULL); |
| 62 | 64 |
| 63 // Skip mode. | 65 // Skip mode. |
| 64 utf8result = char_set_interface_->UTF16ToCharSet( | 66 utf8result = char_set_interface_->UTF16ToCharSet( |
| 65 instance_->pp_instance(), &utf16[0], static_cast<uint32_t>(utf16.size()), | 67 instance_->pp_instance(), &utf16[0], static_cast<uint32_t>(utf16.size()), |
| 66 "latin1", PP_CHARSET_CONVERSIONERROR_SKIP, &utf8result_len); | 68 "latin1", PP_CHARSET_CONVERSIONERROR_SKIP, &utf8result_len); |
| 67 ASSERT_TRUE(utf8result_len == 2); | 69 ASSERT_TRUE(utf8result_len == 2); |
| 68 ASSERT_TRUE(utf8result[0] == 'h' && utf8result[1] == 'i' && | 70 ASSERT_TRUE(utf8result[0] == 'h' && utf8result[1] == 'i' && |
| 69 utf8result[2] == 0); | 71 utf8result[2] == 0); |
| 70 pp::Module::Get()->core()->MemFree(utf8result); | 72 memory.MemFree(utf8result); |
| 71 | 73 |
| 72 // Substitute mode. | 74 // Substitute mode. |
| 73 utf8result = char_set_interface_->UTF16ToCharSet( | 75 utf8result = char_set_interface_->UTF16ToCharSet( |
| 74 instance_->pp_instance(), &utf16[0], static_cast<uint32_t>(utf16.size()), | 76 instance_->pp_instance(), &utf16[0], static_cast<uint32_t>(utf16.size()), |
| 75 "latin1", PP_CHARSET_CONVERSIONERROR_SUBSTITUTE, &utf8result_len); | 77 "latin1", PP_CHARSET_CONVERSIONERROR_SUBSTITUTE, &utf8result_len); |
| 76 ASSERT_TRUE(utf8result_len == 3); | 78 ASSERT_TRUE(utf8result_len == 3); |
| 77 ASSERT_TRUE(utf8result[0] == 'h' && utf8result[1] == '?' && | 79 ASSERT_TRUE(utf8result[0] == 'h' && utf8result[1] == '?' && |
| 78 utf8result[2] == 'i' && utf8result[3] == 0); | 80 utf8result[2] == 'i' && utf8result[3] == 0); |
| 79 pp::Module::Get()->core()->MemFree(utf8result); | 81 memory.MemFree(utf8result); |
| 80 | 82 |
| 81 // Try some invalid input encoding. | 83 // Try some invalid input encoding. |
| 82 utf16.clear(); | 84 utf16.clear(); |
| 83 utf16.push_back(0xD800); // High surrogate. | 85 utf16.push_back(0xD800); // High surrogate. |
| 84 utf16.push_back('A'); // Not a low surrogate. | 86 utf16.push_back('A'); // Not a low surrogate. |
| 85 utf8result = char_set_interface_->UTF16ToCharSet( | 87 utf8result = char_set_interface_->UTF16ToCharSet( |
| 86 instance_->pp_instance(), &utf16[0], static_cast<uint32_t>(utf16.size()), | 88 instance_->pp_instance(), &utf16[0], static_cast<uint32_t>(utf16.size()), |
| 87 "latin1", PP_CHARSET_CONVERSIONERROR_SUBSTITUTE, &utf8result_len); | 89 "latin1", PP_CHARSET_CONVERSIONERROR_SUBSTITUTE, &utf8result_len); |
| 88 ASSERT_TRUE(utf8result_len == 2); | 90 ASSERT_TRUE(utf8result_len == 2); |
| 89 ASSERT_TRUE(utf8result[0] == '?' && utf8result[1] == 'A' && | 91 ASSERT_TRUE(utf8result[0] == '?' && utf8result[1] == 'A' && |
| 90 utf8result[2] == 0); | 92 utf8result[2] == 0); |
| 91 pp::Module::Get()->core()->MemFree(utf8result); | 93 memory.MemFree(utf8result); |
| 92 | 94 |
| 93 // Invalid encoding name. | 95 // Invalid encoding name. |
| 94 utf8result = char_set_interface_->UTF16ToCharSet( | 96 utf8result = char_set_interface_->UTF16ToCharSet( |
| 95 instance_->pp_instance(), &utf16[0], static_cast<uint32_t>(utf16.size()), | 97 instance_->pp_instance(), &utf16[0], static_cast<uint32_t>(utf16.size()), |
| 96 "poopiepants", PP_CHARSET_CONVERSIONERROR_SUBSTITUTE, &utf8result_len); | 98 "poopiepants", PP_CHARSET_CONVERSIONERROR_SUBSTITUTE, &utf8result_len); |
| 97 ASSERT_TRUE(!utf8result); | 99 ASSERT_TRUE(!utf8result); |
| 98 ASSERT_TRUE(utf8result_len == 0); | 100 ASSERT_TRUE(utf8result_len == 0); |
| 99 | 101 |
| 100 PASS(); | 102 PASS(); |
| 101 } | 103 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 uint16_t* result = char_set_interface_->CharSetToUTF16( | 177 uint16_t* result = char_set_interface_->CharSetToUTF16( |
| 176 instance_->pp_instance(), utf8.c_str(), | 178 instance_->pp_instance(), utf8.c_str(), |
| 177 static_cast<uint32_t>(utf8.size()), | 179 static_cast<uint32_t>(utf8.size()), |
| 178 "utf-8", PP_CHARSET_CONVERSIONERROR_FAIL, &result_len); | 180 "utf-8", PP_CHARSET_CONVERSIONERROR_FAIL, &result_len); |
| 179 | 181 |
| 180 std::vector<uint16_t> result_vector; | 182 std::vector<uint16_t> result_vector; |
| 181 if (!result) | 183 if (!result) |
| 182 return result_vector; | 184 return result_vector; |
| 183 | 185 |
| 184 result_vector.assign(result, &result[result_len]); | 186 result_vector.assign(result, &result[result_len]); |
| 185 pp::Module::Get()->core()->MemFree(result); | 187 pp::Memory_Dev memory; |
| 188 memory.MemFree(result); |
| 186 return result_vector; | 189 return result_vector; |
| 187 } | 190 } |
| OLD | NEW |