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_model.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace dom_distiller
{
11 TEST(DomDistillerModelTest
, TestGetByEntryId
) {
13 entry1
.set_entry_id("id1");
14 entry1
.set_title("title1");
16 entry2
.set_entry_id("id2");
17 entry2
.set_title("title1");
19 std::vector
<ArticleEntry
> initial_model
;
20 initial_model
.push_back(entry1
);
21 initial_model
.push_back(entry2
);
23 DomDistillerModel
model(initial_model
);
25 ArticleEntry found_entry
;
26 EXPECT_TRUE(model
.GetEntryById(entry1
.entry_id(), &found_entry
));
27 ASSERT_TRUE(IsEntryValid(found_entry
));
28 EXPECT_TRUE(AreEntriesEqual(entry1
, found_entry
));
30 EXPECT_TRUE(model
.GetEntryById(entry2
.entry_id(), &found_entry
));
31 ASSERT_TRUE(IsEntryValid(found_entry
));
32 EXPECT_TRUE(AreEntriesEqual(entry2
, found_entry
));
34 EXPECT_FALSE(model
.GetEntryById("some_other_id", NULL
));
37 TEST(DomDistillerModelTest
, TestGetByUrl
) {
39 entry1
.set_entry_id("id1");
40 entry1
.set_title("title1");
41 ArticleEntryPage
* page1
= entry1
.add_pages();
42 page1
->set_url("http://example.com/1");
43 ArticleEntryPage
* page2
= entry1
.add_pages();
44 page2
->set_url("http://example.com/2");
47 entry2
.set_entry_id("id2");
48 entry2
.set_title("title1");
49 ArticleEntryPage
* page3
= entry2
.add_pages();
50 page3
->set_url("http://example.com/a1");
52 std::vector
<ArticleEntry
> initial_model
;
53 initial_model
.push_back(entry1
);
54 initial_model
.push_back(entry2
);
56 DomDistillerModel
model(initial_model
);
58 ArticleEntry found_entry
;
59 EXPECT_TRUE(model
.GetEntryByUrl(GURL(page1
->url()), &found_entry
));
60 ASSERT_TRUE(IsEntryValid(found_entry
));
61 EXPECT_TRUE(AreEntriesEqual(entry1
, found_entry
));
63 EXPECT_TRUE(model
.GetEntryByUrl(GURL(page2
->url()), &found_entry
));
64 ASSERT_TRUE(IsEntryValid(found_entry
));
65 EXPECT_TRUE(AreEntriesEqual(entry1
, found_entry
));
67 EXPECT_TRUE(model
.GetEntryByUrl(GURL(page3
->url()), &found_entry
));
68 ASSERT_TRUE(IsEntryValid(found_entry
));
69 EXPECT_TRUE(AreEntriesEqual(entry2
, found_entry
));
71 EXPECT_FALSE(model
.GetEntryByUrl(GURL("http://example.com/foo"), NULL
));
74 // This test ensures that the model handles the case where an URL maps to
75 // multiple entries. In that case, the model is allowed to have an inconsistent
76 // url-to-entry mapping, but it should not fail in other ways (i.e. id-to-entry
77 // should be correct, shouldn't crash).
78 TEST(DomDistillerModelTest
, TestUrlToMultipleEntries
) {
80 entry1
.set_entry_id("id1");
81 entry1
.set_title("title1");
82 ArticleEntryPage
* page1
= entry1
.add_pages();
83 page1
->set_url("http://example.com/1");
84 ArticleEntryPage
* page2
= entry1
.add_pages();
85 page2
->set_url("http://example.com/2");
88 entry2
.set_entry_id("id2");
89 entry2
.set_title("title1");
90 ArticleEntryPage
* page3
= entry2
.add_pages();
91 page3
->set_url("http://example.com/1");
93 std::vector
<ArticleEntry
> initial_model
;
94 initial_model
.push_back(entry1
);
95 initial_model
.push_back(entry2
);
97 DomDistillerModel
model(initial_model
);
99 EXPECT_TRUE(model
.GetEntryByUrl(GURL(page1
->url()), NULL
));
100 EXPECT_TRUE(model
.GetEntryByUrl(GURL(page2
->url()), NULL
));
101 EXPECT_TRUE(model
.GetEntryByUrl(GURL(page3
->url()), NULL
));
103 ArticleEntry found_entry
;
104 EXPECT_TRUE(model
.GetEntryById(entry1
.entry_id(), &found_entry
));
105 ASSERT_TRUE(IsEntryValid(found_entry
));
106 EXPECT_TRUE(AreEntriesEqual(entry1
, found_entry
));
108 EXPECT_TRUE(model
.GetEntryById(entry2
.entry_id(), &found_entry
));
109 ASSERT_TRUE(IsEntryValid(found_entry
));
110 EXPECT_TRUE(AreEntriesEqual(entry2
, found_entry
));
112 syncer::SyncChangeList changes_to_apply
;
113 syncer::SyncChangeList changes_applied
;
114 syncer::SyncChangeList changes_missing
;
116 entry2
.mutable_pages(0)->set_url("http://example.com/foo1");
117 changes_to_apply
.push_back(syncer::SyncChange(
118 FROM_HERE
, syncer::SyncChange::ACTION_UPDATE
, CreateLocalData(entry2
)));
119 model
.ApplyChangesToModel(
120 changes_to_apply
, &changes_applied
, &changes_missing
);
122 EXPECT_TRUE(model
.GetEntryById(entry1
.entry_id(), &found_entry
));
123 ASSERT_TRUE(IsEntryValid(found_entry
));
124 EXPECT_TRUE(AreEntriesEqual(entry1
, found_entry
));
126 EXPECT_TRUE(model
.GetEntryById(entry2
.entry_id(), &found_entry
));
127 ASSERT_TRUE(IsEntryValid(found_entry
));
128 EXPECT_TRUE(AreEntriesEqual(entry2
, found_entry
));
131 } // namespace dom_distiller