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/browser/guest_view/guest_view_manager.h"
9 #include "chrome/browser/guest_view/guest_view_manager_factory.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "chrome/test/base/ui_test_utils.h"
12 #include "content/public/browser/notification_service.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/test/browser_test_utils.h"
15 #include "content/public/test/test_utils.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 GuestViewManager
{
24 explicit TestGuestViewManager(content::BrowserContext
* context
) :
25 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 GuestViewManager::AddGuest(guest_instance_id
, guest_web_contents
);
42 web_contents_
= guest_web_contents
;
44 if (message_loop_runner_
)
45 message_loop_runner_
->Quit();
48 content::WebContents
* web_contents_
;
49 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
52 // Test factory for creating test instances of GuestViewManager.
53 class TestGuestViewManagerFactory
: public GuestViewManagerFactory
{
55 TestGuestViewManagerFactory() :
56 test_guest_view_manager_(NULL
) {}
58 virtual ~TestGuestViewManagerFactory() {}
60 virtual GuestViewManager
* CreateGuestViewManager(
61 content::BrowserContext
* context
) OVERRIDE
{
62 return GetManager(context
);
65 TestGuestViewManager
* GetManager(content::BrowserContext
* context
) {
66 if (!test_guest_view_manager_
) {
67 test_guest_view_manager_
= new TestGuestViewManager(context
);
69 return test_guest_view_manager_
;
73 TestGuestViewManager
* test_guest_view_manager_
;
75 DISALLOW_COPY_AND_ASSIGN(TestGuestViewManagerFactory
);
80 class AppViewTest
: public extensions::PlatformAppBrowserTest
{
83 GuestViewManager::set_factory_for_testing(&factory_
);
86 TestGuestViewManager
* GetGuestViewManager() {
87 return factory_
.GetManager(browser()->profile());
95 void TestHelper(const std::string
& test_name
,
96 const std::string
& app_location
,
97 const std::string
& app_to_embed
,
98 TestServer test_server
) {
99 // For serving guest pages.
100 if (test_server
== NEEDS_TEST_SERVER
) {
101 if (!StartEmbeddedTestServer()) {
102 LOG(ERROR
) << "FAILED TO START TEST SERVER.";
107 LoadAndLaunchPlatformApp(app_location
.c_str(), "Launched");
109 // Flush any pending events to make sure we start with a clean slate.
110 content::RunAllPendingInMessageLoop();
112 content::WebContents
* embedder_web_contents
=
113 GetFirstAppWindowWebContents();
114 if (!embedder_web_contents
) {
115 LOG(ERROR
) << "UNABLE TO FIND EMBEDDER WEB CONTENTS.";
119 ExtensionTestMessageListener
done_listener("TEST_PASSED", false);
120 done_listener
.set_failure_message("TEST_FAILED");
121 if (!content::ExecuteScript(
122 embedder_web_contents
,
123 base::StringPrintf("runTest('%s', '%s')",
125 app_to_embed
.c_str()))) {
126 LOG(ERROR
) << "UNABLE TO START TEST.";
129 ASSERT_TRUE(done_listener
.WaitUntilSatisfied());
133 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
134 command_line
->AppendSwitch(switches::kEnableAppView
);
135 extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line
);
138 TestGuestViewManagerFactory factory_
;
141 // Tests that <appview> is able to navigate to another installed app.
142 IN_PROC_BROWSER_TEST_F(AppViewTest
, TestAppViewBasic
) {
143 const extensions::Extension
* skeleton_app
=
144 InstallPlatformApp("app_view/shim/skeleton");
145 TestHelper("testAppViewBasic",