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_model.h"
6
7#include "testing/gtest/include/gtest/gtest.h"
8
9namespace dom_distiller {
10
11TEST(DomDistillerModelTest, TestGetByEntryId) {
12  ArticleEntry entry1;
13  entry1.set_entry_id("id1");
14  entry1.set_title("title1");
15  ArticleEntry entry2;
16  entry2.set_entry_id("id2");
17  entry2.set_title("title1");
18
19  std::vector<ArticleEntry> initial_model;
20  initial_model.push_back(entry1);
21  initial_model.push_back(entry2);
22
23  DomDistillerModel model(initial_model);
24
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));
29
30  EXPECT_TRUE(model.GetEntryById(entry2.entry_id(), &found_entry));
31  ASSERT_TRUE(IsEntryValid(found_entry));
32  EXPECT_TRUE(AreEntriesEqual(entry2, found_entry));
33
34  EXPECT_FALSE(model.GetEntryById("some_other_id", NULL));
35}
36
37TEST(DomDistillerModelTest, TestGetByUrl) {
38  ArticleEntry entry1;
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");
45
46  ArticleEntry entry2;
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");
51
52  std::vector<ArticleEntry> initial_model;
53  initial_model.push_back(entry1);
54  initial_model.push_back(entry2);
55
56  DomDistillerModel model(initial_model);
57
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));
62
63  EXPECT_TRUE(model.GetEntryByUrl(GURL(page2->url()), &found_entry));
64  ASSERT_TRUE(IsEntryValid(found_entry));
65  EXPECT_TRUE(AreEntriesEqual(entry1, found_entry));
66
67  EXPECT_TRUE(model.GetEntryByUrl(GURL(page3->url()), &found_entry));
68  ASSERT_TRUE(IsEntryValid(found_entry));
69  EXPECT_TRUE(AreEntriesEqual(entry2, found_entry));
70
71  EXPECT_FALSE(model.GetEntryByUrl(GURL("http://example.com/foo"), NULL));
72}
73
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).
78TEST(DomDistillerModelTest, TestUrlToMultipleEntries) {
79  ArticleEntry entry1;
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");
86
87  ArticleEntry entry2;
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");
92
93  std::vector<ArticleEntry> initial_model;
94  initial_model.push_back(entry1);
95  initial_model.push_back(entry2);
96
97  DomDistillerModel model(initial_model);
98
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));
102
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));
107
108  EXPECT_TRUE(model.GetEntryById(entry2.entry_id(), &found_entry));
109  ASSERT_TRUE(IsEntryValid(found_entry));
110  EXPECT_TRUE(AreEntriesEqual(entry2, found_entry));
111
112  syncer::SyncChangeList changes_to_apply;
113  syncer::SyncChangeList changes_applied;
114  syncer::SyncChangeList changes_missing;
115
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);
121
122  EXPECT_TRUE(model.GetEntryById(entry1.entry_id(), &found_entry));
123  ASSERT_TRUE(IsEntryValid(found_entry));
124  EXPECT_TRUE(AreEntriesEqual(entry1, found_entry));
125
126  EXPECT_TRUE(model.GetEntryById(entry2.entry_id(), &found_entry));
127  ASSERT_TRUE(IsEntryValid(found_entry));
128  EXPECT_TRUE(AreEntriesEqual(entry2, found_entry));
129}
130
131}  // namespace dom_distiller
132