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 <windows.h> | 5 #include <windows.h> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/win_util.h" | 9 #include "base/win_util.h" |
10 #include "base/win/windows_version.h" | 10 #include "base/win/windows_version.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 ThreadLocaleSaver() : original_locale_id_(GetThreadLocale()) {} | 43 ThreadLocaleSaver() : original_locale_id_(GetThreadLocale()) {} |
44 ~ThreadLocaleSaver() { SetThreadLocale(original_locale_id_); } | 44 ~ThreadLocaleSaver() { SetThreadLocale(original_locale_id_); } |
45 | 45 |
46 private: | 46 private: |
47 LCID original_locale_id_; | 47 LCID original_locale_id_; |
48 | 48 |
49 DISALLOW_COPY_AND_ASSIGN(ThreadLocaleSaver); | 49 DISALLOW_COPY_AND_ASSIGN(ThreadLocaleSaver); |
50 }; | 50 }; |
51 | 51 |
52 } // namespace | 52 } // namespace |
53 | |
54 TEST(BaseWinUtilTest, FormatMessage) { | |
55 // Because we cannot write tests of every language, we only test the message | |
56 // of en-US locale. Here, we change the current locale temporarily. | |
57 ThreadLocaleSaver thread_locale_saver; | |
58 WORD language_id = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US); | |
59 LCID locale_id = MAKELCID(language_id, SORT_DEFAULT); | |
60 ASSERT_TRUE(SetThreadLocale(locale_id)); | |
61 | |
62 const int kAccessDeniedErrorCode = 5; | |
63 SetLastError(kAccessDeniedErrorCode); | |
64 ASSERT_EQ(GetLastError(), kAccessDeniedErrorCode); | |
65 std::wstring value; | |
66 TrimWhitespace(win_util::FormatLastWin32Error(), TRIM_ALL, &value); | |
67 EXPECT_EQ(std::wstring(L"Access is denied."), value); | |
68 | |
69 // Manually call the OS function | |
70 wchar_t * string_buffer = NULL; | |
71 unsigned string_length = | |
72 ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | | |
73 FORMAT_MESSAGE_FROM_SYSTEM | | |
74 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, | |
75 kAccessDeniedErrorCode, 0, | |
76 reinterpret_cast<wchar_t *>(&string_buffer), 0, NULL); | |
77 | |
78 // Verify the call succeeded | |
79 ASSERT_TRUE(string_length); | |
80 ASSERT_TRUE(string_buffer); | |
81 | |
82 // Verify the string is the same by different calls | |
83 EXPECT_EQ(win_util::FormatLastWin32Error(), std::wstring(string_buffer)); | |
84 EXPECT_EQ(win_util::FormatMessage(kAccessDeniedErrorCode), | |
85 std::wstring(string_buffer)); | |
86 | |
87 // Done with the buffer allocated by ::FormatMessage() | |
88 LocalFree(string_buffer); | |
89 } | |
OLD | NEW |