Home | History | Annotate | Download | only in test
      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 
     41 TestExtensionFunction::~TestExtensionFunction() {}
     42 
     43 void TestExtensionFunction::Run() {
     44   if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
     45     error_ = kNotTestProcessError;
     46     SendResponse(false);
     47     return;
     48   }
     49   SendResponse(RunImpl());
     50 }
     51 
     52 TestNotifyPassFunction::~TestNotifyPassFunction() {}
     53 
     54 bool 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 
     62 TestNotifyFailFunction::~TestNotifyFailFunction() {}
     63 
     64 bool 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>(&params->message));
     71   return true;
     72 }
     73 
     74 TestLogFunction::~TestLogFunction() {}
     75 
     76 bool 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 
     83 TestResetQuotaFunction::~TestResetQuotaFunction() {}
     84 
     85 bool 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::
     94    ~TestCreateIncognitoTabFunction() {}
     95 
     96 bool 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 
    105 bool 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>(&params->message));
    113   return true;
    114 }
    115 
    116 TestSendMessageFunction::~TestSendMessageFunction() {}
    117 
    118 void TestSendMessageFunction::Reply(const std::string& message) {
    119   SetResult(new base::StringValue(message));
    120   SendResponse(true);
    121 }
    122 
    123 // static
    124 void 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 
    130 TestGetConfigFunction::TestConfigState::TestConfigState()
    131   : config_state_(NULL) {}
    132 
    133 // static
    134 TestGetConfigFunction::TestConfigState*
    135 TestGetConfigFunction::TestConfigState::GetInstance() {
    136   return Singleton<TestConfigState>::get();
    137 }
    138 
    139 TestGetConfigFunction::~TestGetConfigFunction() {}
    140 
    141 bool 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