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