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 "components/guest_view/browser/guest_view_manager.h"
9 #include "components/guest_view/browser/guest_view_manager_delegate.h"
10 #include "components/guest_view/browser/guest_view_manager_factory.h"
11 #include "components/guest_view/browser/test_guest_view_manager.h"
12 #include "content/public/test/browser_test_utils.h"
13 #include "extensions/browser/api/extensions_api_client.h"
14 #include "extensions/test/extension_test_message_listener.h"
16 using extensions::ExtensionsAPIClient
;
17 using guest_view::GuestViewManager
;
18 using guest_view::TestGuestViewManager
;
19 using guest_view::TestGuestViewManagerFactory
;
21 class ExtensionViewTest
: public extensions::PlatformAppBrowserTest
{
24 GuestViewManager::set_factory_for_testing(&factory_
);
27 TestGuestViewManager
* GetGuestViewManager() {
28 TestGuestViewManager
* manager
= static_cast<TestGuestViewManager
*>(
29 TestGuestViewManager::FromBrowserContext(browser()->profile()));
30 // TestGuestViewManager::WaitForSingleGuestCreated may and will get called
31 // before a guest is created.
33 manager
= static_cast<TestGuestViewManager
*>(
34 GuestViewManager::CreateWithDelegate(
36 ExtensionsAPIClient::Get()->CreateGuestViewManagerDelegate(
37 browser()->profile())));
42 void TestHelper(const std::string
& test_name
,
43 const std::string
& app_location
,
44 const std::string
& app_to_embed
,
45 const std::string
& second_app_to_embed
) {
46 LoadAndLaunchPlatformApp(app_location
.c_str(), "Launched");
48 // Flush any pending events to make sure we start with a clean slate.
49 content::RunAllPendingInMessageLoop();
51 content::WebContents
* embedder_web_contents
=
52 GetFirstAppWindowWebContents();
53 if (!embedder_web_contents
) {
54 LOG(ERROR
) << "UNABLE TO FIND EMBEDDER WEB CONTENTS.";
58 ExtensionTestMessageListener
done_listener("TEST_PASSED", false);
59 done_listener
.set_failure_message("TEST_FAILED");
60 if (!content::ExecuteScript(
61 embedder_web_contents
,
62 base::StringPrintf("runTest('%s', '%s', '%s')", test_name
.c_str(),
64 second_app_to_embed
.c_str()))) {
65 LOG(ERROR
) << "UNABLE TO START TEST.";
68 ASSERT_TRUE(done_listener
.WaitUntilSatisfied());
72 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
73 extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line
);
76 TestGuestViewManagerFactory factory_
;
79 // Tests that <extensionview> can be created and added to the DOM.
80 IN_PROC_BROWSER_TEST_F(ExtensionViewTest
,
81 TestExtensionViewCreationShouldSucceed
) {
82 const extensions::Extension
* skeleton_app
=
83 InstallPlatformApp("extension_view/skeleton");
84 TestHelper("testExtensionViewCreationShouldSucceed", "extension_view",
85 skeleton_app
->id(), "");
88 // Tests that verify that <extensionview> does not change extension ID if
89 // someone tries to change it in JavaScript.
90 IN_PROC_BROWSER_TEST_F(ExtensionViewTest
, ShimExtensionAttribute
) {
91 const extensions::Extension
* skeleton_app
=
92 InstallPlatformApp("extension_view/skeleton");
93 TestHelper("testExtensionAttribute", "extension_view/extension_attribute",
94 skeleton_app
->id(), "");
97 // Tests that verify that <extensionview> does not change src if
98 // someone tries to change it in JavaScript.
99 IN_PROC_BROWSER_TEST_F(ExtensionViewTest
, ShimSrcAttribute
) {
100 const extensions::Extension
* skeleton_app
=
101 InstallPlatformApp("extension_view/skeleton");
102 TestHelper("testSrcAttribute", "extension_view/src_attribute",
103 skeleton_app
->id(), "");
106 // Tests that verify that <extensionview> can call the load function.
107 IN_PROC_BROWSER_TEST_F(ExtensionViewTest
, LoadAPICall
) {
108 const extensions::Extension
* skeleton_app
=
109 InstallPlatformApp("extension_view/skeleton");
110 const extensions::Extension
* skeleton_app_two
=
111 InstallPlatformApp("extension_view/skeleton_two");
112 TestHelper("testLoadAPIFunction", "extension_view/load_api",
114 skeleton_app_two
->id());
117 #if defined(MEMORY_SANITIZER)
118 // Flaky under MemorySanitizer: https://crbug.com/512092
119 #define MAYBE_QueuedLoadAPICall DISABLED_QueuedLoadAPICall
121 #define MAYBE_QueuedLoadAPICall QueuedLoadAPICall
123 // Tests that verify that <extensionview> can queue up multiple calls to the
125 IN_PROC_BROWSER_TEST_F(ExtensionViewTest
, MAYBE_QueuedLoadAPICall
) {
126 const extensions::Extension
* skeleton_app
=
127 InstallPlatformApp("extension_view/skeleton");
128 const extensions::Extension
* skeleton_app_two
=
129 InstallPlatformApp("extension_view/skeleton_two");
130 TestHelper("testQueuedLoadAPIFunction", "extension_view/load_api",
132 skeleton_app_two
->id());