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