1 // Copyright 2014 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.
6 #include "public/web/WebHelperPlugin.h"
8 #include "public/web/WebFrameClient.h"
9 #include "public/web/WebLocalFrame.h"
10 #include "web/tests/FakeWebPlugin.h"
11 #include "web/tests/FrameTestHelpers.h"
12 #include <gtest/gtest.h>
18 class FakePlaceholderWebPlugin : public FakeWebPlugin {
20 FakePlaceholderWebPlugin(WebFrame* frame, const WebPluginParams& params)
21 : FakeWebPlugin(frame, params)
24 virtual ~FakePlaceholderWebPlugin() { }
26 virtual bool isPlaceholder() OVERRIDE { return true; }
29 class WebHelperPluginFrameClient : public FrameTestHelpers::TestWebFrameClient {
31 WebHelperPluginFrameClient() : m_createPlaceholder(false) { }
32 virtual ~WebHelperPluginFrameClient() { }
34 virtual WebPlugin* createPlugin(WebLocalFrame* frame, const WebPluginParams& params) OVERRIDE
36 return m_createPlaceholder ? new FakePlaceholderWebPlugin(frame, params) : new FakeWebPlugin(frame, params);
39 void setCreatePlaceholder(bool createPlaceholder) { m_createPlaceholder = createPlaceholder; }
42 bool m_createPlaceholder;
45 class WebHelperPluginTest : public testing::Test {
47 virtual void SetUp() OVERRIDE
49 m_helper.initializeAndLoad("about:blank", false, &m_frameClient);
53 void destroyHelperPlugin()
56 // WebHelperPlugin is destroyed by a task posted to the message loop.
57 FrameTestHelpers::runPendingTasks();
60 FrameTestHelpers::WebViewHelper m_helper;
61 WebHelperPluginFrameClient m_frameClient;
62 OwnPtr<WebHelperPlugin> m_plugin;
65 TEST_F(WebHelperPluginTest, CreateAndDestroyAfterWebViewDestruction)
67 m_plugin = adoptPtr(WebHelperPlugin::create("hello", m_helper.webView()->mainFrame()->toWebLocalFrame()));
68 EXPECT_TRUE(m_plugin);
69 EXPECT_TRUE(m_plugin->getPlugin());
72 destroyHelperPlugin();
75 TEST_F(WebHelperPluginTest, CreateAndDestroyBeforeWebViewDestruction)
77 m_plugin = adoptPtr(WebHelperPlugin::create("hello", m_helper.webView()->mainFrame()->toWebLocalFrame()));
78 EXPECT_TRUE(m_plugin);
79 EXPECT_TRUE(m_plugin->getPlugin());
81 destroyHelperPlugin();
85 TEST_F(WebHelperPluginTest, CreateFailsWithPlaceholder)
87 m_frameClient.setCreatePlaceholder(true);
89 m_plugin = adoptPtr(WebHelperPlugin::create("hello", m_helper.webView()->mainFrame()->toWebLocalFrame()));
90 EXPECT_EQ(0, m_plugin.get());