- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / extensions / extension_settings_browsertest.cc
1 // Copyright 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 "chrome/browser/ui/webui/extensions/extension_settings_browsertest.h"
6
7 #include "base/files/file_path.h"
8 #include "base/path_service.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/extensions/crx_installer.h"
12 #include "chrome/browser/extensions/extension_error_reporter.h"
13 #include "chrome/browser/extensions/extension_install_prompt.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_system.h"
16 #include "chrome/browser/extensions/unpacked_installer.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "content/public/browser/notification_registrar.h"
23 #include "content/public/browser/notification_service.h"
24 #include "content/public/browser/render_view_host.h"
25 #include "content/public/test/browser_test_utils.h"
26 #include "content/public/test/test_utils.h"
27
28 using extensions::Extension;
29
30 ExtensionSettingsUIBrowserTest::ExtensionSettingsUIBrowserTest()
31     : profile_(NULL) {}
32
33 ExtensionSettingsUIBrowserTest::~ExtensionSettingsUIBrowserTest() {}
34
35 Profile* ExtensionSettingsUIBrowserTest::GetProfile() {
36   if (!profile_) {
37     profile_ =
38         browser() ? browser()->profile() : ProfileManager::GetDefaultProfile();
39   }
40   return profile_;
41 }
42
43 void ExtensionSettingsUIBrowserTest::SetUpOnMainThread() {
44   WebUIBrowserTest::SetUpOnMainThread();
45   observer_.reset(new ExtensionTestNotificationObserver(browser()));
46 }
47
48 void ExtensionSettingsUIBrowserTest::InstallGoodExtension() {
49   base::FilePath test_data_dir;
50   if (!PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)) {
51     ADD_FAILURE();
52     return;
53   }
54   base::FilePath extensions_data_dir = test_data_dir.AppendASCII("extensions");
55   InstallExtension(extensions_data_dir.AppendASCII("good.crx"));
56 }
57
58 class MockAutoConfirmExtensionInstallPrompt : public ExtensionInstallPrompt {
59  public:
60   explicit MockAutoConfirmExtensionInstallPrompt(
61       content::WebContents* web_contents)
62     : ExtensionInstallPrompt(web_contents) {}
63
64   // Proceed without confirmation prompt.
65   virtual void ConfirmInstall(
66       Delegate* delegate,
67       const Extension* extension,
68       const ShowDialogCallback& show_dialog_callback) OVERRIDE {
69     delegate->InstallUIProceed();
70   }
71 };
72
73 const Extension* ExtensionSettingsUIBrowserTest::InstallExtension(
74     const base::FilePath& path) {
75   Profile* profile = this->GetProfile();
76   ExtensionService* service = profile->GetExtensionService();
77   service->set_show_extensions_prompts(false);
78   size_t num_before = service->extensions()->size();
79   {
80     scoped_ptr<ExtensionInstallPrompt> install_ui;
81     install_ui.reset(new MockAutoConfirmExtensionInstallPrompt(
82         browser()->tab_strip_model()->GetActiveWebContents()));
83
84     base::FilePath crx_path = path;
85     DCHECK(crx_path.Extension() == FILE_PATH_LITERAL(".crx"));
86     if (crx_path.empty())
87       return NULL;
88
89     scoped_refptr<extensions::CrxInstaller> installer(
90         extensions::CrxInstaller::Create(service, install_ui.Pass()));
91     installer->set_expected_id(std::string());
92     installer->set_is_gallery_install(false);
93     installer->set_install_source(extensions::Manifest::INTERNAL);
94     installer->set_install_wait_for_idle(false);
95     installer->set_off_store_install_allow_reason(
96         extensions::CrxInstaller::OffStoreInstallAllowedInTest);
97
98     observer_->Watch(
99         chrome::NOTIFICATION_CRX_INSTALLER_DONE,
100         content::Source<extensions::CrxInstaller>(installer.get()));
101
102     installer->InstallCrx(crx_path);
103
104     observer_->Wait();
105   }
106
107   size_t num_after = service->extensions()->size();
108   if (num_before + 1 != num_after) {
109     VLOG(1) << "Num extensions before: " << base::IntToString(num_before)
110             << " num after: " << base::IntToString(num_after)
111             << " Installed extensions follow:";
112
113     for (ExtensionSet::const_iterator it = service->extensions()->begin();
114          it != service->extensions()->end(); ++it)
115       VLOG(1) << "  " << (*it)->id();
116
117     VLOG(1) << "Errors follow:";
118     const std::vector<string16>* errors =
119         ExtensionErrorReporter::GetInstance()->GetErrors();
120     for (std::vector<string16>::const_iterator iter = errors->begin();
121          iter != errors->end(); ++iter)
122       VLOG(1) << *iter;
123
124     return NULL;
125   }
126
127   if (!observer_->WaitForExtensionViewsToLoad())
128     return NULL;
129   return service->GetExtensionById(last_loaded_extension_id(), false);
130 }