1 /*
2  *  Copyright 2010 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 #include "webrtc/base/arraysize.h"
11 #include "webrtc/base/gunit.h"
12 #include "webrtc/base/common.h"
13 #include "webrtc/base/logging.h"
14 #include "webrtc/base/win32window.h"
15 #include "webrtc/base/win32windowpicker.h"
16 #include "webrtc/base/windowpicker.h"
17 
18 #if !defined(WEBRTC_WIN)
19 #error Only for Windows
20 #endif
21 
22 namespace rtc {
23 
24 static const TCHAR* kVisibleWindowTitle = L"Visible Window";
25 static const TCHAR* kInvisibleWindowTitle = L"Invisible Window";
26 
27 class Win32WindowPickerForTest : public Win32WindowPicker {
28  public:
Win32WindowPickerForTest()29   Win32WindowPickerForTest() {
30     EXPECT_TRUE(visible_window_.Create(NULL, kVisibleWindowTitle, WS_VISIBLE,
31                                        0, 0, 0, 0, 0));
32     EXPECT_TRUE(invisible_window_.Create(NULL, kInvisibleWindowTitle, 0,
33                                          0, 0, 0, 0, 0));
34   }
35 
~Win32WindowPickerForTest()36   ~Win32WindowPickerForTest() {
37     visible_window_.Destroy();
38     invisible_window_.Destroy();
39   }
40 
GetWindowList(WindowDescriptionList * descriptions)41   virtual bool GetWindowList(WindowDescriptionList* descriptions) {
42     if (!Win32WindowPicker::EnumProc(visible_window_.handle(),
43                                      reinterpret_cast<LPARAM>(descriptions))) {
44       return false;
45     }
46     if (!Win32WindowPicker::EnumProc(invisible_window_.handle(),
47                                      reinterpret_cast<LPARAM>(descriptions))) {
48       return false;
49     }
50     return true;
51   }
52 
visible_window()53   Win32Window* visible_window() {
54     return &visible_window_;
55   }
56 
invisible_window()57   Win32Window* invisible_window() {
58     return &invisible_window_;
59   }
60 
61  private:
62   Win32Window visible_window_;
63   Win32Window invisible_window_;
64 };
65 
TEST(Win32WindowPickerTest,TestGetWindowList)66 TEST(Win32WindowPickerTest, TestGetWindowList) {
67   Win32WindowPickerForTest window_picker;
68   WindowDescriptionList descriptions;
69   EXPECT_TRUE(window_picker.GetWindowList(&descriptions));
70   EXPECT_EQ(1, descriptions.size());
71   WindowDescription desc = descriptions.front();
72   EXPECT_EQ(window_picker.visible_window()->handle(), desc.id().id());
73   TCHAR window_title[500];
74   GetWindowText(window_picker.visible_window()->handle(), window_title,
75                 arraysize(window_title));
76   EXPECT_EQ(0, wcscmp(window_title, kVisibleWindowTitle));
77 }
78 
TEST(Win32WindowPickerTest,TestIsVisible)79 TEST(Win32WindowPickerTest, TestIsVisible) {
80   Win32WindowPickerForTest window_picker;
81   HWND visible_id = window_picker.visible_window()->handle();
82   HWND invisible_id = window_picker.invisible_window()->handle();
83   EXPECT_TRUE(window_picker.IsVisible(WindowId(visible_id)));
84   EXPECT_FALSE(window_picker.IsVisible(WindowId(invisible_id)));
85 }
86 
TEST(Win32WindowPickerTest,TestMoveToFront)87 TEST(Win32WindowPickerTest, TestMoveToFront) {
88   Win32WindowPickerForTest window_picker;
89   HWND visible_id = window_picker.visible_window()->handle();
90   HWND invisible_id = window_picker.invisible_window()->handle();
91 
92   // There are a number of condition where SetForegroundWindow might
93   // fail depending on the state of the calling process. To be on the
94   // safe side we doesn't expect MoveToFront to return true, just test
95   // that we don't crash.
96   window_picker.MoveToFront(WindowId(visible_id));
97   window_picker.MoveToFront(WindowId(invisible_id));
98 }
99 
100 }  // namespace rtc
101