summaryrefslogtreecommitdiffstats
path: root/webkit/tools/test_shell/plugin_tests.cc
blob: eae50e060f691a3c0f3dc0deda5908ad93d95086 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) 2006-2008 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 <string>

#include "base/file_path.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/string_util.h"
#include "net/base/escape.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/WebKit/chromium/public/WebScriptSource.h"
#include "third_party/WebKit/WebKit/chromium/public/WebInputEvent.h"
#include "webkit/glue/webframe.h"
#include "webkit/glue/webview.h"
#include "webkit/tools/test_shell/test_shell.h"
#include "webkit/tools/test_shell/test_shell_test.h"

using WebKit::WebScriptSource;
using WebKit::WebString;

#if defined(OS_WIN)
#define TEST_PLUGIN_NAME "npapi_test_plugin.dll"
#elif defined(OS_MACOSX)
#define TEST_PLUGIN_NAME "npapi_test_plugin.bundle"
#elif defined(OS_LINUX)
#define TEST_PLUGIN_NAME "npapi_test_plugin.so"
#endif

// Provides functionality for creating plugin tests.
class PluginTest : public TestShellTest {
 public:
  PluginTest() {
    FilePath executable_directory;
    PathService::Get(base::DIR_EXE, &executable_directory);
    plugin_src_ = executable_directory.AppendASCII(TEST_PLUGIN_NAME);
    CHECK(file_util::PathExists(plugin_src_));

    plugin_file_path_ = executable_directory.AppendASCII("plugins");
    file_util::CreateDirectory(plugin_file_path_);

    plugin_file_path_ = plugin_file_path_.AppendASCII(TEST_PLUGIN_NAME);
  }

  void CopyTestPlugin() {
    ASSERT_TRUE(file_util::CopyDirectory(plugin_src_, plugin_file_path_, true));
  }

  void DeleteTestPlugin() {
    file_util::Delete(plugin_file_path_, true);
  }

  FilePath plugin_src_;
  FilePath plugin_file_path_;
};

// Tests navigator.plugins.refresh() works.
TEST_F(PluginTest, Refresh) {
  std::string html = "\
      <div id='result'>Test running....</div>\
      <script>\
      function check() {\
      var l = navigator.plugins.length;\
      var result = document.getElementById('result');\
      for(var i = 0; i < l; i++) {\
        if (navigator.plugins[i].filename == '" TEST_PLUGIN_NAME "') {\
          result.innerHTML = 'DONE';\
          break;\
        }\
      }\
      \
      if (result.innerHTML != 'DONE')\
        result.innerHTML = 'FAIL';\
      }\
      </script>\
      ";

  WebScriptSource call_check(
      WebString::fromUTF8("check();"));
  WebScriptSource refresh(
      WebString::fromUTF8("navigator.plugins.refresh(false)"));

  // Remove any leftover from previous tests if they exist.  We must also
  // refresh WebKit's plugin cache since it might have had an entry for the
  // test plugin from a previous test.
  DeleteTestPlugin();
  ASSERT_FALSE(file_util::PathExists(plugin_file_path_));
  test_shell_->webView()->GetMainFrame()->ExecuteScript(refresh);

  test_shell_->webView()->GetMainFrame()->LoadHTMLString(
      html, GURL("about:blank"));
  test_shell_->WaitTestFinished();

  std::wstring text;
  test_shell_->webView()->GetMainFrame()->ExecuteScript(call_check);
  test_shell_->webView()->GetMainFrame()->GetContentAsPlainText(10000, &text);
  ASSERT_EQ(text, L"FAIL");

  CopyTestPlugin();

  test_shell_->webView()->GetMainFrame()->ExecuteScript(refresh);
  test_shell_->webView()->GetMainFrame()->ExecuteScript(call_check);
  test_shell_->webView()->GetMainFrame()->GetContentAsPlainText(10000, &text);
  ASSERT_EQ(text, L"DONE");

  DeleteTestPlugin();
}

#if defined(OS_WIN)
// TODO(port): Reenable on mac and linux once they have working default plugins.
TEST_F(PluginTest, DefaultPluginLoadTest) {
  std::string html = "\
      <div id='result'>Test running....</div>\
      <script>\
      function onSuccess() {\
        var result = document.getElementById('result');\
        result.innerHTML = 'DONE';\
      }\
      </script>\
      <DIV ID=PluginDiv>\
      <object classid=\"clsid:9E8BC6CE-AF35-400c-ABF6-A3F746A1871D\">\
      <embed type=\"application/chromium-test-default-plugin\"\
        mode=\"np_embed\"\
      ></embed>\
      </object>\
      </DIV>\
      ";

  test_shell_->webView()->GetMainFrame()->LoadHTMLString(
      html, GURL("about:blank"));
  test_shell_->WaitTestFinished();

  std::wstring text;
  test_shell_->webView()->GetMainFrame()->GetContentAsPlainText(10000, &text);

  ASSERT_EQ(true, StartsWith(text, L"DONE", true));
}
#endif

// Tests that if a frame is deleted as a result of calling NPP_HandleEvent, we
// don't crash.
TEST_F(PluginTest, DeleteFrameDuringEvent) {
  FilePath test_html = data_dir_;
  test_html = test_html.AppendASCII("plugins");
  test_html = test_html.AppendASCII("delete_frame.html");
  test_shell_->LoadURL(test_html.ToWStringHack().c_str());
  test_shell_->WaitTestFinished();

  WebKit::WebMouseEvent input;
  input.button = WebKit::WebMouseEvent::ButtonLeft;
  input.x = 50;
  input.y = 50;
  input.type = WebKit::WebInputEvent::MouseUp;
  test_shell_->webView()->HandleInputEvent(&input);

  // No crash means we passed.
}

#if defined(OS_WIN)
// Tests that a hidden plugin is not shown.  See http://crbug.com/8927
TEST_F(PluginTest, HiddenPlugin) {
  CopyTestPlugin();

  FilePath test_html = data_dir_;
  test_html = test_html.AppendASCII("plugins");
  test_html = test_html.AppendASCII("hidden_plugin.html");
  test_shell_->LoadURL(test_html.ToWStringHack().c_str());
  test_shell_->WaitTestFinished();

  std::wstring text;
  test_shell_->webView()->GetMainFrame()->GetContentAsPlainText(10000, &text);
  ASSERT_EQ(true, StartsWith(text, L"DONE", true));
}
#endif