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/api/test/test_api.h" 6 7 #include <string> 8 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_function_dispatcher.h" 13 #include "chrome/browser/extensions/extension_service.h" 14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/ui/browser_commands.h" 16 #include "chrome/common/chrome_switches.h" 17 #include "chrome/common/extensions/api/test.h" 18 #include "content/public/browser/notification_service.h" 19 #include "extensions/browser/quota_service.h" 20 21 namespace { 22 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) 26 // in test set up. 27 const char kNoTestConfigDataError[] = "Test configuration was not set."; 28 29 const char kNotTestProcessError[] = 30 "The chrome.test namespace is only available in tests."; 31 32 } // namespace 33 34 namespace extensions { 35 36 namespace CreateIncognitoTab = api::test::CreateIncognitoTab; 37 namespace Log = api::test::Log; 38 namespace NotifyFail = api::test::NotifyFail; 39 namespace PassMessage = api::test::PassMessage; 40 ~TestExtensionFunction()41TestExtensionFunction::~TestExtensionFunction() {} 42 Run()43void TestExtensionFunction::Run() { 44 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) { 45 error_ = kNotTestProcessError; 46 SendResponse(false); 47 return; 48 } 49 SendResponse(RunImpl()); 50 } 51 ~TestNotifyPassFunction()52TestNotifyPassFunction::~TestNotifyPassFunction() {} 53 RunImpl()54bool TestNotifyPassFunction::RunImpl() { 55 content::NotificationService::current()->Notify( 56 chrome::NOTIFICATION_EXTENSION_TEST_PASSED, 57 content::Source<content::BrowserContext>(dispatcher()->browser_context()), 58 content::NotificationService::NoDetails()); 59 return true; 60 } 61 ~TestNotifyFailFunction()62TestNotifyFailFunction::~TestNotifyFailFunction() {} 63 RunImpl()64bool 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<content::BrowserContext>(dispatcher()->browser_context()), 70 content::Details<std::string>(¶ms->message)); 71 return true; 72 } 73 ~TestLogFunction()74TestLogFunction::~TestLogFunction() {} 75 RunImpl()76bool TestLogFunction::RunImpl() { 77 scoped_ptr<Log::Params> params(Log::Params::Create(*args_)); 78 EXTENSION_FUNCTION_VALIDATE(params.get()); 79 VLOG(1) << params->message; 80 return true; 81 } 82 ~TestResetQuotaFunction()83TestResetQuotaFunction::~TestResetQuotaFunction() {} 84 RunImpl()85bool TestResetQuotaFunction::RunImpl() { 86 ExtensionService* service = GetProfile()->GetExtensionService(); 87 QuotaService* quota = service->quota_service(); 88 quota->Purge(); 89 quota->violation_errors_.clear(); 90 return true; 91 } 92 93 TestCreateIncognitoTabFunction:: ~TestCreateIncognitoTabFunction()94 ~TestCreateIncognitoTabFunction() {} 95 RunImpl()96bool 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()); 102 return true; 103 } 104 RunImpl()105bool TestSendMessageFunction::RunImpl() { 106 scoped_ptr<PassMessage::Params> params( 107 PassMessage::Params::Create(*args_)); 108 EXTENSION_FUNCTION_VALIDATE(params.get()); 109 content::NotificationService::current()->Notify( 110 chrome::NOTIFICATION_EXTENSION_TEST_MESSAGE, 111 content::Source<TestSendMessageFunction>(this), 112 content::Details<std::string>(¶ms->message)); 113 return true; 114 } 115 ~TestSendMessageFunction()116TestSendMessageFunction::~TestSendMessageFunction() {} 117 Reply(const std::string & message)118void TestSendMessageFunction::Reply(const std::string& message) { 119 SetResult(new base::StringValue(message)); 120 SendResponse(true); 121 } 122 123 // static set_test_config_state(base::DictionaryValue * value)124void TestGetConfigFunction::set_test_config_state( 125 base::DictionaryValue* value) { 126 TestConfigState* test_config_state = TestConfigState::GetInstance(); 127 test_config_state->set_config_state(value); 128 } 129 TestConfigState()130TestGetConfigFunction::TestConfigState::TestConfigState() 131 : config_state_(NULL) {} 132 133 // static 134 TestGetConfigFunction::TestConfigState* GetInstance()135TestGetConfigFunction::TestConfigState::GetInstance() { 136 return Singleton<TestConfigState>::get(); 137 } 138 ~TestGetConfigFunction()139TestGetConfigFunction::~TestGetConfigFunction() {} 140 RunImpl()141bool TestGetConfigFunction::RunImpl() { 142 TestConfigState* test_config_state = TestConfigState::GetInstance(); 143 144 if (!test_config_state->config_state()) { 145 error_ = kNoTestConfigDataError; 146 return false; 147 } 148 149 SetResult(test_config_state->config_state()->DeepCopy()); 150 return true; 151 } 152 153 } // namespace extensions 154