1 // Copyright (c) 2013 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/bind.h" 6 #include "chrome/browser/browser_process.h" 7 #include "chrome/browser/chrome_notification_types.h" 8 #include "chrome/browser/extensions/extension_browsertest.h" 9 #include "chrome/browser/extensions/extension_host.h" 10 #include "chrome/browser/extensions/extension_process_manager.h" 11 #include "chrome/browser/extensions/extension_service.h" 12 #include "chrome/browser/extensions/extension_system.h" 13 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser_commands.h" 16 #include "chrome/test/base/ui_test_utils.h" 17 #include "content/public/browser/render_process_host.h" 18 #include "content/public/browser/render_view_host.h" 19 #include "content/public/browser/web_contents.h" 20 #include "content/public/test/browser_test_utils.h" 21 22 using content::RenderViewHost; 23 using content::WebContents; 24 using extensions::Extension; 25 26 class GtalkExtensionTest : public ExtensionBrowserTest { 27 protected: 28 ExtensionProcessManager* GetExtensionProcessManager() { 29 return extensions::ExtensionSystem::Get(browser()->profile())-> 30 process_manager(); 31 } 32 33 void InstallGtalkExtension(const std::string& version) { 34 const Extension* extension = InstallExtensionWithUIAutoConfirm( 35 test_data_dir_.AppendASCII("gtalk").AppendASCII(version + ".crx"), 36 1, browser()); 37 installed_extension_id_ = extension->id(); 38 } 39 40 const std::string& GetInstalledExtensionId() { 41 return installed_extension_id_; 42 } 43 44 RenderViewHost* GetViewer() { 45 std::vector<RenderViewHost*> views = GetMatchingViews(GetViewerUrl()); 46 EXPECT_EQ(1U, views.size()); 47 if (views.empty()) 48 return NULL; 49 return views.front(); 50 } 51 52 std::string GetViewerUrl() { 53 return "chrome-extension://" + GetInstalledExtensionId() + "/viewer.html"; 54 } 55 56 std::vector<RenderViewHost*> GetMatchingViews(std::string url_query) { 57 ExtensionProcessManager* manager = GetExtensionProcessManager(); 58 ExtensionProcessManager::ViewSet all_views = manager->GetAllViews(); 59 std::vector<RenderViewHost*> matching_views; 60 for (ExtensionProcessManager::ViewSet::const_iterator iter = 61 all_views.begin(); iter != all_views.end(); ++iter) { 62 WebContents* web_contents = WebContents::FromRenderViewHost(*iter); 63 std::string url = web_contents->GetURL().spec(); 64 if (url.find(url_query) != std::string::npos) 65 matching_views.push_back(*iter); 66 } 67 return matching_views; 68 } 69 70 std::string ReadCurrentVersion() { 71 std::string response; 72 EXPECT_TRUE(file_util::ReadFileToString( 73 test_data_dir_.AppendASCII("gtalk").AppendASCII("current_version"), 74 &response)); 75 return response; 76 } 77 78 private: 79 std::string installed_extension_id_; 80 }; 81 82 IN_PROC_BROWSER_TEST_F(GtalkExtensionTest, InstallCurrent) { 83 content::WindowedNotificationObserver panel_loaded( 84 chrome::NOTIFICATION_EXTENSION_VIEW_REGISTERED, 85 content::NotificationService::AllSources()); 86 InstallGtalkExtension(ReadCurrentVersion()); 87 panel_loaded.Wait(); 88 ASSERT_TRUE(GetViewer()); 89 } 90