extension_test_api.cc revision 4a5e2dc747d50c653511c68ccb2cfbfb740bd5a7
1// Copyright (c) 2009 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.
4
5#include "chrome/browser/extensions/extension_test_api.h"
6
7#include <string>
8
9#include "chrome/browser/extensions/extensions_service.h"
10#include "chrome/browser/extensions/extensions_quota_service.h"
11#include "chrome/browser/profile.h"
12#include "chrome/browser/ui/browser.h"
13#include "chrome/common/notification_service.h"
14
15namespace {
16
17// If you see this error in your test, you need to set the config state
18// to be returned by chrome.test.getConfig().  Do this by calling
19// ExtensionTestGetConfigFunction::set_test_config_state(Value* state)
20// in test set up.
21const char kNoTestConfigDataError[] = "Test configuration was not set.";
22
23}  // namespace
24
25ExtensionTestPassFunction::~ExtensionTestPassFunction() {}
26
27bool ExtensionTestPassFunction::RunImpl() {
28  NotificationService::current()->Notify(
29      NotificationType::EXTENSION_TEST_PASSED,
30      Source<Profile>(dispatcher()->profile()),
31      NotificationService::NoDetails());
32  return true;
33}
34
35ExtensionTestFailFunction::~ExtensionTestFailFunction() {}
36
37bool ExtensionTestFailFunction::RunImpl() {
38  std::string message;
39  EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message));
40  NotificationService::current()->Notify(
41      NotificationType::EXTENSION_TEST_FAILED,
42      Source<Profile>(dispatcher()->profile()),
43      Details<std::string>(&message));
44  return true;
45}
46
47ExtensionTestLogFunction::~ExtensionTestLogFunction() {}
48
49bool ExtensionTestLogFunction::RunImpl() {
50  std::string message;
51  EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message));
52  return true;
53}
54
55ExtensionTestQuotaResetFunction::~ExtensionTestQuotaResetFunction() {}
56
57bool ExtensionTestQuotaResetFunction::RunImpl() {
58  ExtensionsService* service = profile()->GetExtensionsService();
59  ExtensionsQuotaService* quota = service->quota_service();
60  quota->Purge();
61  quota->violators_.clear();
62  return true;
63}
64
65ExtensionTestCreateIncognitoTabFunction::
66   ~ExtensionTestCreateIncognitoTabFunction() {}
67
68bool ExtensionTestCreateIncognitoTabFunction::RunImpl() {
69  std::string url;
70  EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url));
71  Browser::OpenURLOffTheRecord(profile(), GURL(url));
72  return true;
73}
74
75bool ExtensionTestSendMessageFunction::RunImpl() {
76  std::string message;
77  EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message));
78  AddRef();  // balanced in Reply
79  NotificationService::current()->Notify(
80      NotificationType::EXTENSION_TEST_MESSAGE,
81      Source<ExtensionTestSendMessageFunction>(this),
82      Details<std::string>(&message));
83  return true;
84}
85ExtensionTestSendMessageFunction::~ExtensionTestSendMessageFunction() {}
86
87void ExtensionTestSendMessageFunction::Reply(const std::string& message) {
88  result_.reset(Value::CreateStringValue(message));
89  SendResponse(true);
90  Release();  // balanced in RunImpl
91}
92
93// static
94void ExtensionTestGetConfigFunction::set_test_config_state(
95    DictionaryValue* value) {
96  TestConfigState* test_config_state = Singleton<TestConfigState>::get();
97  test_config_state->set_config_state(value);
98}
99
100ExtensionTestGetConfigFunction::TestConfigState::TestConfigState()
101  : config_state_(NULL) {}
102
103ExtensionTestGetConfigFunction::~ExtensionTestGetConfigFunction() {}
104
105bool ExtensionTestGetConfigFunction::RunImpl() {
106  TestConfigState* test_config_state = Singleton<TestConfigState>::get();
107
108  if (!test_config_state->config_state()) {
109    error_ = kNoTestConfigDataError;
110    return false;
111  }
112
113  result_.reset(test_config_state->config_state()->DeepCopy());
114  return true;
115}
116