Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / browser / test_extension_registry_observer.cc
blobb4f5c6033c3e518cd9c836217bea83918c7fd18c
1 // Copyright 2014 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.
5 #include "extensions/browser/test_extension_registry_observer.h"
7 #include "content/public/test/test_utils.h"
8 #include "extensions/browser/extension_registry.h"
10 namespace extensions {
12 class TestExtensionRegistryObserver::Waiter {
13 public:
14 Waiter(const std::string& extension_id) : observed_(false), runner_(NULL) {}
16 void Wait() {
17 if (observed_)
18 return;
20 runner_ = new content::MessageLoopRunner();
21 runner_->Run();
24 void OnObserved() {
25 observed_ = true;
27 if (runner_.get()) {
28 runner_->Quit();
29 runner_ = NULL;
33 private:
34 bool observed_;
35 scoped_refptr<content::MessageLoopRunner> runner_;
37 DISALLOW_COPY_AND_ASSIGN(Waiter);
40 TestExtensionRegistryObserver::TestExtensionRegistryObserver(
41 ExtensionRegistry* registry,
42 const std::string& extension_id)
43 : will_be_installed_waiter_(new Waiter(extension_id)),
44 uninstalled_waiter_(new Waiter(extension_id)),
45 loaded_waiter_(new Waiter(extension_id)),
46 unloaded_waiter_(new Waiter(extension_id)),
47 extension_registry_observer_(this),
48 extension_id_(extension_id) {
49 extension_registry_observer_.Add(registry);
52 TestExtensionRegistryObserver::~TestExtensionRegistryObserver() {
55 void TestExtensionRegistryObserver::WaitForExtensionUninstalled() {
56 uninstalled_waiter_->Wait();
59 void TestExtensionRegistryObserver::WaitForExtensionWillBeInstalled() {
60 will_be_installed_waiter_->Wait();
63 void TestExtensionRegistryObserver::WaitForExtensionLoaded() {
64 loaded_waiter_->Wait();
67 void TestExtensionRegistryObserver::WaitForExtensionUnloaded() {
68 unloaded_waiter_->Wait();
71 void TestExtensionRegistryObserver::OnExtensionWillBeInstalled(
72 content::BrowserContext* browser_context,
73 const Extension* extension,
74 bool is_update,
75 bool from_ephemeral,
76 const std::string& old_name) {
77 if (extension->id() == extension_id_)
78 will_be_installed_waiter_->OnObserved();
81 void TestExtensionRegistryObserver::OnExtensionUninstalled(
82 content::BrowserContext* browser_context,
83 const Extension* extension,
84 extensions::UninstallReason reason) {
85 if (extension->id() == extension_id_)
86 uninstalled_waiter_->OnObserved();
89 void TestExtensionRegistryObserver::OnExtensionLoaded(
90 content::BrowserContext* browser_context,
91 const Extension* extension) {
92 if (extension->id() == extension_id_)
93 loaded_waiter_->OnObserved();
96 void TestExtensionRegistryObserver::OnExtensionUnloaded(
97 content::BrowserContext* browser_context,
98 const Extension* extension,
99 UnloadedExtensionInfo::Reason reason) {
100 if (extension->id() == extension_id_)
101 unloaded_waiter_->OnObserved();
104 } // namespace extensions