OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_apitest.h" | 5 #include "chrome/browser/extensions/extension_apitest.h" |
6 | 6 |
7 #include "chrome/browser/browser.h" | 7 #include "chrome/browser/browser.h" |
8 #include "chrome/common/notification_registrar.h" | 8 #include "chrome/common/notification_registrar.h" |
9 #include "chrome/test/ui_test_utils.h" | 9 #include "chrome/test/ui_test_utils.h" |
10 | 10 |
11 namespace { | 11 namespace { |
12 static const int kTimeoutMs = 60 * 1000; // 1 minute | 12 static const int kTimeoutMs = 60 * 1000; // 1 minute |
13 }; | 13 }; |
14 | 14 |
15 // Load an extension and wait for it to notify of PASSED or FAILED. | 15 // Load an extension and wait for it to notify of PASSED or FAILED. |
16 bool ExtensionApiTest::RunExtensionTest(const char* extension_name) { | 16 bool ExtensionApiTest::RunExtensionTest(const char* extension_name) { |
17 bool result; | |
18 completed_ = false; | |
19 { | 17 { |
20 NotificationRegistrar registrar; | 18 NotificationRegistrar registrar; |
21 registrar.Add(this, NotificationType::EXTENSION_TEST_PASSED, | 19 registrar.Add(this, NotificationType::EXTENSION_TEST_PASSED, |
22 NotificationService::AllSources()); | 20 NotificationService::AllSources()); |
23 registrar.Add(this, NotificationType::EXTENSION_TEST_FAILED, | 21 registrar.Add(this, NotificationType::EXTENSION_TEST_FAILED, |
24 NotificationService::AllSources()); | 22 NotificationService::AllSources()); |
25 result = LoadExtension(test_data_dir_.AppendASCII(extension_name)); | |
26 | 23 |
27 // If the test runs quickly, we may get the notification while waiting | 24 if (!LoadExtension(test_data_dir_.AppendASCII(extension_name))) { |
28 // for the Load to finish. | 25 message_ = "Failed to load extension."; |
29 if (completed_) { | 26 return false; |
30 result = passed_; | |
31 } else { | |
32 result = WaitForPassFail(); | |
33 } | 27 } |
34 } | 28 } |
35 return result; | 29 |
| 30 // TODO(erikkay) perhaps we shouldn't do this implicitly. |
| 31 return WaitForPassFail(); |
36 } | 32 } |
37 | 33 |
38 bool ExtensionApiTest::WaitForPassFail() { | 34 bool ExtensionApiTest::WaitForPassFail() { |
39 completed_ = false; | 35 NotificationRegistrar registrar; |
40 passed_ = false; | 36 registrar.Add(this, NotificationType::EXTENSION_TEST_PASSED, |
41 MessageLoop::current()->PostDelayedTask( | 37 NotificationService::AllSources()); |
42 FROM_HERE, new MessageLoop::QuitTask, kTimeoutMs); | 38 registrar.Add(this, NotificationType::EXTENSION_TEST_FAILED, |
43 ui_test_utils::RunMessageLoop(); | 39 NotificationService::AllSources()); |
44 return passed_; | 40 |
| 41 // Depending on the tests, multiple results can come in from a single call |
| 42 // to RunMessageLoop(), so we maintain a queue of results and just pull them |
| 43 // off as the test calls this, going to the run loop only when the queue is |
| 44 // empty. |
| 45 if (!results_.size()) { |
| 46 MessageLoop::current()->PostDelayedTask( |
| 47 FROM_HERE, new MessageLoop::QuitTask, kTimeoutMs); |
| 48 ui_test_utils::RunMessageLoop(); |
| 49 } |
| 50 if (results_.size()) { |
| 51 bool ret = results_.front(); |
| 52 results_.pop_front(); |
| 53 message_ = messages_.front(); |
| 54 messages_.pop_front(); |
| 55 return ret; |
| 56 } |
| 57 message_ = "No response from message loop."; |
| 58 return false; |
45 } | 59 } |
46 | 60 |
47 void ExtensionApiTest::SetUpCommandLine(CommandLine* command_line) { | 61 void ExtensionApiTest::SetUpCommandLine(CommandLine* command_line) { |
48 ExtensionBrowserTest::SetUpCommandLine(command_line); | 62 ExtensionBrowserTest::SetUpCommandLine(command_line); |
49 test_data_dir_ = test_data_dir_.AppendASCII("api_test"); | 63 test_data_dir_ = test_data_dir_.AppendASCII("api_test"); |
50 } | 64 } |
51 | 65 |
52 void ExtensionApiTest::Observe(NotificationType type, | 66 void ExtensionApiTest::Observe(NotificationType type, |
53 const NotificationSource& source, | 67 const NotificationSource& source, |
54 const NotificationDetails& details) { | 68 const NotificationDetails& details) { |
55 switch (type.value) { | 69 switch (type.value) { |
56 case NotificationType::EXTENSION_TEST_PASSED: | 70 case NotificationType::EXTENSION_TEST_PASSED: |
57 std::cout << "Got EXTENSION_TEST_PASSED notification.\n"; | 71 std::cout << "Got EXTENSION_TEST_PASSED notification.\n"; |
58 completed_ = true; | 72 results_.push_back(true); |
59 passed_ = true; | 73 messages_.push_back(""); |
60 MessageLoopForUI::current()->Quit(); | 74 MessageLoopForUI::current()->Quit(); |
61 break; | 75 break; |
62 | 76 |
63 case NotificationType::EXTENSION_TEST_FAILED: | 77 case NotificationType::EXTENSION_TEST_FAILED: |
64 std::cout << "Got EXTENSION_TEST_FAILED notification.\n"; | 78 std::cout << "Got EXTENSION_TEST_FAILED notification.\n"; |
65 completed_ = true; | 79 results_.push_back(false); |
66 passed_ = false; | 80 messages_.push_back(*(Details<std::string>(details).ptr())); |
67 message_ = *(Details<std::string>(details).ptr()); | |
68 MessageLoopForUI::current()->Quit(); | 81 MessageLoopForUI::current()->Quit(); |
69 break; | 82 break; |
70 | 83 |
71 default: | 84 default: |
72 ExtensionBrowserTest::Observe(type, source, details); | 85 ExtensionBrowserTest::Observe(type, source, details); |
73 } | 86 } |
74 } | 87 } |
OLD | NEW |