1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/test/plugin/plugin_windowed_test.h"
7 #include "base/logging.h"
8 #include "content/test/plugin/plugin_client.h"
10 namespace NPAPIClient
{
12 WindowedPluginTest::WindowedPluginTest(NPP id
, NPNetscapeFuncs
*host_functions
)
13 : PluginTest(id
, host_functions
),
14 window_(NULL
), done_(false) {
17 WindowedPluginTest::~WindowedPluginTest() {
19 DestroyWindow(window_
);
22 NPError
WindowedPluginTest::SetWindow(NPWindow
* pNPWindow
) {
23 if (pNPWindow
->window
== NULL
)
24 return NPERR_NO_ERROR
;
26 if (test_name() == "create_instance_in_paint" && test_id() == "2") {
27 SignalTestCompleted();
28 return NPERR_NO_ERROR
;
32 return NPERR_NO_ERROR
;
34 HWND parent
= reinterpret_cast<HWND
>(pNPWindow
->window
);
35 if (!::IsWindow(parent
)) {
36 SetError("Invalid arguments passed in");
37 return NPERR_INVALID_PARAM
;
40 if ((test_name() == "create_instance_in_paint" && test_id() == "1") ||
41 test_name() == "alert_in_window_message" ||
42 test_name() == "set_title_in_paint" ||
43 test_name() == "set_title_in_set_window_and_paint") {
44 static ATOM window_class
= 0;
47 wcex
.cbSize
= sizeof(WNDCLASSEX
);
48 wcex
.style
= CS_DBLCLKS
;
49 wcex
.lpfnWndProc
= &NPAPIClient::WindowedPluginTest::WindowProc
;
52 wcex
.hInstance
= GetModuleHandle(NULL
);
55 wcex
.hbrBackground
= reinterpret_cast<HBRUSH
>(COLOR_WINDOW
+1);
56 wcex
.lpszMenuName
= 0;
57 wcex
.lpszClassName
= L
"CreateInstanceInPaintTestWindowClass";
59 window_class
= RegisterClassEx(&wcex
);
62 window_
= CreateWindowEx(
63 WS_EX_LEFT
| WS_EX_LTRREADING
| WS_EX_RIGHTSCROLLBAR
,
64 MAKEINTATOM(window_class
), 0,
65 WS_CHILD
| WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
| WS_VISIBLE
,
66 0, 0, 100, 100, parent
, 0, GetModuleHandle(NULL
), 0);
68 // TODO: this propery leaks.
69 ::SetProp(window_
, L
"Plugin_Instance", this);
72 if (test_name() == "set_title_in_set_window_and_paint")
73 CallJSFunction(this, "PluginCreated");
75 return NPERR_NO_ERROR
;
78 NPError
WindowedPluginTest::Destroy() {
79 if (test_name() != "ensure_scripting_works_in_destroy")
80 return NPERR_NO_ERROR
;
82 // Bug 23706: ensure that scripting works with no asserts.
83 NPObject
*window_obj
= NULL
;
84 HostFunctions()->getvalue(id(), NPNVWindowNPObject
,&window_obj
);
87 SetError("Failed to get NPObject for plugin instance");
89 std::string script
= "javascript:GetMagicNumber()";
90 NPString script_string
;
91 script_string
.UTF8Characters
= script
.c_str();
92 script_string
.UTF8Length
=
93 static_cast<unsigned int>(script
.length());
96 bool result
= HostFunctions()->evaluate(
97 id(), window_obj
, &script_string
, &result_var
);
99 result_var
.type
!= NPVariantType_Double
||
100 result_var
.value
.doubleValue
!= 42.0) {
101 SetError("Failed to script during NPP_Destroy");
105 SignalTestCompleted();
106 return NPERR_NO_ERROR
;
109 void WindowedPluginTest::CallJSFunction(
110 WindowedPluginTest
* this_ptr
, const char* function
) {
111 NPIdentifier function_id
= this_ptr
->HostFunctions()->getstringidentifier(
114 NPObject
*window_obj
= NULL
;
115 this_ptr
->HostFunctions()->getvalue(
116 this_ptr
->id(), NPNVWindowNPObject
, &window_obj
);
119 this_ptr
->HostFunctions()->invoke(
120 this_ptr
->id(), window_obj
, function_id
, NULL
, 0, &rv
);
123 LRESULT CALLBACK
WindowedPluginTest::WindowProc(
124 HWND window
, UINT message
, WPARAM wparam
, LPARAM lparam
) {
125 WindowedPluginTest
* this_ptr
=
126 reinterpret_cast<WindowedPluginTest
*>
127 (::GetProp(window
, L
"Plugin_Instance"));
129 if (message
== WM_PAINT
) {
131 HDC hdc
= BeginPaint(window
, &ps
);
132 HBRUSH brush
= CreateSolidBrush(RGB(255, 0, 0));
133 HGDIOBJ orig
= SelectObject(hdc
, brush
);
135 GetClientRect(window
, &r
);
136 Rectangle(hdc
, 0, 0, r
.right
, r
.bottom
);
137 SelectObject(hdc
, orig
); // restore
139 EndPaint(window
, &ps
);
142 if (this_ptr
&& !this_ptr
->done_
) {
143 if (this_ptr
->test_name() == "create_instance_in_paint" &&
144 message
== WM_PAINT
) {
145 this_ptr
->done_
= true;
146 CallJSFunction(this_ptr
, "CreateNewInstance");
147 } else if (this_ptr
->test_name() == "alert_in_window_message" &&
148 message
== WM_PAINT
) {
149 this_ptr
->done_
= true;
150 // We call this function twice as we want to display two alerts
151 // and verify that we don't hang the browser.
152 CallJSFunction(this_ptr
, "CallAlert");
153 CallJSFunction(this_ptr
, "CallAlert");
154 } else if (this_ptr
->test_name() == "set_title_in_paint" &&
155 message
== WM_PAINT
) {
156 this_ptr
->done_
= true;
157 CallJSFunction(this_ptr
, "SetTitle");
158 } else if (this_ptr
->test_name() == "set_title_in_set_window_and_paint" &&
159 message
== WM_PAINT
) {
160 this_ptr
->done_
= true;
161 CallJSFunction(this_ptr
, "PluginShown");
164 if (this_ptr
->done_
) {
165 ::RemoveProp(window
, L
"Plugin_Instance");
169 return DefWindowProc(window
, message
, wparam
, lparam
);
172 } // namespace NPAPIClient