- add sources.
[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/common/extensions/feature_switch.h"
23 #include "chrome/test/base/in_process_browser_test.h"
24 #include "chrome/test/base/testing_profile.h"
25 #include "chrome/test/base/ui_test_utils.h"
26 #include "content/public/browser/notification_details.h"
27 #include "content/public/browser/notification_service.h"
28 #include "content/public/browser/web_contents.h"
29 #include "content/public/test/browser_test_utils.h"
30 #include "net/base/net_util.h"
31
32 using 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
38 class 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
159 class ExtensionsStartupTest : public ExtensionStartupTestBase {
160  public:
161   ExtensionsStartupTest() {
162     enable_extensions_ = true;
163   }
164 };
165
166 IN_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 #if defined(OS_MACOSX)
173 #define MAYBE_NoFileAccess DISABLED_NoFileAccess
174 #else
175 #define MAYBE_NoFileAccess NoFileAccess
176 #endif
177 // Tests that disallowing file access on an extension prevents it from injecting
178 // script into a page with a file URL.
179 IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, MAYBE_NoFileAccess) {
180   WaitForServicesToStart(num_expected_extensions_, true);
181
182   // Keep a separate list of extensions for which to disable file access, since
183   // doing so reloads them.
184   std::vector<const extensions::Extension*> extension_list;
185
186   ExtensionService* service = extensions::ExtensionSystem::Get(
187       browser()->profile())->extension_service();
188   for (ExtensionSet::const_iterator it = service->extensions()->begin();
189        it != service->extensions()->end(); ++it) {
190     if ((*it)->location() == extensions::Manifest::COMPONENT)
191       continue;
192     if (extension_util::AllowFileAccess(it->get(), service))
193       extension_list.push_back(it->get());
194   }
195
196   for (size_t i = 0; i < extension_list.size(); ++i) {
197     content::WindowedNotificationObserver user_scripts_observer(
198         chrome::NOTIFICATION_USER_SCRIPTS_UPDATED,
199         content::NotificationService::AllSources());
200     extension_util::SetAllowFileAccess(extension_list[i], service, false);
201     user_scripts_observer.Wait();
202   }
203
204   TestInjection(false, false);
205 }
206
207 // ExtensionsLoadTest
208 // Ensures that we can startup the browser with --load-extension and see them
209 // run.
210
211 class ExtensionsLoadTest : public ExtensionStartupTestBase {
212  public:
213   ExtensionsLoadTest() {
214     enable_extensions_ = true;
215     base::FilePath one_extension_path;
216     PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
217     one_extension_path = one_extension_path
218         .AppendASCII("extensions")
219         .AppendASCII("good")
220         .AppendASCII("Extensions")
221         .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
222         .AppendASCII("1.0.0.0");
223     load_extensions_.push_back(one_extension_path.value());
224   }
225 };
226
227 // Fails inconsistently on Linux x64. http://crbug.com/80961
228 // TODO(dpapad): Has not failed since October 2011, let's reenable, monitor
229 // and act accordingly.
230 IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Test) {
231   WaitForServicesToStart(1, true);
232   TestInjection(true, true);
233 }
234
235 // ExtensionsLoadMultipleTest
236 // Ensures that we can startup the browser with multiple extensions
237 // via --load-extension=X1,X2,X3.
238 class ExtensionsLoadMultipleTest : public ExtensionStartupTestBase {
239  public:
240   ExtensionsLoadMultipleTest() {
241     enable_extensions_ = true;
242     base::FilePath one_extension_path;
243     PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
244     one_extension_path = one_extension_path
245         .AppendASCII("extensions")
246         .AppendASCII("good")
247         .AppendASCII("Extensions")
248         .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
249         .AppendASCII("1.0.0.0");
250     load_extensions_.push_back(one_extension_path.value());
251
252     base::FilePath second_extension_path;
253     PathService::Get(chrome::DIR_TEST_DATA, &second_extension_path);
254     second_extension_path = second_extension_path
255         .AppendASCII("extensions")
256         .AppendASCII("app");
257     load_extensions_.push_back(second_extension_path.value());
258
259     base::FilePath third_extension_path;
260     PathService::Get(chrome::DIR_TEST_DATA, &third_extension_path);
261     third_extension_path = third_extension_path
262         .AppendASCII("extensions")
263         .AppendASCII("app1");
264     load_extensions_.push_back(third_extension_path.value());
265
266     base::FilePath fourth_extension_path;
267     PathService::Get(chrome::DIR_TEST_DATA, &fourth_extension_path);
268     fourth_extension_path = fourth_extension_path
269         .AppendASCII("extensions")
270         .AppendASCII("app2");
271     load_extensions_.push_back(fourth_extension_path.value());
272   }
273 };
274
275 IN_PROC_BROWSER_TEST_F(ExtensionsLoadMultipleTest, Test) {
276   WaitForServicesToStart(4, true);
277   TestInjection(true, true);
278 }