1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: recent_docs.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_shell.hxx"
34 #include <cppunit/simpleheader.hxx>
36 #include "systemshell.hxx"
37 #include "osl/file.hxx"
38 #include "osl/security.hxx"
39 #include "rtl/ustring.hxx"
44 //########################################
48 namespace syssh
= SystemShell
;
50 //########################################
51 const OUString SXW_MIME_TYPE
= OUString::createFromAscii("application/vnd.sun.xml.writer");
52 const OUString SXC_MIME_TYPE
= OUString::createFromAscii("application/vnd.sun.xml.calc");
53 const OUString SXI_MIME_TYPE
= OUString::createFromAscii("application/vnd.sun.xml.impress");
54 const OUString SXD_MIME_TYPE
= OUString::createFromAscii("application/vnd.sun.xml.draw");
55 const OUString SXM_MIME_TYPE
= OUString::createFromAscii("application/vnd.sun.xml.math");
57 class Test_AddToRecentDocs
: public CppUnit::TestFixture
61 Test_AddToRecentDocs()
65 //------------------------------------------------
66 void test_existing_recently_used_file_with_valid_xml()
68 system("rm $HOME/.recently-used");
69 system("echo '<?xml version=\"1.0\"?>\n<RecentFiles>\n<RecentItem>\n<URI>file:///home/federico/gedit.txt</URI>\n<Mime-Type>text/plain</Mime-Type>\n<Timestamp>1046485966</Timestamp>\n<Groups>\n<Group>gedit</Group>\n</Groups>\n</RecentItem>\n</RecentFiles>' > $HOME/.recently-used");
71 rtl::OUString url
= rtl::OUString::createFromAscii("file:///home_athene/test.sxw");
72 syssh::AddToRecentDocumentList(url
, SXW_MIME_TYPE
);
74 url
= rtl::OUString::createFromAscii("file:///home_athene/test.sxc");
75 syssh::AddToRecentDocumentList(url
, SXC_MIME_TYPE
);
77 url
= rtl::OUString::createFromAscii("file:///home_athene/test.sxi");
78 syssh::AddToRecentDocumentList(url
, SXI_MIME_TYPE
);
80 url
= rtl::OUString::createFromAscii("file:///home_athene/test.sxd");
81 syssh::AddToRecentDocumentList(url
, SXD_MIME_TYPE
);
83 url
= rtl::OUString::createFromAscii("file:///home_athene/test.sxm");
84 syssh::AddToRecentDocumentList(url
, SXM_MIME_TYPE
);
87 //------------------------------------------------
88 void test_existing_recently_used_file_with_invalid_xml()
90 system("rm $HOME/.recently-used");
91 system("echo '<?xml version=\"1.0\"?>\n<RecentFiles>\n<RecentItem>\n<URI>file:///home/federico/gedit.txt</URI>\n<Mime-Type>text/plain</Mime-Type>\n<Timestamp>1046485966</Timestamp>\n<Groups>\n<Group>gedit</Group>\n</Groups>\n<RecentItem>\n<URI>file:///home/federico/gedit2.txt</URI>\n<Mime-Type>text/plain</Mime-Type>\n<Timestamp>1046485966</Timestamp>\n<Groups>\n<Group>gedit</Group>\n</Groups>\n</RecentItem>\n</RecentFiles>' > $HOME/.recently-used");
93 rtl::OUString url
= rtl::OUString::createFromAscii("file:///home_athene/test.sxw");
94 syssh::AddToRecentDocumentList(url
, SXW_MIME_TYPE
);
97 //------------------------------------------------
98 void test_non_existing_recently_used_file()
100 int ret
= system("rm $HOME/.recently-used");
102 rtl::OUString url
= rtl::OUString::createFromAscii("file:///home_athene/test.sxw");
103 syssh::AddToRecentDocumentList(url
, SXW_MIME_TYPE
);
105 url
= rtl::OUString::createFromAscii("file:///home_athene/test.sxc");
106 syssh::AddToRecentDocumentList(url
, SXC_MIME_TYPE
);
108 url
= rtl::OUString::createFromAscii("file:///home_athene/test.sxi");
109 syssh::AddToRecentDocumentList(url
, SXI_MIME_TYPE
);
111 url
= rtl::OUString::createFromAscii("file:///home_athene/test.sxd");
112 syssh::AddToRecentDocumentList(url
, SXD_MIME_TYPE
);
114 url
= rtl::OUString::createFromAscii("file:///home_athene/test.sxm");
115 syssh::AddToRecentDocumentList(url
, SXM_MIME_TYPE
);
117 ret
= system("ls $HOME/.recently-used");
119 CPPUNIT_ASSERT_MESSAGE(".recently-used doesn't exist", 0 == ret
);
122 //------------------------------------------------
123 OUString
get_recently_used_url()
126 OUString recently_used_url
;
128 CPPUNIT_ASSERT_MESSAGE("cannot get user home directory", sec
.getHomeDir(recently_used_url
));
130 if ((recently_used_url
.getLength() > 0) &&
131 ('/' != recently_used_url
.pData
->buffer
[recently_used_url
.getLength() - 1]))
132 recently_used_url
+= rtl::OUString::createFromAscii("/");
134 recently_used_url
+= rtl::OUString::createFromAscii(".recently-used");
135 return recently_used_url
;
138 //------------------------------------------------
139 void read_recently_used(void* buffer
, size_t size
)
141 File
ruf(get_recently_used_url());
142 FileBase::RC rc
= ruf
.open(OpenFlag_Read
);
144 CPPUNIT_ASSERT_MESSAGE("Cannot open ~/.recently-used", FileBase::E_None
== rc
);
147 ruf
.read(buffer
, size
, read
);
149 CPPUNIT_ASSERT_MESSAGE("Cannot read .recently-used", (read
> 0));
152 //------------------------------------------------
153 void test_existing_recently_used_file_without_xml()
155 // remove an existing .recently-used file
156 // create a new one with non xml data
157 system("rm $HOME/.recently-used");
158 system("date > $HOME/.recently-used");
160 char cnt_before
[128];
161 memset(cnt_before
, 0, sizeof(cnt_before
));
162 read_recently_used(cnt_before
, sizeof(cnt_before
));
164 OUString url
= OUString::createFromAscii("file:///home_athene/test.sxw");
165 syssh::AddToRecentDocumentList(url
, SXW_MIME_TYPE
);
168 memset(cnt_after
, 0, sizeof(cnt_after
));
169 read_recently_used(cnt_after
, sizeof(cnt_after
));
170 CPPUNIT_ASSERT_MESSAGE(".recently-used unexpectly touched", (0 == strcmp(cnt_before
, cnt_after
)));
173 //------------------------------------------------
174 void test_existing_recently_used_with_unknown_xml()
176 system("rm $HOME/.recently-used");
178 char* xml_unknown
= "<?xml version=\"1.0\"?>\n<test>test</test>";
180 sprintf(cmd
, "echo '%s' > $HOME/.recently-used", xml_unknown
);
183 OUString url
= OUString::createFromAscii("file:///home_athene/test.sxw");
184 syssh::AddToRecentDocumentList(url
, SXW_MIME_TYPE
);
187 memset(buffer
, 0, sizeof(buffer
));
188 read_recently_used(buffer
, sizeof(buffer
));
190 CPPUNIT_ASSERT_MESSAGE("Unexpected recently-used content", (0 == strncmp(xml_unknown
, buffer
, strlen(xml_unknown
))));
193 //------------------------------------------------
194 void test_existing_empty_recently_used_file()
196 system("rm $HOME/.recently-used");
197 system("touch $HOME/.recently-used");
199 OUString url
= OUString::createFromAscii("file:///home_athene/test.sxw");
200 syssh::AddToRecentDocumentList(url
, SXW_MIME_TYPE
);
203 memset(buffer
, 0, sizeof(buffer
));
204 read_recently_used(buffer
, sizeof(buffer
));
206 char* expected
= "<?xml version=\"1.0\"?>\n<RecentFiles>";
207 CPPUNIT_ASSERT_MESSAGE("Unexpected recently-used content", (0 == strncmp(buffer
, expected
, strlen(expected
))));
210 //------------------------------------------------
211 CPPUNIT_TEST_SUITE(Test_AddToRecentDocs
);
212 CPPUNIT_TEST(test_existing_recently_used_file_with_valid_xml
);
213 CPPUNIT_TEST(test_non_existing_recently_used_file
);
214 CPPUNIT_TEST(test_existing_recently_used_file_without_xml
);
215 CPPUNIT_TEST(test_existing_recently_used_with_unknown_xml
);
216 CPPUNIT_TEST(test_existing_empty_recently_used_file
);
217 CPPUNIT_TEST(test_existing_recently_used_file_with_invalid_xml
);
218 CPPUNIT_TEST_SUITE_END();
222 //#####################################
223 // register test suites
224 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Test_AddToRecentDocs
, "Test_AddToRecentDocs");