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_service.h"
     13 #include "chrome/browser/extensions/extension_function_dispatcher.h"
     14 #include "chrome/browser/extensions/extensions_quota_service.h"
     15 #include "chrome/browser/profiles/profile.h"
     16 #include "chrome/browser/ui/browser_commands.h"
     17 #include "chrome/common/chrome_switches.h"
     18 #include "chrome/common/extensions/api/test.h"
     19 #include "content/public/browser/notification_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<Profile>(dispatcher()->profile()),
     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<Profile>(dispatcher()->profile()),
     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 = profile()->GetExtensionService();
     87   ExtensionsQuotaService* 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(profile(), GURL(params->url),
    101                               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   AddRef();  // balanced in Reply
    110   content::NotificationService::current()->Notify(
    111       chrome::NOTIFICATION_EXTENSION_TEST_MESSAGE,
    112       content::Source<TestSendMessageFunction>(this),
    113       content::Details<std::string>(&params->message));
    114   return true;
    115 }
    116 
    117 TestSendMessageFunction::~TestSendMessageFunction() {}
    118 
    119 void TestSendMessageFunction::Reply(const std::string& message) {
    120   SetResult(Value::CreateStringValue(message));
    121   SendResponse(true);
    122   Release();  // balanced in RunImpl
    123 }
    124 
    125 // static
    126 void TestGetConfigFunction::set_test_config_state(
    127     base::DictionaryValue* value) {
    128   TestConfigState* test_config_state = TestConfigState::GetInstance();
    129   test_config_state->set_config_state(value);
    130 }
    131 
    132 TestGetConfigFunction::TestConfigState::TestConfigState()
    133   : config_state_(NULL) {}
    134 
    135 // static
    136 TestGetConfigFunction::TestConfigState*
    137 TestGetConfigFunction::TestConfigState::GetInstance() {
    138   return Singleton<TestConfigState>::get();
    139 }
    140 
    141 TestGetConfigFunction::~TestGetConfigFunction() {}
    142 
    143 bool TestGetConfigFunction::RunImpl() {
    144   TestConfigState* test_config_state = TestConfigState::GetInstance();
    145 
    146   if (!test_config_state->config_state()) {
    147     error_ = kNoTestConfigDataError;
    148     return false;
    149   }
    150 
    151   SetResult(test_config_state->config_state()->DeepCopy());
    152   return true;
    153 }
    154 
    155 }  // namespace extensions
    156