14af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org/*
24af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
34af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org *
44af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org *  Use of this source code is governed by a BSD-style license
54af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org *  that can be found in the LICENSE file in the root of the source
64af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org *  tree. An additional intellectual property rights grant can be found
74af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org *  in the file PATENTS.  All contributing project authors may
84af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org *  be found in the AUTHORS file in the root of the source tree.
94af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org */
104af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
114af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org#include "testing/gmock/include/gmock/gmock.h"
1200b8f6b3643332cce1ee711715f7fbb824d793cakwiberg@webrtc.org#include "webrtc/base/scoped_ptr.h"
132df89c0c8b6416c69d70da5f29615733986a0716sergeyu@chromium.org#include "webrtc/modules/desktop_capture/desktop_frame.h"
144af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org#include "webrtc/modules/desktop_capture/desktop_geometry.h"
152df89c0c8b6416c69d70da5f29615733986a0716sergeyu@chromium.org#include "webrtc/modules/desktop_capture/mouse_cursor.h"
164af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org#include "webrtc/modules/desktop_capture/win/cursor.h"
174af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org#include "webrtc/modules/desktop_capture/win/cursor_unittest_resources.h"
184af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org#include "webrtc/modules/desktop_capture/win/scoped_gdi_object.h"
194af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
204af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.orgnamespace webrtc {
214af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
224af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.orgnamespace {
234af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
242df89c0c8b6416c69d70da5f29615733986a0716sergeyu@chromium.org// Loads |left| from resources, converts it to a |MouseCursor| instance and
252df89c0c8b6416c69d70da5f29615733986a0716sergeyu@chromium.org// compares pixels with |right|. Returns true of MouseCursor bits match |right|.
262df89c0c8b6416c69d70da5f29615733986a0716sergeyu@chromium.org// |right| must be a 32bpp cursor with alpha channel.
274af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.orgbool ConvertToMouseShapeAndCompare(unsigned left, unsigned right) {
284af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  HMODULE instance = GetModuleHandle(NULL);
294af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
304af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  // Load |left| from the EXE module's resources.
314af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  win::ScopedCursor cursor(reinterpret_cast<HCURSOR>(
324af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org      LoadImage(instance, MAKEINTRESOURCE(left), IMAGE_CURSOR, 0, 0, 0)));
334af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  EXPECT_TRUE(cursor != NULL);
344af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
354af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  // Convert |cursor| to |mouse_shape|.
364af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  HDC dc = GetDC(NULL);
3700b8f6b3643332cce1ee711715f7fbb824d793cakwiberg@webrtc.org  rtc::scoped_ptr<MouseCursor> mouse_shape(
382df89c0c8b6416c69d70da5f29615733986a0716sergeyu@chromium.org      CreateMouseCursorFromHCursor(dc, cursor));
394af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  ReleaseDC(NULL, dc);
404af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
414af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  EXPECT_TRUE(mouse_shape.get());
424af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
434af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  // Load |right|.
444af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  cursor.Set(reinterpret_cast<HCURSOR>(
454af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org      LoadImage(instance, MAKEINTRESOURCE(right), IMAGE_CURSOR, 0, 0, 0)));
464af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
474af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  ICONINFO iinfo;
484af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  EXPECT_TRUE(GetIconInfo(cursor, &iinfo));
494af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  EXPECT_TRUE(iinfo.hbmColor);
504af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
514af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  // Make sure the bitmaps will be freed.
524af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  win::ScopedBitmap scoped_mask(iinfo.hbmMask);
534af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  win::ScopedBitmap scoped_color(iinfo.hbmColor);
544af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
554af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  // Get |scoped_color| dimensions.
564af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  BITMAP bitmap_info;
574af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  EXPECT_TRUE(GetObject(scoped_color, sizeof(bitmap_info), &bitmap_info));
584af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
594af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  int width = bitmap_info.bmWidth;
604af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  int height = bitmap_info.bmHeight;
618ae72560dd67a59922a49bfb7808f2290c07991dsergeyu@chromium.org  EXPECT_TRUE(DesktopSize(width, height).equals(mouse_shape->image()->size()));
624af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
634af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  // Get the pixels from |scoped_color|.
644af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  int size = width * height;
6500b8f6b3643332cce1ee711715f7fbb824d793cakwiberg@webrtc.org  rtc::scoped_ptr<uint32_t[]> data(new uint32_t[size]);
664af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  EXPECT_TRUE(GetBitmapBits(scoped_color, size * sizeof(uint32_t), data.get()));
674af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
684af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  // Compare the 32bpp image in |mouse_shape| with the one loaded from |right|.
698ae72560dd67a59922a49bfb7808f2290c07991dsergeyu@chromium.org  return memcmp(data.get(), mouse_shape->image()->data(),
704af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org                size * sizeof(uint32_t)) == 0;
714af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org}
724af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
734af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org}  // namespace
744af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
752df89c0c8b6416c69d70da5f29615733986a0716sergeyu@chromium.orgTEST(MouseCursorTest, MatchCursors) {
764af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  EXPECT_TRUE(ConvertToMouseShapeAndCompare(IDD_CURSOR1_24BPP,
774af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org                                            IDD_CURSOR1_32BPP));
784af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
794af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  EXPECT_TRUE(ConvertToMouseShapeAndCompare(IDD_CURSOR1_8BPP,
804af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org                                            IDD_CURSOR1_32BPP));
814af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
824af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  EXPECT_TRUE(ConvertToMouseShapeAndCompare(IDD_CURSOR2_1BPP,
834af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org                                            IDD_CURSOR2_32BPP));
844af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
854af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org  EXPECT_TRUE(ConvertToMouseShapeAndCompare(IDD_CURSOR3_4BPP,
864af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org                                            IDD_CURSOR3_32BPP));
874af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org}
884af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org
894af0878e57b54c5800e87de239477df1a45f1033alexeypa@chromium.org}  // namespace webrtc
90