Update path of checkdeps to buildtools checkout
[chromium-blink-merge.git] / extensions / browser / api / test / test_api.cc
blob87252b70faff8e79527943da04e994c14c3a35ff
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"
7 #include <string>
9 #include "base/command_line.h"
10 #include "base/memory/singleton.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "content/public/browser/notification_service.h"
13 #include "content/public/common/content_switches.h"
14 #include "extensions/browser/extension_function_dispatcher.h"
15 #include "extensions/browser/extension_system.h"
16 #include "extensions/browser/quota_service.h"
17 #include "extensions/common/api/test.h"
19 namespace {
21 // If you see this error in your test, you need to set the config state
22 // to be returned by chrome.test.getConfig(). Do this by calling
23 // TestGetConfigFunction::set_test_config_state(Value* state)
24 // in test set up.
25 const char kNoTestConfigDataError[] = "Test configuration was not set.";
27 const char kNotTestProcessError[] =
28 "The chrome.test namespace is only available in tests.";
30 } // namespace
32 namespace extensions {
34 namespace Log = core_api::test::Log;
35 namespace NotifyFail = core_api::test::NotifyFail;
36 namespace PassMessage = core_api::test::PassMessage;
37 namespace WaitForRoundTrip = core_api::test::WaitForRoundTrip;
39 TestExtensionFunction::~TestExtensionFunction() {}
41 bool TestExtensionFunction::RunSync() {
42 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
43 error_ = kNotTestProcessError;
44 return false;
46 return RunSafe();
49 TestNotifyPassFunction::~TestNotifyPassFunction() {}
51 bool TestNotifyPassFunction::RunSafe() {
52 content::NotificationService::current()->Notify(
53 chrome::NOTIFICATION_EXTENSION_TEST_PASSED,
54 content::Source<content::BrowserContext>(dispatcher()->browser_context()),
55 content::NotificationService::NoDetails());
56 return true;
59 TestNotifyFailFunction::~TestNotifyFailFunction() {}
61 bool TestNotifyFailFunction::RunSafe() {
62 scoped_ptr<NotifyFail::Params> params(NotifyFail::Params::Create(*args_));
63 EXTENSION_FUNCTION_VALIDATE(params.get());
64 content::NotificationService::current()->Notify(
65 chrome::NOTIFICATION_EXTENSION_TEST_FAILED,
66 content::Source<content::BrowserContext>(dispatcher()->browser_context()),
67 content::Details<std::string>(&params->message));
68 return true;
71 TestLogFunction::~TestLogFunction() {}
73 bool TestLogFunction::RunSafe() {
74 scoped_ptr<Log::Params> params(Log::Params::Create(*args_));
75 EXTENSION_FUNCTION_VALIDATE(params.get());
76 VLOG(1) << params->message;
77 return true;
80 TestResetQuotaFunction::~TestResetQuotaFunction() {}
82 bool TestResetQuotaFunction::RunSafe() {
83 QuotaService* quota =
84 ExtensionSystem::Get(browser_context())->quota_service();
85 quota->Purge();
86 quota->violation_errors_.clear();
87 return true;
90 bool TestSendMessageFunction::RunAsync() {
91 scoped_ptr<PassMessage::Params> params(PassMessage::Params::Create(*args_));
92 EXTENSION_FUNCTION_VALIDATE(params.get());
93 content::NotificationService::current()->Notify(
94 chrome::NOTIFICATION_EXTENSION_TEST_MESSAGE,
95 content::Source<TestSendMessageFunction>(this),
96 content::Details<std::string>(&params->message));
97 return true;
100 TestSendMessageFunction::~TestSendMessageFunction() {}
102 void TestSendMessageFunction::Reply(const std::string& message) {
103 SetResult(new base::StringValue(message));
104 SendResponse(true);
107 // static
108 void TestGetConfigFunction::set_test_config_state(
109 base::DictionaryValue* value) {
110 TestConfigState* test_config_state = TestConfigState::GetInstance();
111 test_config_state->set_config_state(value);
114 TestGetConfigFunction::TestConfigState::TestConfigState()
115 : config_state_(NULL) {}
117 // static
118 TestGetConfigFunction::TestConfigState*
119 TestGetConfigFunction::TestConfigState::GetInstance() {
120 return Singleton<TestConfigState>::get();
123 TestGetConfigFunction::~TestGetConfigFunction() {}
125 bool TestGetConfigFunction::RunSafe() {
126 TestConfigState* test_config_state = TestConfigState::GetInstance();
128 if (!test_config_state->config_state()) {
129 error_ = kNoTestConfigDataError;
130 return false;
133 SetResult(test_config_state->config_state()->DeepCopy());
134 return true;
137 TestWaitForRoundTripFunction::~TestWaitForRoundTripFunction() {}
139 bool TestWaitForRoundTripFunction::RunSafe() {
140 scoped_ptr<WaitForRoundTrip::Params> params(
141 WaitForRoundTrip::Params::Create(*args_));
142 SetResult(new base::StringValue(params->message));
143 return true;
146 } // namespace extensions