Home | History | Annotate | Download | only in core
      1 // Copyright 2013 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 "components/dom_distiller/core/dom_distiller_test_util.h"
      6 
      7 #include "components/dom_distiller/core/dom_distiller_store.h"
      8 #include "components/dom_distiller/core/fake_distiller.h"
      9 
     10 using leveldb_proto::test::FakeDB;
     11 
     12 namespace dom_distiller {
     13 namespace test {
     14 namespace util {
     15 
     16 namespace {
     17 
     18 std::vector<ArticleEntry> EntryMapToList(
     19     const FakeDB<ArticleEntry>::EntryMap& entries) {
     20   std::vector<ArticleEntry> entry_list;
     21   for (FakeDB<ArticleEntry>::EntryMap::const_iterator it = entries.begin();
     22        it != entries.end(); ++it) {
     23     entry_list.push_back(it->second);
     24   }
     25   return entry_list;
     26 }
     27 }  // namespace
     28 
     29 ObserverUpdatesMatcher::ObserverUpdatesMatcher(
     30     const std::vector<DomDistillerObserver::ArticleUpdate>& expected_updates)
     31     : expected_updates_(expected_updates) {}
     32 
     33 bool ObserverUpdatesMatcher::MatchAndExplain(
     34     const std::vector<DomDistillerObserver::ArticleUpdate>& actual_updates,
     35     testing::MatchResultListener* listener) const {
     36   if (actual_updates.size() != expected_updates_.size()) {
     37     *listener << "Incorrect number of updates. Expected: "
     38               << expected_updates_.size() << " got: " << actual_updates.size();
     39     return false;
     40   }
     41   std::vector<DomDistillerObserver::ArticleUpdate>::const_iterator expected,
     42       actual;
     43   for (expected = expected_updates_.begin(), actual = actual_updates.begin();
     44        expected != expected_updates_.end(); ++expected, ++actual) {
     45     if (expected->entry_id != actual->entry_id) {
     46       *listener << " Mismatched entry id. Expected: " << expected->entry_id
     47                 << " actual: " << actual->entry_id;
     48       return false;
     49     }
     50     if (expected->update_type != actual->update_type) {
     51       *listener << " Mismatched update for entryid:" << expected->entry_id
     52                 << ". Expected: " << expected->update_type
     53                 << " actual: " << actual->update_type;
     54       return false;
     55     }
     56   }
     57   return true;
     58 }
     59 
     60 void ObserverUpdatesMatcher::DescribeUpdates(std::ostream* os) const {
     61   bool start = true;
     62   for (std::vector<DomDistillerObserver::ArticleUpdate>::const_iterator i =
     63            expected_updates_.begin();
     64        i != expected_updates_.end(); ++i) {
     65     if (start) {
     66       start = false;
     67     } else {
     68       *os << ", ";
     69     }
     70     *os << "( EntryId: " << i->entry_id << ", UpdateType: " << i->update_type
     71         << " )";
     72   }
     73 }
     74 
     75 void ObserverUpdatesMatcher::DescribeTo(std::ostream* os) const {
     76   *os << " has updates: { ";
     77   DescribeUpdates(os);
     78   *os << "}";
     79 }
     80 void ObserverUpdatesMatcher::DescribeNegationTo(std::ostream* os) const {
     81   *os << " does not have updates: { ";
     82   DescribeUpdates(os);
     83   *os << "}";
     84 }
     85 
     86 testing::Matcher<const std::vector<DomDistillerObserver::ArticleUpdate>&>
     87 HasExpectedUpdates(
     88     const std::vector<DomDistillerObserver::ArticleUpdate>& expected_updates) {
     89   return testing::MakeMatcher(new ObserverUpdatesMatcher(expected_updates));
     90 }
     91 
     92 // static
     93 DomDistillerStore* CreateStoreWithFakeDB(
     94     FakeDB<ArticleEntry>* fake_db,
     95     const FakeDB<ArticleEntry>::EntryMap& store_model) {
     96   return new DomDistillerStore(
     97       scoped_ptr<leveldb_proto::ProtoDatabase<ArticleEntry> >(fake_db),
     98       EntryMapToList(store_model), FakeDB<ArticleEntry>::DirectoryForTestDB());
     99 }
    100 
    101 }  // namespace util
    102 }  // namespace test
    103 }  // namespace dom_distiller
    104