xref: /aosp_15_r20/external/webrtc/rtc_base/win32_window_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2009 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 
11 #include "rtc_base/win32_window.h"
12 
13 #include "rtc_base/gunit.h"
14 #include "rtc_base/logging.h"
15 
16 static LRESULT kDummyResult = 0x1234ABCD;
17 
18 class TestWindow : public rtc::Win32Window {
19  public:
TestWindow()20   TestWindow() : destroyed_(false) { memset(&msg_, 0, sizeof(msg_)); }
msg() const21   const MSG& msg() const { return msg_; }
destroyed() const22   bool destroyed() const { return destroyed_; }
23 
OnMessage(UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT & result)24   bool OnMessage(UINT uMsg,
25                  WPARAM wParam,
26                  LPARAM lParam,
27                  LRESULT& result) override {
28     msg_.message = uMsg;
29     msg_.wParam = wParam;
30     msg_.lParam = lParam;
31     result = kDummyResult;
32     return true;
33   }
OnNcDestroy()34   void OnNcDestroy() override { destroyed_ = true; }
35 
36  private:
37   MSG msg_;
38   bool destroyed_;
39 };
40 
TEST(Win32WindowTest,Basics)41 TEST(Win32WindowTest, Basics) {
42   TestWindow wnd;
43   EXPECT_TRUE(wnd.handle() == nullptr);
44   EXPECT_FALSE(wnd.destroyed());
45   EXPECT_TRUE(wnd.Create(0, L"Test", 0, 0, 0, 0, 100, 100));
46   EXPECT_TRUE(wnd.handle() != nullptr);
47   EXPECT_EQ(kDummyResult, ::SendMessage(wnd.handle(), WM_USER, 1, 2));
48   EXPECT_EQ(static_cast<UINT>(WM_USER), wnd.msg().message);
49   EXPECT_EQ(1u, wnd.msg().wParam);
50   EXPECT_EQ(2l, wnd.msg().lParam);
51   wnd.Destroy();
52   EXPECT_TRUE(wnd.handle() == nullptr);
53   EXPECT_TRUE(wnd.destroyed());
54 }
55 
TEST(Win32WindowTest,MultipleWindows)56 TEST(Win32WindowTest, MultipleWindows) {
57   TestWindow wnd1, wnd2;
58   EXPECT_TRUE(wnd1.Create(0, L"Test", 0, 0, 0, 0, 100, 100));
59   EXPECT_TRUE(wnd2.Create(0, L"Test", 0, 0, 0, 0, 100, 100));
60   EXPECT_TRUE(wnd1.handle() != nullptr);
61   EXPECT_TRUE(wnd2.handle() != nullptr);
62   wnd1.Destroy();
63   wnd2.Destroy();
64   EXPECT_TRUE(wnd2.handle() == nullptr);
65   EXPECT_TRUE(wnd1.handle() == nullptr);
66 }
67