| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_test_api.h" | 5 #include "chrome/browser/extensions/api/test/test_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 12 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
| 13 #include "chrome/browser/extensions/extensions_quota_service.h" | 13 #include "chrome/browser/extensions/extensions_quota_service.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/ui/browser_commands.h" | 15 #include "chrome/browser/ui/browser_commands.h" |
| 16 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 18 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // If you see this error in your test, you need to set the config state | 22 // If you see this error in your test, you need to set the config state |
| 23 // to be returned by chrome.test.getConfig(). Do this by calling | 23 // to be returned by chrome.test.getConfig(). Do this by calling |
| 24 // ExtensionTestGetConfigFunction::set_test_config_state(Value* state) | 24 // TestGetConfigFunction::set_test_config_state(Value* state) |
| 25 // in test set up. | 25 // in test set up. |
| 26 const char kNoTestConfigDataError[] = "Test configuration was not set."; | 26 const char kNoTestConfigDataError[] = "Test configuration was not set."; |
| 27 | 27 |
| 28 const char kNotTestProcessError[] = | 28 const char kNotTestProcessError[] = |
| 29 "The chrome.test namespace is only available in tests."; | 29 "The chrome.test namespace is only available in tests."; |
| 30 | 30 |
| 31 } // namespace | 31 } // namespace |
| 32 | 32 |
| 33 namespace extensions { |
| 34 |
| 33 TestExtensionFunction::~TestExtensionFunction() {} | 35 TestExtensionFunction::~TestExtensionFunction() {} |
| 34 | 36 |
| 35 void TestExtensionFunction::Run() { | 37 void TestExtensionFunction::Run() { |
| 36 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) { | 38 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) { |
| 37 error_ = kNotTestProcessError; | 39 error_ = kNotTestProcessError; |
| 38 SendResponse(false); | 40 SendResponse(false); |
| 39 return; | 41 return; |
| 40 } | 42 } |
| 41 SendResponse(RunImpl()); | 43 SendResponse(RunImpl()); |
| 42 } | 44 } |
| 43 | 45 |
| 44 ExtensionTestPassFunction::~ExtensionTestPassFunction() {} | 46 TestNotifyPassFunction::~TestNotifyPassFunction() {} |
| 45 | 47 |
| 46 bool ExtensionTestPassFunction::RunImpl() { | 48 bool TestNotifyPassFunction::RunImpl() { |
| 47 content::NotificationService::current()->Notify( | 49 content::NotificationService::current()->Notify( |
| 48 chrome::NOTIFICATION_EXTENSION_TEST_PASSED, | 50 chrome::NOTIFICATION_EXTENSION_TEST_PASSED, |
| 49 content::Source<Profile>(dispatcher()->profile()), | 51 content::Source<Profile>(dispatcher()->profile()), |
| 50 content::NotificationService::NoDetails()); | 52 content::NotificationService::NoDetails()); |
| 51 return true; | 53 return true; |
| 52 } | 54 } |
| 53 | 55 |
| 54 ExtensionTestFailFunction::~ExtensionTestFailFunction() {} | 56 TestFailFunction::~TestFailFunction() {} |
| 55 | 57 |
| 56 bool ExtensionTestFailFunction::RunImpl() { | 58 bool TestFailFunction::RunImpl() { |
| 57 std::string message; | 59 std::string message; |
| 58 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message)); | 60 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message)); |
| 59 content::NotificationService::current()->Notify( | 61 content::NotificationService::current()->Notify( |
| 60 chrome::NOTIFICATION_EXTENSION_TEST_FAILED, | 62 chrome::NOTIFICATION_EXTENSION_TEST_FAILED, |
| 61 content::Source<Profile>(dispatcher()->profile()), | 63 content::Source<Profile>(dispatcher()->profile()), |
| 62 content::Details<std::string>(&message)); | 64 content::Details<std::string>(&message)); |
| 63 return true; | 65 return true; |
| 64 } | 66 } |
| 65 | 67 |
| 66 ExtensionTestLogFunction::~ExtensionTestLogFunction() {} | 68 TestLogFunction::~TestLogFunction() {} |
| 67 | 69 |
| 68 bool ExtensionTestLogFunction::RunImpl() { | 70 bool TestLogFunction::RunImpl() { |
| 69 std::string message; | 71 std::string message; |
| 70 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message)); | 72 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message)); |
| 71 VLOG(1) << message; | 73 VLOG(1) << message; |
| 72 return true; | 74 return true; |
| 73 } | 75 } |
| 74 | 76 |
| 75 ExtensionTestQuotaResetFunction::~ExtensionTestQuotaResetFunction() {} | 77 TestResetQuotaFunction::~TestResetQuotaFunction() {} |
| 76 | 78 |
| 77 bool ExtensionTestQuotaResetFunction::RunImpl() { | 79 bool TestResetQuotaFunction::RunImpl() { |
| 78 ExtensionService* service = profile()->GetExtensionService(); | 80 ExtensionService* service = profile()->GetExtensionService(); |
| 79 ExtensionsQuotaService* quota = service->quota_service(); | 81 ExtensionsQuotaService* quota = service->quota_service(); |
| 80 quota->Purge(); | 82 quota->Purge(); |
| 81 quota->violators_.clear(); | 83 quota->violators_.clear(); |
| 82 return true; | 84 return true; |
| 83 } | 85 } |
| 84 | 86 |
| 85 ExtensionTestCreateIncognitoTabFunction:: | 87 TestCreateIncognitoTabFunction:: |
| 86 ~ExtensionTestCreateIncognitoTabFunction() {} | 88 ~TestCreateIncognitoTabFunction() {} |
| 87 | 89 |
| 88 bool ExtensionTestCreateIncognitoTabFunction::RunImpl() { | 90 bool TestCreateIncognitoTabFunction::RunImpl() { |
| 89 std::string url; | 91 std::string url; |
| 90 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); | 92 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); |
| 91 chrome::OpenURLOffTheRecord(profile(), GURL(url)); | 93 chrome::OpenURLOffTheRecord(profile(), GURL(url)); |
| 92 return true; | 94 return true; |
| 93 } | 95 } |
| 94 | 96 |
| 95 bool ExtensionTestSendMessageFunction::RunImpl() { | 97 bool TestSendMessageFunction::RunImpl() { |
| 96 std::string message; | 98 std::string message; |
| 97 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message)); | 99 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message)); |
| 98 AddRef(); // balanced in Reply | 100 AddRef(); // balanced in Reply |
| 99 content::NotificationService::current()->Notify( | 101 content::NotificationService::current()->Notify( |
| 100 chrome::NOTIFICATION_EXTENSION_TEST_MESSAGE, | 102 chrome::NOTIFICATION_EXTENSION_TEST_MESSAGE, |
| 101 content::Source<ExtensionTestSendMessageFunction>(this), | 103 content::Source<TestSendMessageFunction>(this), |
| 102 content::Details<std::string>(&message)); | 104 content::Details<std::string>(&message)); |
| 103 return true; | 105 return true; |
| 104 } | 106 } |
| 105 ExtensionTestSendMessageFunction::~ExtensionTestSendMessageFunction() {} | 107 TestSendMessageFunction::~TestSendMessageFunction() {} |
| 106 | 108 |
| 107 void ExtensionTestSendMessageFunction::Reply(const std::string& message) { | 109 void TestSendMessageFunction::Reply(const std::string& message) { |
| 108 result_.reset(Value::CreateStringValue(message)); | 110 result_.reset(Value::CreateStringValue(message)); |
| 109 SendResponse(true); | 111 SendResponse(true); |
| 110 Release(); // balanced in RunImpl | 112 Release(); // balanced in RunImpl |
| 111 } | 113 } |
| 112 | 114 |
| 113 // static | 115 // static |
| 114 void ExtensionTestGetConfigFunction::set_test_config_state( | 116 void TestGetConfigFunction::set_test_config_state( |
| 115 DictionaryValue* value) { | 117 DictionaryValue* value) { |
| 116 TestConfigState* test_config_state = TestConfigState::GetInstance(); | 118 TestConfigState* test_config_state = TestConfigState::GetInstance(); |
| 117 test_config_state->set_config_state(value); | 119 test_config_state->set_config_state(value); |
| 118 } | 120 } |
| 119 | 121 |
| 120 ExtensionTestGetConfigFunction::TestConfigState::TestConfigState() | 122 TestGetConfigFunction::TestConfigState::TestConfigState() |
| 121 : config_state_(NULL) {} | 123 : config_state_(NULL) {} |
| 122 | 124 |
| 123 // static | 125 // static |
| 124 ExtensionTestGetConfigFunction::TestConfigState* | 126 TestGetConfigFunction::TestConfigState* |
| 125 ExtensionTestGetConfigFunction::TestConfigState::GetInstance() { | 127 TestGetConfigFunction::TestConfigState::GetInstance() { |
| 126 return Singleton<TestConfigState>::get(); | 128 return Singleton<TestConfigState>::get(); |
| 127 } | 129 } |
| 128 | 130 |
| 129 ExtensionTestGetConfigFunction::~ExtensionTestGetConfigFunction() {} | 131 TestGetConfigFunction::~TestGetConfigFunction() {} |
| 130 | 132 |
| 131 bool ExtensionTestGetConfigFunction::RunImpl() { | 133 bool TestGetConfigFunction::RunImpl() { |
| 132 TestConfigState* test_config_state = TestConfigState::GetInstance(); | 134 TestConfigState* test_config_state = TestConfigState::GetInstance(); |
| 133 | 135 |
| 134 if (!test_config_state->config_state()) { | 136 if (!test_config_state->config_state()) { |
| 135 error_ = kNoTestConfigDataError; | 137 error_ = kNoTestConfigDataError; |
| 136 return false; | 138 return false; |
| 137 } | 139 } |
| 138 | 140 |
| 139 result_.reset(test_config_state->config_state()->DeepCopy()); | 141 result_.reset(test_config_state->config_state()->DeepCopy()); |
| 140 return true; | 142 return true; |
| 141 } | 143 } |
| 144 |
| 145 } // namespace extensions |
| OLD | NEW |