diff options
Diffstat (limited to 'chrome/browser/extensions/api/test')
-rw-r--r-- | chrome/browser/extensions/api/test/apitest_apitest.cc | 9 | ||||
-rw-r--r-- | chrome/browser/extensions/api/test/test_api.cc | 145 | ||||
-rw-r--r-- | chrome/browser/extensions/api/test/test_api.h | 138 |
3 files changed, 292 insertions, 0 deletions
diff --git a/chrome/browser/extensions/api/test/apitest_apitest.cc b/chrome/browser/extensions/api/test/apitest_apitest.cc new file mode 100644 index 0000000..385a8d0 --- /dev/null +++ b/chrome/browser/extensions/api/test/apitest_apitest.cc @@ -0,0 +1,9 @@ +// Copyright (c) 2012 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 "chrome/browser/extensions/extension_apitest.h" + +IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ApiTest) { + ASSERT_TRUE(RunExtensionTest("apitest")) << message_; +} diff --git a/chrome/browser/extensions/api/test/test_api.cc b/chrome/browser/extensions/api/test/test_api.cc new file mode 100644 index 0000000..03cc46d --- /dev/null +++ b/chrome/browser/extensions/api/test/test_api.cc @@ -0,0 +1,145 @@ +// Copyright (c) 2012 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 "chrome/browser/extensions/api/test/test_api.h" + +#include <string> + +#include "base/command_line.h" +#include "base/memory/singleton.h" +#include "chrome/browser/extensions/extension_service.h" +#include "chrome/browser/extensions/extension_function_dispatcher.h" +#include "chrome/browser/extensions/extensions_quota_service.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/ui/browser_commands.h" +#include "chrome/common/chrome_notification_types.h" +#include "chrome/common/chrome_switches.h" +#include "content/public/browser/notification_service.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 { + +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<Profile>(dispatcher()->profile()), + content::NotificationService::NoDetails()); + return true; +} + +TestFailFunction::~TestFailFunction() {} + +bool TestFailFunction::RunImpl() { + std::string message; + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message)); + content::NotificationService::current()->Notify( + chrome::NOTIFICATION_EXTENSION_TEST_FAILED, + content::Source<Profile>(dispatcher()->profile()), + content::Details<std::string>(&message)); + return true; +} + +TestLogFunction::~TestLogFunction() {} + +bool TestLogFunction::RunImpl() { + std::string message; + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message)); + VLOG(1) << message; + return true; +} + +TestResetQuotaFunction::~TestResetQuotaFunction() {} + +bool TestResetQuotaFunction::RunImpl() { + ExtensionService* service = profile()->GetExtensionService(); + ExtensionsQuotaService* quota = service->quota_service(); + quota->Purge(); + quota->violators_.clear(); + return true; +} + +TestCreateIncognitoTabFunction:: + ~TestCreateIncognitoTabFunction() {} + +bool TestCreateIncognitoTabFunction::RunImpl() { + std::string url; + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); + chrome::OpenURLOffTheRecord(profile(), GURL(url)); + return true; +} + +bool TestSendMessageFunction::RunImpl() { + std::string message; + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message)); + AddRef(); // balanced in Reply + content::NotificationService::current()->Notify( + chrome::NOTIFICATION_EXTENSION_TEST_MESSAGE, + content::Source<TestSendMessageFunction>(this), + content::Details<std::string>(&message)); + return true; +} +TestSendMessageFunction::~TestSendMessageFunction() {} + +void TestSendMessageFunction::Reply(const std::string& message) { + result_.reset(Value::CreateStringValue(message)); + SendResponse(true); + Release(); // balanced in RunImpl +} + +// static +void TestGetConfigFunction::set_test_config_state( + 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; + } + + result_.reset(test_config_state->config_state()->DeepCopy()); + return true; +} + +} // namespace extensions diff --git a/chrome/browser/extensions/api/test/test_api.h b/chrome/browser/extensions/api/test/test_api.h new file mode 100644 index 0000000..3886385 --- /dev/null +++ b/chrome/browser/extensions/api/test/test_api.h @@ -0,0 +1,138 @@ +// Copyright (c) 2012 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 CHROME_BROWSER_EXTENSIONS_API_TEST_TEST_API_H_ +#define CHROME_BROWSER_EXTENSIONS_API_TEST_TEST_API_H_ +#pragma once + +#include "base/values.h" +#include "chrome/browser/extensions/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 an extension process. +class TestExtensionFunction : public SyncExtensionFunction { + protected: + virtual ~TestExtensionFunction(); + + // ExtensionFunction: + virtual void Run() OVERRIDE; +}; + +class TestNotifyPassFunction : public TestExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION_NAME("test.notifyPass") + + protected: + virtual ~TestNotifyPassFunction(); + + // ExtensionFunction: + virtual bool RunImpl() OVERRIDE; +}; + +class TestFailFunction : public TestExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION_NAME("test.notifyFail") + + protected: + virtual ~TestFailFunction(); + + // ExtensionFunction: + virtual bool RunImpl() OVERRIDE; +}; + +class TestLogFunction : public TestExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION_NAME("test.log") + + protected: + virtual ~TestLogFunction(); + + // ExtensionFunction: + virtual bool RunImpl() OVERRIDE; +}; + +class TestResetQuotaFunction : public TestExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION_NAME("test.resetQuota") + + protected: + virtual ~TestResetQuotaFunction(); + + // ExtensionFunction: + virtual bool RunImpl() OVERRIDE; +}; + +class TestCreateIncognitoTabFunction : public TestExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION_NAME("test.createIncognitoTab") + + protected: + virtual ~TestCreateIncognitoTabFunction(); + + // ExtensionFunction: + virtual bool RunImpl() OVERRIDE; +}; + +class TestSendMessageFunction : public AsyncExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION_NAME("test.sendMessage") + + // 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_NAME("test.getConfig") + + // Set the dictionary returned by chrome.test.getConfig(). + // Does not take ownership of |value|. + static void set_test_config_state(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(DictionaryValue* config_state) { + config_state_ = config_state; + } + + const DictionaryValue* config_state() { + return config_state_; + } + + private: + friend struct DefaultSingletonTraits<TestConfigState>; + TestConfigState(); + + DictionaryValue* config_state_; + + DISALLOW_COPY_AND_ASSIGN(TestConfigState); + }; + + virtual ~TestGetConfigFunction(); + + // ExtensionFunction: + virtual bool RunImpl() OVERRIDE; +}; + +} // namespace extensions + +#endif // CHROME_BROWSER_EXTENSIONS_API_TEST_TEST_API_H_ |