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