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 "content/test/plugin/plugin_windowed_test.h" 6 7 #include "base/logging.h" 8 #include "content/test/plugin/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 (!::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 test_name() == "set_title_in_paint" || 43 test_name() == "set_title_in_set_window_and_paint") { 44 static ATOM window_class = 0; 45 if (!window_class) { 46 WNDCLASSEX wcex; 47 wcex.cbSize = sizeof(WNDCLASSEX); 48 wcex.style = CS_DBLCLKS; 49 wcex.lpfnWndProc = &NPAPIClient::WindowedPluginTest::WindowProc; 50 wcex.cbClsExtra = 0; 51 wcex.cbWndExtra = 0; 52 wcex.hInstance = GetModuleHandle(NULL); 53 wcex.hIcon = 0; 54 wcex.hCursor = 0; 55 wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW+1); 56 wcex.lpszMenuName = 0; 57 wcex.lpszClassName = L"CreateInstanceInPaintTestWindowClass"; 58 wcex.hIconSm = 0; 59 window_class = RegisterClassEx(&wcex); 60 } 61 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); 67 DCHECK(window_); 68 // TODO: this propery leaks. 69 ::SetProp(window_, L"Plugin_Instance", this); 70 } 71 72 if (test_name() == "set_title_in_set_window_and_paint") 73 CallJSFunction(this, "PluginCreated"); 74 75 return NPERR_NO_ERROR; 76 } 77 78 NPError WindowedPluginTest::Destroy() { 79 if (test_name() != "ensure_scripting_works_in_destroy") 80 return NPERR_NO_ERROR; 81 82 // Bug 23706: ensure that scripting works with no asserts. 83 NPObject *window_obj = NULL; 84 HostFunctions()->getvalue(id(), NPNVWindowNPObject,&window_obj); 85 86 if (!window_obj) { 87 SetError("Failed to get NPObject for plugin instance"); 88 } else { 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()); 94 95 NPVariant result_var; 96 bool result = HostFunctions()->evaluate( 97 id(), window_obj, &script_string, &result_var); 98 if (!result || 99 result_var.type != NPVariantType_Double || 100 result_var.value.doubleValue != 42.0) { 101 SetError("Failed to script during NPP_Destroy"); 102 } 103 } 104 105 SignalTestCompleted(); 106 return NPERR_NO_ERROR; 107 } 108 109 void WindowedPluginTest::CallJSFunction( 110 WindowedPluginTest* this_ptr, const char* function) { 111 NPIdentifier function_id = this_ptr->HostFunctions()->getstringidentifier( 112 function); 113 114 NPObject *window_obj = NULL; 115 this_ptr->HostFunctions()->getvalue( 116 this_ptr->id(), NPNVWindowNPObject, &window_obj); 117 118 NPVariant rv; 119 this_ptr->HostFunctions()->invoke( 120 this_ptr->id(), window_obj, function_id, NULL, 0, &rv); 121 } 122 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")); 128 129 if (message == WM_PAINT) { 130 PAINTSTRUCT ps; 131 HDC hdc = BeginPaint(window, &ps); 132 HBRUSH brush = CreateSolidBrush(RGB(255, 0, 0)); 133 SelectObject(hdc, brush); 134 RECT r; 135 GetClientRect(window, &r); 136 Rectangle(hdc, 0, 0, r.right, r.bottom); 137 DeleteObject(brush); 138 EndPaint(window, &ps); 139 } 140 141 if (this_ptr && !this_ptr->done_) { 142 if (this_ptr->test_name() == "create_instance_in_paint" && 143 message == WM_PAINT) { 144 this_ptr->done_ = true; 145 CallJSFunction(this_ptr, "CreateNewInstance"); 146 } else if (this_ptr->test_name() == "alert_in_window_message" && 147 message == WM_PAINT) { 148 this_ptr->done_ = true; 149 // We call this function twice as we want to display two alerts 150 // and verify that we don't hang the browser. 151 CallJSFunction(this_ptr, "CallAlert"); 152 CallJSFunction(this_ptr, "CallAlert"); 153 } else if (this_ptr->test_name() == "set_title_in_paint" && 154 message == WM_PAINT) { 155 this_ptr->done_ = true; 156 CallJSFunction(this_ptr, "SetTitle"); 157 } else if (this_ptr->test_name() == "set_title_in_set_window_and_paint" && 158 message == WM_PAINT) { 159 this_ptr->done_ = true; 160 CallJSFunction(this_ptr, "PluginShown"); 161 } 162 163 if (this_ptr->done_) { 164 ::RemoveProp(window, L"Plugin_Instance"); 165 } 166 } 167 168 return DefWindowProc(window, message, wparam, lparam); 169 } 170 171 } // namespace NPAPIClient 172