// 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 #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; // Provides functionality for creating plugin tests. class PluginTest : public TestShellTest { public: PluginTest() { std::wstring current_directory; PathService::Get(base::DIR_EXE, ¤t_directory); plugin_src_ = current_directory + L"\\npapi_test_plugin.dll"; CHECK(file_util::PathExists(plugin_src_)); plugin_file_path_ = current_directory + L"\\plugins"; ::CreateDirectory(plugin_file_path_.c_str(), NULL); plugin_file_path_ += L"\\npapi_test_plugin.dll"; } void CopyTestPlugin() { ASSERT_TRUE(CopyFile(plugin_src_.c_str(), plugin_file_path_.c_str(), FALSE)); } void DeleteTestPlugin() { ::DeleteFile(plugin_file_path_.c_str()); } std::wstring plugin_src_; std::wstring plugin_file_path_; }; // Tests navigator.plugins.refresh() works. TEST_F(PluginTest, Refresh) { std::string html = "\
Test running....
\ \ "; 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(); } TEST_F(PluginTest, DefaultPluginLoadTest) { std::string html = "\
Test running....
\ \
\ \ \ \
\ "; 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)); } // 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. }