1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/browser/api/test/test_api.h"
9 #include "base/command_line.h"
10 #include "base/memory/singleton.h"
11 #include "content/public/browser/notification_service.h"
12 #include "content/public/common/content_switches.h"
13 #include "extensions/browser/extension_function_dispatcher.h"
14 #include "extensions/browser/extension_system.h"
15 #include "extensions/browser/notification_types.h"
16 #include "extensions/common/api/test.h"
20 // If you see this error in your test, you need to set the config state
21 // to be returned by chrome.test.getConfig(). Do this by calling
22 // TestGetConfigFunction::set_test_config_state(Value* state)
24 const char kNoTestConfigDataError
[] = "Test configuration was not set.";
26 const char kNotTestProcessError
[] =
27 "The chrome.test namespace is only available in tests.";
31 namespace extensions
{
33 namespace Log
= api::test::Log
;
34 namespace NotifyFail
= api::test::NotifyFail
;
35 namespace PassMessage
= api::test::PassMessage
;
36 namespace WaitForRoundTrip
= api::test::WaitForRoundTrip
;
38 TestExtensionFunction::~TestExtensionFunction() {}
40 bool TestExtensionFunction::RunSync() {
41 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType
)) {
42 error_
= kNotTestProcessError
;
48 TestNotifyPassFunction::~TestNotifyPassFunction() {}
50 bool TestNotifyPassFunction::RunSafe() {
51 content::NotificationService::current()->Notify(
52 extensions::NOTIFICATION_EXTENSION_TEST_PASSED
,
53 content::Source
<content::BrowserContext
>(dispatcher()->browser_context()),
54 content::NotificationService::NoDetails());
58 TestNotifyFailFunction::~TestNotifyFailFunction() {}
60 bool TestNotifyFailFunction::RunSafe() {
61 scoped_ptr
<NotifyFail::Params
> params(NotifyFail::Params::Create(*args_
));
62 EXTENSION_FUNCTION_VALIDATE(params
.get());
63 content::NotificationService::current()->Notify(
64 extensions::NOTIFICATION_EXTENSION_TEST_FAILED
,
65 content::Source
<content::BrowserContext
>(dispatcher()->browser_context()),
66 content::Details
<std::string
>(¶ms
->message
));
70 TestLogFunction::~TestLogFunction() {}
72 bool TestLogFunction::RunSafe() {
73 scoped_ptr
<Log::Params
> params(Log::Params::Create(*args_
));
74 EXTENSION_FUNCTION_VALIDATE(params
.get());
75 VLOG(1) << params
->message
;
79 bool TestSendMessageFunction::RunAsync() {
80 scoped_ptr
<PassMessage::Params
> params(PassMessage::Params::Create(*args_
));
81 EXTENSION_FUNCTION_VALIDATE(params
.get());
82 content::NotificationService::current()->Notify(
83 extensions::NOTIFICATION_EXTENSION_TEST_MESSAGE
,
84 content::Source
<TestSendMessageFunction
>(this),
85 content::Details
<std::string
>(¶ms
->message
));
89 TestSendMessageFunction::~TestSendMessageFunction() {}
91 void TestSendMessageFunction::Reply(const std::string
& message
) {
92 SetResult(new base::StringValue(message
));
96 void TestSendMessageFunction::ReplyWithError(const std::string
& error
) {
102 void TestGetConfigFunction::set_test_config_state(
103 base::DictionaryValue
* value
) {
104 TestConfigState
* test_config_state
= TestConfigState::GetInstance();
105 test_config_state
->set_config_state(value
);
108 TestGetConfigFunction::TestConfigState::TestConfigState()
109 : config_state_(NULL
) {}
112 TestGetConfigFunction::TestConfigState
*
113 TestGetConfigFunction::TestConfigState::GetInstance() {
114 return Singleton
<TestConfigState
>::get();
117 TestGetConfigFunction::~TestGetConfigFunction() {}
119 bool TestGetConfigFunction::RunSafe() {
120 TestConfigState
* test_config_state
= TestConfigState::GetInstance();
122 if (!test_config_state
->config_state()) {
123 error_
= kNoTestConfigDataError
;
127 SetResult(test_config_state
->config_state()->DeepCopy());
131 TestWaitForRoundTripFunction::~TestWaitForRoundTripFunction() {}
133 bool TestWaitForRoundTripFunction::RunSafe() {
134 scoped_ptr
<WaitForRoundTrip::Params
> params(
135 WaitForRoundTrip::Params::Create(*args_
));
136 SetResult(new base::StringValue(params
->message
));
140 } // namespace extensions