extension_browsertest.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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 "chrome/browser/extensions/extension_browsertest.h"
6
7#include <vector>
8
9#include "base/command_line.h"
10#include "base/file_util.h"
11#include "base/files/file_path.h"
12#include "base/files/scoped_temp_dir.h"
13#include "base/path_service.h"
14#include "base/stringprintf.h"
15#include "base/strings/string_number_conversions.h"
16#include "base/utf_string_conversions.h"
17#include "chrome/browser/extensions/component_loader.h"
18#include "chrome/browser/extensions/crx_installer.h"
19#include "chrome/browser/extensions/extension_creator.h"
20#include "chrome/browser/extensions/extension_error_reporter.h"
21#include "chrome/browser/extensions/extension_host.h"
22#include "chrome/browser/extensions/extension_install_prompt.h"
23#include "chrome/browser/extensions/extension_service.h"
24#include "chrome/browser/extensions/extension_system.h"
25#include "chrome/browser/extensions/unpacked_installer.h"
26#include "chrome/browser/profiles/profile.h"
27#include "chrome/browser/profiles/profile_manager.h"
28#include "chrome/browser/ui/browser.h"
29#include "chrome/browser/ui/browser_window.h"
30#include "chrome/browser/ui/omnibox/location_bar.h"
31#include "chrome/browser/ui/tabs/tab_strip_model.h"
32#include "chrome/common/chrome_notification_types.h"
33#include "chrome/common/chrome_paths.h"
34#include "chrome/common/chrome_switches.h"
35#include "chrome/common/chrome_version_info.h"
36#include "chrome/common/extensions/extension_manifest_constants.h"
37#include "chrome/common/extensions/extension_set.h"
38#include "chrome/test/base/ui_test_utils.h"
39#include "content/public/browser/navigation_controller.h"
40#include "content/public/browser/navigation_entry.h"
41#include "content/public/browser/notification_registrar.h"
42#include "content/public/browser/notification_service.h"
43#include "content/public/browser/render_view_host.h"
44#include "content/public/test/browser_test_utils.h"
45#include "extensions/common/constants.h"
46#include "sync/api/string_ordinal.h"
47
48#if defined(OS_CHROMEOS)
49#include "chromeos/chromeos_switches.h"
50#endif
51
52using extensions::Extension;
53using extensions::ExtensionCreator;
54using extensions::FeatureSwitch;
55using extensions::Manifest;
56
57ExtensionBrowserTest::ExtensionBrowserTest()
58    : loaded_(false),
59      installed_(false),
60      extension_installs_observed_(0),
61      extension_load_errors_observed_(0),
62      target_page_action_count_(-1),
63      target_visible_page_action_count_(-1),
64      current_channel_(chrome::VersionInfo::CHANNEL_DEV),
65      override_prompt_for_external_extensions_(
66          FeatureSwitch::prompt_for_external_extensions(), false),
67      profile_(NULL) {
68  EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
69}
70
71ExtensionBrowserTest::~ExtensionBrowserTest() {}
72
73Profile* ExtensionBrowserTest::profile() {
74  if (!profile_) {
75    if (browser())
76      profile_ = browser()->profile();
77    else
78      profile_ = ProfileManager::GetDefaultProfile();
79  }
80  return profile_;
81}
82
83// static
84const Extension* ExtensionBrowserTest::GetExtensionByPath(
85    const ExtensionSet* extensions, const base::FilePath& path) {
86  base::FilePath extension_path = base::MakeAbsoluteFilePath(path);
87  EXPECT_TRUE(!extension_path.empty());
88  for (ExtensionSet::const_iterator iter = extensions->begin();
89       iter != extensions->end(); ++iter) {
90    if ((*iter)->path() == extension_path) {
91      return *iter;
92    }
93  }
94  return NULL;
95}
96
97void ExtensionBrowserTest::SetUpCommandLine(CommandLine* command_line) {
98  PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
99  test_data_dir_ = test_data_dir_.AppendASCII("extensions");
100
101#if defined(OS_CHROMEOS)
102  // This makes sure that we create the Default profile first, with no
103  // ExtensionService and then the real profile with one, as we do when
104  // running on chromeos.
105  command_line->AppendSwitchASCII(chromeos::switches::kLoginUser,
106                                  "TestUser@gmail.com");
107  command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user");
108#endif
109}
110
111void ExtensionBrowserTest::SetUpOnMainThread() {
112  InProcessBrowserTest::SetUpOnMainThread();
113}
114
115const Extension* ExtensionBrowserTest::LoadExtensionWithFlags(
116    const base::FilePath& path, int flags) {
117  ExtensionService* service = extensions::ExtensionSystem::Get(
118      profile())->extension_service();
119  {
120    content::NotificationRegistrar registrar;
121    registrar.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
122                  content::NotificationService::AllSources());
123    scoped_refptr<extensions::UnpackedInstaller> installer(
124        extensions::UnpackedInstaller::Create(service));
125    installer->set_prompt_for_plugins(false);
126    installer->set_require_modern_manifest_version(
127        (flags & kFlagAllowOldManifestVersions) == 0);
128    installer->Load(path);
129    content::RunMessageLoop();
130  }
131
132  // Find the loaded extension by its path. See crbug.com/59531 for why
133  // we cannot just use last_loaded_extension_id_.
134  const Extension* extension = GetExtensionByPath(service->extensions(), path);
135  if (!extension)
136    return NULL;
137
138  if (!(flags & kFlagIgnoreManifestWarnings)) {
139    const std::vector<extensions::InstallWarning>& install_warnings =
140        extension->install_warnings();
141    if (!install_warnings.empty()) {
142      std::string install_warnings_message = base::StringPrintf(
143          "Unexpected warnings when loading test extension %s:\n",
144          path.AsUTF8Unsafe().c_str());
145
146      for (std::vector<extensions::InstallWarning>::const_iterator it =
147          install_warnings.begin(); it != install_warnings.end(); ++it) {
148        install_warnings_message += "  " + it->message + "\n";
149      }
150
151      EXPECT_TRUE(extension->install_warnings().empty()) <<
152          install_warnings_message;
153      return NULL;
154    }
155  }
156
157  const std::string extension_id = extension->id();
158
159  // The call to OnExtensionInstalled ensures the other extension prefs
160  // are set up with the defaults.
161  service->extension_prefs()->OnExtensionInstalled(
162      extension, Extension::ENABLED,
163      syncer::StringOrdinal::CreateInitialOrdinal());
164
165  // Toggling incognito or file access will reload the extension, so wait for
166  // the reload and grab the new extension instance. The default state is
167  // incognito disabled and file access enabled, so we don't wait in those
168  // cases.
169  {
170    content::WindowedNotificationObserver load_signal(
171        chrome::NOTIFICATION_EXTENSION_LOADED,
172        content::Source<Profile>(profile()));
173    CHECK(!service->IsIncognitoEnabled(extension_id));
174
175    if (flags & kFlagEnableIncognito) {
176      service->SetIsIncognitoEnabled(extension_id, true);
177      load_signal.Wait();
178      extension = service->GetExtensionById(extension_id, false);
179      CHECK(extension) << extension_id << " not found after reloading.";
180    }
181  }
182
183  {
184    content::WindowedNotificationObserver load_signal(
185        chrome::NOTIFICATION_EXTENSION_LOADED,
186        content::Source<Profile>(profile()));
187    CHECK(service->AllowFileAccess(extension));
188    if (!(flags & kFlagEnableFileAccess)) {
189      service->SetAllowFileAccess(extension, false);
190      load_signal.Wait();
191      extension = service->GetExtensionById(extension_id, false);
192      CHECK(extension) << extension_id << " not found after reloading.";
193    }
194  }
195
196  if (!WaitForExtensionViewsToLoad())
197    return NULL;
198
199  return extension;
200}
201
202const Extension* ExtensionBrowserTest::LoadExtension(
203    const base::FilePath& path) {
204  return LoadExtensionWithFlags(path, kFlagEnableFileAccess);
205}
206
207const Extension* ExtensionBrowserTest::LoadExtensionIncognito(
208    const base::FilePath& path) {
209  return LoadExtensionWithFlags(path,
210                                kFlagEnableFileAccess | kFlagEnableIncognito);
211}
212
213const Extension* ExtensionBrowserTest::LoadExtensionAsComponentWithManifest(
214    const base::FilePath& path,
215    const base::FilePath::CharType* manifest_relative_path) {
216  ExtensionService* service = extensions::ExtensionSystem::Get(
217      profile())->extension_service();
218
219  std::string manifest;
220  if (!file_util::ReadFileToString(path.Append(manifest_relative_path),
221                                   &manifest)) {
222    return NULL;
223  }
224
225  std::string extension_id = service->component_loader()->Add(manifest, path);
226  const Extension* extension = service->extensions()->GetByID(extension_id);
227  if (!extension)
228    return NULL;
229  last_loaded_extension_id_ = extension->id();
230  return extension;
231}
232
233const Extension* ExtensionBrowserTest::LoadExtensionAsComponent(
234    const base::FilePath& path) {
235  return LoadExtensionAsComponentWithManifest(path,
236                                              extensions::kManifestFilename);
237}
238
239base::FilePath ExtensionBrowserTest::PackExtension(
240    const base::FilePath& dir_path) {
241  base::FilePath crx_path = temp_dir_.path().AppendASCII("temp.crx");
242  if (!file_util::Delete(crx_path, false)) {
243    ADD_FAILURE() << "Failed to delete crx: " << crx_path.value();
244    return base::FilePath();
245  }
246
247  // Look for PEM files with the same name as the directory.
248  base::FilePath pem_path =
249      dir_path.ReplaceExtension(FILE_PATH_LITERAL(".pem"));
250  base::FilePath pem_path_out;
251
252  if (!file_util::PathExists(pem_path)) {
253    pem_path = base::FilePath();
254    pem_path_out = crx_path.DirName().AppendASCII("temp.pem");
255    if (!file_util::Delete(pem_path_out, false)) {
256      ADD_FAILURE() << "Failed to delete pem: " << pem_path_out.value();
257      return base::FilePath();
258    }
259  }
260
261  return PackExtensionWithOptions(dir_path, crx_path, pem_path, pem_path_out);
262}
263
264base::FilePath ExtensionBrowserTest::PackExtensionWithOptions(
265    const base::FilePath& dir_path,
266    const base::FilePath& crx_path,
267    const base::FilePath& pem_path,
268    const base::FilePath& pem_out_path) {
269  if (!file_util::PathExists(dir_path)) {
270    ADD_FAILURE() << "Extension dir not found: " << dir_path.value();
271    return base::FilePath();
272  }
273
274  if (!file_util::PathExists(pem_path) && pem_out_path.empty()) {
275    ADD_FAILURE() << "Must specify a PEM file or PEM output path";
276    return base::FilePath();
277  }
278
279  scoped_ptr<ExtensionCreator> creator(new ExtensionCreator());
280  if (!creator->Run(dir_path,
281                    crx_path,
282                    pem_path,
283                    pem_out_path,
284                    ExtensionCreator::kOverwriteCRX)) {
285    ADD_FAILURE() << "ExtensionCreator::Run() failed: "
286                  << creator->error_message();
287    return base::FilePath();
288  }
289
290  if (!file_util::PathExists(crx_path)) {
291    ADD_FAILURE() << crx_path.value() << " was not created.";
292    return base::FilePath();
293  }
294  return crx_path;
295}
296
297// This class is used to simulate an installation abort by the user.
298class MockAbortExtensionInstallPrompt : public ExtensionInstallPrompt {
299 public:
300  MockAbortExtensionInstallPrompt() : ExtensionInstallPrompt(NULL) {
301  }
302
303  // Simulate a user abort on an extension installation.
304  virtual void ConfirmInstall(
305      Delegate* delegate,
306      const Extension* extension,
307      const ShowDialogCallback& show_dialog_callback) OVERRIDE {
308    delegate->InstallUIAbort(true);
309    MessageLoopForUI::current()->Quit();
310  }
311
312  virtual void OnInstallSuccess(const Extension* extension,
313                                SkBitmap* icon) OVERRIDE {}
314
315  virtual void OnInstallFailure(
316      const extensions::CrxInstallerError& error) OVERRIDE {}
317};
318
319class MockAutoConfirmExtensionInstallPrompt : public ExtensionInstallPrompt {
320 public:
321  explicit MockAutoConfirmExtensionInstallPrompt(
322      content::WebContents* web_contents)
323    : ExtensionInstallPrompt(web_contents) {}
324
325  // Proceed without confirmation prompt.
326  virtual void ConfirmInstall(
327      Delegate* delegate,
328      const Extension* extension,
329      const ShowDialogCallback& show_dialog_callback) OVERRIDE {
330    delegate->InstallUIProceed();
331  }
332};
333
334const Extension* ExtensionBrowserTest::InstallExtensionFromWebstore(
335    const base::FilePath& path,
336    int expected_change) {
337  return InstallOrUpdateExtension(std::string(),
338                                  path,
339                                  INSTALL_UI_TYPE_NONE,
340                                  expected_change,
341                                  Manifest::INTERNAL,
342                                  browser(),
343                                  true);
344}
345
346const Extension* ExtensionBrowserTest::InstallOrUpdateExtension(
347    const std::string& id,
348    const base::FilePath& path,
349    InstallUIType ui_type,
350    int expected_change) {
351  return InstallOrUpdateExtension(id, path, ui_type, expected_change,
352                                  Manifest::INTERNAL, browser(), false);
353}
354
355const Extension* ExtensionBrowserTest::InstallOrUpdateExtension(
356    const std::string& id,
357    const base::FilePath& path,
358    InstallUIType ui_type,
359    int expected_change,
360    Browser* browser,
361    bool from_webstore) {
362  return InstallOrUpdateExtension(id, path, ui_type, expected_change,
363                                  Manifest::INTERNAL, browser, from_webstore);
364}
365
366const Extension* ExtensionBrowserTest::InstallOrUpdateExtension(
367    const std::string& id,
368    const base::FilePath& path,
369    InstallUIType ui_type,
370    int expected_change,
371    Manifest::Location install_source) {
372  return InstallOrUpdateExtension(id, path, ui_type, expected_change,
373                                  install_source, browser(), false);
374}
375
376const Extension* ExtensionBrowserTest::InstallOrUpdateExtension(
377    const std::string& id,
378    const base::FilePath& path,
379    InstallUIType ui_type,
380    int expected_change,
381    Manifest::Location install_source,
382    Browser* browser,
383    bool from_webstore) {
384  ExtensionService* service = profile()->GetExtensionService();
385  service->set_show_extensions_prompts(false);
386  size_t num_before = service->extensions()->size();
387
388  {
389    ExtensionInstallPrompt* install_ui = NULL;
390    if (ui_type == INSTALL_UI_TYPE_CANCEL) {
391      install_ui = new MockAbortExtensionInstallPrompt();
392    } else if (ui_type == INSTALL_UI_TYPE_NORMAL) {
393      install_ui = new ExtensionInstallPrompt(
394          browser->tab_strip_model()->GetActiveWebContents());
395    } else if (ui_type == INSTALL_UI_TYPE_AUTO_CONFIRM) {
396      install_ui = new MockAutoConfirmExtensionInstallPrompt(
397          browser->tab_strip_model()->GetActiveWebContents());
398    }
399
400    // TODO(tessamac): Update callers to always pass an unpacked extension
401    //                 and then always pack the extension here.
402    base::FilePath crx_path = path;
403    if (crx_path.Extension() != FILE_PATH_LITERAL(".crx")) {
404      crx_path = PackExtension(path);
405    }
406    if (crx_path.empty())
407      return NULL;
408
409    scoped_refptr<extensions::CrxInstaller> installer(
410        extensions::CrxInstaller::Create(service, install_ui));
411    installer->set_expected_id(id);
412    installer->set_is_gallery_install(from_webstore);
413    installer->set_install_source(install_source);
414    if (!from_webstore) {
415      installer->set_off_store_install_allow_reason(
416          extensions::CrxInstaller::OffStoreInstallAllowedInTest);
417    }
418
419    content::NotificationRegistrar registrar;
420    registrar.Add(this, chrome::NOTIFICATION_CRX_INSTALLER_DONE,
421                  content::Source<extensions::CrxInstaller>(installer.get()));
422
423    installer->InstallCrx(crx_path);
424
425    content::RunMessageLoop();
426  }
427
428  size_t num_after = service->extensions()->size();
429  EXPECT_EQ(num_before + expected_change, num_after);
430  if (num_before + expected_change != num_after) {
431    VLOG(1) << "Num extensions before: " << base::IntToString(num_before)
432            << " num after: " << base::IntToString(num_after)
433            << " Installed extensions follow:";
434
435    for (ExtensionSet::const_iterator it = service->extensions()->begin();
436         it != service->extensions()->end(); ++it)
437      VLOG(1) << "  " << (*it)->id();
438
439    VLOG(1) << "Errors follow:";
440    const std::vector<string16>* errors =
441        ExtensionErrorReporter::GetInstance()->GetErrors();
442    for (std::vector<string16>::const_iterator iter = errors->begin();
443         iter != errors->end(); ++iter)
444      VLOG(1) << *iter;
445
446    return NULL;
447  }
448
449  if (!WaitForExtensionViewsToLoad())
450    return NULL;
451  return service->GetExtensionById(last_loaded_extension_id_, false);
452}
453
454void ExtensionBrowserTest::ReloadExtension(const std::string& extension_id) {
455  ExtensionService* service = extensions::ExtensionSystem::Get(
456      profile())->extension_service();
457  service->ReloadExtension(extension_id);
458  ui_test_utils::RegisterAndWait(this,
459                                 chrome::NOTIFICATION_EXTENSION_LOADED,
460                                 content::NotificationService::AllSources());
461}
462
463void ExtensionBrowserTest::UnloadExtension(const std::string& extension_id) {
464  ExtensionService* service = extensions::ExtensionSystem::Get(
465      profile())->extension_service();
466  service->UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_DISABLE);
467}
468
469void ExtensionBrowserTest::UninstallExtension(const std::string& extension_id) {
470  ExtensionService* service = extensions::ExtensionSystem::Get(
471      profile())->extension_service();
472  service->UninstallExtension(extension_id, false, NULL);
473}
474
475void ExtensionBrowserTest::DisableExtension(const std::string& extension_id) {
476  ExtensionService* service = extensions::ExtensionSystem::Get(
477      profile())->extension_service();
478  service->DisableExtension(extension_id, Extension::DISABLE_USER_ACTION);
479}
480
481void ExtensionBrowserTest::EnableExtension(const std::string& extension_id) {
482  ExtensionService* service = extensions::ExtensionSystem::Get(
483      profile())->extension_service();
484  service->EnableExtension(extension_id);
485}
486
487bool ExtensionBrowserTest::WaitForPageActionCountChangeTo(int count) {
488  LocationBarTesting* location_bar =
489      browser()->window()->GetLocationBar()->GetLocationBarForTesting();
490  if (location_bar->PageActionCount() != count) {
491    target_page_action_count_ = count;
492    ui_test_utils::RegisterAndWait(this,
493        chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED,
494        content::NotificationService::AllSources());
495  }
496  return location_bar->PageActionCount() == count;
497}
498
499bool ExtensionBrowserTest::WaitForPageActionVisibilityChangeTo(int count) {
500  LocationBarTesting* location_bar =
501      browser()->window()->GetLocationBar()->GetLocationBarForTesting();
502  if (location_bar->PageActionVisibleCount() != count) {
503    target_visible_page_action_count_ = count;
504    ui_test_utils::RegisterAndWait(this,
505        chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED,
506        content::NotificationService::AllSources());
507  }
508  return location_bar->PageActionVisibleCount() == count;
509}
510
511bool ExtensionBrowserTest::WaitForExtensionViewsToLoad() {
512  // Wait for all the extension render view hosts that exist to finish loading.
513  content::NotificationRegistrar registrar;
514  registrar.Add(this, content::NOTIFICATION_LOAD_STOP,
515                content::NotificationService::AllSources());
516
517  ExtensionProcessManager* manager =
518      extensions::ExtensionSystem::Get(profile())->process_manager();
519  ExtensionProcessManager::ViewSet all_views = manager->GetAllViews();
520  for (ExtensionProcessManager::ViewSet::const_iterator iter =
521           all_views.begin();
522       iter != all_views.end();) {
523    if (!(*iter)->IsLoading()) {
524      ++iter;
525    } else {
526      content::RunMessageLoop();
527
528      // Test activity may have modified the set of extension processes during
529      // message processing, so re-start the iteration to catch added/removed
530      // processes.
531      all_views = manager->GetAllViews();
532      iter = all_views.begin();
533    }
534  }
535  return true;
536}
537
538bool ExtensionBrowserTest::WaitForExtensionInstall() {
539  int before = extension_installs_observed_;
540  ui_test_utils::RegisterAndWait(this,
541                                 chrome::NOTIFICATION_EXTENSION_INSTALLED,
542                                 content::NotificationService::AllSources());
543  return extension_installs_observed_ == (before + 1);
544}
545
546bool ExtensionBrowserTest::WaitForExtensionInstallError() {
547  int before = extension_installs_observed_;
548  ui_test_utils::RegisterAndWait(this,
549                                 chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR,
550                                 content::NotificationService::AllSources());
551  return extension_installs_observed_ == before;
552}
553
554void ExtensionBrowserTest::WaitForExtensionLoad() {
555  ui_test_utils::RegisterAndWait(this, chrome::NOTIFICATION_EXTENSION_LOADED,
556                                 content::NotificationService::AllSources());
557  WaitForExtensionViewsToLoad();
558}
559
560bool ExtensionBrowserTest::WaitForExtensionLoadError() {
561  int before = extension_load_errors_observed_;
562  ui_test_utils::RegisterAndWait(this,
563                                 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
564                                 content::NotificationService::AllSources());
565  return extension_load_errors_observed_ != before;
566}
567
568bool ExtensionBrowserTest::WaitForExtensionCrash(
569    const std::string& extension_id) {
570  ExtensionService* service = extensions::ExtensionSystem::Get(
571      profile())->extension_service();
572
573  if (!service->GetExtensionById(extension_id, true)) {
574    // The extension is already unloaded, presumably due to a crash.
575    return true;
576  }
577  ui_test_utils::RegisterAndWait(
578      this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
579      content::NotificationService::AllSources());
580  return (service->GetExtensionById(extension_id, true) == NULL);
581}
582
583bool ExtensionBrowserTest::WaitForCrxInstallerDone() {
584  int before = crx_installers_done_observed_;
585  ui_test_utils::RegisterAndWait(this,
586                                 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
587                                 content::NotificationService::AllSources());
588  return crx_installers_done_observed_ == (before + 1);
589}
590
591void ExtensionBrowserTest::OpenWindow(content::WebContents* contents,
592                                      const GURL& url,
593                                      bool newtab_process_should_equal_opener,
594                                      content::WebContents** newtab_result) {
595  content::WindowedNotificationObserver observer(
596      content::NOTIFICATION_LOAD_STOP,
597      content::NotificationService::AllSources());
598  ASSERT_TRUE(content::ExecuteScript(contents,
599                                     "window.open('" + url.spec() + "');"));
600
601  // The above window.open call is not user-initiated, so it will create
602  // a popup window instead of a new tab in current window.
603  // The stop notification will come from the new tab.
604  observer.Wait();
605  content::NavigationController* controller =
606      content::Source<content::NavigationController>(observer.source()).ptr();
607  content::WebContents* newtab = controller->GetWebContents();
608  ASSERT_TRUE(newtab);
609  EXPECT_EQ(url, controller->GetLastCommittedEntry()->GetURL());
610  if (newtab_process_should_equal_opener)
611    EXPECT_EQ(contents->GetRenderProcessHost(), newtab->GetRenderProcessHost());
612  else
613    EXPECT_NE(contents->GetRenderProcessHost(), newtab->GetRenderProcessHost());
614
615  if (newtab_result)
616    *newtab_result = newtab;
617}
618
619void ExtensionBrowserTest::NavigateInRenderer(content::WebContents* contents,
620                                              const GURL& url) {
621  bool result = false;
622  content::WindowedNotificationObserver observer(
623      content::NOTIFICATION_LOAD_STOP,
624      content::NotificationService::AllSources());
625  ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
626      contents,
627      "window.addEventListener('unload', function() {"
628      "    window.domAutomationController.send(true);"
629      "}, false);"
630      "window.location = '" + url.spec() + "';",
631      &result));
632  ASSERT_TRUE(result);
633  observer.Wait();
634  EXPECT_EQ(url, contents->GetController().GetLastCommittedEntry()->GetURL());
635}
636
637extensions::ExtensionHost* ExtensionBrowserTest::FindHostWithPath(
638    ExtensionProcessManager* manager,
639    const std::string& path,
640    int expected_hosts) {
641  extensions::ExtensionHost* host = NULL;
642  int num_hosts = 0;
643  ExtensionProcessManager::ExtensionHostSet background_hosts =
644      manager->background_hosts();
645  for (ExtensionProcessManager::const_iterator iter = background_hosts.begin();
646       iter != background_hosts.end(); ++iter) {
647    if ((*iter)->GetURL().path() == path) {
648      EXPECT_FALSE(host);
649      host = *iter;
650    }
651    num_hosts++;
652  }
653  EXPECT_EQ(expected_hosts, num_hosts);
654  return host;
655}
656
657void ExtensionBrowserTest::Observe(
658    int type,
659    const content::NotificationSource& source,
660    const content::NotificationDetails& details) {
661  switch (type) {
662    case chrome::NOTIFICATION_EXTENSION_LOADED:
663      last_loaded_extension_id_ =
664          content::Details<const Extension>(details).ptr()->id();
665      VLOG(1) << "Got EXTENSION_LOADED notification.";
666      MessageLoopForUI::current()->Quit();
667      break;
668
669    case chrome::NOTIFICATION_CRX_INSTALLER_DONE:
670      VLOG(1) << "Got CRX_INSTALLER_DONE notification.";
671      {
672        const Extension* extension =
673            content::Details<const Extension>(details).ptr();
674        if (extension)
675          last_loaded_extension_id_ = extension->id();
676        else
677          last_loaded_extension_id_ = "";
678      }
679      ++crx_installers_done_observed_;
680      MessageLoopForUI::current()->Quit();
681      break;
682
683    case chrome::NOTIFICATION_EXTENSION_INSTALLED:
684      VLOG(1) << "Got EXTENSION_INSTALLED notification.";
685      ++extension_installs_observed_;
686      MessageLoopForUI::current()->Quit();
687      break;
688
689    case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR:
690      VLOG(1) << "Got EXTENSION_INSTALL_ERROR notification.";
691      MessageLoopForUI::current()->Quit();
692      break;
693
694    case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED:
695      VLOG(1) << "Got EXTENSION_PROCESS_TERMINATED notification.";
696      MessageLoopForUI::current()->Quit();
697      break;
698
699    case chrome::NOTIFICATION_EXTENSION_LOAD_ERROR:
700      VLOG(1) << "Got EXTENSION_LOAD_ERROR notification.";
701      ++extension_load_errors_observed_;
702      MessageLoopForUI::current()->Quit();
703      break;
704
705    case chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED: {
706      LocationBarTesting* location_bar =
707          browser()->window()->GetLocationBar()->GetLocationBarForTesting();
708      VLOG(1) << "Got EXTENSION_PAGE_ACTION_COUNT_CHANGED notification. Number "
709                 "of page actions: " << location_bar->PageActionCount();
710      if (location_bar->PageActionCount() ==
711          target_page_action_count_) {
712        target_page_action_count_ = -1;
713        MessageLoopForUI::current()->Quit();
714      }
715      break;
716    }
717
718    case chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED: {
719      LocationBarTesting* location_bar =
720          browser()->window()->GetLocationBar()->GetLocationBarForTesting();
721      VLOG(1) << "Got EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED notification. "
722                 "Number of visible page actions: "
723              << location_bar->PageActionVisibleCount();
724      if (location_bar->PageActionVisibleCount() ==
725          target_visible_page_action_count_) {
726        target_visible_page_action_count_ = -1;
727        MessageLoopForUI::current()->Quit();
728      }
729      break;
730    }
731
732    case content::NOTIFICATION_LOAD_STOP:
733      VLOG(1) << "Got LOAD_STOP notification.";
734      MessageLoopForUI::current()->Quit();
735      break;
736
737    default:
738      NOTREACHED();
739      break;
740  }
741}
742