Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / sfx2 / qa / cppunit / test_metadatable.cxx
blobdcec3aed005fcd6cf2fccf85ced940322d79cad5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "sal/config.h"
30 #include "sal/precppunit.hxx"
32 #include "cppunit/TestAssert.h"
33 #include "cppunit/TestFixture.h"
34 #include "cppunit/extensions/HelperMacros.h"
35 #include "cppunit/plugin/TestPlugIn.h"
37 #include <rtl/ustrbuf.hxx>
39 #include <com/sun/star/util/DateTime.hpp>
40 #include <com/sun/star/util/Date.hpp>
41 #include <com/sun/star/util/Duration.hpp>
43 #include <sfx2/Metadatable.hxx>
44 #include <sfx2/XmlIdRegistry.hxx>
46 #include <boost/scoped_ptr.hpp>
49 using namespace ::com::sun::star;
52 namespace {
54 class MetadatableTest
55 : public ::CppUnit::TestFixture
57 public:
58 virtual void setUp();
59 virtual void tearDown();
61 void test();
63 CPPUNIT_TEST_SUITE(MetadatableTest);
64 CPPUNIT_TEST(test);
65 CPPUNIT_TEST_SUITE_END();
67 private:
70 void MetadatableTest::setUp()
74 void MetadatableTest::tearDown()
79 class MockMetadatable
80 : public ::sfx2::Metadatable
82 private:
83 ::sfx2::IXmlIdRegistry & m_rRegistry;
85 public:
86 MockMetadatable(::sfx2::IXmlIdRegistry & i_rReg,
87 bool const i_isInClip = false)
88 : m_rRegistry(i_rReg)
89 , m_bInClipboard(i_isInClip), m_bInUndo(false), m_bInContent(true) {}
90 bool m_bInClipboard;
91 bool m_bInUndo;
92 bool m_bInContent;
93 virtual bool IsInClipboard() const { return m_bInClipboard; }
94 virtual bool IsInUndo() const { return m_bInUndo; }
95 virtual bool IsInContent() const { return m_bInContent; }
96 virtual ::sfx2::IXmlIdRegistry& GetRegistry() { return m_rRegistry; }
97 virtual ::com::sun::star::uno::Reference<
98 ::com::sun::star::rdf::XMetadatable > MakeUnoObject() { return 0; }
101 static bool operator==(beans::StringPair p1, beans::StringPair p2)
103 return p1.First == p2.First && p1.Second == p2.Second;
106 void MetadatableTest::test()
108 OSL_TRACE("SwMetadatable test(): start");
109 boost::scoped_ptr< ::sfx2::IXmlIdRegistry > const pReg(
110 ::sfx2::createXmlIdRegistry(false) );
111 boost::scoped_ptr< ::sfx2::IXmlIdRegistry > const pRegClip(
112 ::sfx2::createXmlIdRegistry(true) );
114 MockMetadatable m1(*pReg);
115 MockMetadatable m2(*pReg);
116 MockMetadatable m3(*pReg);
117 MockMetadatable m4(*pReg);
118 MockMetadatable m5(*pReg);
119 ::rtl::OUString empty;
120 ::rtl::OUString content( "content.xml" );
121 ::rtl::OUString styles( "styles.xml" );
122 ::rtl::OUString sid1( "id1" );
123 ::rtl::OUString sid2( "id2" );
124 ::rtl::OUString sid3( "id3" );
125 ::rtl::OUString sid4( "id4" );
126 beans::StringPair id1(content, sid1);
127 beans::StringPair id2(content, sid2);
128 beans::StringPair id3(content, sid3);
129 beans::StringPair id4(styles, sid4);
130 beans::StringPair id3e(empty, sid3);
131 beans::StringPair id4e(empty, sid4);
132 m1.SetMetadataReference(id1);
133 CPPUNIT_ASSERT_MESSAGE("set failed", m1.GetMetadataReference() == id1);
134 try {
135 m2.SetMetadataReference(id1);
136 CPPUNIT_ASSERT_MESSAGE("set duplicate succeeded", false);
137 } catch (const lang::IllegalArgumentException &) { }
138 m1.SetMetadataReference(id1);
139 CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
140 m1.GetMetadataReference() == id1);
141 m1.EnsureMetadataReference();
142 CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
143 m1.GetMetadataReference() == id1);
145 m2.EnsureMetadataReference();
146 beans::StringPair m2id(m2.GetMetadataReference());
147 CPPUNIT_ASSERT_MESSAGE("ensure failed", !m2id.Second.isEmpty());
148 m2.EnsureMetadataReference();
149 CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
150 m2.GetMetadataReference() == m2id);
152 m1.m_bInUndo = true;
153 CPPUNIT_ASSERT_MESSAGE("move to undo failed",
154 m1.GetMetadataReference().Second.isEmpty());
156 m1.m_bInUndo = false;
157 CPPUNIT_ASSERT_MESSAGE("move from undo failed",
158 m1.GetMetadataReference() == id1);
160 m1.m_bInUndo = true;
161 try {
162 m2.SetMetadataReference(id1); // steal!
163 } catch (lang::IllegalArgumentException &) {
164 CPPUNIT_FAIL("set duplicate to undo failed");
166 m1.m_bInUndo = false;
167 CPPUNIT_ASSERT_MESSAGE("move from undo: duplicate",
168 m1.GetMetadataReference().Second.isEmpty());
170 m3.RegisterAsCopyOf(m2);
171 CPPUNIT_ASSERT_MESSAGE("copy: source", m2.GetMetadataReference() == id1);
172 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
173 m3.GetMetadataReference().Second.isEmpty());
174 m4.RegisterAsCopyOf(m3);
175 CPPUNIT_ASSERT_MESSAGE("copy: source", m2.GetMetadataReference() == id1);
176 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
177 m3.GetMetadataReference().Second.isEmpty());
178 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
179 m4.GetMetadataReference().Second.isEmpty());
180 m2.m_bInUndo = true;
181 CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
182 m3.GetMetadataReference() == id1);
183 CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
184 m2.GetMetadataReference().Second.isEmpty());
185 m2.m_bInUndo = false;
186 CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
187 m2.GetMetadataReference() == id1);
188 CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
189 m3.GetMetadataReference().Second.isEmpty());
191 m4.EnsureMetadataReference(); // new!
192 beans::StringPair m4id(m4.GetMetadataReference());
193 CPPUNIT_ASSERT_MESSAGE("ensure on duplicate",
194 !m4id.Second.isEmpty() && !(m4id == id1));
196 MockMetadatable mc1(*pRegClip, true); // in clipboard
197 MockMetadatable mc2(*pRegClip, true);
198 MockMetadatable mc3(*pRegClip, true);
199 MockMetadatable mc4(*pRegClip, true);
200 MockMetadatable m2p(*pReg);
201 MockMetadatable m3p(*pReg);
203 mc1.SetMetadataReference(id2);
204 CPPUNIT_ASSERT_MESSAGE("set failed", mc1.GetMetadataReference() == id2);
205 try {
206 mc2.SetMetadataReference(id2);
207 CPPUNIT_FAIL("set duplicate succeeded");
208 } catch (const lang::IllegalArgumentException &) { }
209 mc1.SetMetadataReference(id2);
210 CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
211 mc1.GetMetadataReference() == id2);
212 mc1.EnsureMetadataReference();
213 CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
214 mc1.GetMetadataReference() == id2);
215 mc2.EnsureMetadataReference();
216 beans::StringPair mc2id(mc2.GetMetadataReference());
217 CPPUNIT_ASSERT_MESSAGE("ensure failed", !mc2id.Second.isEmpty());
218 mc2.EnsureMetadataReference();
219 CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
220 mc2.GetMetadataReference() == mc2id);
221 mc2.RemoveMetadataReference();
222 CPPUNIT_ASSERT_MESSAGE("remove failed",
223 mc2.GetMetadataReference().Second.isEmpty());
225 // set up mc2 as copy of m2 and mc3 as copy of m3
226 mc3.RegisterAsCopyOf(m3);
227 CPPUNIT_ASSERT_MESSAGE("copy to clipboard (latent)",
228 mc3.GetMetadataReference().Second.isEmpty() );
229 mc2.RegisterAsCopyOf(m2);
230 CPPUNIT_ASSERT_MESSAGE("copy to clipboard (non-latent)",
231 mc2.GetMetadataReference() == id1);
232 // paste mc2 to m2p and mc3 to m3p
233 m2p.RegisterAsCopyOf(mc2);
234 CPPUNIT_ASSERT_MESSAGE("paste from clipboard (non-latent)",
235 m2p.GetMetadataReference().Second.isEmpty() );
236 m3p.RegisterAsCopyOf(mc3);
237 CPPUNIT_ASSERT_MESSAGE("paste from clipboard (latent)",
238 m3p.GetMetadataReference().Second.isEmpty() );
239 // delete m2, m2p, m3
240 m2.RemoveMetadataReference();
241 CPPUNIT_ASSERT_MESSAGE("remove failed",
242 m2.GetMetadataReference().Second.isEmpty());
243 CPPUNIT_ASSERT_MESSAGE("paste-remove (non-latent)",
244 m2p.GetMetadataReference() == id1);
245 m2p.RemoveMetadataReference();
246 CPPUNIT_ASSERT_MESSAGE("remove failed",
247 m2p.GetMetadataReference().Second.isEmpty());
248 CPPUNIT_ASSERT_MESSAGE("paste-remove2 (non-latent)",
249 m3.GetMetadataReference() == id1);
250 m3.RemoveMetadataReference();
251 CPPUNIT_ASSERT_MESSAGE("remove failed",
252 m3.GetMetadataReference().Second.isEmpty());
253 CPPUNIT_ASSERT_MESSAGE("paste-remove (latent)",
254 m3p.GetMetadataReference() == id1);
255 // delete mc2
256 mc2.SetMetadataReference(beans::StringPair());
257 CPPUNIT_ASSERT_MESSAGE("in clipboard becomes non-latent",
258 mc3.GetMetadataReference().Second.isEmpty() );
259 // paste mc2
260 m2p.RegisterAsCopyOf(mc2);
261 CPPUNIT_ASSERT_MESSAGE("remove-paste",
262 m2p.GetMetadataReference().Second.isEmpty());
263 CPPUNIT_ASSERT_MESSAGE("remove-paste (stolen)",
264 m3p.GetMetadataReference() == id1);
266 // auto-detect stream
267 m5.SetMetadataReference(id3e);
268 CPPUNIT_ASSERT_MESSAGE("auto-detect (content)",
269 m5.GetMetadataReference() == id3);
270 m5.m_bInContent = false;
271 m5.SetMetadataReference(id4e);
272 CPPUNIT_ASSERT_MESSAGE("auto-detect (styles)",
273 m5.GetMetadataReference() == id4);
275 OSL_TRACE("sfx2::Metadatable test(): finished");
279 CPPUNIT_TEST_SUITE_REGISTRATION(MetadatableTest);
283 CPPUNIT_PLUGIN_IMPLEMENT();
285 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */