| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "webkit/plugins/npapi/test/plugin_windowed_test.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "webkit/plugins/npapi/test/plugin_client.h" | |
| 9 | |
| 10 namespace NPAPIClient { | |
| 11 | |
| 12 WindowedPluginTest::WindowedPluginTest(NPP id, NPNetscapeFuncs *host_functions) | |
| 13 : PluginTest(id, host_functions), | |
| 14 window_(NULL), done_(false) { | |
| 15 } | |
| 16 | |
| 17 WindowedPluginTest::~WindowedPluginTest() { | |
| 18 if (window_) | |
| 19 DestroyWindow(window_); | |
| 20 } | |
| 21 | |
| 22 NPError WindowedPluginTest::SetWindow(NPWindow* pNPWindow) { | |
| 23 if (pNPWindow->window == NULL) | |
| 24 return NPERR_NO_ERROR; | |
| 25 | |
| 26 if (test_name() == "create_instance_in_paint" && test_id() == "2") { | |
| 27 SignalTestCompleted(); | |
| 28 return NPERR_NO_ERROR; | |
| 29 } | |
| 30 | |
| 31 if (window_) | |
| 32 return NPERR_NO_ERROR; | |
| 33 | |
| 34 HWND parent = reinterpret_cast<HWND>(pNPWindow->window); | |
| 35 if (!pNPWindow || !::IsWindow(parent)) { | |
| 36 SetError("Invalid arguments passed in"); | |
| 37 return NPERR_INVALID_PARAM; | |
| 38 } | |
| 39 | |
| 40 if ((test_name() == "create_instance_in_paint" && test_id() == "1") || | |
| 41 test_name() == "alert_in_window_message") { | |
| 42 static ATOM window_class = 0; | |
| 43 if (!window_class) { | |
| 44 WNDCLASSEX wcex; | |
| 45 wcex.cbSize = sizeof(WNDCLASSEX); | |
| 46 wcex.style = CS_DBLCLKS; | |
| 47 wcex.lpfnWndProc = &NPAPIClient::WindowedPluginTest::WindowProc; | |
| 48 wcex.cbClsExtra = 0; | |
| 49 wcex.cbWndExtra = 0; | |
| 50 wcex.hInstance = GetModuleHandle(NULL); | |
| 51 wcex.hIcon = 0; | |
| 52 wcex.hCursor = 0; | |
| 53 wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW+1); | |
| 54 wcex.lpszMenuName = 0; | |
| 55 wcex.lpszClassName = L"CreateInstanceInPaintTestWindowClass"; | |
| 56 wcex.hIconSm = 0; | |
| 57 window_class = RegisterClassEx(&wcex); | |
| 58 } | |
| 59 | |
| 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); | |
| 65 DCHECK(window_); | |
| 66 // TODO: this propery leaks. | |
| 67 ::SetProp(window_, L"Plugin_Instance", this); | |
| 68 } | |
| 69 | |
| 70 return NPERR_NO_ERROR; | |
| 71 } | |
| 72 | |
| 73 NPError WindowedPluginTest::Destroy() { | |
| 74 if (test_name() != "ensure_scripting_works_in_destroy") | |
| 75 return NPERR_NO_ERROR; | |
| 76 | |
| 77 // Bug 23706: ensure that scripting works with no asserts. | |
| 78 NPObject *window_obj = NULL; | |
| 79 HostFunctions()->getvalue(id(), NPNVWindowNPObject,&window_obj); | |
| 80 | |
| 81 if (!window_obj) { | |
| 82 SetError("Failed to get NPObject for plugin instance"); | |
| 83 } else { | |
| 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()); | |
| 89 | |
| 90 NPVariant result_var; | |
| 91 bool result = HostFunctions()->evaluate( | |
| 92 id(), window_obj, &script_string, &result_var); | |
| 93 if (!result || | |
| 94 result_var.type != NPVariantType_Double || | |
| 95 result_var.value.doubleValue != 42.0) { | |
| 96 SetError("Failed to script during NPP_Destroy"); | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 SignalTestCompleted(); | |
| 101 return NPERR_NO_ERROR; | |
| 102 } | |
| 103 | |
| 104 void WindowedPluginTest::CallJSFunction( | |
| 105 WindowedPluginTest* this_ptr, const char* function) { | |
| 106 NPIdentifier function_id = this_ptr->HostFunctions()->getstringidentifier( | |
| 107 function); | |
| 108 | |
| 109 NPObject *window_obj = NULL; | |
| 110 this_ptr->HostFunctions()->getvalue( | |
| 111 this_ptr->id(), NPNVWindowNPObject, &window_obj); | |
| 112 | |
| 113 NPVariant rv; | |
| 114 this_ptr->HostFunctions()->invoke( | |
| 115 this_ptr->id(), window_obj, function_id, NULL, 0, &rv); | |
| 116 } | |
| 117 | |
| 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")); | |
| 123 | |
| 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"); | |
| 136 } | |
| 137 | |
| 138 if (this_ptr->done_) { | |
| 139 ::RemoveProp(window, L"Plugin_Instance"); | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 return DefWindowProc(window, message, wparam, lparam); | |
| 144 } | |
| 145 | |
| 146 } // namespace NPAPIClient | |
| OLD | NEW |