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