OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "base/registry.h" | 8 #include "base/registry.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/win_util.h" | 10 #include "base/win_util.h" |
11 | 11 |
12 class BaseWinUtilTest: public testing::Test { | |
13 protected: | |
14 // Retrieve the OS primary language | |
15 static unsigned GetSystemLanguage() { | |
16 std::wstring language; | |
17 | |
18 typedef BOOL (WINAPI *fnGetThreadPreferredUILanguages)( | |
19 DWORD dwFlags, | |
20 PULONG pulNumLanguages, | |
21 PWSTR pwszLanguagesBuffer, | |
22 PULONG pcchLanguagesBuffer); | |
23 fnGetThreadPreferredUILanguages pGetThreadPreferredUILanguages = NULL; | |
24 pGetThreadPreferredUILanguages = | |
25 reinterpret_cast<fnGetThreadPreferredUILanguages>( | |
26 GetProcAddress(GetModuleHandle(L"kernel32.dll"), | |
27 "GetThreadPreferredUILanguages")); | |
28 if (pGetThreadPreferredUILanguages) { | |
29 // Vista, MUI-aware. | |
30 ULONG number = 0; | |
31 wchar_t buffer[256] = {0}; | |
32 ULONG buffer_size = sizeof(buffer); | |
33 EXPECT_TRUE(pGetThreadPreferredUILanguages(MUI_LANGUAGE_ID, &number, | |
34 buffer, &buffer_size)); | |
35 language = buffer; | |
36 } else { | |
37 // XP | |
38 RegKey language_key(HKEY_LOCAL_MACHINE, | |
39 L"SYSTEM\\CurrentControlSet\\Control\\Nls\\Language"); | |
40 language_key.ReadValue(L"InstallLanguage", &language); | |
41 } | |
42 wchar_t * unused_endptr; | |
43 return PRIMARYLANGID(wcstol(language.c_str(), &unused_endptr, 16)); | |
44 } | |
45 }; | |
46 | |
47 // The test is somewhat silly, because the Vista bots some have UAC enabled | 12 // The test is somewhat silly, because the Vista bots some have UAC enabled |
48 // and some have it disabled. At least we check that it does not crash. | 13 // and some have it disabled. At least we check that it does not crash. |
49 TEST_F(BaseWinUtilTest, TestIsUACEnabled) { | 14 TEST(BaseWinUtilTest, TestIsUACEnabled) { |
50 if (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA) { | 15 if (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA) { |
51 win_util::UserAccountControlIsEnabled(); | 16 win_util::UserAccountControlIsEnabled(); |
52 } else { | 17 } else { |
53 EXPECT_TRUE(win_util::UserAccountControlIsEnabled()); | 18 EXPECT_TRUE(win_util::UserAccountControlIsEnabled()); |
54 } | 19 } |
55 } | 20 } |
56 | 21 |
57 TEST_F(BaseWinUtilTest, TestGetUserSidString) { | 22 TEST(BaseWinUtilTest, TestGetUserSidString) { |
58 std::wstring user_sid; | 23 std::wstring user_sid; |
59 EXPECT_TRUE(win_util::GetUserSidString(&user_sid)); | 24 EXPECT_TRUE(win_util::GetUserSidString(&user_sid)); |
60 EXPECT_TRUE(!user_sid.empty()); | 25 EXPECT_TRUE(!user_sid.empty()); |
61 } | 26 } |
62 | 27 |
63 TEST_F(BaseWinUtilTest, TestGetNonClientMetrics) { | 28 TEST(BaseWinUtilTest, TestGetNonClientMetrics) { |
64 NONCLIENTMETRICS metrics = {0}; | 29 NONCLIENTMETRICS metrics = {0}; |
65 win_util::GetNonClientMetrics(&metrics); | 30 win_util::GetNonClientMetrics(&metrics); |
66 EXPECT_TRUE(metrics.cbSize > 0); | 31 EXPECT_TRUE(metrics.cbSize > 0); |
67 EXPECT_TRUE(metrics.iScrollWidth > 0); | 32 EXPECT_TRUE(metrics.iScrollWidth > 0); |
68 EXPECT_TRUE(metrics.iScrollHeight > 0); | 33 EXPECT_TRUE(metrics.iScrollHeight > 0); |
69 } | 34 } |
70 | 35 |
71 TEST_F(BaseWinUtilTest, FormatMessage) { | 36 namespace { |
72 unsigned language = GetSystemLanguage(); | 37 |
73 ASSERT_TRUE(language); | 38 // Saves the current thread's locale ID when initialized, and restores it when |
| 39 // the instance is going out of scope. |
| 40 class ThreadLocaleSaver { |
| 41 public: |
| 42 ThreadLocaleSaver() : original_locale_id_(GetThreadLocale()) {} |
| 43 ~ThreadLocaleSaver() { SetThreadLocale(original_locale_id_); } |
| 44 |
| 45 private: |
| 46 LCID original_locale_id_; |
| 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(ThreadLocaleSaver); |
| 49 }; |
| 50 |
| 51 } // namespace |
| 52 |
| 53 TEST(BaseWinUtilTest, FormatMessage) { |
| 54 // Because we cannot write tests of every language, we only test the message |
| 55 // of en-US locale. Here, we change the current locale temporarily. |
| 56 ThreadLocaleSaver thread_locale_saver; |
| 57 WORD language_id = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US); |
| 58 LCID locale_id = MAKELCID(language_id, SORT_DEFAULT); |
| 59 ASSERT_TRUE(SetThreadLocale(locale_id)); |
74 | 60 |
75 const int kAccessDeniedErrorCode = 5; | 61 const int kAccessDeniedErrorCode = 5; |
76 SetLastError(kAccessDeniedErrorCode); | 62 SetLastError(kAccessDeniedErrorCode); |
77 ASSERT_EQ(GetLastError(), kAccessDeniedErrorCode); | 63 ASSERT_EQ(GetLastError(), kAccessDeniedErrorCode); |
78 std::wstring value; | 64 std::wstring value; |
79 | 65 TrimWhitespace(win_util::FormatLastWin32Error(), TRIM_ALL, &value); |
80 if (language == LANG_ENGLISH) { | 66 EXPECT_EQ(std::wstring(L"Access is denied."), value); |
81 // This test would fail on non-English system. | |
82 TrimWhitespace(win_util::FormatLastWin32Error(), TRIM_ALL, &value); | |
83 EXPECT_EQ(std::wstring(L"Access is denied."), value); | |
84 } else if (language == LANG_FRENCH) { | |
85 // This test would fail on non-French system. | |
86 TrimWhitespace(win_util::FormatLastWin32Error(), TRIM_ALL, &value); | |
87 EXPECT_EQ(std::wstring(L"Acc\u00e8s refus\u00e9."), value); | |
88 } else { | |
89 EXPECT_TRUE(0) << "Please implement the test for your OS language."; | |
90 } | |
91 | 67 |
92 // Manually call the OS function | 68 // Manually call the OS function |
93 wchar_t * string_buffer = NULL; | 69 wchar_t * string_buffer = NULL; |
94 unsigned string_length = | 70 unsigned string_length = |
95 ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | | 71 ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
96 FORMAT_MESSAGE_FROM_SYSTEM | | 72 FORMAT_MESSAGE_FROM_SYSTEM | |
97 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, | 73 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, |
98 kAccessDeniedErrorCode, 0, | 74 kAccessDeniedErrorCode, 0, |
99 reinterpret_cast<wchar_t *>(&string_buffer), 0, NULL); | 75 reinterpret_cast<wchar_t *>(&string_buffer), 0, NULL); |
100 | 76 |
101 // Verify the call succeeded | 77 // Verify the call succeeded |
102 ASSERT_TRUE(string_length); | 78 ASSERT_TRUE(string_length); |
103 ASSERT_TRUE(string_buffer); | 79 ASSERT_TRUE(string_buffer); |
104 | 80 |
105 // Verify the string is the same by different calls | 81 // Verify the string is the same by different calls |
106 EXPECT_EQ(win_util::FormatLastWin32Error(), std::wstring(string_buffer)); | 82 EXPECT_EQ(win_util::FormatLastWin32Error(), std::wstring(string_buffer)); |
107 EXPECT_EQ(win_util::FormatMessage(kAccessDeniedErrorCode), | 83 EXPECT_EQ(win_util::FormatMessage(kAccessDeniedErrorCode), |
108 std::wstring(string_buffer)); | 84 std::wstring(string_buffer)); |
109 | 85 |
110 // Done with the buffer allocated by ::FormatMessage() | 86 // Done with the buffer allocated by ::FormatMessage() |
111 LocalFree(string_buffer); | 87 LocalFree(string_buffer); |
112 } | 88 } |
OLD | NEW |