Home | History | Annotate | Download | only in test
      1 // Copyright 2014 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 "extensions/browser/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 "content/public/browser/notification_service.h"
     13 #include "content/public/common/content_switches.h"
     14 #include "extensions/browser/extension_function_dispatcher.h"
     15 #include "extensions/browser/extension_system.h"
     16 #include "extensions/browser/quota_service.h"
     17 #include "extensions/common/api/test.h"
     18 
     19 namespace {
     20 
     21 // If you see this error in your test, you need to set the config state
     22 // to be returned by chrome.test.getConfig().  Do this by calling
     23 // TestGetConfigFunction::set_test_config_state(Value* state)
     24 // in test set up.
     25 const char kNoTestConfigDataError[] = "Test configuration was not set.";
     26 
     27 const char kNotTestProcessError[] =
     28     "The chrome.test namespace is only available in tests.";
     29 
     30 }  // namespace
     31 
     32 namespace extensions {
     33 
     34 namespace Log = core_api::test::Log;
     35 namespace NotifyFail = core_api::test::NotifyFail;
     36 namespace PassMessage = core_api::test::PassMessage;
     37 namespace WaitForRoundTrip = core_api::test::WaitForRoundTrip;
     38 
     39 TestExtensionFunction::~TestExtensionFunction() {}
     40 
     41 bool TestExtensionFunction::RunSync() {
     42   if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
     43     error_ = kNotTestProcessError;
     44     return false;
     45   }
     46   return RunSafe();
     47 }
     48 
     49 TestNotifyPassFunction::~TestNotifyPassFunction() {}
     50 
     51 bool TestNotifyPassFunction::RunSafe() {
     52   content::NotificationService::current()->Notify(
     53       chrome::NOTIFICATION_EXTENSION_TEST_PASSED,
     54       content::Source<content::BrowserContext>(dispatcher()->browser_context()),
     55       content::NotificationService::NoDetails());
     56   return true;
     57 }
     58 
     59 TestNotifyFailFunction::~TestNotifyFailFunction() {}
     60 
     61 bool TestNotifyFailFunction::RunSafe() {
     62   scoped_ptr<NotifyFail::Params> params(NotifyFail::Params::Create(*args_));
     63   EXTENSION_FUNCTION_VALIDATE(params.get());
     64   content::NotificationService::current()->Notify(
     65       chrome::NOTIFICATION_EXTENSION_TEST_FAILED,
     66       content::Source<content::BrowserContext>(dispatcher()->browser_context()),
     67       content::Details<std::string>(&params->message));
     68   return true;
     69 }
     70 
     71 TestLogFunction::~TestLogFunction() {}
     72 
     73 bool TestLogFunction::RunSafe() {
     74   scoped_ptr<Log::Params> params(Log::Params::Create(*args_));
     75   EXTENSION_FUNCTION_VALIDATE(params.get());
     76   VLOG(1) << params->message;
     77   return true;
     78 }
     79 
     80 TestResetQuotaFunction::~TestResetQuotaFunction() {}
     81 
     82 bool TestResetQuotaFunction::RunSafe() {
     83   QuotaService* quota =
     84       ExtensionSystem::Get(browser_context())->quota_service();
     85   quota->Purge();
     86   quota->violation_errors_.clear();
     87   return true;
     88 }
     89 
     90 bool TestSendMessageFunction::RunAsync() {
     91   scoped_ptr<PassMessage::Params> params(PassMessage::Params::Create(*args_));
     92   EXTENSION_FUNCTION_VALIDATE(params.get());
     93   content::NotificationService::current()->Notify(
     94       chrome::NOTIFICATION_EXTENSION_TEST_MESSAGE,
     95       content::Source<TestSendMessageFunction>(this),
     96       content::Details<std::string>(&params->message));
     97   return true;
     98 }
     99 
    100 TestSendMessageFunction::~TestSendMessageFunction() {}
    101 
    102 void TestSendMessageFunction::Reply(const std::string& message) {
    103   SetResult(new base::StringValue(message));
    104   SendResponse(true);
    105 }
    106 
    107 // static
    108 void TestGetConfigFunction::set_test_config_state(
    109     base::DictionaryValue* value) {
    110   TestConfigState* test_config_state = TestConfigState::GetInstance();
    111   test_config_state->set_config_state(value);
    112 }
    113 
    114 TestGetConfigFunction::TestConfigState::TestConfigState()
    115     : config_state_(NULL) {}
    116 
    117 // static
    118 TestGetConfigFunction::TestConfigState*
    119 TestGetConfigFunction::TestConfigState::GetInstance() {
    120   return Singleton<TestConfigState>::get();
    121 }
    122 
    123 TestGetConfigFunction::~TestGetConfigFunction() {}
    124 
    125 bool TestGetConfigFunction::RunSafe() {
    126   TestConfigState* test_config_state = TestConfigState::GetInstance();
    127 
    128   if (!test_config_state->config_state()) {
    129     error_ = kNoTestConfigDataError;
    130     return false;
    131   }
    132 
    133   SetResult(test_config_state->config_state()->DeepCopy());
    134   return true;
    135 }
    136 
    137 TestWaitForRoundTripFunction::~TestWaitForRoundTripFunction() {}
    138 
    139 bool TestWaitForRoundTripFunction::RunSafe() {
    140   scoped_ptr<WaitForRoundTrip::Params> params(
    141       WaitForRoundTrip::Params::Create(*args_));
    142   SetResult(new base::StringValue(params->message));
    143   return true;
    144 }
    145 
    146 }  // namespace extensions
    147