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 static ATOM window_class
= 0;
45 wcex
.cbSize
= sizeof(WNDCLASSEX
);
46 wcex
.style
= CS_DBLCLKS
;
47 wcex
.lpfnWndProc
= &NPAPIClient::WindowedPluginTest::WindowProc
;
50 wcex
.hInstance
= GetModuleHandle(NULL
);
53 wcex
.hbrBackground
= reinterpret_cast<HBRUSH
>(COLOR_WINDOW
+1);
54 wcex
.lpszMenuName
= 0;
55 wcex
.lpszClassName
= L
"CreateInstanceInPaintTestWindowClass";
57 window_class
= RegisterClassEx(&wcex
);
60 window_
= CreateWindowEx(
61 WS_EX_LEFT
| WS_EX_LTRREADING
| WS_EX_RIGHTSCROLLBAR
,
62 MAKEINTATOM(window_class
), 0,
63 WS_CHILD
| WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
| WS_VISIBLE
,
64 0, 0, 100, 100, parent
, 0, GetModuleHandle(NULL
), 0);
66 // TODO: this propery leaks.
67 ::SetProp(window_
, L
"Plugin_Instance", this);
70 return NPERR_NO_ERROR
;
73 NPError
WindowedPluginTest::Destroy() {
74 if (test_name() != "ensure_scripting_works_in_destroy")
75 return NPERR_NO_ERROR
;
77 // Bug 23706: ensure that scripting works with no asserts.
78 NPObject
*window_obj
= NULL
;
79 HostFunctions()->getvalue(id(), NPNVWindowNPObject
,&window_obj
);
82 SetError("Failed to get NPObject for plugin instance");
84 std::string script
= "javascript:GetMagicNumber()";
85 NPString script_string
;
86 script_string
.UTF8Characters
= script
.c_str();
87 script_string
.UTF8Length
=
88 static_cast<unsigned int>(script
.length());
91 bool result
= HostFunctions()->evaluate(
92 id(), window_obj
, &script_string
, &result_var
);
94 result_var
.type
!= NPVariantType_Double
||
95 result_var
.value
.doubleValue
!= 42.0) {
96 SetError("Failed to script during NPP_Destroy");
100 SignalTestCompleted();
101 return NPERR_NO_ERROR
;
104 void WindowedPluginTest::CallJSFunction(
105 WindowedPluginTest
* this_ptr
, const char* function
) {
106 NPIdentifier function_id
= this_ptr
->HostFunctions()->getstringidentifier(
109 NPObject
*window_obj
= NULL
;
110 this_ptr
->HostFunctions()->getvalue(
111 this_ptr
->id(), NPNVWindowNPObject
, &window_obj
);
114 this_ptr
->HostFunctions()->invoke(
115 this_ptr
->id(), window_obj
, function_id
, NULL
, 0, &rv
);
118 LRESULT CALLBACK
WindowedPluginTest::WindowProc(
119 HWND window
, UINT message
, WPARAM wparam
, LPARAM lparam
) {
120 WindowedPluginTest
* this_ptr
=
121 reinterpret_cast<WindowedPluginTest
*>
122 (::GetProp(window
, L
"Plugin_Instance"));
124 if (this_ptr
&& !this_ptr
->done_
) {
125 if (this_ptr
->test_name() == "create_instance_in_paint" &&
126 message
== WM_PAINT
) {
127 this_ptr
->done_
= true;
128 CallJSFunction(this_ptr
, "CreateNewInstance");
129 } else if (this_ptr
->test_name() == "alert_in_window_message" &&
130 message
== WM_PAINT
) {
131 this_ptr
->done_
= true;
132 // We call this function twice as we want to display two alerts
133 // and verify that we don't hang the browser.
134 CallJSFunction(this_ptr
, "CallAlert");
135 CallJSFunction(this_ptr
, "CallAlert");
138 if (this_ptr
->done_
) {
139 ::RemoveProp(window
, L
"Plugin_Instance");
143 return DefWindowProc(window
, message
, wparam
, lparam
);
146 } // namespace NPAPIClient