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.
4
5#include "config.h"
6#include "public/web/WebHelperPlugin.h"
7
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>
13
14namespace blink {
15
16namespace {
17
18class FakePlaceholderWebPlugin : public FakeWebPlugin {
19public:
20    FakePlaceholderWebPlugin(WebFrame* frame, const WebPluginParams& params)
21        : FakeWebPlugin(frame, params)
22    {
23    }
24    virtual ~FakePlaceholderWebPlugin() { }
25
26    virtual bool isPlaceholder() OVERRIDE { return true; }
27};
28
29class WebHelperPluginFrameClient : public FrameTestHelpers::TestWebFrameClient {
30public:
31    WebHelperPluginFrameClient() : m_createPlaceholder(false) { }
32    virtual ~WebHelperPluginFrameClient() { }
33
34    virtual WebPlugin* createPlugin(WebLocalFrame* frame, const WebPluginParams& params) OVERRIDE
35    {
36        return m_createPlaceholder ? new FakePlaceholderWebPlugin(frame, params) : new FakeWebPlugin(frame, params);
37    }
38
39    void setCreatePlaceholder(bool createPlaceholder) { m_createPlaceholder = createPlaceholder; }
40
41private:
42    bool m_createPlaceholder;
43};
44
45class WebHelperPluginTest : public testing::Test {
46protected:
47    virtual void SetUp() OVERRIDE
48    {
49        m_helper.initializeAndLoad("about:blank", false, &m_frameClient);
50    }
51
52
53    void destroyHelperPlugin()
54    {
55        m_plugin.clear();
56        // WebHelperPlugin is destroyed by a task posted to the message loop.
57        FrameTestHelpers::runPendingTasks();
58    }
59
60    FrameTestHelpers::WebViewHelper m_helper;
61    WebHelperPluginFrameClient m_frameClient;
62    OwnPtr<WebHelperPlugin> m_plugin;
63};
64
65TEST_F(WebHelperPluginTest, CreateAndDestroyAfterWebViewDestruction)
66{
67    m_plugin = adoptPtr(WebHelperPlugin::create("hello", m_helper.webView()->mainFrame()->toWebLocalFrame()));
68    EXPECT_TRUE(m_plugin);
69    EXPECT_TRUE(m_plugin->getPlugin());
70
71    m_helper.reset();
72    destroyHelperPlugin();
73}
74
75TEST_F(WebHelperPluginTest, CreateAndDestroyBeforeWebViewDestruction)
76{
77    m_plugin = adoptPtr(WebHelperPlugin::create("hello", m_helper.webView()->mainFrame()->toWebLocalFrame()));
78    EXPECT_TRUE(m_plugin);
79    EXPECT_TRUE(m_plugin->getPlugin());
80
81    destroyHelperPlugin();
82    m_helper.reset();
83}
84
85TEST_F(WebHelperPluginTest, CreateFailsWithPlaceholder)
86{
87    m_frameClient.setCreatePlaceholder(true);
88
89    m_plugin = adoptPtr(WebHelperPlugin::create("hello", m_helper.webView()->mainFrame()->toWebLocalFrame()));
90    EXPECT_EQ(0, m_plugin.get());
91}
92
93} // namespace
94
95} // namespace
96