extension_browsertest.cc revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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/strings/string_number_conversions.h"
15#include "base/strings/stringprintf.h"
16#include "base/strings/utf_string_conversions.h"
17#include "chrome/browser/chrome_notification_types.h"
18#include "chrome/browser/extensions/component_loader.h"
19#include "chrome/browser/extensions/crx_installer.h"
20#include "chrome/browser/extensions/extension_creator.h"
21#include "chrome/browser/extensions/extension_error_reporter.h"
22#include "chrome/browser/extensions/extension_host.h"
23#include "chrome/browser/extensions/extension_install_prompt.h"
24#include "chrome/browser/extensions/extension_service.h"
25#include "chrome/browser/extensions/extension_system.h"
26#include "chrome/browser/extensions/unpacked_installer.h"
27#include "chrome/browser/profiles/profile.h"
28#include "chrome/browser/profiles/profile_manager.h"
29#include "chrome/browser/ui/browser.h"
30#include "chrome/browser/ui/browser_window.h"
31#include "chrome/browser/ui/omnibox/location_bar.h"
32#include "chrome/browser/ui/tabs/tab_strip_model.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->get();
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,
163      Extension::ENABLED,
164      extensions::Blacklist::NOT_BLACKLISTED,
165      syncer::StringOrdinal::CreateInitialOrdinal());
166
167  // Toggling incognito or file access will reload the extension, so wait for
168  // the reload and grab the new extension instance. The default state is
169  // incognito disabled and file access enabled, so we don't wait in those
170  // cases.
171  {
172    content::WindowedNotificationObserver load_signal(
173        chrome::NOTIFICATION_EXTENSION_LOADED,
174        content::Source<Profile>(profile()));
175    CHECK(!service->IsIncognitoEnabled(extension_id) ||
176          extension->force_incognito_enabled());
177
178    if (flags & kFlagEnableIncognito) {
179      service->SetIsIncognitoEnabled(extension_id, true);
180      load_signal.Wait();
181      extension = service->GetExtensionById(extension_id, false);
182      CHECK(extension) << extension_id << " not found after reloading.";
183    }
184  }
185
186  {
187    content::WindowedNotificationObserver load_signal(
188        chrome::NOTIFICATION_EXTENSION_LOADED,
189        content::Source<Profile>(profile()));
190    CHECK(service->AllowFileAccess(extension));
191    if (!(flags & kFlagEnableFileAccess)) {
192      service->SetAllowFileAccess(extension, false);
193      load_signal.Wait();
194      extension = service->GetExtensionById(extension_id, false);
195      CHECK(extension) << extension_id << " not found after reloading.";
196    }
197  }
198
199  if (!WaitForExtensionViewsToLoad())
200    return NULL;
201
202  return extension;
203}
204
205const Extension* ExtensionBrowserTest::LoadExtension(
206    const base::FilePath& path) {
207  return LoadExtensionWithFlags(path, kFlagEnableFileAccess);
208}
209
210const Extension* ExtensionBrowserTest::LoadExtensionIncognito(
211    const base::FilePath& path) {
212  return LoadExtensionWithFlags(path,
213                                kFlagEnableFileAccess | kFlagEnableIncognito);
214}
215
216const Extension* ExtensionBrowserTest::LoadExtensionAsComponentWithManifest(
217    const base::FilePath& path,
218    const base::FilePath::CharType* manifest_relative_path) {
219  ExtensionService* service = extensions::ExtensionSystem::Get(
220      profile())->extension_service();
221
222  std::string manifest;
223  if (!file_util::ReadFileToString(path.Append(manifest_relative_path),
224                                   &manifest)) {
225    return NULL;
226  }
227
228  std::string extension_id = service->component_loader()->Add(manifest, path);
229  const Extension* extension = service->extensions()->GetByID(extension_id);
230  if (!extension)
231    return NULL;
232  last_loaded_extension_id_ = extension->id();
233  return extension;
234}
235
236const Extension* ExtensionBrowserTest::LoadExtensionAsComponent(
237    const base::FilePath& path) {
238  return LoadExtensionAsComponentWithManifest(path,
239                                              extensions::kManifestFilename);
240}
241
242base::FilePath ExtensionBrowserTest::PackExtension(
243    const base::FilePath& dir_path) {
244  base::FilePath crx_path = temp_dir_.path().AppendASCII("temp.crx");
245  if (!base::DeleteFile(crx_path, false)) {
246    ADD_FAILURE() << "Failed to delete crx: " << crx_path.value();
247    return base::FilePath();
248  }
249
250  // Look for PEM files with the same name as the directory.
251  base::FilePath pem_path =
252      dir_path.ReplaceExtension(FILE_PATH_LITERAL(".pem"));
253  base::FilePath pem_path_out;
254
255  if (!base::PathExists(pem_path)) {
256    pem_path = base::FilePath();
257    pem_path_out = crx_path.DirName().AppendASCII("temp.pem");
258    if (!base::DeleteFile(pem_path_out, false)) {
259      ADD_FAILURE() << "Failed to delete pem: " << pem_path_out.value();
260      return base::FilePath();
261    }
262  }
263
264  return PackExtensionWithOptions(dir_path, crx_path, pem_path, pem_path_out);
265}
266
267base::FilePath ExtensionBrowserTest::PackExtensionWithOptions(
268    const base::FilePath& dir_path,
269    const base::FilePath& crx_path,
270    const base::FilePath& pem_path,
271    const base::FilePath& pem_out_path) {
272  if (!base::PathExists(dir_path)) {
273    ADD_FAILURE() << "Extension dir not found: " << dir_path.value();
274    return base::FilePath();
275  }
276
277  if (!base::PathExists(pem_path) && pem_out_path.empty()) {
278    ADD_FAILURE() << "Must specify a PEM file or PEM output path";
279    return base::FilePath();
280  }
281
282  scoped_ptr<ExtensionCreator> creator(new ExtensionCreator());
283  if (!creator->Run(dir_path,
284                    crx_path,
285                    pem_path,
286                    pem_out_path,
287                    ExtensionCreator::kOverwriteCRX)) {
288    ADD_FAILURE() << "ExtensionCreator::Run() failed: "
289                  << creator->error_message();
290    return base::FilePath();
291  }
292
293  if (!base::PathExists(crx_path)) {
294    ADD_FAILURE() << crx_path.value() << " was not created.";
295    return base::FilePath();
296  }
297  return crx_path;
298}
299
300// This class is used to simulate an installation abort by the user.
301class MockAbortExtensionInstallPrompt : public ExtensionInstallPrompt {
302 public:
303  MockAbortExtensionInstallPrompt() : ExtensionInstallPrompt(NULL) {
304  }
305
306  // Simulate a user abort on an extension installation.
307  virtual void ConfirmInstall(
308      Delegate* delegate,
309      const Extension* extension,
310      const ShowDialogCallback& show_dialog_callback) OVERRIDE {
311    delegate->InstallUIAbort(true);
312    base::MessageLoopForUI::current()->Quit();
313  }
314
315  virtual void OnInstallSuccess(const Extension* extension,
316                                SkBitmap* icon) OVERRIDE {}
317
318  virtual void OnInstallFailure(
319      const extensions::CrxInstallerError& error) OVERRIDE {}
320};
321
322class MockAutoConfirmExtensionInstallPrompt : public ExtensionInstallPrompt {
323 public:
324  explicit MockAutoConfirmExtensionInstallPrompt(
325      content::WebContents* web_contents)
326    : ExtensionInstallPrompt(web_contents) {}
327
328  // Proceed without confirmation prompt.
329  virtual void ConfirmInstall(
330      Delegate* delegate,
331      const Extension* extension,
332      const ShowDialogCallback& show_dialog_callback) OVERRIDE {
333    delegate->InstallUIProceed();
334  }
335};
336
337const Extension* ExtensionBrowserTest::UpdateExtensionWaitForIdle(
338    const std::string& id,
339    const base::FilePath& path,
340    int expected_change) {
341  return InstallOrUpdateExtension(id,
342                                  path,
343                                  INSTALL_UI_TYPE_NONE,
344                                  expected_change,
345                                  Manifest::INTERNAL,
346                                  browser(),
347                                  false,
348                                  true);
349}
350
351const Extension* ExtensionBrowserTest::InstallExtensionFromWebstore(
352    const base::FilePath& path,
353    int expected_change) {
354  return InstallOrUpdateExtension(std::string(),
355                                  path,
356                                  INSTALL_UI_TYPE_NONE,
357                                  expected_change,
358                                  Manifest::INTERNAL,
359                                  browser(),
360                                  true,
361                                  false);
362}
363
364const Extension* ExtensionBrowserTest::InstallOrUpdateExtension(
365    const std::string& id,
366    const base::FilePath& path,
367    InstallUIType ui_type,
368    int expected_change) {
369  return InstallOrUpdateExtension(id, path, ui_type, expected_change,
370                                  Manifest::INTERNAL, browser(), false, false);
371}
372
373const Extension* ExtensionBrowserTest::InstallOrUpdateExtension(
374    const std::string& id,
375    const base::FilePath& path,
376    InstallUIType ui_type,
377    int expected_change,
378    Browser* browser,
379    bool from_webstore) {
380  return InstallOrUpdateExtension(id, path, ui_type, expected_change,
381                                  Manifest::INTERNAL, browser, from_webstore,
382                                  false);
383}
384
385const Extension* ExtensionBrowserTest::InstallOrUpdateExtension(
386    const std::string& id,
387    const base::FilePath& path,
388    InstallUIType ui_type,
389    int expected_change,
390    Manifest::Location install_source) {
391  return InstallOrUpdateExtension(id, path, ui_type, expected_change,
392                                  install_source, browser(), false, false);
393}
394
395const Extension* ExtensionBrowserTest::InstallOrUpdateExtension(
396    const std::string& id,
397    const base::FilePath& path,
398    InstallUIType ui_type,
399    int expected_change,
400    Manifest::Location install_source,
401    Browser* browser,
402    bool from_webstore,
403    bool wait_for_idle) {
404  ExtensionService* service = profile()->GetExtensionService();
405  service->set_show_extensions_prompts(false);
406  size_t num_before = service->extensions()->size();
407
408  {
409    ExtensionInstallPrompt* install_ui = NULL;
410    if (ui_type == INSTALL_UI_TYPE_CANCEL) {
411      install_ui = new MockAbortExtensionInstallPrompt();
412    } else if (ui_type == INSTALL_UI_TYPE_NORMAL) {
413      install_ui = new ExtensionInstallPrompt(
414          browser->tab_strip_model()->GetActiveWebContents());
415    } else if (ui_type == INSTALL_UI_TYPE_AUTO_CONFIRM) {
416      install_ui = new MockAutoConfirmExtensionInstallPrompt(
417          browser->tab_strip_model()->GetActiveWebContents());
418    }
419
420    // TODO(tessamac): Update callers to always pass an unpacked extension
421    //                 and then always pack the extension here.
422    base::FilePath crx_path = path;
423    if (crx_path.Extension() != FILE_PATH_LITERAL(".crx")) {
424      crx_path = PackExtension(path);
425    }
426    if (crx_path.empty())
427      return NULL;
428
429    scoped_refptr<extensions::CrxInstaller> installer(
430        extensions::CrxInstaller::Create(service, install_ui));
431    installer->set_expected_id(id);
432    installer->set_is_gallery_install(from_webstore);
433    installer->set_install_source(install_source);
434    installer->set_install_wait_for_idle(wait_for_idle);
435    if (!from_webstore) {
436      installer->set_off_store_install_allow_reason(
437          extensions::CrxInstaller::OffStoreInstallAllowedInTest);
438    }
439
440    content::NotificationRegistrar registrar;
441    registrar.Add(this, chrome::NOTIFICATION_CRX_INSTALLER_DONE,
442                  content::Source<extensions::CrxInstaller>(installer.get()));
443
444    installer->InstallCrx(crx_path);
445
446    content::RunMessageLoop();
447  }
448
449  size_t num_after = service->extensions()->size();
450  EXPECT_EQ(num_before + expected_change, num_after);
451  if (num_before + expected_change != num_after) {
452    VLOG(1) << "Num extensions before: " << base::IntToString(num_before)
453            << " num after: " << base::IntToString(num_after)
454            << " Installed extensions follow:";
455
456    for (ExtensionSet::const_iterator it = service->extensions()->begin();
457         it != service->extensions()->end(); ++it)
458      VLOG(1) << "  " << (*it)->id();
459
460    VLOG(1) << "Errors follow:";
461    const std::vector<string16>* errors =
462        ExtensionErrorReporter::GetInstance()->GetErrors();
463    for (std::vector<string16>::const_iterator iter = errors->begin();
464         iter != errors->end(); ++iter)
465      VLOG(1) << *iter;
466
467    return NULL;
468  }
469
470  if (!WaitForExtensionViewsToLoad())
471    return NULL;
472  return service->GetExtensionById(last_loaded_extension_id_, false);
473}
474
475void ExtensionBrowserTest::ReloadExtension(const std::string extension_id) {
476  ExtensionService* service = extensions::ExtensionSystem::Get(
477      profile())->extension_service();
478  service->ReloadExtension(extension_id);
479  ui_test_utils::RegisterAndWait(this,
480                                 chrome::NOTIFICATION_EXTENSION_LOADED,
481                                 content::NotificationService::AllSources());
482}
483
484void ExtensionBrowserTest::UnloadExtension(const std::string& extension_id) {
485  ExtensionService* service = extensions::ExtensionSystem::Get(
486      profile())->extension_service();
487  service->UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_DISABLE);
488}
489
490void ExtensionBrowserTest::UninstallExtension(const std::string& extension_id) {
491  ExtensionService* service = extensions::ExtensionSystem::Get(
492      profile())->extension_service();
493  service->UninstallExtension(extension_id, false, NULL);
494}
495
496void ExtensionBrowserTest::DisableExtension(const std::string& extension_id) {
497  ExtensionService* service = extensions::ExtensionSystem::Get(
498      profile())->extension_service();
499  service->DisableExtension(extension_id, Extension::DISABLE_USER_ACTION);
500}
501
502void ExtensionBrowserTest::EnableExtension(const std::string& extension_id) {
503  ExtensionService* service = extensions::ExtensionSystem::Get(
504      profile())->extension_service();
505  service->EnableExtension(extension_id);
506}
507
508bool ExtensionBrowserTest::WaitForPageActionCountChangeTo(int count) {
509  LocationBarTesting* location_bar =
510      browser()->window()->GetLocationBar()->GetLocationBarForTesting();
511  if (location_bar->PageActionCount() != count) {
512    target_page_action_count_ = count;
513    ui_test_utils::RegisterAndWait(this,
514        chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED,
515        content::NotificationService::AllSources());
516  }
517  return location_bar->PageActionCount() == count;
518}
519
520bool ExtensionBrowserTest::WaitForPageActionVisibilityChangeTo(int count) {
521  LocationBarTesting* location_bar =
522      browser()->window()->GetLocationBar()->GetLocationBarForTesting();
523  if (location_bar->PageActionVisibleCount() != count) {
524    target_visible_page_action_count_ = count;
525    ui_test_utils::RegisterAndWait(this,
526        chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED,
527        content::NotificationService::AllSources());
528  }
529  return location_bar->PageActionVisibleCount() == count;
530}
531
532bool ExtensionBrowserTest::WaitForExtensionViewsToLoad() {
533  // Wait for all the extension render view hosts that exist to finish loading.
534  content::NotificationRegistrar registrar;
535  registrar.Add(this, content::NOTIFICATION_LOAD_STOP,
536                content::NotificationService::AllSources());
537
538  ExtensionProcessManager* manager =
539      extensions::ExtensionSystem::Get(profile())->process_manager();
540  ExtensionProcessManager::ViewSet all_views = manager->GetAllViews();
541  for (ExtensionProcessManager::ViewSet::const_iterator iter =
542           all_views.begin();
543       iter != all_views.end();) {
544    if (!(*iter)->IsLoading()) {
545      ++iter;
546    } else {
547      content::RunMessageLoop();
548
549      // Test activity may have modified the set of extension processes during
550      // message processing, so re-start the iteration to catch added/removed
551      // processes.
552      all_views = manager->GetAllViews();
553      iter = all_views.begin();
554    }
555  }
556  return true;
557}
558
559bool ExtensionBrowserTest::WaitForExtensionInstall() {
560  int before = extension_installs_observed_;
561  ui_test_utils::RegisterAndWait(this,
562                                 chrome::NOTIFICATION_EXTENSION_INSTALLED,
563                                 content::NotificationService::AllSources());
564  return extension_installs_observed_ == (before + 1);
565}
566
567bool ExtensionBrowserTest::WaitForExtensionInstallError() {
568  int before = extension_installs_observed_;
569  ui_test_utils::RegisterAndWait(this,
570                                 chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR,
571                                 content::NotificationService::AllSources());
572  return extension_installs_observed_ == before;
573}
574
575void ExtensionBrowserTest::WaitForExtensionLoad() {
576  ui_test_utils::RegisterAndWait(this, chrome::NOTIFICATION_EXTENSION_LOADED,
577                                 content::NotificationService::AllSources());
578  WaitForExtensionViewsToLoad();
579}
580
581bool ExtensionBrowserTest::WaitForExtensionLoadError() {
582  int before = extension_load_errors_observed_;
583  ui_test_utils::RegisterAndWait(this,
584                                 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
585                                 content::NotificationService::AllSources());
586  return extension_load_errors_observed_ != before;
587}
588
589bool ExtensionBrowserTest::WaitForExtensionCrash(
590    const std::string& extension_id) {
591  ExtensionService* service = extensions::ExtensionSystem::Get(
592      profile())->extension_service();
593
594  if (!service->GetExtensionById(extension_id, true)) {
595    // The extension is already unloaded, presumably due to a crash.
596    return true;
597  }
598  ui_test_utils::RegisterAndWait(
599      this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
600      content::NotificationService::AllSources());
601  return (service->GetExtensionById(extension_id, true) == NULL);
602}
603
604bool ExtensionBrowserTest::WaitForCrxInstallerDone() {
605  int before = crx_installers_done_observed_;
606  ui_test_utils::RegisterAndWait(this,
607                                 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
608                                 content::NotificationService::AllSources());
609  return crx_installers_done_observed_ == (before + 1);
610}
611
612void ExtensionBrowserTest::OpenWindow(content::WebContents* contents,
613                                      const GURL& url,
614                                      bool newtab_process_should_equal_opener,
615                                      content::WebContents** newtab_result) {
616  content::WindowedNotificationObserver observer(
617      content::NOTIFICATION_LOAD_STOP,
618      content::NotificationService::AllSources());
619  ASSERT_TRUE(content::ExecuteScript(contents,
620                                     "window.open('" + url.spec() + "');"));
621
622  // The above window.open call is not user-initiated, so it will create
623  // a popup window instead of a new tab in current window.
624  // The stop notification will come from the new tab.
625  observer.Wait();
626  content::NavigationController* controller =
627      content::Source<content::NavigationController>(observer.source()).ptr();
628  content::WebContents* newtab = controller->GetWebContents();
629  ASSERT_TRUE(newtab);
630  EXPECT_EQ(url, controller->GetLastCommittedEntry()->GetURL());
631  if (newtab_process_should_equal_opener)
632    EXPECT_EQ(contents->GetRenderProcessHost(), newtab->GetRenderProcessHost());
633  else
634    EXPECT_NE(contents->GetRenderProcessHost(), newtab->GetRenderProcessHost());
635
636  if (newtab_result)
637    *newtab_result = newtab;
638}
639
640void ExtensionBrowserTest::NavigateInRenderer(content::WebContents* contents,
641                                              const GURL& url) {
642  bool result = false;
643  content::WindowedNotificationObserver observer(
644      content::NOTIFICATION_LOAD_STOP,
645      content::NotificationService::AllSources());
646  ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
647      contents,
648      "window.addEventListener('unload', function() {"
649      "    window.domAutomationController.send(true);"
650      "}, false);"
651      "window.location = '" + url.spec() + "';",
652      &result));
653  ASSERT_TRUE(result);
654  observer.Wait();
655  EXPECT_EQ(url, contents->GetController().GetLastCommittedEntry()->GetURL());
656}
657
658extensions::ExtensionHost* ExtensionBrowserTest::FindHostWithPath(
659    ExtensionProcessManager* manager,
660    const std::string& path,
661    int expected_hosts) {
662  extensions::ExtensionHost* host = NULL;
663  int num_hosts = 0;
664  ExtensionProcessManager::ExtensionHostSet background_hosts =
665      manager->background_hosts();
666  for (ExtensionProcessManager::const_iterator iter = background_hosts.begin();
667       iter != background_hosts.end(); ++iter) {
668    if ((*iter)->GetURL().path() == path) {
669      EXPECT_FALSE(host);
670      host = *iter;
671    }
672    num_hosts++;
673  }
674  EXPECT_EQ(expected_hosts, num_hosts);
675  return host;
676}
677
678void ExtensionBrowserTest::Observe(
679    int type,
680    const content::NotificationSource& source,
681    const content::NotificationDetails& details) {
682  switch (type) {
683    case chrome::NOTIFICATION_EXTENSION_LOADED:
684      last_loaded_extension_id_ =
685          content::Details<const Extension>(details).ptr()->id();
686      VLOG(1) << "Got EXTENSION_LOADED notification.";
687      base::MessageLoopForUI::current()->Quit();
688      break;
689
690    case chrome::NOTIFICATION_CRX_INSTALLER_DONE:
691      VLOG(1) << "Got CRX_INSTALLER_DONE notification.";
692      {
693        const Extension* extension =
694            content::Details<const Extension>(details).ptr();
695        if (extension)
696          last_loaded_extension_id_ = extension->id();
697        else
698          last_loaded_extension_id_ = "";
699      }
700      ++crx_installers_done_observed_;
701      base::MessageLoopForUI::current()->Quit();
702      break;
703
704    case chrome::NOTIFICATION_EXTENSION_INSTALLED:
705      VLOG(1) << "Got EXTENSION_INSTALLED notification.";
706      ++extension_installs_observed_;
707      base::MessageLoopForUI::current()->Quit();
708      break;
709
710    case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR:
711      VLOG(1) << "Got EXTENSION_INSTALL_ERROR notification.";
712      base::MessageLoopForUI::current()->Quit();
713      break;
714
715    case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED:
716      VLOG(1) << "Got EXTENSION_PROCESS_TERMINATED notification.";
717      base::MessageLoopForUI::current()->Quit();
718      break;
719
720    case chrome::NOTIFICATION_EXTENSION_LOAD_ERROR:
721      VLOG(1) << "Got EXTENSION_LOAD_ERROR notification.";
722      ++extension_load_errors_observed_;
723      base::MessageLoopForUI::current()->Quit();
724      break;
725
726    case chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED: {
727      LocationBarTesting* location_bar =
728          browser()->window()->GetLocationBar()->GetLocationBarForTesting();
729      VLOG(1) << "Got EXTENSION_PAGE_ACTION_COUNT_CHANGED notification. Number "
730                 "of page actions: " << location_bar->PageActionCount();
731      if (location_bar->PageActionCount() ==
732          target_page_action_count_) {
733        target_page_action_count_ = -1;
734        base::MessageLoopForUI::current()->Quit();
735      }
736      break;
737    }
738
739    case chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED: {
740      LocationBarTesting* location_bar =
741          browser()->window()->GetLocationBar()->GetLocationBarForTesting();
742      VLOG(1) << "Got EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED notification. "
743                 "Number of visible page actions: "
744              << location_bar->PageActionVisibleCount();
745      if (location_bar->PageActionVisibleCount() ==
746          target_visible_page_action_count_) {
747        target_visible_page_action_count_ = -1;
748        base::MessageLoopForUI::current()->Quit();
749      }
750      break;
751    }
752
753    case content::NOTIFICATION_LOAD_STOP:
754      VLOG(1) << "Got LOAD_STOP notification.";
755      base::MessageLoopForUI::current()->Quit();
756      break;
757
758    default:
759      NOTREACHED();
760      break;
761  }
762}
763