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