extension_startup_browsertest.cc revision f2477e01787aa58f445919b809d89e252beef54f
1// Copyright (c) 2012 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 <vector>
6
7#include "base/command_line.h"
8#include "base/file_util.h"
9#include "base/files/file_path.h"
10#include "base/path_service.h"
11#include "base/strings/string_util.h"
12#include "chrome/browser/chrome_notification_types.h"
13#include "chrome/browser/extensions/extension_service.h"
14#include "chrome/browser/extensions/extension_system.h"
15#include "chrome/browser/extensions/extension_util.h"
16#include "chrome/browser/extensions/user_script_master.h"
17#include "chrome/browser/profiles/profile.h"
18#include "chrome/browser/ui/browser.h"
19#include "chrome/browser/ui/tabs/tab_strip_model.h"
20#include "chrome/common/chrome_paths.h"
21#include "chrome/common/chrome_switches.h"
22#include "chrome/test/base/in_process_browser_test.h"
23#include "chrome/test/base/testing_profile.h"
24#include "chrome/test/base/ui_test_utils.h"
25#include "content/public/browser/notification_details.h"
26#include "content/public/browser/notification_service.h"
27#include "content/public/browser/web_contents.h"
28#include "content/public/test/browser_test_utils.h"
29#include "extensions/common/feature_switch.h"
30#include "net/base/net_util.h"
31
32using extensions::FeatureSwitch;
33
34// This file contains high-level startup tests for the extensions system. We've
35// had many silly bugs where command line flags did not get propagated correctly
36// into the services, so we didn't start correctly.
37
38class ExtensionStartupTestBase : public InProcessBrowserTest {
39 public:
40  ExtensionStartupTestBase() :
41      enable_extensions_(false) {
42    num_expected_extensions_ = 3;
43  }
44
45 protected:
46  // InProcessBrowserTest
47  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
48    if (!enable_extensions_)
49      command_line->AppendSwitch(switches::kDisableExtensions);
50
51    if (!load_extensions_.empty()) {
52      base::FilePath::StringType paths = JoinString(load_extensions_, ',');
53      command_line->AppendSwitchNative(switches::kLoadExtension,
54                                       paths);
55      command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck);
56    }
57  }
58
59  virtual bool SetUpUserDataDirectory() OVERRIDE {
60    base::FilePath profile_dir;
61    PathService::Get(chrome::DIR_USER_DATA, &profile_dir);
62    profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir);
63    file_util::CreateDirectory(profile_dir);
64
65    preferences_file_ = profile_dir.AppendASCII("Preferences");
66    user_scripts_dir_ = profile_dir.AppendASCII("User Scripts");
67    extensions_dir_ = profile_dir.AppendASCII("Extensions");
68
69    if (enable_extensions_ && load_extensions_.empty()) {
70      base::FilePath src_dir;
71      PathService::Get(chrome::DIR_TEST_DATA, &src_dir);
72      src_dir = src_dir.AppendASCII("extensions").AppendASCII("good");
73
74      base::CopyFile(src_dir.AppendASCII("Preferences"), preferences_file_);
75      base::CopyDirectory(src_dir.AppendASCII("Extensions"),
76                          profile_dir, true);  // recursive
77    }
78    return true;
79  }
80
81  virtual void TearDown() {
82    EXPECT_TRUE(base::DeleteFile(preferences_file_, false));
83
84    // TODO(phajdan.jr): Check return values of the functions below, carefully.
85    base::DeleteFile(user_scripts_dir_, true);
86    base::DeleteFile(extensions_dir_, true);
87
88    InProcessBrowserTest::TearDown();
89  }
90
91  void WaitForServicesToStart(int num_expected_extensions,
92                              bool expect_extensions_enabled) {
93    ExtensionService* service = extensions::ExtensionSystem::Get(
94        browser()->profile())->extension_service();
95
96    // Count the number of non-component extensions.
97    int found_extensions = 0;
98    for (ExtensionSet::const_iterator it = service->extensions()->begin();
99         it != service->extensions()->end(); ++it)
100      if ((*it)->location() != extensions::Manifest::COMPONENT)
101        found_extensions++;
102
103    ASSERT_EQ(static_cast<uint32>(num_expected_extensions),
104              static_cast<uint32>(found_extensions));
105    ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled());
106
107    content::WindowedNotificationObserver user_scripts_observer(
108        chrome::NOTIFICATION_USER_SCRIPTS_UPDATED,
109        content::NotificationService::AllSources());
110    extensions::UserScriptMaster* master =
111        extensions::ExtensionSystem::Get(browser()->profile())->
112            user_script_master();
113    if (!master->ScriptsReady())
114      user_scripts_observer.Wait();
115    ASSERT_TRUE(master->ScriptsReady());
116  }
117
118  void TestInjection(bool expect_css, bool expect_script) {
119    // Load a page affected by the content script and test to see the effect.
120    base::FilePath test_file;
121    PathService::Get(chrome::DIR_TEST_DATA, &test_file);
122    test_file = test_file.AppendASCII("extensions")
123                         .AppendASCII("test_file.html");
124
125    ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file));
126
127    bool result = false;
128    ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
129        browser()->tab_strip_model()->GetActiveWebContents(),
130        "window.domAutomationController.send("
131        "    document.defaultView.getComputedStyle(document.body, null)."
132        "    getPropertyValue('background-color') == 'rgb(245, 245, 220)')",
133        &result));
134    EXPECT_EQ(expect_css, result);
135
136    result = false;
137    ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
138        browser()->tab_strip_model()->GetActiveWebContents(),
139        "window.domAutomationController.send(document.title == 'Modified')",
140        &result));
141    EXPECT_EQ(expect_script, result);
142  }
143
144  base::FilePath preferences_file_;
145  base::FilePath extensions_dir_;
146  base::FilePath user_scripts_dir_;
147  bool enable_extensions_;
148  // Extensions to load from the command line.
149  std::vector<base::FilePath::StringType> load_extensions_;
150
151  int num_expected_extensions_;
152};
153
154
155// ExtensionsStartupTest
156// Ensures that we can startup the browser with --enable-extensions and some
157// extensions installed and see them run and do basic things.
158
159class ExtensionsStartupTest : public ExtensionStartupTestBase {
160 public:
161  ExtensionsStartupTest() {
162    enable_extensions_ = true;
163  }
164};
165
166IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) {
167  WaitForServicesToStart(num_expected_extensions_, true);
168  TestInjection(true, true);
169}
170
171// Sometimes times out on Mac.  http://crbug.com/48151
172//
173// TODO(erg): linux_aura bringup: http://crbug.com/163931
174#if defined(OS_MACOSX) || (defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA))
175#define MAYBE_NoFileAccess DISABLED_NoFileAccess
176#else
177#define MAYBE_NoFileAccess NoFileAccess
178#endif
179// Tests that disallowing file access on an extension prevents it from injecting
180// script into a page with a file URL.
181IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, MAYBE_NoFileAccess) {
182  WaitForServicesToStart(num_expected_extensions_, true);
183
184  // Keep a separate list of extensions for which to disable file access, since
185  // doing so reloads them.
186  std::vector<const extensions::Extension*> extension_list;
187
188  ExtensionService* service = extensions::ExtensionSystem::Get(
189      browser()->profile())->extension_service();
190  for (ExtensionSet::const_iterator it = service->extensions()->begin();
191       it != service->extensions()->end(); ++it) {
192    if ((*it)->location() == extensions::Manifest::COMPONENT)
193      continue;
194    if (extension_util::AllowFileAccess(it->get(), service))
195      extension_list.push_back(it->get());
196  }
197
198  for (size_t i = 0; i < extension_list.size(); ++i) {
199    content::WindowedNotificationObserver user_scripts_observer(
200        chrome::NOTIFICATION_USER_SCRIPTS_UPDATED,
201        content::NotificationService::AllSources());
202    extension_util::SetAllowFileAccess(extension_list[i], service, false);
203    user_scripts_observer.Wait();
204  }
205
206  TestInjection(false, false);
207}
208
209// ExtensionsLoadTest
210// Ensures that we can startup the browser with --load-extension and see them
211// run.
212
213class ExtensionsLoadTest : public ExtensionStartupTestBase {
214 public:
215  ExtensionsLoadTest() {
216    enable_extensions_ = true;
217    base::FilePath one_extension_path;
218    PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
219    one_extension_path = one_extension_path
220        .AppendASCII("extensions")
221        .AppendASCII("good")
222        .AppendASCII("Extensions")
223        .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
224        .AppendASCII("1.0.0.0");
225    load_extensions_.push_back(one_extension_path.value());
226  }
227};
228
229// Fails inconsistently on Linux x64. http://crbug.com/80961
230// TODO(dpapad): Has not failed since October 2011, let's reenable, monitor
231// and act accordingly.
232IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Test) {
233  WaitForServicesToStart(1, true);
234  TestInjection(true, true);
235}
236
237// ExtensionsLoadMultipleTest
238// Ensures that we can startup the browser with multiple extensions
239// via --load-extension=X1,X2,X3.
240class ExtensionsLoadMultipleTest : public ExtensionStartupTestBase {
241 public:
242  ExtensionsLoadMultipleTest() {
243    enable_extensions_ = true;
244    base::FilePath one_extension_path;
245    PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
246    one_extension_path = one_extension_path
247        .AppendASCII("extensions")
248        .AppendASCII("good")
249        .AppendASCII("Extensions")
250        .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
251        .AppendASCII("1.0.0.0");
252    load_extensions_.push_back(one_extension_path.value());
253
254    base::FilePath second_extension_path;
255    PathService::Get(chrome::DIR_TEST_DATA, &second_extension_path);
256    second_extension_path = second_extension_path
257        .AppendASCII("extensions")
258        .AppendASCII("app");
259    load_extensions_.push_back(second_extension_path.value());
260
261    base::FilePath third_extension_path;
262    PathService::Get(chrome::DIR_TEST_DATA, &third_extension_path);
263    third_extension_path = third_extension_path
264        .AppendASCII("extensions")
265        .AppendASCII("app1");
266    load_extensions_.push_back(third_extension_path.value());
267
268    base::FilePath fourth_extension_path;
269    PathService::Get(chrome::DIR_TEST_DATA, &fourth_extension_path);
270    fourth_extension_path = fourth_extension_path
271        .AppendASCII("extensions")
272        .AppendASCII("app2");
273    load_extensions_.push_back(fourth_extension_path.value());
274  }
275};
276
277IN_PROC_BROWSER_TEST_F(ExtensionsLoadMultipleTest, Test) {
278  WaitForServicesToStart(4, true);
279  TestInjection(true, true);
280}
281