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 "content/public/test/browser_test.h"
8 #include "content/public/test/browser_test_utils.h"
9 #include "content/public/test/test_utils.h"
10 #include "extensions/browser/app_window/app_window.h"
11 #include "extensions/browser/app_window/app_window_registry.h"
12 #include "extensions/browser/guest_view/guest_view_manager.h"
13 #include "extensions/browser/guest_view/guest_view_manager_factory.h"
14 #include "extensions/browser/guest_view/test_guest_view_manager.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"
29 class MockShellAppDelegate
: public extensions::ShellAppDelegate
{
31 MockShellAppDelegate() : requested_(false) {
32 DCHECK(instance_
== nullptr);
35 ~MockShellAppDelegate() override
{ instance_
= nullptr; }
37 void RequestMediaAccessPermission(
38 content::WebContents
* web_contents
,
39 const content::MediaStreamRequest
& request
,
40 const content::MediaResponseCallback
& callback
,
41 const extensions::Extension
* extension
) override
{
43 if (request_message_loop_runner_
.get())
44 request_message_loop_runner_
->Quit();
47 void WaitForRequestMediaPermission() {
50 request_message_loop_runner_
= new content::MessageLoopRunner
;
51 request_message_loop_runner_
->Run();
54 static MockShellAppDelegate
* Get() { return instance_
; }
58 scoped_refptr
<content::MessageLoopRunner
> request_message_loop_runner_
;
59 static MockShellAppDelegate
* instance_
;
62 MockShellAppDelegate
* MockShellAppDelegate::instance_
= nullptr;
64 class MockShellAppViewGuestDelegate
65 : public extensions::ShellAppViewGuestDelegate
{
67 MockShellAppViewGuestDelegate() {}
69 extensions::AppDelegate
* CreateAppDelegate() override
{
70 return new MockShellAppDelegate();
74 class MockExtensionsAPIClient
: public extensions::ShellExtensionsAPIClient
{
76 MockExtensionsAPIClient() {}
78 extensions::AppViewGuestDelegate
* CreateAppViewGuestDelegate()
80 return new MockShellAppViewGuestDelegate();
86 namespace extensions
{
88 class AppViewTest
: public AppShellTest
{
90 AppViewTest() { GuestViewManager::set_factory_for_testing(&factory_
); }
92 TestGuestViewManager
* GetGuestViewManager() {
93 return static_cast<TestGuestViewManager
*>(
94 TestGuestViewManager::FromBrowserContext(
95 ShellContentBrowserClient::Get()->GetBrowserContext()));
98 content::WebContents
* GetFirstAppWindowWebContents() {
99 const AppWindowRegistry::AppWindowList
& app_window_list
=
100 AppWindowRegistry::Get(browser_context_
)->app_windows();
101 DCHECK(app_window_list
.size() == 1);
102 return (*app_window_list
.begin())->web_contents();
105 const Extension
* LoadApp(const std::string
& app_location
) {
106 base::FilePath test_data_dir
;
107 PathService::Get(DIR_TEST_DATA
, &test_data_dir
);
108 test_data_dir
= test_data_dir
.AppendASCII(app_location
.c_str());
109 return extension_system_
->LoadApp(test_data_dir
);
112 void RunTest(const std::string
& test_name
,
113 const std::string
& app_location
,
114 const std::string
& app_to_embed
) {
115 extension_system_
->Init();
117 const Extension
* app_embedder
= LoadApp(app_location
);
118 ASSERT_TRUE(app_embedder
);
119 const Extension
* app_embedded
= LoadApp(app_to_embed
);
120 ASSERT_TRUE(app_embedded
);
122 extension_system_
->LaunchApp(app_embedder
->id());
124 ExtensionTestMessageListener
launch_listener("LAUNCHED", false);
125 ASSERT_TRUE(launch_listener
.WaitUntilSatisfied());
127 embedder_web_contents_
= GetFirstAppWindowWebContents();
129 ExtensionTestMessageListener
done_listener("TEST_PASSED", false);
130 done_listener
.set_failure_message("TEST_FAILED");
132 content::ExecuteScript(embedder_web_contents_
,
133 base::StringPrintf("runTest('%s', '%s')",
135 app_embedded
->id().c_str())))
136 << "Unable to start test.";
137 ASSERT_TRUE(done_listener
.WaitUntilSatisfied());
141 content::WebContents
* embedder_web_contents_
;
142 TestGuestViewManagerFactory factory_
;
145 // Tests that <appview> correctly processes parameters passed on connect.
146 IN_PROC_BROWSER_TEST_F(AppViewTest
, TestAppViewGoodDataShouldSucceed
) {
147 RunTest("testAppViewGoodDataShouldSucceed",
149 "app_view/apitest/skeleton");
152 // Tests that <appview> can handle media permission requests.
153 IN_PROC_BROWSER_TEST_F(AppViewTest
, TestAppViewMediaRequest
) {
154 static_cast<ShellExtensionsBrowserClient
*>(ExtensionsBrowserClient::Get())
155 ->SetAPIClientForTest(nullptr);
156 static_cast<ShellExtensionsBrowserClient
*>(ExtensionsBrowserClient::Get())
157 ->SetAPIClientForTest(new MockExtensionsAPIClient
);
159 RunTest("testAppViewMediaRequest", "app_view/apitest",
160 "app_view/apitest/media_request");
162 MockShellAppDelegate::Get()->WaitForRequestMediaPermission();
165 // Tests that <appview> correctly processes parameters passed on connect.
166 // This test should fail to connect because the embedded app (skeleton) will
167 // refuse the data passed by the embedder app and deny the request.
168 IN_PROC_BROWSER_TEST_F(AppViewTest
, TestAppViewRefusedDataShouldFail
) {
169 RunTest("testAppViewRefusedDataShouldFail",
171 "app_view/apitest/skeleton");
174 // Tests that <appview> is able to navigate to another installed app.
175 IN_PROC_BROWSER_TEST_F(AppViewTest
, TestAppViewWithUndefinedDataShouldSucceed
) {
176 RunTest("testAppViewWithUndefinedDataShouldSucceed",
178 "app_view/apitest/skeleton");
181 } // namespace extensions