Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / sfx2 / qa / cppunit / test_metadatable.cxx
blob97f9fc753a181a39550aaf79a351b6827c9b061b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/types.h>
21 #include "cppunit/TestAssert.h"
22 #include "cppunit/TestFixture.h"
23 #include "cppunit/extensions/HelperMacros.h"
24 #include "cppunit/plugin/TestPlugIn.h"
26 #include <rtl/ustrbuf.hxx>
28 #include <com/sun/star/util/DateTime.hpp>
29 #include <com/sun/star/util/Date.hpp>
30 #include <com/sun/star/util/Duration.hpp>
32 #include <sfx2/Metadatable.hxx>
33 #include <sfx2/XmlIdRegistry.hxx>
35 #include <boost/scoped_ptr.hpp>
38 using namespace ::com::sun::star;
41 namespace {
43 class MetadatableTest
44 : public ::CppUnit::TestFixture
46 public:
47 virtual void setUp();
48 virtual void tearDown();
50 void test();
52 CPPUNIT_TEST_SUITE(MetadatableTest);
53 CPPUNIT_TEST(test);
54 CPPUNIT_TEST_SUITE_END();
56 private:
59 void MetadatableTest::setUp()
63 void MetadatableTest::tearDown()
68 class MockMetadatable
69 : public ::sfx2::Metadatable
71 private:
72 ::sfx2::IXmlIdRegistry & m_rRegistry;
74 public:
75 MockMetadatable(::sfx2::IXmlIdRegistry & i_rReg,
76 bool const i_isInClip = false)
77 : m_rRegistry(i_rReg)
78 , m_bInClipboard(i_isInClip), m_bInUndo(false), m_bInContent(true) {}
79 bool m_bInClipboard;
80 bool m_bInUndo;
81 bool m_bInContent;
82 virtual bool IsInClipboard() const { return m_bInClipboard; }
83 virtual bool IsInUndo() const { return m_bInUndo; }
84 virtual bool IsInContent() const { return m_bInContent; }
85 virtual ::sfx2::IXmlIdRegistry& GetRegistry() { return m_rRegistry; }
86 virtual ::com::sun::star::uno::Reference<
87 ::com::sun::star::rdf::XMetadatable > MakeUnoObject() { return 0; }
90 static bool operator==(beans::StringPair p1, beans::StringPair p2)
92 return p1.First == p2.First && p1.Second == p2.Second;
95 void MetadatableTest::test()
97 OSL_TRACE("SwMetadatable test(): start");
98 boost::scoped_ptr< ::sfx2::IXmlIdRegistry > const pReg(
99 ::sfx2::createXmlIdRegistry(false) );
100 boost::scoped_ptr< ::sfx2::IXmlIdRegistry > const pRegClip(
101 ::sfx2::createXmlIdRegistry(true) );
103 MockMetadatable m1(*pReg);
104 MockMetadatable m2(*pReg);
105 MockMetadatable m3(*pReg);
106 MockMetadatable m4(*pReg);
107 MockMetadatable m5(*pReg);
108 ::rtl::OUString empty;
109 ::rtl::OUString content( "content.xml" );
110 ::rtl::OUString styles( "styles.xml" );
111 ::rtl::OUString sid1( "id1" );
112 ::rtl::OUString sid2( "id2" );
113 ::rtl::OUString sid3( "id3" );
114 ::rtl::OUString sid4( "id4" );
115 beans::StringPair id1(content, sid1);
116 beans::StringPair id2(content, sid2);
117 beans::StringPair id3(content, sid3);
118 beans::StringPair id4(styles, sid4);
119 beans::StringPair id3e(empty, sid3);
120 beans::StringPair id4e(empty, sid4);
121 m1.SetMetadataReference(id1);
122 CPPUNIT_ASSERT_MESSAGE("set failed", m1.GetMetadataReference() == id1);
123 try {
124 m2.SetMetadataReference(id1);
125 CPPUNIT_ASSERT_MESSAGE("set duplicate succeeded", false);
126 } catch (const lang::IllegalArgumentException &) { }
127 m1.SetMetadataReference(id1);
128 CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
129 m1.GetMetadataReference() == id1);
130 m1.EnsureMetadataReference();
131 CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
132 m1.GetMetadataReference() == id1);
134 m2.EnsureMetadataReference();
135 beans::StringPair m2id(m2.GetMetadataReference());
136 CPPUNIT_ASSERT_MESSAGE("ensure failed", !m2id.Second.isEmpty());
137 m2.EnsureMetadataReference();
138 CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
139 m2.GetMetadataReference() == m2id);
141 m1.m_bInUndo = true;
142 CPPUNIT_ASSERT_MESSAGE("move to undo failed",
143 m1.GetMetadataReference().Second.isEmpty());
145 m1.m_bInUndo = false;
146 CPPUNIT_ASSERT_MESSAGE("move from undo failed",
147 m1.GetMetadataReference() == id1);
149 m1.m_bInUndo = true;
150 try {
151 m2.SetMetadataReference(id1); // steal!
152 } catch (lang::IllegalArgumentException &) {
153 CPPUNIT_FAIL("set duplicate to undo failed");
155 m1.m_bInUndo = false;
156 CPPUNIT_ASSERT_MESSAGE("move from undo: duplicate",
157 m1.GetMetadataReference().Second.isEmpty());
159 m3.RegisterAsCopyOf(m2);
160 CPPUNIT_ASSERT_MESSAGE("copy: source", m2.GetMetadataReference() == id1);
161 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
162 m3.GetMetadataReference().Second.isEmpty());
163 m4.RegisterAsCopyOf(m3);
164 CPPUNIT_ASSERT_MESSAGE("copy: source", m2.GetMetadataReference() == id1);
165 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
166 m3.GetMetadataReference().Second.isEmpty());
167 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
168 m4.GetMetadataReference().Second.isEmpty());
169 m2.m_bInUndo = true;
170 CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
171 m3.GetMetadataReference() == id1);
172 CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
173 m2.GetMetadataReference().Second.isEmpty());
174 m2.m_bInUndo = false;
175 CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
176 m2.GetMetadataReference() == id1);
177 CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
178 m3.GetMetadataReference().Second.isEmpty());
180 m4.EnsureMetadataReference(); // new!
181 beans::StringPair m4id(m4.GetMetadataReference());
182 CPPUNIT_ASSERT_MESSAGE("ensure on duplicate",
183 !m4id.Second.isEmpty() && !(m4id == id1));
185 MockMetadatable mc1(*pRegClip, true); // in clipboard
186 MockMetadatable mc2(*pRegClip, true);
187 MockMetadatable mc3(*pRegClip, true);
188 MockMetadatable mc4(*pRegClip, true);
189 MockMetadatable m2p(*pReg);
190 MockMetadatable m3p(*pReg);
192 mc1.SetMetadataReference(id2);
193 CPPUNIT_ASSERT_MESSAGE("set failed", mc1.GetMetadataReference() == id2);
194 try {
195 mc2.SetMetadataReference(id2);
196 CPPUNIT_FAIL("set duplicate succeeded");
197 } catch (const lang::IllegalArgumentException &) { }
198 mc1.SetMetadataReference(id2);
199 CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
200 mc1.GetMetadataReference() == id2);
201 mc1.EnsureMetadataReference();
202 CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
203 mc1.GetMetadataReference() == id2);
204 mc2.EnsureMetadataReference();
205 beans::StringPair mc2id(mc2.GetMetadataReference());
206 CPPUNIT_ASSERT_MESSAGE("ensure failed", !mc2id.Second.isEmpty());
207 mc2.EnsureMetadataReference();
208 CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
209 mc2.GetMetadataReference() == mc2id);
210 mc2.RemoveMetadataReference();
211 CPPUNIT_ASSERT_MESSAGE("remove failed",
212 mc2.GetMetadataReference().Second.isEmpty());
214 // set up mc2 as copy of m2 and mc3 as copy of m3
215 mc3.RegisterAsCopyOf(m3);
216 CPPUNIT_ASSERT_MESSAGE("copy to clipboard (latent)",
217 mc3.GetMetadataReference().Second.isEmpty() );
218 mc2.RegisterAsCopyOf(m2);
219 CPPUNIT_ASSERT_MESSAGE("copy to clipboard (non-latent)",
220 mc2.GetMetadataReference() == id1);
221 // paste mc2 to m2p and mc3 to m3p
222 m2p.RegisterAsCopyOf(mc2);
223 CPPUNIT_ASSERT_MESSAGE("paste from clipboard (non-latent)",
224 m2p.GetMetadataReference().Second.isEmpty() );
225 m3p.RegisterAsCopyOf(mc3);
226 CPPUNIT_ASSERT_MESSAGE("paste from clipboard (latent)",
227 m3p.GetMetadataReference().Second.isEmpty() );
228 // delete m2, m2p, m3
229 m2.RemoveMetadataReference();
230 CPPUNIT_ASSERT_MESSAGE("remove failed",
231 m2.GetMetadataReference().Second.isEmpty());
232 CPPUNIT_ASSERT_MESSAGE("paste-remove (non-latent)",
233 m2p.GetMetadataReference() == id1);
234 m2p.RemoveMetadataReference();
235 CPPUNIT_ASSERT_MESSAGE("remove failed",
236 m2p.GetMetadataReference().Second.isEmpty());
237 CPPUNIT_ASSERT_MESSAGE("paste-remove2 (non-latent)",
238 m3.GetMetadataReference() == id1);
239 m3.RemoveMetadataReference();
240 CPPUNIT_ASSERT_MESSAGE("remove failed",
241 m3.GetMetadataReference().Second.isEmpty());
242 CPPUNIT_ASSERT_MESSAGE("paste-remove (latent)",
243 m3p.GetMetadataReference() == id1);
244 // delete mc2
245 mc2.SetMetadataReference(beans::StringPair());
246 CPPUNIT_ASSERT_MESSAGE("in clipboard becomes non-latent",
247 mc3.GetMetadataReference().Second.isEmpty() );
248 // paste mc2
249 m2p.RegisterAsCopyOf(mc2);
250 CPPUNIT_ASSERT_MESSAGE("remove-paste",
251 m2p.GetMetadataReference().Second.isEmpty());
252 CPPUNIT_ASSERT_MESSAGE("remove-paste (stolen)",
253 m3p.GetMetadataReference() == id1);
255 // auto-detect stream
256 m5.SetMetadataReference(id3e);
257 CPPUNIT_ASSERT_MESSAGE("auto-detect (content)",
258 m5.GetMetadataReference() == id3);
259 m5.m_bInContent = false;
260 m5.SetMetadataReference(id4e);
261 CPPUNIT_ASSERT_MESSAGE("auto-detect (styles)",
262 m5.GetMetadataReference() == id4);
264 OSL_TRACE("sfx2::Metadatable test(): finished");
268 CPPUNIT_TEST_SUITE_REGISTRATION(MetadatableTest);
272 CPPUNIT_PLUGIN_IMPLEMENT();
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */