Home | History | Annotate | Download | only in js
      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 "sync/js/js_test_util.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/memory/scoped_ptr.h"
      9 #include "sync/js/js_arg_list.h"
     10 #include "sync/js/js_event_details.h"
     11 
     12 namespace syncer {
     13 
     14 void PrintTo(const JsArgList& args, ::std::ostream* os) {
     15   *os << args.ToString();
     16 }
     17 
     18 void PrintTo(const JsEventDetails& details, ::std::ostream* os) {
     19   *os << details.ToString();
     20 }
     21 
     22 namespace {
     23 
     24 // Matcher implementation for HasArgs().
     25 class HasArgsMatcher
     26     : public ::testing::MatcherInterface<const JsArgList&> {
     27  public:
     28   explicit HasArgsMatcher(const JsArgList& expected_args)
     29       : expected_args_(expected_args) {}
     30 
     31   virtual ~HasArgsMatcher() {}
     32 
     33   virtual bool MatchAndExplain(
     34       const JsArgList& args,
     35       ::testing::MatchResultListener* listener) const {
     36     // No need to annotate listener since we already define PrintTo().
     37     return args.Get().Equals(&expected_args_.Get());
     38   }
     39 
     40   virtual void DescribeTo(::std::ostream* os) const {
     41     *os << "has args " << expected_args_.ToString();
     42   }
     43 
     44   virtual void DescribeNegationTo(::std::ostream* os) const {
     45     *os << "doesn't have args " << expected_args_.ToString();
     46   }
     47 
     48  private:
     49   const JsArgList expected_args_;
     50 
     51   DISALLOW_COPY_AND_ASSIGN(HasArgsMatcher);
     52 };
     53 
     54 // Matcher implementation for HasDetails().
     55 class HasDetailsMatcher
     56     : public ::testing::MatcherInterface<const JsEventDetails&> {
     57  public:
     58   explicit HasDetailsMatcher(const JsEventDetails& expected_details)
     59       : expected_details_(expected_details) {}
     60 
     61   virtual ~HasDetailsMatcher() {}
     62 
     63   virtual bool MatchAndExplain(
     64       const JsEventDetails& details,
     65       ::testing::MatchResultListener* listener) const {
     66     // No need to annotate listener since we already define PrintTo().
     67     return details.Get().Equals(&expected_details_.Get());
     68   }
     69 
     70   virtual void DescribeTo(::std::ostream* os) const {
     71     *os << "has details " << expected_details_.ToString();
     72   }
     73 
     74   virtual void DescribeNegationTo(::std::ostream* os) const {
     75     *os << "doesn't have details " << expected_details_.ToString();
     76   }
     77 
     78  private:
     79   const JsEventDetails expected_details_;
     80 
     81   DISALLOW_COPY_AND_ASSIGN(HasDetailsMatcher);
     82 };
     83 
     84 }  // namespace
     85 
     86 ::testing::Matcher<const JsArgList&> HasArgs(const JsArgList& expected_args) {
     87   return ::testing::MakeMatcher(new HasArgsMatcher(expected_args));
     88 }
     89 
     90 ::testing::Matcher<const JsArgList&> HasArgsAsList(
     91     const base::ListValue& expected_args) {
     92   scoped_ptr<base::ListValue> expected_args_copy(expected_args.DeepCopy());
     93   return HasArgs(JsArgList(expected_args_copy.get()));
     94 }
     95 
     96 ::testing::Matcher<const JsEventDetails&> HasDetails(
     97     const JsEventDetails& expected_details) {
     98   return ::testing::MakeMatcher(new HasDetailsMatcher(expected_details));
     99 }
    100 
    101 ::testing::Matcher<const JsEventDetails&> HasDetailsAsDictionary(
    102     const base::DictionaryValue& expected_details) {
    103   scoped_ptr<base::DictionaryValue> expected_details_copy(
    104       expected_details.DeepCopy());
    105   return HasDetails(JsEventDetails(expected_details_copy.get()));
    106 }
    107 
    108 MockJsBackend::MockJsBackend() {}
    109 
    110 MockJsBackend::~MockJsBackend() {}
    111 
    112 WeakHandle<JsBackend> MockJsBackend::AsWeakHandle() {
    113   return MakeWeakHandle(AsWeakPtr());
    114 }
    115 
    116 MockJsController::MockJsController() {}
    117 
    118 MockJsController::~MockJsController() {}
    119 
    120 MockJsEventHandler::MockJsEventHandler() {}
    121 
    122 WeakHandle<JsEventHandler> MockJsEventHandler::AsWeakHandle() {
    123   return MakeWeakHandle(AsWeakPtr());
    124 }
    125 
    126 MockJsEventHandler::~MockJsEventHandler() {}
    127 
    128 MockJsReplyHandler::MockJsReplyHandler() {}
    129 
    130 MockJsReplyHandler::~MockJsReplyHandler() {}
    131 
    132 WeakHandle<JsReplyHandler> MockJsReplyHandler::AsWeakHandle() {
    133   return MakeWeakHandle(AsWeakPtr());
    134 }
    135 
    136 }  // namespace syncer
    137 
    138