summaryrefslogtreecommitdiffstats
path: root/webkit/glue/plugins/test/plugin_windowed_test.cc
blob: 5ae8e30cfbaa7898ad8a5cb05268736fa285c6e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "webkit/glue/plugins/test/plugin_windowed_test.h"
#include "webkit/glue/plugins/test/plugin_client.h"

namespace NPAPIClient {

WindowedPluginTest::WindowedPluginTest(NPP id, NPNetscapeFuncs *host_functions)
    : PluginTest(id, host_functions),
      window_(NULL), done_(false) {
}

WindowedPluginTest::~WindowedPluginTest() {
  if (window_)
    DestroyWindow(window_);
}

NPError WindowedPluginTest::SetWindow(NPWindow* pNPWindow) {
  if (pNPWindow->window == NULL)
    return NPERR_NO_ERROR;

  if (test_name() == "create_instance_in_paint" && test_id() == "2") {
    SignalTestCompleted();
    return NPERR_NO_ERROR;
  }

  if (window_)
    return NPERR_NO_ERROR;

  HWND parent = reinterpret_cast<HWND>(pNPWindow->window);
  if (!pNPWindow || !::IsWindow(parent)) {
    SetError("Invalid arguments passed in");
    return NPERR_INVALID_PARAM;
  }

  if ((test_name() == "create_instance_in_paint" && test_id() == "1") ||
      test_name() == "alert_in_window_message") {
    static ATOM window_class = 0;
    if (!window_class) {
      WNDCLASSEX wcex;
      wcex.cbSize         = sizeof(WNDCLASSEX);
      wcex.style          = CS_DBLCLKS;
      wcex.lpfnWndProc    = &NPAPIClient::WindowedPluginTest::WindowProc;
      wcex.cbClsExtra     = 0;
      wcex.cbWndExtra     = 0;
      wcex.hInstance      = GetModuleHandle(NULL);
      wcex.hIcon          = 0;
      wcex.hCursor        = 0;
      wcex.hbrBackground  = reinterpret_cast<HBRUSH>(COLOR_WINDOW+1);
      wcex.lpszMenuName   = 0;
      wcex.lpszClassName  = L"CreateInstanceInPaintTestWindowClass";
      wcex.hIconSm        = 0;
      window_class = RegisterClassEx(&wcex);
    }

    window_ = CreateWindowEx(
        WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR,
        MAKEINTATOM(window_class), 0,
        WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE ,
        0, 0, 100, 100, parent, 0, GetModuleHandle(NULL), 0);
    DCHECK(window_);
    ::SetProp(window_, L"Plugin_Instance", this);
  }

  return NPERR_NO_ERROR;
}

NPError WindowedPluginTest::Destroy() {
  if (test_name() != "ensure_scripting_works_in_destroy")
    return NPERR_NO_ERROR;

  // Bug 23706: ensure that scripting works with no asserts.
  NPObject *window_obj = NULL;
  HostFunctions()->getvalue(id(), NPNVWindowNPObject,&window_obj);

  if (!window_obj) {
    SetError("Failed to get NPObject for plugin instance");
  } else {
    std::string script = "javascript:GetMagicNumber()";
    NPString script_string;
    script_string.UTF8Characters = script.c_str();
    script_string.UTF8Length =
        static_cast<unsigned int>(script.length());

    NPVariant result_var;
    bool result = HostFunctions()->evaluate(
        id(), window_obj, &script_string, &result_var);
    if (!result ||
        result_var.type != NPVariantType_Int32 ||
        result_var.value.intValue != 42) {
      SetError("Failed to script during NPP_Destroy");
    }
  }

  SignalTestCompleted();
  return NPERR_NO_ERROR;
}

void WindowedPluginTest::CallJSFunction(
    WindowedPluginTest* this_ptr, const char* function) {
  NPIdentifier function_id = this_ptr->HostFunctions()->getstringidentifier(
      function);

  NPObject *window_obj = NULL;
  this_ptr->HostFunctions()->getvalue(
      this_ptr->id(), NPNVWindowNPObject, &window_obj);

  NPVariant rv;
  this_ptr->HostFunctions()->invoke(
      this_ptr->id(), window_obj, function_id, NULL, 0, &rv);
}

LRESULT CALLBACK WindowedPluginTest::WindowProc(
    HWND window, UINT message, WPARAM wparam, LPARAM lparam) {
  WindowedPluginTest* this_ptr =
      reinterpret_cast<WindowedPluginTest*>
          (::GetProp(window, L"Plugin_Instance"));

  if (this_ptr && !this_ptr->done_) {
    if (this_ptr->test_name() == "create_instance_in_paint" &&
        message == WM_PAINT) {
      this_ptr->done_ = true;
      CallJSFunction(this_ptr, "CreateNewInstance");
    } else if (this_ptr->test_name() == "alert_in_window_message" &&
               message == WM_PAINT) {
      this_ptr->done_ = true;
      // We call this function twice as we want to display two alerts
      // and verify that we don't hang the browser.
      CallJSFunction(this_ptr, "CallAlert");
      CallJSFunction(this_ptr, "CallAlert");
    }
  }

  return DefWindowProc(window, message, wparam, lparam);
}

} // namespace NPAPIClient