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