summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/test_extension_loader.cc
blob: d8bacb7bd6ac539971b32684c086c8113d9ad824 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/test_extension_loader.h"

#include "base/file_path.h"
#include "base/message_loop.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/common/notification_service.h"
#include "chrome/test/ui_test_utils.h"

namespace {

// How long to wait for the extension to load before giving up.
const int kLoadTimeoutMs = 5000;

}  // namespace

TestExtensionLoader::TestExtensionLoader(Profile* profile)
    : profile_(profile),
      extension_(NULL) {
  registrar_.Add(this, NotificationType::EXTENSIONS_LOADED,
      NotificationService::AllSources());

  profile_->GetExtensionsService()->Init();
  DCHECK(profile_->GetExtensionsService()->extensions()->empty()); 
}

Extension* TestExtensionLoader::Load(const char* extension_id,
                                     const FilePath& path) {
  loading_extension_id_ = extension_id;

  // Load the extension.
  profile_->GetExtensionsService()->LoadExtension(path);

  // Wait for the load to complete.  Stick a QuitTask into the message loop
  // with the timeout so it will exit if the extension never loads.
  extension_ = NULL;
  MessageLoop::current()->PostDelayedTask(FROM_HERE,
      new MessageLoop::QuitTask, kLoadTimeoutMs);
  ui_test_utils::RunMessageLoop();

  return extension_;
}

void TestExtensionLoader::Observe(NotificationType type,
                                  const NotificationSource& source,
                                  const NotificationDetails& details) {
  if (type == NotificationType::EXTENSIONS_LOADED) {
    ExtensionList* extensions = Details<ExtensionList>(details).ptr();
    for (size_t i = 0; i < (*extensions).size(); ++i) {
      if ((*extensions)[i]->id() == loading_extension_id_) {
        extension_ = (*extensions)[i];
        MessageLoopForUI::current()->Quit();
        break;
      }
    }
  } else {
    NOTREACHED();
  }
}