xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/win/cursor_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2013 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 "modules/desktop_capture/win/cursor.h"
12 
13 #include <memory>
14 
15 #include "modules/desktop_capture/desktop_frame.h"
16 #include "modules/desktop_capture/desktop_geometry.h"
17 #include "modules/desktop_capture/mouse_cursor.h"
18 #include "modules/desktop_capture/win/cursor_unittest_resources.h"
19 #include "modules/desktop_capture/win/scoped_gdi_object.h"
20 #include "test/gmock.h"
21 
22 namespace webrtc {
23 
24 namespace {
25 
26 // Loads `left` from resources, converts it to a `MouseCursor` instance and
27 // compares pixels with `right`. Returns true of MouseCursor bits match `right`.
28 // `right` must be a 32bpp cursor with alpha channel.
ConvertToMouseShapeAndCompare(unsigned left,unsigned right)29 bool ConvertToMouseShapeAndCompare(unsigned left, unsigned right) {
30   HMODULE instance = GetModuleHandle(NULL);
31 
32   // Load `left` from the EXE module's resources.
33   win::ScopedCursor cursor(reinterpret_cast<HCURSOR>(
34       LoadImage(instance, MAKEINTRESOURCE(left), IMAGE_CURSOR, 0, 0, 0)));
35   EXPECT_TRUE(cursor != NULL);
36 
37   // Convert `cursor` to `mouse_shape`.
38   HDC dc = GetDC(NULL);
39   std::unique_ptr<MouseCursor> mouse_shape(
40       CreateMouseCursorFromHCursor(dc, cursor));
41   ReleaseDC(NULL, dc);
42 
43   EXPECT_TRUE(mouse_shape.get());
44 
45   // Load `right`.
46   cursor.Set(reinterpret_cast<HCURSOR>(
47       LoadImage(instance, MAKEINTRESOURCE(right), IMAGE_CURSOR, 0, 0, 0)));
48 
49   ICONINFO iinfo;
50   EXPECT_TRUE(GetIconInfo(cursor, &iinfo));
51   EXPECT_TRUE(iinfo.hbmColor);
52 
53   // Make sure the bitmaps will be freed.
54   win::ScopedBitmap scoped_mask(iinfo.hbmMask);
55   win::ScopedBitmap scoped_color(iinfo.hbmColor);
56 
57   // Get `scoped_color` dimensions.
58   BITMAP bitmap_info;
59   EXPECT_TRUE(GetObject(scoped_color, sizeof(bitmap_info), &bitmap_info));
60 
61   int width = bitmap_info.bmWidth;
62   int height = bitmap_info.bmHeight;
63   EXPECT_TRUE(DesktopSize(width, height).equals(mouse_shape->image()->size()));
64 
65   // Get the pixels from `scoped_color`.
66   int size = width * height;
67   std::unique_ptr<uint32_t[]> data(new uint32_t[size]);
68   EXPECT_TRUE(GetBitmapBits(scoped_color, size * sizeof(uint32_t), data.get()));
69 
70   // Compare the 32bpp image in `mouse_shape` with the one loaded from `right`.
71   return memcmp(data.get(), mouse_shape->image()->data(),
72                 size * sizeof(uint32_t)) == 0;
73 }
74 
75 }  // namespace
76 
TEST(MouseCursorTest,MatchCursors)77 TEST(MouseCursorTest, MatchCursors) {
78   EXPECT_TRUE(
79       ConvertToMouseShapeAndCompare(IDD_CURSOR1_24BPP, IDD_CURSOR1_32BPP));
80 
81   EXPECT_TRUE(
82       ConvertToMouseShapeAndCompare(IDD_CURSOR1_8BPP, IDD_CURSOR1_32BPP));
83 
84   EXPECT_TRUE(
85       ConvertToMouseShapeAndCompare(IDD_CURSOR2_1BPP, IDD_CURSOR2_32BPP));
86 
87   EXPECT_TRUE(
88       ConvertToMouseShapeAndCompare(IDD_CURSOR3_4BPP, IDD_CURSOR3_32BPP));
89 }
90 
91 }  // namespace webrtc
92