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