1 // Copyright 2010 Google Inc. All Rights Reserved 2 3 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" 10 11 #ifndef WIN32 12 #error Only for Windows 13 #endif 14 15 namespace talk_base { 16 17 static const TCHAR* kVisibleWindowTitle = L"Visible Window"; 18 static const TCHAR* kInvisibleWindowTitle = L"Invisible Window"; 19 20 class Win32WindowPickerForTest : public Win32WindowPicker { 21 public: 22 Win32WindowPickerForTest() { 23 EXPECT_TRUE(visible_window_.Create(NULL, kVisibleWindowTitle, WS_VISIBLE, 24 0, 0, 0, 0, 0)); 25 EXPECT_TRUE(invisible_window_.Create(NULL, kInvisibleWindowTitle, 0, 26 0, 0, 0, 0, 0)); 27 } 28 29 ~Win32WindowPickerForTest() { 30 visible_window_.Destroy(); 31 invisible_window_.Destroy(); 32 } 33 34 virtual bool GetWindowList(WindowDescriptionList* descriptions) { 35 if (!Win32WindowPicker::EnumProc(visible_window_.handle(), 36 reinterpret_cast<LPARAM>(descriptions))) { 37 return false; 38 } 39 if (!Win32WindowPicker::EnumProc(invisible_window_.handle(), 40 reinterpret_cast<LPARAM>(descriptions))) { 41 return false; 42 } 43 return true; 44 } 45 46 Win32Window* visible_window() { 47 return &visible_window_; 48 } 49 50 Win32Window* invisible_window() { 51 return &invisible_window_; 52 } 53 54 private: 55 Win32Window visible_window_; 56 Win32Window invisible_window_; 57 }; 58 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)); 70 } 71 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))); 78 } 79 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(); 84 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)); 91 } 92 93 } // namespace talk_base 94