OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/extensions/extension_test_api.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/command_line.h" | |
10 #include "base/memory/singleton.h" | |
11 #include "chrome/browser/extensions/extension_service.h" | |
12 #include "chrome/browser/extensions/extension_function_dispatcher.h" | |
13 #include "chrome/browser/extensions/extensions_quota_service.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/browser/ui/browser_commands.h" | |
16 #include "chrome/common/chrome_notification_types.h" | |
17 #include "chrome/common/chrome_switches.h" | |
18 #include "content/public/browser/notification_service.h" | |
19 | |
20 namespace { | |
21 | |
22 // If you see this error in your test, you need to set the config state | |
23 // to be returned by chrome.test.getConfig(). Do this by calling | |
24 // ExtensionTestGetConfigFunction::set_test_config_state(Value* state) | |
25 // in test set up. | |
26 const char kNoTestConfigDataError[] = "Test configuration was not set."; | |
27 | |
28 const char kNotTestProcessError[] = | |
29 "The chrome.test namespace is only available in tests."; | |
30 | |
31 } // namespace | |
32 | |
33 TestExtensionFunction::~TestExtensionFunction() {} | |
34 | |
35 void TestExtensionFunction::Run() { | |
36 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) { | |
37 error_ = kNotTestProcessError; | |
38 SendResponse(false); | |
39 return; | |
40 } | |
41 SendResponse(RunImpl()); | |
42 } | |
43 | |
44 ExtensionTestPassFunction::~ExtensionTestPassFunction() {} | |
45 | |
46 bool ExtensionTestPassFunction::RunImpl() { | |
47 content::NotificationService::current()->Notify( | |
48 chrome::NOTIFICATION_EXTENSION_TEST_PASSED, | |
49 content::Source<Profile>(dispatcher()->profile()), | |
50 content::NotificationService::NoDetails()); | |
51 return true; | |
52 } | |
53 | |
54 ExtensionTestFailFunction::~ExtensionTestFailFunction() {} | |
55 | |
56 bool ExtensionTestFailFunction::RunImpl() { | |
57 std::string message; | |
58 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message)); | |
59 content::NotificationService::current()->Notify( | |
60 chrome::NOTIFICATION_EXTENSION_TEST_FAILED, | |
61 content::Source<Profile>(dispatcher()->profile()), | |
62 content::Details<std::string>(&message)); | |
63 return true; | |
64 } | |
65 | |
66 ExtensionTestLogFunction::~ExtensionTestLogFunction() {} | |
67 | |
68 bool ExtensionTestLogFunction::RunImpl() { | |
69 std::string message; | |
70 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message)); | |
71 VLOG(1) << message; | |
72 return true; | |
73 } | |
74 | |
75 ExtensionTestQuotaResetFunction::~ExtensionTestQuotaResetFunction() {} | |
76 | |
77 bool ExtensionTestQuotaResetFunction::RunImpl() { | |
78 ExtensionService* service = profile()->GetExtensionService(); | |
79 ExtensionsQuotaService* quota = service->quota_service(); | |
80 quota->Purge(); | |
81 quota->violators_.clear(); | |
82 return true; | |
83 } | |
84 | |
85 ExtensionTestCreateIncognitoTabFunction:: | |
86 ~ExtensionTestCreateIncognitoTabFunction() {} | |
87 | |
88 bool ExtensionTestCreateIncognitoTabFunction::RunImpl() { | |
89 std::string url; | |
90 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); | |
91 chrome::OpenURLOffTheRecord(profile(), GURL(url)); | |
92 return true; | |
93 } | |
94 | |
95 bool ExtensionTestSendMessageFunction::RunImpl() { | |
96 std::string message; | |
97 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &message)); | |
98 AddRef(); // balanced in Reply | |
99 content::NotificationService::current()->Notify( | |
100 chrome::NOTIFICATION_EXTENSION_TEST_MESSAGE, | |
101 content::Source<ExtensionTestSendMessageFunction>(this), | |
102 content::Details<std::string>(&message)); | |
103 return true; | |
104 } | |
105 ExtensionTestSendMessageFunction::~ExtensionTestSendMessageFunction() {} | |
106 | |
107 void ExtensionTestSendMessageFunction::Reply(const std::string& message) { | |
108 result_.reset(Value::CreateStringValue(message)); | |
109 SendResponse(true); | |
110 Release(); // balanced in RunImpl | |
111 } | |
112 | |
113 // static | |
114 void ExtensionTestGetConfigFunction::set_test_config_state( | |
115 DictionaryValue* value) { | |
116 TestConfigState* test_config_state = TestConfigState::GetInstance(); | |
117 test_config_state->set_config_state(value); | |
118 } | |
119 | |
120 ExtensionTestGetConfigFunction::TestConfigState::TestConfigState() | |
121 : config_state_(NULL) {} | |
122 | |
123 // static | |
124 ExtensionTestGetConfigFunction::TestConfigState* | |
125 ExtensionTestGetConfigFunction::TestConfigState::GetInstance() { | |
126 return Singleton<TestConfigState>::get(); | |
127 } | |
128 | |
129 ExtensionTestGetConfigFunction::~ExtensionTestGetConfigFunction() {} | |
130 | |
131 bool ExtensionTestGetConfigFunction::RunImpl() { | |
132 TestConfigState* test_config_state = TestConfigState::GetInstance(); | |
133 | |
134 if (!test_config_state->config_state()) { | |
135 error_ = kNoTestConfigDataError; | |
136 return false; | |
137 } | |
138 | |
139 result_.reset(test_config_state->config_state()->DeepCopy()); | |
140 return true; | |
141 } | |
OLD | NEW |