| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef URL_URL_TEST_UTILS_H_ | 5 #ifndef URL_URL_TEST_UTILS_H_ |
| 6 #define URL_URL_TEST_UTILS_H_ | 6 #define URL_URL_TEST_UTILS_H_ |
| 7 | 7 |
| 8 // Convenience functions for string conversions. | 8 // Convenience functions for string conversions. |
| 9 // These are mostly intended for use in unit tests. | 9 // These are mostly intended for use in unit tests. |
| 10 | 10 |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "url/url_canon_internal.h" | 15 #include "url/url_canon_internal.h" |
| 16 | 16 |
| 17 namespace url { | 17 namespace url { |
| 18 | 18 |
| 19 namespace test_utils { | 19 namespace test_utils { |
| 20 | 20 |
| 21 // Converts a UTF-16 string from native wchar_t format to char16, by | 21 // Converts a UTF-16 string from native wchar_t format to char16, by |
| 22 // truncating the high 32 bits. This is not meant to handle true UTF-32 | 22 // truncating the high 32 bits. This is not meant to handle true UTF-32 |
| 23 // encoded strings. | 23 // encoded strings. |
| 24 inline base::string16 WStringToUTF16(const wchar_t* src) { | 24 inline base::string16 WStringToUTF16(const wchar_t* src) { |
| 25 base::string16 str; | 25 base::string16 str; |
| 26 int length = static_cast<int>(wcslen(src)); | 26 int length = static_cast<int>(wcslen(src)); |
| 27 for (int i = 0; i < length; ++i) { | 27 for (int i = 0; i < length; ++i) { |
| 28 str.push_back(static_cast<base::char16>(src[i])); | 28 str.push_back(static_cast<base::char16>(src[i])); |
| 29 } | 29 } |
| 30 return str; | 30 return str; |
| 31 } | 31 } |
| 32 | 32 |
| 33 // Converts a string from UTF-8 to UTF-16 | 33 // Converts a string from UTF-8 to UTF-16. |
| 34 inline base::string16 ConvertUTF8ToUTF16(const std::string& src) { | 34 inline base::string16 ConvertUTF8ToUTF16(const std::string& src) { |
| 35 int length = static_cast<int>(src.length()); | 35 int length = static_cast<int>(src.length()); |
| 36 EXPECT_LT(length, 1024); | 36 EXPECT_LT(length, 1024); |
| 37 RawCanonOutputW<1024> output; | 37 RawCanonOutputW<1024> output; |
| 38 EXPECT_TRUE(ConvertUTF8ToUTF16(src.data(), length, &output)); | 38 EXPECT_TRUE(ConvertUTF8ToUTF16(src.data(), length, &output)); |
| 39 return base::string16(output.data(), output.length()); | 39 return base::string16(output.data(), output.length()); |
| 40 } | 40 } |
| 41 | 41 |
| 42 // Converts a string from UTF-16 to UTF-8 | 42 // Converts a string from UTF-16 to UTF-8. |
| 43 inline std::string ConvertUTF16ToUTF8(const base::string16& src) { | 43 inline std::string ConvertUTF16ToUTF8(const base::string16& src) { |
| 44 std::string str; | 44 std::string str; |
| 45 StdStringCanonOutput output(&str); | 45 StdStringCanonOutput output(&str); |
| 46 EXPECT_TRUE(ConvertUTF16ToUTF8(src.data(), static_cast<int>(src.length()), | 46 EXPECT_TRUE(ConvertUTF16ToUTF8(src.data(), static_cast<int>(src.length()), |
| 47 &output)); | 47 &output)); |
| 48 output.Complete(); | 48 output.Complete(); |
| 49 return str; | 49 return str; |
| 50 } | 50 } |
| 51 | 51 |
| 52 } // namespace test_utils | 52 } // namespace test_utils |
| 53 | 53 |
| 54 } // namespace url | 54 } // namespace url |
| 55 | 55 |
| 56 #endif // URL_URL_TEST_UTILS_H_ | 56 #endif // URL_URL_TEST_UTILS_H_ |
| OLD | NEW |