Upstream version 11.40.277.0
[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_install_prompt_show_params.h"
15 #include "chrome/browser/extensions/extension_service.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 #include "extensions/browser/extension_system.h"
28 #include "extensions/common/extension_set.h"
29
30 using extensions::Extension;
31
32 ExtensionSettingsUIBrowserTest::ExtensionSettingsUIBrowserTest()
33     : profile_(NULL) {}
34
35 ExtensionSettingsUIBrowserTest::~ExtensionSettingsUIBrowserTest() {}
36
37 Profile* ExtensionSettingsUIBrowserTest::GetProfile() {
38   if (!profile_) {
39     profile_ = browser() ? browser()->profile() :
40                            ProfileManager::GetActiveUserProfile();
41   }
42   return profile_;
43 }
44
45 void ExtensionSettingsUIBrowserTest::SetUpOnMainThread() {
46   WebUIBrowserTest::SetUpOnMainThread();
47   observer_.reset(new ExtensionTestNotificationObserver(browser()));
48 }
49
50 void ExtensionSettingsUIBrowserTest::InstallGoodExtension() {
51   base::FilePath test_data_dir;
52   if (!PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)) {
53     ADD_FAILURE();
54     return;
55   }
56   base::FilePath extensions_data_dir = test_data_dir.AppendASCII("extensions");
57   InstallExtension(extensions_data_dir.AppendASCII("good.crx"));
58 }
59
60 class MockAutoConfirmExtensionInstallPrompt : public ExtensionInstallPrompt {
61  public:
62   explicit MockAutoConfirmExtensionInstallPrompt(
63       content::WebContents* web_contents)
64     : ExtensionInstallPrompt(web_contents) {}
65
66   // Proceed without confirmation prompt.
67   void ConfirmInstall(Delegate* delegate,
68                       const Extension* extension,
69                       const ShowDialogCallback& show_dialog_callback) override {
70     delegate->InstallUIProceed();
71   }
72 };
73
74 const Extension* ExtensionSettingsUIBrowserTest::InstallExtension(
75     const base::FilePath& path) {
76   Profile* profile = this->GetProfile();
77   ExtensionService* service =
78       extensions::ExtensionSystem::Get(profile)->extension_service();
79   service->set_show_extensions_prompts(false);
80   size_t num_before = service->extensions()->size();
81   {
82     scoped_ptr<ExtensionInstallPrompt> install_ui;
83     install_ui.reset(new MockAutoConfirmExtensionInstallPrompt(
84         browser()->tab_strip_model()->GetActiveWebContents()));
85
86     base::FilePath crx_path = path;
87     DCHECK(crx_path.Extension() == FILE_PATH_LITERAL(".crx"));
88     if (crx_path.empty())
89       return NULL;
90
91     scoped_refptr<extensions::CrxInstaller> installer(
92         extensions::CrxInstaller::Create(service, install_ui.Pass()));
93     installer->set_expected_id(std::string());
94     installer->set_is_gallery_install(false);
95     installer->set_install_source(extensions::Manifest::INTERNAL);
96     installer->set_install_immediately(true);
97     installer->set_off_store_install_allow_reason(
98         extensions::CrxInstaller::OffStoreInstallAllowedInTest);
99
100     observer_->Watch(
101         extensions::NOTIFICATION_CRX_INSTALLER_DONE,
102         content::Source<extensions::CrxInstaller>(installer.get()));
103
104     installer->InstallCrx(crx_path);
105
106     observer_->Wait();
107   }
108
109   size_t num_after = service->extensions()->size();
110   if (num_before + 1 != num_after) {
111     VLOG(1) << "Num extensions before: " << base::IntToString(num_before)
112             << " num after: " << base::IntToString(num_after)
113             << " Installed extensions follow:";
114
115     for (extensions::ExtensionSet::const_iterator it =
116              service->extensions()->begin();
117          it != service->extensions()->end(); ++it)
118       VLOG(1) << "  " << (*it)->id();
119
120     VLOG(1) << "Errors follow:";
121     const std::vector<base::string16>* errors =
122         ExtensionErrorReporter::GetInstance()->GetErrors();
123     for (std::vector<base::string16>::const_iterator iter = errors->begin();
124          iter != errors->end(); ++iter)
125       VLOG(1) << *iter;
126
127     return NULL;
128   }
129
130   if (!observer_->WaitForExtensionViewsToLoad())
131     return NULL;
132   return service->GetExtensionById(last_loaded_extension_id(), false);
133 }