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 const int kInstallTimeoutMs = 10000; | |
19 | |
20 } // namespace | |
21 | |
22 TestExtensionLoader::TestExtensionLoader(Profile* profile) | |
23 : profile_(profile), | |
24 extension_(NULL) { | |
25 registrar_.Add(this, NotificationType::EXTENSIONS_LOADED, | |
26 NotificationService::AllSources()); | |
27 | |
28 profile_->GetExtensionsService()->Init(); | |
29 profile_->GetExtensionsService()->set_show_extensions_prompts(false); | |
30 DCHECK(profile_->GetExtensionsService()->extensions()->empty()); | |
31 } | |
32 | |
33 Extension* TestExtensionLoader::Load(const char* extension_id, | |
34 const FilePath& path) { | |
35 loading_extension_id_ = extension_id; | |
36 | |
37 // Load the extension. | |
38 profile_->GetExtensionsService()->LoadExtension(path); | |
39 | |
40 // Wait for the load to complete. Stick a QuitTask into the message loop | |
41 // with the timeout so it will exit if the extension never loads. | |
42 extension_ = NULL; | |
43 MessageLoop::current()->PostDelayedTask(FROM_HERE, | |
44 new MessageLoop::QuitTask, kLoadTimeoutMs); | |
45 ui_test_utils::RunMessageLoop(); | |
46 | |
47 return extension_; | |
48 } | |
49 | |
50 Extension* TestExtensionLoader::Install(const char* extension_id, | |
51 const FilePath& path) { | |
52 loading_extension_id_ = extension_id; | |
53 | |
54 // Install the extension. When installed, the extension will automatically | |
55 // be loaded. | |
56 profile_->GetExtensionsService()->InstallExtension(path); | |
57 | |
58 // Wait for the load to complete. | |
59 extension_ = NULL; | |
60 MessageLoop::current()->PostDelayedTask(FROM_HERE, | |
61 new MessageLoop::QuitTask, kInstallTimeoutMs); | |
62 ui_test_utils::RunMessageLoop(); | |
63 | |
64 return extension_; | |
65 } | |
66 | |
67 void TestExtensionLoader::Observe(NotificationType type, | |
68 const NotificationSource& source, | |
69 const NotificationDetails& details) { | |
70 if (type == NotificationType::EXTENSIONS_LOADED) { | |
71 ExtensionList* extensions = Details<ExtensionList>(details).ptr(); | |
72 for (size_t i = 0; i < (*extensions).size(); ++i) { | |
73 if ((*extensions)[i]->id() == loading_extension_id_) { | |
74 extension_ = (*extensions)[i]; | |
75 MessageLoopForUI::current()->Quit(); | |
76 break; | |
77 } | |
78 } | |
79 } else { | |
80 NOTREACHED(); | |
81 } | |
82 } | |
OLD | NEW |