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