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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/test/test_api.h"
9 #include "base/command_line.h"
10 #include "base/memory/singleton.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_function_dispatcher.h"
14 #include "chrome/browser/extensions/extensions_quota_service.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser_commands.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/extensions/api/test.h"
19 #include "content/public/browser/notification_service.h"
23 // If you see this error in your test, you need to set the config state
24 // to be returned by chrome.test.getConfig(). Do this by calling
25 // TestGetConfigFunction::set_test_config_state(Value* state)
27 const char kNoTestConfigDataError[] = "Test configuration was not set.";
29 const char kNotTestProcessError[] =
30 "The chrome.test namespace is only available in tests.";
34 namespace extensions {
36 namespace CreateIncognitoTab = api::test::CreateIncognitoTab;
37 namespace Log = api::test::Log;
38 namespace NotifyFail = api::test::NotifyFail;
39 namespace PassMessage = api::test::PassMessage;
41 TestExtensionFunction::~TestExtensionFunction() {}
43 void TestExtensionFunction::Run() {
44 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
45 error_ = kNotTestProcessError;
49 SendResponse(RunImpl());
52 TestNotifyPassFunction::~TestNotifyPassFunction() {}
54 bool TestNotifyPassFunction::RunImpl() {
55 content::NotificationService::current()->Notify(
56 chrome::NOTIFICATION_EXTENSION_TEST_PASSED,
57 content::Source<Profile>(dispatcher()->profile()),
58 content::NotificationService::NoDetails());
62 TestNotifyFailFunction::~TestNotifyFailFunction() {}
64 bool TestNotifyFailFunction::RunImpl() {
65 scoped_ptr<NotifyFail::Params> params(NotifyFail::Params::Create(*args_));
66 EXTENSION_FUNCTION_VALIDATE(params.get());
67 content::NotificationService::current()->Notify(
68 chrome::NOTIFICATION_EXTENSION_TEST_FAILED,
69 content::Source<Profile>(dispatcher()->profile()),
70 content::Details<std::string>(¶ms->message));
74 TestLogFunction::~TestLogFunction() {}
76 bool TestLogFunction::RunImpl() {
77 scoped_ptr<Log::Params> params(Log::Params::Create(*args_));
78 EXTENSION_FUNCTION_VALIDATE(params.get());
79 VLOG(1) << params->message;
83 TestResetQuotaFunction::~TestResetQuotaFunction() {}
85 bool TestResetQuotaFunction::RunImpl() {
86 ExtensionService* service = GetProfile()->GetExtensionService();
87 ExtensionsQuotaService* quota = service->quota_service();
89 quota->violation_errors_.clear();
93 TestCreateIncognitoTabFunction::
94 ~TestCreateIncognitoTabFunction() {}
96 bool TestCreateIncognitoTabFunction::RunImpl() {
97 scoped_ptr<CreateIncognitoTab::Params> params(
98 CreateIncognitoTab::Params::Create(*args_));
99 EXTENSION_FUNCTION_VALIDATE(params.get());
100 chrome::OpenURLOffTheRecord(
101 GetProfile(), GURL(params->url), chrome::GetActiveDesktop());
105 bool TestSendMessageFunction::RunImpl() {
106 scoped_ptr<PassMessage::Params> params(
107 PassMessage::Params::Create(*args_));
108 EXTENSION_FUNCTION_VALIDATE(params.get());
109 AddRef(); // balanced in Reply
110 content::NotificationService::current()->Notify(
111 chrome::NOTIFICATION_EXTENSION_TEST_MESSAGE,
112 content::Source<TestSendMessageFunction>(this),
113 content::Details<std::string>(¶ms->message));
117 TestSendMessageFunction::~TestSendMessageFunction() {}
119 void TestSendMessageFunction::Reply(const std::string& message) {
120 SetResult(new base::StringValue(message));
122 Release(); // balanced in RunImpl
126 void TestGetConfigFunction::set_test_config_state(
127 base::DictionaryValue* value) {
128 TestConfigState* test_config_state = TestConfigState::GetInstance();
129 test_config_state->set_config_state(value);
132 TestGetConfigFunction::TestConfigState::TestConfigState()
133 : config_state_(NULL) {}
136 TestGetConfigFunction::TestConfigState*
137 TestGetConfigFunction::TestConfigState::GetInstance() {
138 return Singleton<TestConfigState>::get();
141 TestGetConfigFunction::~TestGetConfigFunction() {}
143 bool TestGetConfigFunction::RunImpl() {
144 TestConfigState* test_config_state = TestConfigState::GetInstance();
146 if (!test_config_state->config_state()) {
147 error_ = kNoTestConfigDataError;
151 SetResult(test_config_state->config_state()->DeepCopy());
155 } // namespace extensions