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. 4 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 "content/public/browser/notification_service.h" 9 #include "content/public/browser/render_process_host.h" 10 #include "content/public/test/browser_test_utils.h" 11 #include "content/public/test/test_utils.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/common/switches.h" 15 #include "extensions/test/extension_test_message_listener.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" 19 20 namespace { 21 22 class TestGuestViewManager : public extensions::GuestViewManager { 23 public: 24 explicit TestGuestViewManager(content::BrowserContext* context) : 25 extensions::GuestViewManager(context), 26 web_contents_(NULL) {} 27 28 content::WebContents* WaitForGuestCreated() { 29 if (web_contents_) 30 return web_contents_; 31 32 message_loop_runner_ = new content::MessageLoopRunner; 33 message_loop_runner_->Run(); 34 return web_contents_; 35 } 36 37 private: 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; 44 45 if (message_loop_runner_.get()) 46 message_loop_runner_->Quit(); 47 } 48 49 content::WebContents* web_contents_; 50 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; 51 }; 52 53 // Test factory for creating test instances of GuestViewManager. 54 class TestGuestViewManagerFactory : public extensions::GuestViewManagerFactory { 55 public: 56 TestGuestViewManagerFactory() : 57 test_guest_view_manager_(NULL) {} 58 59 virtual ~TestGuestViewManagerFactory() {} 60 61 virtual extensions::GuestViewManager* CreateGuestViewManager( 62 content::BrowserContext* context) OVERRIDE { 63 return GetManager(context); 64 } 65 66 TestGuestViewManager* GetManager(content::BrowserContext* context) { 67 if (!test_guest_view_manager_) { 68 test_guest_view_manager_ = new TestGuestViewManager(context); 69 } 70 return test_guest_view_manager_; 71 } 72 73 private: 74 TestGuestViewManager* test_guest_view_manager_; 75 76 DISALLOW_COPY_AND_ASSIGN(TestGuestViewManagerFactory); 77 }; 78 79 } // namespace 80 81 class AppViewTest : public extensions::PlatformAppBrowserTest { 82 public: 83 AppViewTest() { 84 extensions::GuestViewManager::set_factory_for_testing(&factory_); 85 } 86 87 TestGuestViewManager* GetGuestViewManager() { 88 return factory_.GetManager(browser()->profile()); 89 } 90 91 enum TestServer { 92 NEEDS_TEST_SERVER, 93 NO_TEST_SERVER 94 }; 95 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."; 104 return; 105 } 106 } 107 108 LoadAndLaunchPlatformApp(app_location.c_str(), "Launched"); 109 110 // Flush any pending events to make sure we start with a clean slate. 111 content::RunAllPendingInMessageLoop(); 112 113 content::WebContents* embedder_web_contents = 114 GetFirstAppWindowWebContents(); 115 if (!embedder_web_contents) { 116 LOG(ERROR) << "UNABLE TO FIND EMBEDDER WEB CONTENTS."; 117 return; 118 } 119 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')", 125 test_name.c_str(), 126 app_to_embed.c_str()))) { 127 LOG(ERROR) << "UNABLE TO START TEST."; 128 return; 129 } 130 ASSERT_TRUE(done_listener.WaitUntilSatisfied()); 131 } 132 133 private: 134 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 135 command_line->AppendSwitch(extensions::switches::kEnableAppView); 136 extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line); 137 } 138 139 TestGuestViewManagerFactory factory_; 140 }; 141 142 // Tests that <appview> is able to navigate to another installed app. 143 IN_PROC_BROWSER_TEST_F(AppViewTest, TestAppViewWithUndefinedDataShouldSucceed) { 144 const extensions::Extension* skeleton_app = 145 InstallPlatformApp("app_view/shim/skeleton"); 146 TestHelper("testAppViewWithUndefinedDataShouldSucceed", 147 "app_view/shim", 148 skeleton_app->id(), 149 NO_TEST_SERVER); 150 } 151 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", 157 "app_view/shim", 158 skeleton_app->id(), 159 NO_TEST_SERVER); 160 } 161 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", 167 "app_view/shim", 168 skeleton_app->id(), 169 NO_TEST_SERVER); 170 } 171