Refactor management of overview window copy lifetime into a separate class.
[chromium-blink-merge.git] / content / test / plugin / plugin_windowed_test.cc
bloba7958575cec99e3789e36c6ca1f14105ef2ddd88
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() {
18 if (window_)
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;
31 if (window_)
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;
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);
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);
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);
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());
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");
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(
107 function);
109 NPObject *window_obj = NULL;
110 this_ptr->HostFunctions()->getvalue(
111 this_ptr->id(), NPNVWindowNPObject, &window_obj);
113 NPVariant rv;
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