diff options
author | yoz@chromium.org <yoz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-04 01:33:33 +0000 |
---|---|---|
committer | yoz@chromium.org <yoz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-04 01:33:33 +0000 |
commit | 09186d830706b79c823d046ce4daae20edfba4df (patch) | |
tree | af21aef0d7768c6315709ff4b7bb55ed8a92adee /extensions | |
parent | def2b8c5bfa475eb53cd495f230fa91b2970a66b (diff) | |
download | chromium_src-09186d830706b79c823d046ce4daae20edfba4df.zip chromium_src-09186d830706b79c823d046ce4daae20edfba4df.tar.gz chromium_src-09186d830706b79c823d046ce4daae20edfba4df.tar.bz2 |
Move chrome.test API files to src/extensions.
BUG=355192
TBR=satorux@chromium.org
Review URL: https://codereview.chromium.org/224173004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@261628 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'extensions')
-rw-r--r-- | extensions/browser/api/test/test_api.cc | 147 | ||||
-rw-r--r-- | extensions/browser/api/test/test_api.h | 136 | ||||
-rw-r--r-- | extensions/common/api/api.gyp | 1 | ||||
-rw-r--r-- | extensions/common/api/test.json | 396 | ||||
-rw-r--r-- | extensions/extensions.gyp | 2 |
5 files changed, 682 insertions, 0 deletions
diff --git a/extensions/browser/api/test/test_api.cc b/extensions/browser/api/test/test_api.cc new file mode 100644 index 0000000..d4ccc5c --- /dev/null +++ b/extensions/browser/api/test/test_api.cc @@ -0,0 +1,147 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "extensions/browser/api/test/test_api.h" + +#include <string> + +#include "base/command_line.h" +#include "base/memory/singleton.h" +#include "chrome/browser/chrome_notification_types.h" +#include "content/public/browser/notification_service.h" +#include "content/public/common/content_switches.h" +#include "extensions/browser/extension_function_dispatcher.h" +#include "extensions/browser/extension_system.h" +#include "extensions/browser/quota_service.h" +#include "extensions/common/api/test.h" + +namespace { + +// If you see this error in your test, you need to set the config state +// to be returned by chrome.test.getConfig(). Do this by calling +// TestGetConfigFunction::set_test_config_state(Value* state) +// in test set up. +const char kNoTestConfigDataError[] = "Test configuration was not set."; + +const char kNotTestProcessError[] = + "The chrome.test namespace is only available in tests."; + +} // namespace + +namespace extensions { + +namespace Log = core_api::test::Log; +namespace NotifyFail = core_api::test::NotifyFail; +namespace PassMessage = core_api::test::PassMessage; +namespace WaitForRoundTrip = core_api::test::WaitForRoundTrip; + +TestExtensionFunction::~TestExtensionFunction() {} + +void TestExtensionFunction::Run() { + if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) { + error_ = kNotTestProcessError; + SendResponse(false); + return; + } + SendResponse(RunImpl()); +} + +TestNotifyPassFunction::~TestNotifyPassFunction() {} + +bool TestNotifyPassFunction::RunImpl() { + content::NotificationService::current()->Notify( + chrome::NOTIFICATION_EXTENSION_TEST_PASSED, + content::Source<content::BrowserContext>(dispatcher()->browser_context()), + content::NotificationService::NoDetails()); + return true; +} + +TestNotifyFailFunction::~TestNotifyFailFunction() {} + +bool TestNotifyFailFunction::RunImpl() { + scoped_ptr<NotifyFail::Params> params(NotifyFail::Params::Create(*args_)); + EXTENSION_FUNCTION_VALIDATE(params.get()); + content::NotificationService::current()->Notify( + chrome::NOTIFICATION_EXTENSION_TEST_FAILED, + content::Source<content::BrowserContext>(dispatcher()->browser_context()), + content::Details<std::string>(¶ms->message)); + return true; +} + +TestLogFunction::~TestLogFunction() {} + +bool TestLogFunction::RunImpl() { + scoped_ptr<Log::Params> params(Log::Params::Create(*args_)); + EXTENSION_FUNCTION_VALIDATE(params.get()); + VLOG(1) << params->message; + return true; +} + +TestResetQuotaFunction::~TestResetQuotaFunction() {} + +bool TestResetQuotaFunction::RunImpl() { + QuotaService* quota = + ExtensionSystem::Get(browser_context())->quota_service(); + quota->Purge(); + quota->violation_errors_.clear(); + return true; +} + +bool TestSendMessageFunction::RunImpl() { + scoped_ptr<PassMessage::Params> params(PassMessage::Params::Create(*args_)); + EXTENSION_FUNCTION_VALIDATE(params.get()); + content::NotificationService::current()->Notify( + chrome::NOTIFICATION_EXTENSION_TEST_MESSAGE, + content::Source<TestSendMessageFunction>(this), + content::Details<std::string>(¶ms->message)); + return true; +} + +TestSendMessageFunction::~TestSendMessageFunction() {} + +void TestSendMessageFunction::Reply(const std::string& message) { + SetResult(new base::StringValue(message)); + SendResponse(true); +} + +// static +void TestGetConfigFunction::set_test_config_state( + base::DictionaryValue* value) { + TestConfigState* test_config_state = TestConfigState::GetInstance(); + test_config_state->set_config_state(value); +} + +TestGetConfigFunction::TestConfigState::TestConfigState() + : config_state_(NULL) {} + +// static +TestGetConfigFunction::TestConfigState* +TestGetConfigFunction::TestConfigState::GetInstance() { + return Singleton<TestConfigState>::get(); +} + +TestGetConfigFunction::~TestGetConfigFunction() {} + +bool TestGetConfigFunction::RunImpl() { + TestConfigState* test_config_state = TestConfigState::GetInstance(); + + if (!test_config_state->config_state()) { + error_ = kNoTestConfigDataError; + return false; + } + + SetResult(test_config_state->config_state()->DeepCopy()); + return true; +} + +TestWaitForRoundTripFunction::~TestWaitForRoundTripFunction() {} + +bool TestWaitForRoundTripFunction::RunImpl() { + scoped_ptr<WaitForRoundTrip::Params> params( + WaitForRoundTrip::Params::Create(*args_)); + SetResult(new base::StringValue(params->message)); + return true; +} + +} // namespace extensions diff --git a/extensions/browser/api/test/test_api.h b/extensions/browser/api/test/test_api.h new file mode 100644 index 0000000..c84b02d --- /dev/null +++ b/extensions/browser/api/test/test_api.h @@ -0,0 +1,136 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef EXTENSIONS_BROWSER_API_TEST_TEST_API_H_ +#define EXTENSIONS_BROWSER_API_TEST_TEST_API_H_ + +#include "base/values.h" +#include "extensions/browser/extension_function.h" + +template <typename T> +struct DefaultSingletonTraits; + +namespace extensions { + +// A function that is only available in tests. +// Prior to running, checks that we are in a testing process. +class TestExtensionFunction : public SyncExtensionFunction { + protected: + virtual ~TestExtensionFunction(); + + // ExtensionFunction: + virtual void Run() OVERRIDE; +}; + +class TestNotifyPassFunction : public TestExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("test.notifyPass", UNKNOWN) + + protected: + virtual ~TestNotifyPassFunction(); + + // ExtensionFunction: + virtual bool RunImpl() OVERRIDE; +}; + +class TestNotifyFailFunction : public TestExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("test.notifyFail", UNKNOWN) + + protected: + virtual ~TestNotifyFailFunction(); + + // ExtensionFunction: + virtual bool RunImpl() OVERRIDE; +}; + +class TestLogFunction : public TestExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("test.log", UNKNOWN) + + protected: + virtual ~TestLogFunction(); + + // ExtensionFunction: + virtual bool RunImpl() OVERRIDE; +}; + +class TestResetQuotaFunction : public TestExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("test.resetQuota", UNKNOWN) + + protected: + virtual ~TestResetQuotaFunction(); + + // ExtensionFunction: + virtual bool RunImpl() OVERRIDE; +}; + +class TestSendMessageFunction : public AsyncExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("test.sendMessage", UNKNOWN) + + // Sends a reply back to the calling extension. Many extensions don't need + // a reply and will just ignore it. + void Reply(const std::string& message); + + protected: + virtual ~TestSendMessageFunction(); + + // ExtensionFunction: + virtual bool RunImpl() OVERRIDE; +}; + +class TestGetConfigFunction : public SyncExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("test.getConfig", UNKNOWN) + + // Set the dictionary returned by chrome.test.getConfig(). + // Does not take ownership of |value|. + static void set_test_config_state(base::DictionaryValue* value); + + protected: + // Tests that set configuration state do so by calling + // set_test_config_state() as part of test set up, and unsetting it + // during tear down. This singleton class holds a pointer to that + // state, owned by the test code. + class TestConfigState { + public: + static TestConfigState* GetInstance(); + + void set_config_state(base::DictionaryValue* config_state) { + config_state_ = config_state; + } + + const base::DictionaryValue* config_state() { return config_state_; } + + private: + friend struct DefaultSingletonTraits<TestConfigState>; + TestConfigState(); + + base::DictionaryValue* config_state_; + + DISALLOW_COPY_AND_ASSIGN(TestConfigState); + }; + + virtual ~TestGetConfigFunction(); + + // ExtensionFunction: + virtual bool RunImpl() OVERRIDE; +}; + +class TestWaitForRoundTripFunction : public TestExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("test.waitForRoundTrip", UNKNOWN) + + protected: + virtual ~TestWaitForRoundTripFunction(); + + // ExtensionFunction: + virtual bool RunImpl() OVERRIDE; +}; + +} // namespace extensions + +#endif // EXTENSIONS_BROWSER_API_TEST_TEST_API_H_ diff --git a/extensions/common/api/api.gyp b/extensions/common/api/api.gyp index cb2f751..4e3c2b0 100644 --- a/extensions/common/api/api.gyp +++ b/extensions/common/api/api.gyp @@ -29,6 +29,7 @@ 'sockets_tcp_server.idl', 'sockets_udp.idl', 'storage.json', + 'test.json', ], 'cc_dir': 'extensions/common/api', 'root_namespace': 'extensions::core_api', diff --git a/extensions/common/api/test.json b/extensions/common/api/test.json new file mode 100644 index 0000000..4e09054 --- /dev/null +++ b/extensions/common/api/test.json @@ -0,0 +1,396 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +[ + { + "namespace": "test", + "description": "none", + "functions": [ + { + "name": "getConfig", + "type": "function", + "description": "Gives configuration options set by the test.", + "parameters": [ + { + "type": "function", "name": "callback", "parameters": [ + { + "type": "object", + "name": "testConfig", + "properties": { + "customArg": { + "type": "string", + "optional": true, + "description": "Additional string argument to pass to test." + }, + "ftpServer": { + "type": "object", + "optional": true, + "description": "Details on the FTP server used to mock network responses. Will be set only if test calls ExtensionApiTest::StartFTPServer().", + "properties": { + "port": { + "type": "integer", + "description": "The port on which the FTP server is listening.", + "minimum": 1024, + "maximum": 65535 + } + } + }, + "testServer": { + "type": "object", + "optional": true, + "description": "Details on the test server used to mock network responses. Will be set only if test calls ExtensionApiTest::StartTestServer().", + "properties": { + "port": { + "type": "integer", + "description": "The port on which the test server is listening.", + "minimum": 1024, + "maximum": 65535 + } + } + }, + "spawnedTestServer": { + "type": "object", + "optional": true, + "description": "Details on the spawned test server used to mock network responses. Will be set only if test calls ExtensionApiTest::StartSpawnedTestServer().", + "properties": { + "port": { + "type": "integer", + "description": "The port on which the test server is listening.", + "minimum": 1024, + "maximum": 65535 + } + } + }, + "testDataDirectory": { + "type": "string", + "description": "file:/// URL for the API test data directory." + }, + "testWebSocketPort": { + "type": "integer", + "description": "The port on which the test WebSocket server is listening.", + "minimum": 0, + "maximum": 65535 + } + } + } + ] + } + ] + }, + { + "name": "notifyFail", + "type": "function", + "description": "Notifies the browser process that test code running in the extension failed. This is only used for internal unit testing.", + "parameters": [ + {"type": "string", "name": "message"} + ] + }, + { + "name": "notifyPass", + "type": "function", + "description": "Notifies the browser process that test code running in the extension passed. This is only used for internal unit testing.", + "parameters": [ + {"type": "string", "name": "message", "optional": true} + ] + }, + { + "name": "resetQuota", + "type": "function", + "description": "Resets all accumulated quota state for all extensions. This is only used for internal unit testing.", + "parameters": [] + }, + { + "name": "log", + "type": "function", + "description": "Logs a message during internal unit testing.", + "parameters": [ + {"type": "string", "name": "message"} + ] + }, + { + "name": "sendMessage", + "type": "function", + "description": "Sends a string message to the browser process, generating a Notification that C++ test code can wait for.", + "parameters": [ + {"type": "string", "name": "message"}, + { + "type": "function", + "name": "callback", + "optional": true, + "parameters": [ + {"type": "string", "name": "response"} + ] + } + ] + }, + { + "name": "callbackAdded", + "type": "function", + "nocompile": true, + "parameters": [] + }, + { + "name": "runNextTest", + "type": "function", + "nocompile": true, + "parameters": [] + }, + { + "name": "fail", + "type": "function", + "nocompile": true, + "parameters": [ + {"type": "any", "name": "message", "optional": true} + ] + }, + { + "name": "succeed", + "type": "function", + "nocompile": true, + "parameters": [ + {"type": "any", "name": "message", "optional": true} + ] + }, + { + "name": "assertTrue", + "type": "function", + "nocompile": true, + "parameters": [ + { + "name": "test", + "choices": [ + {"type": "string"}, + {"type": "boolean"} + ] + }, + {"type": "string", "name": "message", "optional": true} + ] + }, + { + "name": "assertFalse", + "type": "function", + "nocompile": true, + "parameters": [ + { + "name": "test", + "choices": [ + {"type": "string"}, + {"type": "boolean"} + ] + }, + {"type": "string", "name": "message", "optional": true} + ] + }, + { + "name": "assertBool", + "type": "function", + "nocompile": true, + "parameters": [ + { + "name": "test", + "choices": [ + {"type": "string"}, + {"type": "boolean"} + ] + }, + {"type": "boolean", "name": "expected"}, + {"type": "string", "name": "message", "optional": true} + ] + }, + { + "name": "checkDeepEq", + "type": "function", + "nocompile": true, + "allowAmbiguousOptionalArguments": true, + "parameters": [ + // These need to be optional because they can be null. + {"type": "any", "name": "expected", "optional": true}, + {"type": "any", "name": "actual", "optional": true} + ] + }, + { + "name": "assertEq", + "type": "function", + "nocompile": true, + "allowAmbiguousOptionalArguments": true, + "parameters": [ + // These need to be optional because they can be null. + {"type": "any", "name": "expected", "optional": true}, + {"type": "any", "name": "actual", "optional": true}, + {"type": "string", "name": "message", "optional": true} + ] + }, + { + "name": "assertNoLastError", + "type": "function", + "nocompile": true, + "parameters": [] + }, + { + "name": "assertLastError", + "type": "function", + "nocompile": true, + "parameters": [ + {"type": "string", "name": "expectedError"} + ] + }, + { + "name": "assertThrows", + "type": "function", + "nocompile": true, + "parameters": [ + {"type": "function", "name": "fn"}, + { + "type": "object", + "name": "self", + "additionalProperties": {"type": "any"}, + "optional": true + }, + {"type": "array", "items": {"type": "any"}, "name": "args"}, + {"choices": [ {"type": "string"}, {"type": "object", "isInstanceOf": "RegExp"} ], "name": "message", "optional": true} + ] + }, + { + "name": "callback", + "type": "function", + "nocompile": true, + "parameters": [ + {"type": "function", "name": "func", "optional": true}, + {"type": "string", "name": "expectedError", "optional": true} + ] + }, + { + "name": "listenOnce", + "type": "function", + "nocompile": true, + "parameters": [ + // TODO(cduvall): Make this a $ref to events.Event. + {"type": "any", "name": "event"}, + {"type": "function", "name": "func"} + ] + }, + { + "name": "listenForever", + "type": "function", + "nocompile": true, + "parameters": [ + // TODO(cduvall): Make this a $ref to events.Event. + {"type": "any", "name": "event"}, + {"type": "function", "name": "func"} + ] + }, + { + "name": "callbackPass", + "type": "function", + "nocompile": true, + "parameters": [ + {"type": "function", "name": "func", "optional": true} + ] + }, + { + "name": "callbackFail", + "type": "function", + "nocompile": true, + "parameters": [ + {"type": "string", "name": "expectedError"}, + {"type": "function", "name": "func", "optional": true} + ] + }, + { + "name": "runTests", + "type": "function", + "nocompile": true, + "parameters": [ + { + "type": "array", + "name": "tests", + "items": {"type": "function"} + } + ] + }, + { + "name": "getApiFeatures", + "type": "function", + "nocompile": true, + "parameters": [] + }, + { + "name": "getApiDefinitions", + "type": "function", + "nocompile": true, + "parameters": [ + { + "type": "array", + "name": "apiNames", + "optional": true, + "items": {"type": "string"} + } + ] + }, + { + "name": "isProcessingUserGesture", + "type": "function", + "nocompile": true, + "parameters": [] + }, + { + "name": "runWithUserGesture", + "type": "function", + "description": "Runs the callback in the context of a user gesture.", + "nocompile": true, + "parameters": [ + { + "type": "function", + "name": "callback", + "parameters": [] + } + ] + }, + { + "name": "runWithoutUserGesture", + "type": "function", + "nocompile": true, + "parameters": [ + { + "type": "function", + "name": "callback", + "parameters": [] + } + ] + }, + { + "name": "waitForRoundTrip", + "type": "function", + "description": "Sends a string message one round trip from the renderer to the browser process and back.", + "parameters": [ + {"type": "string", "name": "message"}, + { + "type": "function", + "name": "callback", + "parameters": [ + {"type": "string", "name": "message"} + ] + } + ] + } + ], + "events": [ + { + "name": "onMessage", + "type": "function", + "description": "Used to test sending messages to extensions.", + "parameters": [ + { + "type": "object", + "name": "info", + "properties": { + "data": { "type": "string", "description": "Additional information." }, + "lastMessage": { "type": "boolean", "description": "True if this was the last message for this test" } + } + } + ] + } + ] + } +] diff --git a/extensions/extensions.gyp b/extensions/extensions.gyp index 2d08b26..e34ab22 100644 --- a/extensions/extensions.gyp +++ b/extensions/extensions.gyp @@ -264,6 +264,8 @@ 'browser/api/storage/value_store_cache.h', 'browser/api/storage/weak_unlimited_settings_storage.cc', 'browser/api/storage/weak_unlimited_settings_storage.h', + 'browser/api/test/test_api.cc', + 'browser/api/test/test_api.h', 'browser/api_activity_monitor.h', 'browser/app_sorting.h', 'browser/blacklist_state.h', |