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.
5 #include "base/strings/stringprintf.h"
6 #include "chrome/browser/apps/app_browsertest_util.h"
7 #include "chrome/test/base/ui_test_utils.h"
8 #include "content/public/test/browser_test_utils.h"
9 #include "extensions/browser/guest_view/guest_view_manager.h"
10 #include "extensions/browser/guest_view/guest_view_manager_factory.h"
11 #include "extensions/test/extension_test_message_listener.h"
15 class TestGuestViewManager
: public extensions::GuestViewManager
{
17 explicit TestGuestViewManager(content::BrowserContext
* context
)
18 : extensions::GuestViewManager(context
), web_contents_(NULL
) {}
20 content::WebContents
* WaitForGuestCreated() {
24 message_loop_runner_
= new content::MessageLoopRunner
;
25 message_loop_runner_
->Run();
30 // GuestViewManager override.
31 void AddGuest(int guest_instance_id
,
32 content::WebContents
* guest_web_contents
) override
{
33 extensions::GuestViewManager::AddGuest(guest_instance_id
,
35 web_contents_
= guest_web_contents
;
37 if (message_loop_runner_
.get())
38 message_loop_runner_
->Quit();
41 content::WebContents
* web_contents_
;
42 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
45 // Test factory for creating test instances of GuestViewManager.
46 class TestGuestViewManagerFactory
: public extensions::GuestViewManagerFactory
{
48 TestGuestViewManagerFactory() : test_guest_view_manager_(NULL
) {}
50 ~TestGuestViewManagerFactory() override
{}
52 extensions::GuestViewManager
* CreateGuestViewManager(
53 content::BrowserContext
* context
) override
{
54 return GetManager(context
);
57 TestGuestViewManager
* GetManager(content::BrowserContext
* context
) {
58 if (!test_guest_view_manager_
) {
59 test_guest_view_manager_
= new TestGuestViewManager(context
);
61 return test_guest_view_manager_
;
65 TestGuestViewManager
* test_guest_view_manager_
;
67 DISALLOW_COPY_AND_ASSIGN(TestGuestViewManagerFactory
);
72 class ExtensionViewTest
: public extensions::PlatformAppBrowserTest
{
75 extensions::GuestViewManager::set_factory_for_testing(&factory_
);
78 TestGuestViewManager
* GetGuestViewManager() {
79 return factory_
.GetManager(browser()->profile());
82 void TestHelper(const std::string
& test_name
,
83 const std::string
& app_location
,
84 const std::string
& app_to_embed
) {
85 LoadAndLaunchPlatformApp(app_location
.c_str(), "Launched");
87 // Flush any pending events to make sure we start with a clean slate.
88 content::RunAllPendingInMessageLoop();
90 content::WebContents
* embedder_web_contents
=
91 GetFirstAppWindowWebContents();
92 if (!embedder_web_contents
) {
93 LOG(ERROR
) << "UNABLE TO FIND EMBEDDER WEB CONTENTS.";
97 ExtensionTestMessageListener
done_listener("TEST_PASSED", false);
98 done_listener
.set_failure_message("TEST_FAILED");
99 if (!content::ExecuteScript(
100 embedder_web_contents
,
101 base::StringPrintf("runTest('%s', '%s')", test_name
.c_str(),
102 app_to_embed
.c_str()))) {
103 LOG(ERROR
) << "UNABLE TO START TEST.";
106 ASSERT_TRUE(done_listener
.WaitUntilSatisfied());
110 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
111 extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line
);
114 TestGuestViewManagerFactory factory_
;
117 // Tests that <extensionview> can be created and added to the DOM.
118 IN_PROC_BROWSER_TEST_F(ExtensionViewTest
,
119 TestExtensionViewCreationShouldSucceed
) {
120 const extensions::Extension
* skeleton_app
=
121 InstallPlatformApp("extension_view/skeleton");
122 TestHelper("testExtensionViewCreationShouldSucceed", "extension_view",
126 // Tests that verify that <extensionview> can navigate to different sources.
127 IN_PROC_BROWSER_TEST_F(ExtensionViewTest
, ShimSrcAttribute
) {
128 ASSERT_TRUE(RunPlatformAppTest(
129 "platform_apps/extension_view/src_attribute"));
132 // Tests that verify that <extensionview> can call the connect function.
133 IN_PROC_BROWSER_TEST_F(ExtensionViewTest
, ConnectAPICall
) {
134 ASSERT_TRUE(RunPlatformAppTest("platform_apps/extension_view/connect_api"));
137 // Tests that verify that <extensionview> does not change extension ID if
138 // someone tries to change it in JavaScript.
139 IN_PROC_BROWSER_TEST_F(ExtensionViewTest
, ShimExtensionAttribute
) {
140 ASSERT_TRUE(RunPlatformAppTest(
141 "platform_apps/extension_view/extension_attribute"));