1 // Copyright 2010 Google Inc. All Rights Reserved
4 #include "talk/base/gunit.h"
5 #include "talk/base/common.h"
6 #include "talk/base/logging.h"
7 #include "talk/base/win32window.h"
8 #include "talk/base/win32windowpicker.h"
9 #include "talk/base/windowpicker.h"
12 #error Only for Windows
17 static const TCHAR* kVisibleWindowTitle = L"Visible Window";
18 static const TCHAR* kInvisibleWindowTitle = L"Invisible Window";
20 class Win32WindowPickerForTest : public Win32WindowPicker {
22 Win32WindowPickerForTest() {
23 EXPECT_TRUE(visible_window_.Create(NULL, kVisibleWindowTitle, WS_VISIBLE,
25 EXPECT_TRUE(invisible_window_.Create(NULL, kInvisibleWindowTitle, 0,
29 ~Win32WindowPickerForTest() {
30 visible_window_.Destroy();
31 invisible_window_.Destroy();
34 virtual bool GetWindowList(WindowDescriptionList* descriptions) {
35 if (!Win32WindowPicker::EnumProc(visible_window_.handle(),
36 reinterpret_cast<LPARAM>(descriptions))) {
39 if (!Win32WindowPicker::EnumProc(invisible_window_.handle(),
40 reinterpret_cast<LPARAM>(descriptions))) {
46 Win32Window* visible_window() {
47 return &visible_window_;
50 Win32Window* invisible_window() {
51 return &invisible_window_;
55 Win32Window visible_window_;
56 Win32Window invisible_window_;
59 TEST(Win32WindowPickerTest, TestGetWindowList) {
60 Win32WindowPickerForTest window_picker;
61 WindowDescriptionList descriptions;
62 EXPECT_TRUE(window_picker.GetWindowList(&descriptions));
63 EXPECT_EQ(1, descriptions.size());
64 WindowDescription desc = descriptions.front();
65 EXPECT_EQ(window_picker.visible_window()->handle(), desc.id().id());
66 TCHAR window_title[500];
67 GetWindowText(window_picker.visible_window()->handle(), window_title,
68 ARRAY_SIZE(window_title));
69 EXPECT_EQ(0, wcscmp(window_title, kVisibleWindowTitle));
72 TEST(Win32WindowPickerTest, TestIsVisible) {
73 Win32WindowPickerForTest window_picker;
74 HWND visible_id = window_picker.visible_window()->handle();
75 HWND invisible_id = window_picker.invisible_window()->handle();
76 EXPECT_TRUE(window_picker.IsVisible(WindowId(visible_id)));
77 EXPECT_FALSE(window_picker.IsVisible(WindowId(invisible_id)));
80 TEST(Win32WindowPickerTest, TestMoveToFront) {
81 Win32WindowPickerForTest window_picker;
82 HWND visible_id = window_picker.visible_window()->handle();
83 HWND invisible_id = window_picker.invisible_window()->handle();
85 // There are a number of condition where SetForegroundWindow might
86 // fail depending on the state of the calling process. To be on the
87 // safe side we doesn't expect MoveToFront to return true, just test
88 // that we don't crash.
89 window_picker.MoveToFront(WindowId(visible_id));
90 window_picker.MoveToFront(WindowId(invisible_id));
93 } // namespace talk_base