Port PluginObject fix downstream. See http://trac.webkit.org/changeset/61415/ for...
[chromium-blink-merge.git] / base / win_util_unittest.cc
blob39162a5356a6a3c1c9974a367a7f81cf7307d6d2
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
3 // found in the LICENSE file.
5 #include <windows.h>
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "base/registry.h"
9 #include "base/string_util.h"
10 #include "base/win_util.h"
12 // The test is somewhat silly, because the Vista bots some have UAC enabled
13 // and some have it disabled. At least we check that it does not crash.
14 TEST(BaseWinUtilTest, TestIsUACEnabled) {
15 if (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA) {
16 win_util::UserAccountControlIsEnabled();
17 } else {
18 EXPECT_TRUE(win_util::UserAccountControlIsEnabled());
22 TEST(BaseWinUtilTest, TestGetUserSidString) {
23 std::wstring user_sid;
24 EXPECT_TRUE(win_util::GetUserSidString(&user_sid));
25 EXPECT_TRUE(!user_sid.empty());
28 TEST(BaseWinUtilTest, TestGetNonClientMetrics) {
29 NONCLIENTMETRICS metrics = {0};
30 win_util::GetNonClientMetrics(&metrics);
31 EXPECT_TRUE(metrics.cbSize > 0);
32 EXPECT_TRUE(metrics.iScrollWidth > 0);
33 EXPECT_TRUE(metrics.iScrollHeight > 0);
36 namespace {
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_); }
45 private:
46 LCID original_locale_id_;
48 DISALLOW_COPY_AND_ASSIGN(ThreadLocaleSaver);
51 } // namespace
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));
61 const int kAccessDeniedErrorCode = 5;
62 SetLastError(kAccessDeniedErrorCode);
63 ASSERT_EQ(GetLastError(), kAccessDeniedErrorCode);
64 std::wstring value;
65 TrimWhitespace(win_util::FormatLastWin32Error(), TRIM_ALL, &value);
66 EXPECT_EQ(std::wstring(L"Access is denied."), value);
68 // Manually call the OS function
69 wchar_t * string_buffer = NULL;
70 unsigned string_length =
71 ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
72 FORMAT_MESSAGE_FROM_SYSTEM |
73 FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
74 kAccessDeniedErrorCode, 0,
75 reinterpret_cast<wchar_t *>(&string_buffer), 0, NULL);
77 // Verify the call succeeded
78 ASSERT_TRUE(string_length);
79 ASSERT_TRUE(string_buffer);
81 // Verify the string is the same by different calls
82 EXPECT_EQ(win_util::FormatLastWin32Error(), std::wstring(string_buffer));
83 EXPECT_EQ(win_util::FormatMessage(kAccessDeniedErrorCode),
84 std::wstring(string_buffer));
86 // Done with the buffer allocated by ::FormatMessage()
87 LocalFree(string_buffer);