OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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/test_extension_loader.h" | |
6 | |
7 #include "base/file_path.h" | |
8 #include "base/message_loop.h" | |
9 #include "chrome/browser/profile.h" | |
10 #include "chrome/browser/extensions/extensions_service.h" | |
11 #include "chrome/common/notification_service.h" | |
12 #include "chrome/test/ui_test_utils.h" | |
13 | |
14 namespace { | |
15 | |
16 // How long to wait for the extension to load before giving up. | |
17 const int kLoadTimeoutMs = 5000; | |
18 | |
19 } // namespace | |
20 | |
21 TestExtensionLoader::TestExtensionLoader(Profile* profile) | |
22 : profile_(profile), | |
23 extension_(NULL) { | |
24 registrar_.Add(this, NotificationType::EXTENSIONS_LOADED, | |
25 NotificationService::AllSources()); | |
26 | |
27 profile_->GetExtensionsService()->Init(); | |
28 DCHECK(profile_->GetExtensionsService()->extensions()->empty()); | |
29 } | |
30 | |
31 Extension* TestExtensionLoader::Load(const char* extension_id, | |
32 const FilePath& path) { | |
33 loading_extension_id_ = extension_id; | |
34 | |
35 // Load the extension. | |
36 profile_->GetExtensionsService()->LoadExtension(path); | |
37 | |
38 // Wait for the load to complete. Stick a QuitTask into the message loop | |
39 // with the timeout so it will exit if the extension never loads. | |
40 extension_ = NULL; | |
41 MessageLoop::current()->PostDelayedTask(FROM_HERE, | |
42 new MessageLoop::QuitTask, kLoadTimeoutMs); | |
43 ui_test_utils::RunMessageLoop(); | |
44 | |
45 return extension_; | |
46 } | |
47 | |
48 void TestExtensionLoader::Observe(NotificationType type, | |
49 const NotificationSource& source, | |
50 const NotificationDetails& details) { | |
51 if (type == NotificationType::EXTENSIONS_LOADED) { | |
52 ExtensionList* extensions = Details<ExtensionList>(details).ptr(); | |
53 for (size_t i = 0; i < (*extensions).size(); ++i) { | |
54 if ((*extensions)[i]->id() == loading_extension_id_) { | |
55 extension_ = (*extensions)[i]; | |
56 MessageLoopForUI::current()->Quit(); | |
57 break; | |
58 } | |
59 } | |
60 } else { | |
61 NOTREACHED(); | |
62 } | |
63 } | |
OLD | NEW |