OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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. | |
4 | |
5 #include "webkit/glue/plugins/test/plugin_windowed_test.h" | |
6 #include "webkit/glue/plugins/test/plugin_client.h" | |
7 | |
8 namespace NPAPIClient { | |
9 | |
10 WindowedPluginTest::WindowedPluginTest(NPP id, NPNetscapeFuncs *host_functions) | |
11 : PluginTest(id, host_functions), | |
12 window_(NULL), done_(false) { | |
13 } | |
14 | |
15 WindowedPluginTest::~WindowedPluginTest() { | |
16 if (window_) | |
17 DestroyWindow(window_); | |
18 } | |
19 | |
20 NPError WindowedPluginTest::SetWindow(NPWindow* pNPWindow) { | |
21 if (pNPWindow->window == NULL) | |
22 return NPERR_NO_ERROR; | |
23 | |
24 if (test_name() == "create_instance_in_paint" && test_id() == "2") { | |
25 SignalTestCompleted(); | |
26 return NPERR_NO_ERROR; | |
27 } | |
28 | |
29 if (window_) | |
30 return NPERR_NO_ERROR; | |
31 | |
32 HWND parent = reinterpret_cast<HWND>(pNPWindow->window); | |
33 if (!pNPWindow || !::IsWindow(parent)) { | |
34 SetError("Invalid arguments passed in"); | |
35 return NPERR_INVALID_PARAM; | |
36 } | |
37 | |
38 if ((test_name() == "create_instance_in_paint" && test_id() == "1") || | |
39 test_name() == "alert_in_window_message" || | |
40 test_name() == "invoke_js_function_on_create") { | |
41 static ATOM window_class = 0; | |
42 if (!window_class) { | |
43 WNDCLASSEX wcex; | |
44 wcex.cbSize = sizeof(WNDCLASSEX); | |
45 wcex.style = CS_DBLCLKS; | |
46 wcex.lpfnWndProc = &NPAPIClient::WindowedPluginTest::WindowProc; | |
47 wcex.cbClsExtra = 0; | |
48 wcex.cbWndExtra = 0; | |
49 wcex.hInstance = GetModuleHandle(NULL); | |
50 wcex.hIcon = 0; | |
51 wcex.hCursor = 0; | |
52 wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW+1); | |
53 wcex.lpszMenuName = 0; | |
54 wcex.lpszClassName = L"CreateInstanceInPaintTestWindowClass"; | |
55 wcex.hIconSm = 0; | |
56 window_class = RegisterClassEx(&wcex); | |
57 } | |
58 | |
59 window_ = CreateWindowEx( | |
60 WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR, | |
61 MAKEINTATOM(window_class), 0, | |
62 WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE , | |
63 0, 0, 100, 100, parent, 0, GetModuleHandle(NULL), 0); | |
64 DCHECK(window_); | |
65 // TODO: this propery leaks. | |
66 ::SetProp(window_, L"Plugin_Instance", this); | |
67 } | |
68 | |
69 return NPERR_NO_ERROR; | |
70 } | |
71 | |
72 NPError WindowedPluginTest::Destroy() { | |
73 if (test_name() != "ensure_scripting_works_in_destroy") | |
74 return NPERR_NO_ERROR; | |
75 | |
76 // Bug 23706: ensure that scripting works with no asserts. | |
77 NPObject *window_obj = NULL; | |
78 HostFunctions()->getvalue(id(), NPNVWindowNPObject,&window_obj); | |
79 | |
80 if (!window_obj) { | |
81 SetError("Failed to get NPObject for plugin instance"); | |
82 } else { | |
83 std::string script = "javascript:GetMagicNumber()"; | |
84 NPString script_string; | |
85 script_string.UTF8Characters = script.c_str(); | |
86 script_string.UTF8Length = | |
87 static_cast<unsigned int>(script.length()); | |
88 | |
89 NPVariant result_var; | |
90 bool result = HostFunctions()->evaluate( | |
91 id(), window_obj, &script_string, &result_var); | |
92 if (!result || | |
93 result_var.type != NPVariantType_Double || | |
94 result_var.value.doubleValue != 42.0) { | |
95 SetError("Failed to script during NPP_Destroy"); | |
96 } | |
97 } | |
98 | |
99 SignalTestCompleted(); | |
100 return NPERR_NO_ERROR; | |
101 } | |
102 | |
103 void WindowedPluginTest::CallJSFunction( | |
104 WindowedPluginTest* this_ptr, const char* function) { | |
105 NPIdentifier function_id = this_ptr->HostFunctions()->getstringidentifier( | |
106 function); | |
107 | |
108 NPObject *window_obj = NULL; | |
109 this_ptr->HostFunctions()->getvalue( | |
110 this_ptr->id(), NPNVWindowNPObject, &window_obj); | |
111 | |
112 NPVariant rv; | |
113 this_ptr->HostFunctions()->invoke( | |
114 this_ptr->id(), window_obj, function_id, NULL, 0, &rv); | |
115 } | |
116 | |
117 LRESULT CALLBACK WindowedPluginTest::WindowProc( | |
118 HWND window, UINT message, WPARAM wparam, LPARAM lparam) { | |
119 WindowedPluginTest* this_ptr = | |
120 reinterpret_cast<WindowedPluginTest*> | |
121 (::GetProp(window, L"Plugin_Instance")); | |
122 | |
123 if (this_ptr && !this_ptr->done_) { | |
124 if (this_ptr->test_name() == "create_instance_in_paint" && | |
125 message == WM_PAINT) { | |
126 this_ptr->done_ = true; | |
127 CallJSFunction(this_ptr, "CreateNewInstance"); | |
128 } else if (this_ptr->test_name() == "alert_in_window_message" && | |
129 message == WM_PAINT) { | |
130 this_ptr->done_ = true; | |
131 // We call this function twice as we want to display two alerts | |
132 // and verify that we don't hang the browser. | |
133 CallJSFunction(this_ptr, "CallAlert"); | |
134 CallJSFunction(this_ptr, "CallAlert"); | |
135 } else if (this_ptr->test_name() == | |
136 "invoke_js_function_on_create" && | |
137 message == WM_PAINT) { | |
138 this_ptr->done_ = true; | |
139 CallJSFunction(this_ptr, "PluginCreated"); | |
140 } | |
141 | |
142 if (this_ptr->done_) { | |
143 ::RemoveProp(window, L"Plugin_Instance"); | |
144 } | |
145 } | |
146 | |
147 return DefWindowProc(window, message, wparam, lparam); | |
148 } | |
149 | |
150 } // namespace NPAPIClient | |
OLD | NEW |