| 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 ExtensionApiTest::ResultCatcher::ResultCatcher() { |
| 16 bool ExtensionApiTest::RunExtensionTest(const char* extension_name) { | 16 registrar_.Add(this, NotificationType::EXTENSION_TEST_PASSED, |
| 17 // Note the inner scope here. The |registrar| will fall out of scope and | 17 NotificationService::AllSources()); |
| 18 // remove listeners *before* the call to WaitForPassFail() below. | 18 registrar_.Add(this, NotificationType::EXTENSION_TEST_FAILED, |
| 19 { | 19 NotificationService::AllSources()); |
| 20 LOG(INFO) << "Running ExtensionApiTest with: " << extension_name; | |
| 21 NotificationRegistrar registrar; | |
| 22 registrar.Add(this, NotificationType::EXTENSION_TEST_PASSED, | |
| 23 NotificationService::AllSources()); | |
| 24 registrar.Add(this, NotificationType::EXTENSION_TEST_FAILED, | |
| 25 NotificationService::AllSources()); | |
| 26 | |
| 27 if (!LoadExtension(test_data_dir_.AppendASCII(extension_name))) { | |
| 28 message_ = "Failed to load extension."; | |
| 29 return false; | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 // TODO(erikkay) perhaps we shouldn't do this implicitly. | |
| 34 return WaitForPassFail(); | |
| 35 } | 20 } |
| 36 | 21 |
| 37 bool ExtensionApiTest::WaitForPassFail() { | 22 bool ExtensionApiTest::ResultCatcher::GetNextResult() { |
| 38 NotificationRegistrar registrar; | |
| 39 registrar.Add(this, NotificationType::EXTENSION_TEST_PASSED, | |
| 40 NotificationService::AllSources()); | |
| 41 registrar.Add(this, NotificationType::EXTENSION_TEST_FAILED, | |
| 42 NotificationService::AllSources()); | |
| 43 | |
| 44 // Depending on the tests, multiple results can come in from a single call | 23 // Depending on the tests, multiple results can come in from a single call |
| 45 // to RunMessageLoop(), so we maintain a queue of results and just pull them | 24 // to RunMessageLoop(), so we maintain a queue of results and just pull them |
| 46 // off as the test calls this, going to the run loop only when the queue is | 25 // off as the test calls this, going to the run loop only when the queue is |
| 47 // empty. | 26 // empty. |
| 48 if (!results_.size()) { | 27 if (!results_.size()) { |
| 49 MessageLoop::current()->PostDelayedTask( | 28 MessageLoop::current()->PostDelayedTask( |
| 50 FROM_HERE, new MessageLoop::QuitTask, kTimeoutMs); | 29 FROM_HERE, new MessageLoop::QuitTask, kTimeoutMs); |
| 51 ui_test_utils::RunMessageLoop(); | 30 ui_test_utils::RunMessageLoop(); |
| 52 } | 31 } |
| 53 if (results_.size()) { | 32 if (results_.size()) { |
| 54 bool ret = results_.front(); | 33 bool ret = results_.front(); |
| 55 results_.pop_front(); | 34 results_.pop_front(); |
| 56 message_ = messages_.front(); | 35 message_ = messages_.front(); |
| 57 messages_.pop_front(); | 36 messages_.pop_front(); |
| 58 return ret; | 37 return ret; |
| 59 } | 38 } |
| 60 message_ = "No response from message loop."; | 39 message_ = "No response from message loop."; |
| 61 return false; | 40 return false; |
| 62 } | 41 } |
| 63 | 42 |
| 64 void ExtensionApiTest::SetUpCommandLine(CommandLine* command_line) { | 43 void ExtensionApiTest::ResultCatcher::Observe( |
| 65 ExtensionBrowserTest::SetUpCommandLine(command_line); | 44 NotificationType type, const NotificationSource& source, |
| 66 test_data_dir_ = test_data_dir_.AppendASCII("api_test"); | 45 const NotificationDetails& details) { |
| 67 } | |
| 68 | |
| 69 void ExtensionApiTest::Observe(NotificationType type, | |
| 70 const NotificationSource& source, | |
| 71 const NotificationDetails& details) { | |
| 72 switch (type.value) { | 46 switch (type.value) { |
| 73 case NotificationType::EXTENSION_TEST_PASSED: | 47 case NotificationType::EXTENSION_TEST_PASSED: |
| 74 std::cout << "Got EXTENSION_TEST_PASSED notification.\n"; | 48 std::cout << "Got EXTENSION_TEST_PASSED notification.\n"; |
| 75 results_.push_back(true); | 49 results_.push_back(true); |
| 76 messages_.push_back(""); | 50 messages_.push_back(""); |
| 77 MessageLoopForUI::current()->Quit(); | 51 MessageLoopForUI::current()->Quit(); |
| 78 break; | 52 break; |
| 79 | 53 |
| 80 case NotificationType::EXTENSION_TEST_FAILED: | 54 case NotificationType::EXTENSION_TEST_FAILED: |
| 81 std::cout << "Got EXTENSION_TEST_FAILED notification.\n"; | 55 std::cout << "Got EXTENSION_TEST_FAILED notification.\n"; |
| 82 results_.push_back(false); | 56 results_.push_back(false); |
| 83 messages_.push_back(*(Details<std::string>(details).ptr())); | 57 messages_.push_back(*(Details<std::string>(details).ptr())); |
| 84 MessageLoopForUI::current()->Quit(); | 58 MessageLoopForUI::current()->Quit(); |
| 85 break; | 59 break; |
| 86 | 60 |
| 87 default: | 61 default: |
| 88 ExtensionBrowserTest::Observe(type, source, details); | 62 NOTREACHED(); |
| 89 } | 63 } |
| 90 } | 64 } |
| 65 |
| 66 // Load an extension and wait for it to notify of PASSED or FAILED. |
| 67 bool ExtensionApiTest::RunExtensionTest(const char* extension_name) { |
| 68 ResultCatcher catcher; |
| 69 |
| 70 LOG(INFO) << "Running ExtensionApiTest with: " << extension_name; |
| 71 if (!LoadExtension(test_data_dir_.AppendASCII(extension_name))) { |
| 72 message_ = "Failed to load extension."; |
| 73 return false; |
| 74 } |
| 75 |
| 76 if (!catcher.GetNextResult()) { |
| 77 message_ = catcher.message(); |
| 78 return false; |
| 79 } else { |
| 80 return true; |
| 81 } |
| 82 } |
| 83 |
| 84 void ExtensionApiTest::SetUpCommandLine(CommandLine* command_line) { |
| 85 ExtensionBrowserTest::SetUpCommandLine(command_line); |
| 86 test_data_dir_ = test_data_dir_.AppendASCII("api_test"); |
| 87 } |
| OLD | NEW |