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/browser/extensions/extension_test_message_listener.h"
8 #include "chrome/test/base/ui_test_utils.h"
9 #include "content/public/browser/notification_service.h"
10 #include "content/public/browser/render_process_host.h"
11 #include "content/public/test/browser_test_utils.h"
12 #include "content/public/test/test_utils.h"
13 #include "extensions/browser/guest_view/guest_view_manager.h"
14 #include "extensions/browser/guest_view/guest_view_manager_factory.h"
15 #include "extensions/common/switches.h"
16 #include "net/test/embedded_test_server/embedded_test_server.h"
17 #include "net/test/embedded_test_server/http_request.h"
18 #include "net/test/embedded_test_server/http_response.h"
22 class TestGuestViewManager
: public extensions::GuestViewManager
{
24 explicit TestGuestViewManager(content::BrowserContext
* context
) :
25 extensions::GuestViewManager(context
),
26 web_contents_(NULL
) {}
28 content::WebContents
* WaitForGuestCreated() {
32 message_loop_runner_
= new content::MessageLoopRunner
;
33 message_loop_runner_
->Run();
38 // GuestViewManager override:
39 virtual void AddGuest(int guest_instance_id
,
40 content::WebContents
* guest_web_contents
) OVERRIDE
{
41 extensions::GuestViewManager::AddGuest(
42 guest_instance_id
, guest_web_contents
);
43 web_contents_
= guest_web_contents
;
45 if (message_loop_runner_
)
46 message_loop_runner_
->Quit();
49 content::WebContents
* web_contents_
;
50 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
53 // Test factory for creating test instances of GuestViewManager.
54 class TestGuestViewManagerFactory
: public extensions::GuestViewManagerFactory
{
56 TestGuestViewManagerFactory() :
57 test_guest_view_manager_(NULL
) {}
59 virtual ~TestGuestViewManagerFactory() {}
61 virtual extensions::GuestViewManager
* CreateGuestViewManager(
62 content::BrowserContext
* context
) OVERRIDE
{
63 return GetManager(context
);
66 TestGuestViewManager
* GetManager(content::BrowserContext
* context
) {
67 if (!test_guest_view_manager_
) {
68 test_guest_view_manager_
= new TestGuestViewManager(context
);
70 return test_guest_view_manager_
;
74 TestGuestViewManager
* test_guest_view_manager_
;
76 DISALLOW_COPY_AND_ASSIGN(TestGuestViewManagerFactory
);
81 class AppViewTest
: public extensions::PlatformAppBrowserTest
{
84 extensions::GuestViewManager::set_factory_for_testing(&factory_
);
87 TestGuestViewManager
* GetGuestViewManager() {
88 return factory_
.GetManager(browser()->profile());
96 void TestHelper(const std::string
& test_name
,
97 const std::string
& app_location
,
98 const std::string
& app_to_embed
,
99 TestServer test_server
) {
100 // For serving guest pages.
101 if (test_server
== NEEDS_TEST_SERVER
) {
102 if (!StartEmbeddedTestServer()) {
103 LOG(ERROR
) << "FAILED TO START TEST SERVER.";
108 LoadAndLaunchPlatformApp(app_location
.c_str(), "Launched");
110 // Flush any pending events to make sure we start with a clean slate.
111 content::RunAllPendingInMessageLoop();
113 content::WebContents
* embedder_web_contents
=
114 GetFirstAppWindowWebContents();
115 if (!embedder_web_contents
) {
116 LOG(ERROR
) << "UNABLE TO FIND EMBEDDER WEB CONTENTS.";
120 ExtensionTestMessageListener
done_listener("TEST_PASSED", false);
121 done_listener
.set_failure_message("TEST_FAILED");
122 if (!content::ExecuteScript(
123 embedder_web_contents
,
124 base::StringPrintf("runTest('%s', '%s')",
126 app_to_embed
.c_str()))) {
127 LOG(ERROR
) << "UNABLE TO START TEST.";
130 ASSERT_TRUE(done_listener
.WaitUntilSatisfied());
134 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
135 command_line
->AppendSwitch(extensions::switches::kEnableAppView
);
136 extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line
);
139 TestGuestViewManagerFactory factory_
;
142 // Tests that <appview> is able to navigate to another installed app.
143 IN_PROC_BROWSER_TEST_F(AppViewTest
, TestAppViewBasic
) {
144 const extensions::Extension
* skeleton_app
=
145 InstallPlatformApp("app_view/shim/skeleton");
146 TestHelper("testAppViewBasic",
152 // Tests that <appview> correctly processes parameters passed on connect.
153 IN_PROC_BROWSER_TEST_F(AppViewTest
, TestAppViewRefusedDataShouldFail
) {
154 const extensions::Extension
* skeleton_app
=
155 InstallPlatformApp("app_view/shim/skeleton");
156 TestHelper("testAppViewRefusedDataShouldFail",
162 // Tests that <appview> correctly processes parameters passed on connect.
163 IN_PROC_BROWSER_TEST_F(AppViewTest
, TestAppViewGoodDataShouldSucceed
) {
164 const extensions::Extension
* skeleton_app
=
165 InstallPlatformApp("app_view/shim/skeleton");
166 TestHelper("testAppViewGoodDataShouldSucceed",