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/path_service.h"
6 #include "base/strings/stringprintf.h"
7 #include "components/guest_view/browser/guest_view_manager.h"
8 #include "components/guest_view/browser/guest_view_manager_factory.h"
9 #include "components/guest_view/browser/test_guest_view_manager.h"
10 #include "content/public/test/browser_test.h"
11 #include "content/public/test/browser_test_utils.h"
12 #include "content/public/test/test_utils.h"
13 #include "extensions/browser/app_window/app_window.h"
14 #include "extensions/browser/app_window/app_window_registry.h"
15 #include "extensions/common/extension.h"
16 #include "extensions/common/extension_paths.h"
17 #include "extensions/shell/browser/shell_app_delegate.h"
18 #include "extensions/shell/browser/shell_app_view_guest_delegate.h"
19 #include "extensions/shell/browser/shell_content_browser_client.h"
20 #include "extensions/shell/browser/shell_extension_system.h"
21 #include "extensions/shell/browser/shell_extensions_api_client.h"
22 #include "extensions/shell/browser/shell_extensions_browser_client.h"
23 #include "extensions/shell/test/shell_test.h"
24 #include "extensions/test/extension_test_message_listener.h"
25 #include "net/base/filename_util.h"
27 using guest_view::GuestViewManager
;
28 using guest_view::TestGuestViewManager
;
29 using guest_view::TestGuestViewManagerFactory
;
33 class MockShellAppDelegate
: public extensions::ShellAppDelegate
{
35 MockShellAppDelegate() : requested_(false) {
36 DCHECK(instance_
== nullptr);
39 ~MockShellAppDelegate() override
{ instance_
= nullptr; }
41 void RequestMediaAccessPermission(
42 content::WebContents
* web_contents
,
43 const content::MediaStreamRequest
& request
,
44 const content::MediaResponseCallback
& callback
,
45 const extensions::Extension
* extension
) override
{
47 if (request_message_loop_runner_
.get())
48 request_message_loop_runner_
->Quit();
51 void WaitForRequestMediaPermission() {
54 request_message_loop_runner_
= new content::MessageLoopRunner
;
55 request_message_loop_runner_
->Run();
58 static MockShellAppDelegate
* Get() { return instance_
; }
62 scoped_refptr
<content::MessageLoopRunner
> request_message_loop_runner_
;
63 static MockShellAppDelegate
* instance_
;
66 MockShellAppDelegate
* MockShellAppDelegate::instance_
= nullptr;
68 class MockShellAppViewGuestDelegate
69 : public extensions::ShellAppViewGuestDelegate
{
71 MockShellAppViewGuestDelegate() {}
73 extensions::AppDelegate
* CreateAppDelegate() override
{
74 return new MockShellAppDelegate();
78 class MockExtensionsAPIClient
: public extensions::ShellExtensionsAPIClient
{
80 MockExtensionsAPIClient() {}
82 extensions::AppViewGuestDelegate
* CreateAppViewGuestDelegate()
84 return new MockShellAppViewGuestDelegate();
90 namespace extensions
{
92 class AppViewTest
: public AppShellTest
{
94 AppViewTest() { GuestViewManager::set_factory_for_testing(&factory_
); }
96 TestGuestViewManager
* GetGuestViewManager() {
97 return static_cast<TestGuestViewManager
*>(
98 TestGuestViewManager::FromBrowserContext(
99 ShellContentBrowserClient::Get()->GetBrowserContext()));
102 content::WebContents
* GetFirstAppWindowWebContents() {
103 const AppWindowRegistry::AppWindowList
& app_window_list
=
104 AppWindowRegistry::Get(browser_context_
)->app_windows();
105 DCHECK(app_window_list
.size() == 1);
106 return (*app_window_list
.begin())->web_contents();
109 const Extension
* LoadApp(const std::string
& app_location
) {
110 base::FilePath test_data_dir
;
111 PathService::Get(DIR_TEST_DATA
, &test_data_dir
);
112 test_data_dir
= test_data_dir
.AppendASCII(app_location
.c_str());
113 return extension_system_
->LoadApp(test_data_dir
);
116 void RunTest(const std::string
& test_name
,
117 const std::string
& app_location
,
118 const std::string
& app_to_embed
) {
119 extension_system_
->Init();
121 const Extension
* app_embedder
= LoadApp(app_location
);
122 ASSERT_TRUE(app_embedder
);
123 const Extension
* app_embedded
= LoadApp(app_to_embed
);
124 ASSERT_TRUE(app_embedded
);
126 extension_system_
->LaunchApp(app_embedder
->id());
128 ExtensionTestMessageListener
launch_listener("LAUNCHED", false);
129 ASSERT_TRUE(launch_listener
.WaitUntilSatisfied());
131 embedder_web_contents_
= GetFirstAppWindowWebContents();
133 ExtensionTestMessageListener
done_listener("TEST_PASSED", false);
134 done_listener
.set_failure_message("TEST_FAILED");
136 content::ExecuteScript(embedder_web_contents_
,
137 base::StringPrintf("runTest('%s', '%s')",
139 app_embedded
->id().c_str())))
140 << "Unable to start test.";
141 ASSERT_TRUE(done_listener
.WaitUntilSatisfied());
145 content::WebContents
* embedder_web_contents_
;
146 TestGuestViewManagerFactory factory_
;
149 // Tests that <appview> correctly processes parameters passed on connect.
150 IN_PROC_BROWSER_TEST_F(AppViewTest
, TestAppViewGoodDataShouldSucceed
) {
151 RunTest("testAppViewGoodDataShouldSucceed",
153 "app_view/apitest/skeleton");
156 // Tests that <appview> can handle media permission requests.
157 IN_PROC_BROWSER_TEST_F(AppViewTest
, TestAppViewMediaRequest
) {
158 static_cast<ShellExtensionsBrowserClient
*>(ExtensionsBrowserClient::Get())
159 ->SetAPIClientForTest(nullptr);
160 static_cast<ShellExtensionsBrowserClient
*>(ExtensionsBrowserClient::Get())
161 ->SetAPIClientForTest(new MockExtensionsAPIClient
);
163 RunTest("testAppViewMediaRequest", "app_view/apitest",
164 "app_view/apitest/media_request");
166 MockShellAppDelegate::Get()->WaitForRequestMediaPermission();
169 // Tests that <appview> correctly processes parameters passed on connect.
170 // This test should fail to connect because the embedded app (skeleton) will
171 // refuse the data passed by the embedder app and deny the request.
172 IN_PROC_BROWSER_TEST_F(AppViewTest
, TestAppViewRefusedDataShouldFail
) {
173 RunTest("testAppViewRefusedDataShouldFail",
175 "app_view/apitest/skeleton");
178 // Tests that <appview> is able to navigate to another installed app.
179 IN_PROC_BROWSER_TEST_F(AppViewTest
, TestAppViewWithUndefinedDataShouldSucceed
) {
180 RunTest("testAppViewWithUndefinedDataShouldSucceed",
182 "app_view/apitest/skeleton");
185 } // namespace extensions