1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "PluginTest.h"
27
28#include "PluginObject.h"
29
30using namespace std;
31
32// The plugin's window's window region should be set to the plugin's clip rect.
33
34class WindowRegionIsSetToClipRect : public PluginTest {
35public:
36    WindowRegionIsSetToClipRect(NPP, const string& identifier);
37
38private:
39    virtual NPError NPP_SetWindow(NPP, NPWindow*);
40
41    bool m_didReceiveInitialSetWindowCall;
42};
43
44static PluginTest::Register<WindowRegionIsSetToClipRect> registrar("window-region-is-set-to-clip-rect");
45
46WindowRegionIsSetToClipRect::WindowRegionIsSetToClipRect(NPP npp, const string& identifier)
47    : PluginTest(npp, identifier)
48    , m_didReceiveInitialSetWindowCall(false)
49{
50}
51
52NPError WindowRegionIsSetToClipRect::NPP_SetWindow(NPP instance, NPWindow* window)
53{
54    if (m_didReceiveInitialSetWindowCall)
55        return NPERR_NO_ERROR;
56    m_didReceiveInitialSetWindowCall = true;
57
58    if (window->type != NPWindowTypeWindow) {
59        pluginLog(instance, "window->type should be NPWindowTypeWindow but was %d", window->type);
60        return NPERR_GENERIC_ERROR;
61    }
62
63    HWND hwnd = reinterpret_cast<HWND>(window->window);
64
65    RECT regionRect;
66    if (::GetWindowRgnBox(hwnd, &regionRect) == ERROR) {
67        pluginLog(instance, "::GetWindowRgnBox failed with error %u", ::GetLastError());
68        return NPERR_GENERIC_ERROR;
69    }
70
71    // This expected rect is based on the layout of window-region-is-set-to-clip-rect.html.
72    RECT expectedRect = { 50, 50, 100, 100 };
73    if (!::EqualRect(&regionRect, &expectedRect)) {
74        pluginLog(instance, "Expected region rect {left=%u, top=%u, right=%u, bottom=%u}, but got {left=%d, top=%d, right=%d, bottom=%d}", expectedRect.left, expectedRect.top, expectedRect.right, expectedRect.bottom, regionRect.left, regionRect.top, regionRect.right, regionRect.bottom);
75        return NPERR_GENERIC_ERROR;
76    }
77
78    pluginLog(instance, "PASS: Plugin's window's window region has been set as expected");
79
80    // While we're here, check that our window class doesn't have the CS_PARENTDC style, which
81    // defeats clipping by ignoring the window region and always clipping to the parent window.
82    // FIXME: It would be nice to have a pixel test that shows that we're
83    // getting clipped correctly, but unfortunately window regions are ignored
84    // during WM_PRINT (see <http://webkit.org/b/49034>).
85    wchar_t className[512];
86    if (!::GetClassNameW(hwnd, className, _countof(className))) {
87        pluginLog(instance, "::GetClassName failed with error %u", ::GetLastError());
88        return NPERR_GENERIC_ERROR;
89    }
90
91#ifdef DEBUG_ALL
92    const wchar_t webKitDLLName[] = L"WebKit_debug.dll";
93#else
94    const wchar_t webKitDLLName[] = L"WebKit.dll";
95#endif
96    HMODULE webKitModule = ::GetModuleHandleW(webKitDLLName);
97    if (!webKitModule) {
98        pluginLog(instance, "::GetModuleHandleW failed with error %u", ::GetLastError());
99        return NPERR_GENERIC_ERROR;
100    }
101
102    WNDCLASSW wndClass;
103    if (!::GetClassInfoW(webKitModule, className, &wndClass)) {
104        pluginLog(instance, "::GetClassInfoW failed with error %u", ::GetLastError());
105        return NPERR_GENERIC_ERROR;
106    }
107
108    if (wndClass.style & CS_PARENTDC)
109        pluginLog(instance, "FAIL: Plugin's window's class has the CS_PARENTDC style, which will defeat clipping");
110    else
111        pluginLog(instance, "PASS: Plugin's window's class does not have the CS_PARENTDC style");
112
113    return NPERR_NO_ERROR;
114}
115