1 /*
2  * gnote
3  *
4  * Copyright (C) 2017,2019-2020 Aurimas Cernius
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 #include <UnitTest++/UnitTest++.h>
22 
23 #include "test/testgnote.hpp"
24 #include "test/testnotemanager.hpp"
25 
26 
SUITE(NoteManager)27 SUITE(NoteManager)
28 {
29   struct Fixture
30   {
31     test::Gnote g;
32     test::NoteManager manager;
33 
34     Fixture()
35       : manager(make_notes_dir(), g)
36     {
37       g.notebook_manager(&manager.notebook_manager());
38     }
39 
40     Glib::ustring make_notes_dir()
41     {
42       char notes_dir_tmpl[] = "/tmp/gnotetestnotesXXXXXX";
43       char *notes_dir = g_mkdtemp(notes_dir_tmpl);
44       return notes_dir;
45     }
46 
47     gnote::NoteBase::Ptr create_template_note()
48     {
49       auto templ = manager.get_or_create_template_note();
50       templ->set_xml_content(Glib::ustring::compose("<note-content><note-title>%1</note-title>\n\ntest template content</note-content>", templ->get_title()));
51       return templ;
52     }
53   };
54 
55 
56   TEST(get_note_content)
57   {
58     auto content = gnote::NoteManagerBase::get_note_content("test_title", "test_content");
59     CHECK(content.find("<note-title>test_title</note-title>") != Glib::ustring::npos);
60     CHECK(content.find("test_content") != Glib::ustring::npos);
61   }
62 
63   TEST(get_note_content_special_chars)
64   {
65     auto content = gnote::NoteManagerBase::get_note_content("test<title", "test>content");
66     CHECK(content.find("<note-title>test&lt;title</note-title>") != Glib::ustring::npos);
67     CHECK(content.find("test&gt;content") != Glib::ustring::npos);
68   }
69 
70   TEST(split_title_from_content)
71   {
72     Glib::ustring body;
73     auto title = gnote::NoteManagerBase::split_title_from_content("test", body);
74     CHECK_EQUAL("test", title);
75     CHECK(body.empty());
76 
77     title = gnote::NoteManagerBase::split_title_from_content("test\ncontent", body);
78     CHECK_EQUAL("test", title);
79     CHECK_EQUAL("content", body);
80   }
81 
82   TEST_FIXTURE(Fixture, create_no_args)
83   {
84     auto note1 = manager.create();
85     auto note2 = manager.create();
86 
87     CHECK_EQUAL("New Note 1", note1->get_title());
88     CHECK(note1->data().text().find("Describe your new note here.") != Glib::ustring::npos);
89     CHECK_EQUAL("New Note 2", note2->get_title());
90     CHECK(note2->data().text().find("Describe your new note here.") != Glib::ustring::npos);
91     CHECK_EQUAL(2, manager.get_notes().size());
92   }
93 
94   TEST_FIXTURE(Fixture, create_no_args_from_template)
95   {
96     auto templ = create_template_note();
97     auto note = manager.create();
98     CHECK_EQUAL("New Note 1", note->get_title());
99     CHECK(note->data().text().find("test template content") != Glib::ustring::npos);
100   }
101 
102   TEST_FIXTURE(Fixture, create_with_title)
103   {
104     auto note = manager.create("test");
105     CHECK_EQUAL("test", note->get_title());
106     CHECK(note->data().text().find("Describe your new note here.") != Glib::ustring::npos);
107     CHECK_EQUAL(1, manager.get_notes().size());
108   }
109 
110   TEST_FIXTURE(Fixture, create_with_title_from_template)
111   {
112     auto templ = create_template_note();
113     auto note = manager.create("test");
114     CHECK_EQUAL("test", note->get_title());
115     CHECK(note->data().text().find("test template content") != Glib::ustring::npos);
116     CHECK_EQUAL(2, manager.get_notes().size());
117   }
118 
119   TEST_FIXTURE(Fixture, create_with_text_content)
120   {
121     auto note = manager.create("test\ntest content");
122     CHECK_EQUAL("test", note->get_title());
123     CHECK(note->data().text().find("test content") != Glib::ustring::npos);
124     CHECK_EQUAL(1, manager.get_notes().size());
125   }
126 
127   TEST_FIXTURE(Fixture, create_with_text_content_having_template)
128   {
129     auto templ = create_template_note();
130     auto note = manager.create("test\ntest content");
131     CHECK_EQUAL("test", note->get_title());
132     CHECK(note->data().text().find("test content") != Glib::ustring::npos);
133     CHECK_EQUAL(2, manager.get_notes().size());
134   }
135 
136   TEST_FIXTURE(Fixture, create_and_find)
137   {
138     manager.create();
139     manager.create();
140     gnote::NoteBase::Ptr test_note = manager.create("test note");
141     CHECK(test_note != NULL);
142     CHECK_EQUAL(3, manager.get_notes().size());
143     CHECK(manager.find("test note") == test_note);
144     CHECK(manager.find_by_uri(test_note->uri()) == test_note);
145   }
146 
147   TEST_FIXTURE(Fixture, create_with_xml)
148   {
149     auto note = manager.create("test", "<note-content><note-title>test</note-title>\n\ntest content");
150     CHECK_EQUAL("test", note->get_title());
151     CHECK(note->data().text().find("test content") != Glib::ustring::npos);
152     CHECK_EQUAL(1, manager.get_notes().size());
153   }
154 
155   TEST_FIXTURE(Fixture, create_with_guid)
156   {
157     auto note = manager.create_with_guid("test", "93b3f3ef-9eea-4cdc-9f78-76af1629987a");
158     CHECK_EQUAL("test", note->get_title());
159     CHECK(note->data().text().find("Describe your new note here.") != Glib::ustring::npos);
160     CHECK_EQUAL("93b3f3ef-9eea-4cdc-9f78-76af1629987a", note->id());
161     CHECK_EQUAL("note://gnote/93b3f3ef-9eea-4cdc-9f78-76af1629987a", note->uri());
162     CHECK_EQUAL(1, manager.get_notes().size());
163     auto other = manager.find_by_uri("note://gnote/93b3f3ef-9eea-4cdc-9f78-76af1629987a");
164     CHECK_EQUAL(note, other);
165   }
166 
167   TEST_FIXTURE(Fixture, create_with_guid_from_template)
168   {
169     auto templ = create_template_note();
170     auto note = manager.create_with_guid("test", "93b3f3ef-9eea-4cdc-9f78-76af1629987a");
171     CHECK_EQUAL("test", note->get_title());
172     CHECK(note->data().text().find("test template content") != Glib::ustring::npos);
173     CHECK_EQUAL("93b3f3ef-9eea-4cdc-9f78-76af1629987a", note->id());
174     CHECK_EQUAL("note://gnote/93b3f3ef-9eea-4cdc-9f78-76af1629987a", note->uri());
175     CHECK_EQUAL(2, manager.get_notes().size());
176     auto other = manager.find_by_uri("note://gnote/93b3f3ef-9eea-4cdc-9f78-76af1629987a");
177     CHECK_EQUAL(note, other);
178   }
179 
180   TEST_FIXTURE(Fixture, create_with_guid_multiline_title)
181   {
182     auto note = manager.create_with_guid("test\ntest content", "93b3f3ef-9eea-4cdc-9f78-76af1629987a");
183     CHECK_EQUAL("test", note->get_title());
184     CHECK(note->data().text().find("test content") != Glib::ustring::npos);
185     CHECK_EQUAL("93b3f3ef-9eea-4cdc-9f78-76af1629987a", note->id());
186     CHECK_EQUAL("note://gnote/93b3f3ef-9eea-4cdc-9f78-76af1629987a", note->uri());
187     CHECK_EQUAL(1, manager.get_notes().size());
188   }
189 }
190 
191