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.
5 #include "components/dom_distiller/core/dom_distiller_test_util.h"
7 #include "components/dom_distiller/core/dom_distiller_store.h"
8 #include "components/dom_distiller/core/fake_distiller.h"
10 using leveldb_proto::test::FakeDB
;
12 namespace dom_distiller
{
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
);
29 ObserverUpdatesMatcher::ObserverUpdatesMatcher(
30 const std::vector
<DomDistillerObserver::ArticleUpdate
>& expected_updates
)
31 : expected_updates_(expected_updates
) {}
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();
41 std::vector
<DomDistillerObserver::ArticleUpdate
>::const_iterator expected
,
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
;
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
;
60 void ObserverUpdatesMatcher::DescribeUpdates(std::ostream
* os
) const {
62 for (std::vector
<DomDistillerObserver::ArticleUpdate
>::const_iterator i
=
63 expected_updates_
.begin();
64 i
!= expected_updates_
.end(); ++i
) {
70 *os
<< "( EntryId: " << i
->entry_id
<< ", UpdateType: " << i
->update_type
75 void ObserverUpdatesMatcher::DescribeTo(std::ostream
* os
) const {
76 *os
<< " has updates: { ";
80 void ObserverUpdatesMatcher::DescribeNegationTo(std::ostream
* os
) const {
81 *os
<< " does not have updates: { ";
86 testing::Matcher
<const std::vector
<DomDistillerObserver::ArticleUpdate
>&>
88 const std::vector
<DomDistillerObserver::ArticleUpdate
>& expected_updates
) {
89 return testing::MakeMatcher(new ObserverUpdatesMatcher(expected_updates
));
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());
103 } // namespace dom_distiller