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 "platform/testing/UnitTestHelpers.h"
9 #include "public/web/WebFrameClient.h"
10 #include "public/web/WebLocalFrame.h"
11 #include "web/tests/FakeWebPlugin.h"
12 #include "web/tests/FrameTestHelpers.h"
13 #include <gtest/gtest.h>
19 class FakePlaceholderWebPlugin
: public FakeWebPlugin
{
21 FakePlaceholderWebPlugin(WebFrame
* frame
, const WebPluginParams
& params
)
22 : FakeWebPlugin(frame
, params
)
25 ~FakePlaceholderWebPlugin() override
{}
27 bool isPlaceholder() override
{ return true; }
30 class WebHelperPluginFrameClient
: public FrameTestHelpers::TestWebFrameClient
{
32 WebHelperPluginFrameClient() : m_createPlaceholder(false) {}
33 ~WebHelperPluginFrameClient() override
{}
35 WebPlugin
* createPlugin(WebLocalFrame
* frame
, const WebPluginParams
& params
) override
37 return m_createPlaceholder
? new FakePlaceholderWebPlugin(frame
, params
) : new FakeWebPlugin(frame
, params
);
40 void setCreatePlaceholder(bool createPlaceholder
) { m_createPlaceholder
= createPlaceholder
; }
43 bool m_createPlaceholder
;
46 class WebHelperPluginTest
: public ::testing::Test
{
50 m_helper
.initializeAndLoad("about:blank", false, &m_frameClient
);
54 void destroyHelperPlugin()
57 // WebHelperPlugin is destroyed by a task posted to the message loop.
58 testing::runPendingTasks();
61 FrameTestHelpers::WebViewHelper m_helper
;
62 WebHelperPluginFrameClient m_frameClient
;
63 OwnPtr
<WebHelperPlugin
> m_plugin
;
66 TEST_F(WebHelperPluginTest
, CreateAndDestroyAfterWebViewDestruction
)
68 m_plugin
= adoptPtr(WebHelperPlugin::create("hello", m_helper
.webView()->mainFrame()->toWebLocalFrame()));
69 EXPECT_TRUE(m_plugin
);
70 EXPECT_TRUE(m_plugin
->getPlugin());
73 destroyHelperPlugin();
76 TEST_F(WebHelperPluginTest
, CreateAndDestroyBeforeWebViewDestruction
)
78 m_plugin
= adoptPtr(WebHelperPlugin::create("hello", m_helper
.webView()->mainFrame()->toWebLocalFrame()));
79 EXPECT_TRUE(m_plugin
);
80 EXPECT_TRUE(m_plugin
->getPlugin());
82 destroyHelperPlugin();
86 TEST_F(WebHelperPluginTest
, CreateFailsWithPlaceholder
)
88 m_frameClient
.setCreatePlaceholder(true);
90 m_plugin
= adoptPtr(WebHelperPlugin::create("hello", m_helper
.webView()->mainFrame()->toWebLocalFrame()));
91 EXPECT_EQ(0, m_plugin
.get());