xref: /aosp_15_r20/external/cronet/base/win/window_enumerator_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/win/window_enumerator.h"
6 
7 #include <windows.h>
8 
9 #include <string>
10 #include <vector>
11 
12 #include "base/test/bind.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 namespace base::win {
16 
TEST(WindowEnumeratorTest,EnumerateTopLevelWindows)17 TEST(WindowEnumeratorTest, EnumerateTopLevelWindows) {
18   EnumerateChildWindows(
19       ::GetDesktopWindow(), base::BindLambdaForTesting([&](HWND hwnd) {
20         const std::wstring window_class = GetWindowClass(hwnd);
21         EXPECT_EQ(window_class, [&]() {
22           constexpr int kMaxWindowClassNameLength = 256;
23           wchar_t buffer[kMaxWindowClassNameLength + 1] = {0};
24           const int name_len = ::GetClassName(hwnd, buffer, std::size(buffer));
25           if (name_len <= 0 || name_len > kMaxWindowClassNameLength) {
26             return std::wstring();
27           }
28           return std::wstring(&buffer[0], static_cast<size_t>(name_len));
29         }());
30 
31         EXPECT_EQ(IsTopmostWindow(hwnd),
32                   (::GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0);
33 
34         EXPECT_EQ(IsSystemDialog(hwnd), window_class == L"#32770");
35 
36         EXPECT_EQ(IsShellWindow(hwnd),
37                   window_class == L"Button" ||
38                       window_class == L"Shell_TrayWnd" ||
39                       window_class == L"Shell_SecondaryTrayWnd");
40         EXPECT_EQ(GetWindowTextString(hwnd), [&]() {
41           const int num_chars = ::GetWindowTextLength(hwnd);
42           if (!num_chars) {
43             return std::wstring();
44           }
45           std::vector<wchar_t> text(static_cast<size_t>(num_chars) + 1);
46           if (!::GetWindowText(hwnd, &text.front(),
47                                static_cast<int>(text.size()))) {
48             return std::wstring();
49           }
50           return std::wstring(text.begin(), --text.end());
51         }());
52         return false;
53       }));
54 }
55 
56 }  // namespace base::win
57