1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
44 : public ::CppUnit::TestFixture
47 virtual void setUp() SAL_OVERRIDE
;
48 virtual void tearDown() SAL_OVERRIDE
;
52 CPPUNIT_TEST_SUITE(MetadatableTest
);
54 CPPUNIT_TEST_SUITE_END();
59 void MetadatableTest::setUp()
63 void MetadatableTest::tearDown()
69 : public ::sfx2::Metadatable
72 ::sfx2::IXmlIdRegistry
& m_rRegistry
;
75 MockMetadatable(::sfx2::IXmlIdRegistry
& i_rReg
,
76 bool const i_isInClip
= false)
78 , m_bInClipboard(i_isInClip
), m_bInUndo(false), m_bInContent(true) {}
82 virtual bool IsInClipboard() const SAL_OVERRIDE
{ return m_bInClipboard
; }
83 virtual bool IsInUndo() const SAL_OVERRIDE
{ return m_bInUndo
; }
84 virtual bool IsInContent() const SAL_OVERRIDE
{ return m_bInContent
; }
85 virtual ::sfx2::IXmlIdRegistry
& GetRegistry() SAL_OVERRIDE
{ return m_rRegistry
; }
86 virtual ::com::sun::star::uno::Reference
<
87 ::com::sun::star::rdf::XMetadatable
> MakeUnoObject() SAL_OVERRIDE
{ 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 boost::scoped_ptr
< ::sfx2::IXmlIdRegistry
> const pReg(
98 ::sfx2::createXmlIdRegistry(false) );
99 boost::scoped_ptr
< ::sfx2::IXmlIdRegistry
> const pRegClip(
100 ::sfx2::createXmlIdRegistry(true) );
102 MockMetadatable
m1(*pReg
);
103 MockMetadatable
m2(*pReg
);
104 MockMetadatable
m3(*pReg
);
105 MockMetadatable
m4(*pReg
);
106 MockMetadatable
m5(*pReg
);
108 OUString
content( "content.xml" );
109 OUString
styles( "styles.xml" );
110 OUString
sid1( "id1" );
111 OUString
sid2( "id2" );
112 OUString
sid3( "id3" );
113 OUString
sid4( "id4" );
114 beans::StringPair
id1(content
, sid1
);
115 beans::StringPair
id2(content
, sid2
);
116 beans::StringPair
id3(content
, sid3
);
117 beans::StringPair
id4(styles
, sid4
);
118 beans::StringPair
id3e(empty
, sid3
);
119 beans::StringPair
id4e(empty
, sid4
);
120 m1
.SetMetadataReference(id1
);
121 CPPUNIT_ASSERT_MESSAGE("set failed", m1
.GetMetadataReference() == id1
);
123 m2
.SetMetadataReference(id1
);
124 CPPUNIT_ASSERT_MESSAGE("set duplicate succeeded", false);
125 } catch (const lang::IllegalArgumentException
&) { }
126 m1
.SetMetadataReference(id1
);
127 CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
128 m1
.GetMetadataReference() == id1
);
129 m1
.EnsureMetadataReference();
130 CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
131 m1
.GetMetadataReference() == id1
);
133 m2
.EnsureMetadataReference();
134 beans::StringPair
m2id(m2
.GetMetadataReference());
135 CPPUNIT_ASSERT_MESSAGE("ensure failed", !m2id
.Second
.isEmpty());
136 m2
.EnsureMetadataReference();
137 CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
138 m2
.GetMetadataReference() == m2id
);
141 CPPUNIT_ASSERT_MESSAGE("move to undo failed",
142 m1
.GetMetadataReference().Second
.isEmpty());
144 m1
.m_bInUndo
= false;
145 CPPUNIT_ASSERT_MESSAGE("move from undo failed",
146 m1
.GetMetadataReference() == id1
);
150 m2
.SetMetadataReference(id1
); // steal!
151 } catch (lang::IllegalArgumentException
&) {
152 CPPUNIT_FAIL("set duplicate to undo failed");
154 m1
.m_bInUndo
= false;
155 CPPUNIT_ASSERT_MESSAGE("move from undo: duplicate",
156 m1
.GetMetadataReference().Second
.isEmpty());
158 m3
.RegisterAsCopyOf(m2
);
159 CPPUNIT_ASSERT_MESSAGE("copy: source", m2
.GetMetadataReference() == id1
);
160 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
161 m3
.GetMetadataReference().Second
.isEmpty());
162 m4
.RegisterAsCopyOf(m3
);
163 CPPUNIT_ASSERT_MESSAGE("copy: source", m2
.GetMetadataReference() == id1
);
164 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
165 m3
.GetMetadataReference().Second
.isEmpty());
166 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
167 m4
.GetMetadataReference().Second
.isEmpty());
169 CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
170 m3
.GetMetadataReference() == id1
);
171 CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
172 m2
.GetMetadataReference().Second
.isEmpty());
173 m2
.m_bInUndo
= false;
174 CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
175 m2
.GetMetadataReference() == id1
);
176 CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
177 m3
.GetMetadataReference().Second
.isEmpty());
179 m4
.EnsureMetadataReference(); // new!
180 beans::StringPair
m4id(m4
.GetMetadataReference());
181 CPPUNIT_ASSERT_MESSAGE("ensure on duplicate",
182 !m4id
.Second
.isEmpty() && !(m4id
== id1
));
184 MockMetadatable
mc1(*pRegClip
, true); // in clipboard
185 MockMetadatable
mc2(*pRegClip
, true);
186 MockMetadatable
mc3(*pRegClip
, true);
187 MockMetadatable
mc4(*pRegClip
, true);
188 MockMetadatable
m2p(*pReg
);
189 MockMetadatable
m3p(*pReg
);
191 mc1
.SetMetadataReference(id2
);
192 CPPUNIT_ASSERT_MESSAGE("set failed", mc1
.GetMetadataReference() == id2
);
194 mc2
.SetMetadataReference(id2
);
195 CPPUNIT_FAIL("set duplicate succeeded");
196 } catch (const lang::IllegalArgumentException
&) { }
197 mc1
.SetMetadataReference(id2
);
198 CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
199 mc1
.GetMetadataReference() == id2
);
200 mc1
.EnsureMetadataReference();
201 CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
202 mc1
.GetMetadataReference() == id2
);
203 mc2
.EnsureMetadataReference();
204 beans::StringPair
mc2id(mc2
.GetMetadataReference());
205 CPPUNIT_ASSERT_MESSAGE("ensure failed", !mc2id
.Second
.isEmpty());
206 mc2
.EnsureMetadataReference();
207 CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
208 mc2
.GetMetadataReference() == mc2id
);
209 mc2
.RemoveMetadataReference();
210 CPPUNIT_ASSERT_MESSAGE("remove failed",
211 mc2
.GetMetadataReference().Second
.isEmpty());
213 // set up mc2 as copy of m2 and mc3 as copy of m3
214 mc3
.RegisterAsCopyOf(m3
);
215 CPPUNIT_ASSERT_MESSAGE("copy to clipboard (latent)",
216 mc3
.GetMetadataReference().Second
.isEmpty() );
217 mc2
.RegisterAsCopyOf(m2
);
218 CPPUNIT_ASSERT_MESSAGE("copy to clipboard (non-latent)",
219 mc2
.GetMetadataReference() == id1
);
220 // paste mc2 to m2p and mc3 to m3p
221 m2p
.RegisterAsCopyOf(mc2
);
222 CPPUNIT_ASSERT_MESSAGE("paste from clipboard (non-latent)",
223 m2p
.GetMetadataReference().Second
.isEmpty() );
224 m3p
.RegisterAsCopyOf(mc3
);
225 CPPUNIT_ASSERT_MESSAGE("paste from clipboard (latent)",
226 m3p
.GetMetadataReference().Second
.isEmpty() );
227 // delete m2, m2p, m3
228 m2
.RemoveMetadataReference();
229 CPPUNIT_ASSERT_MESSAGE("remove failed",
230 m2
.GetMetadataReference().Second
.isEmpty());
231 CPPUNIT_ASSERT_MESSAGE("paste-remove (non-latent)",
232 m2p
.GetMetadataReference() == id1
);
233 m2p
.RemoveMetadataReference();
234 CPPUNIT_ASSERT_MESSAGE("remove failed",
235 m2p
.GetMetadataReference().Second
.isEmpty());
236 CPPUNIT_ASSERT_MESSAGE("paste-remove2 (non-latent)",
237 m3
.GetMetadataReference() == id1
);
238 m3
.RemoveMetadataReference();
239 CPPUNIT_ASSERT_MESSAGE("remove failed",
240 m3
.GetMetadataReference().Second
.isEmpty());
241 CPPUNIT_ASSERT_MESSAGE("paste-remove (latent)",
242 m3p
.GetMetadataReference() == id1
);
244 mc2
.SetMetadataReference(beans::StringPair());
245 CPPUNIT_ASSERT_MESSAGE("in clipboard becomes non-latent",
246 mc3
.GetMetadataReference().Second
.isEmpty() );
248 m2p
.RegisterAsCopyOf(mc2
);
249 CPPUNIT_ASSERT_MESSAGE("remove-paste",
250 m2p
.GetMetadataReference().Second
.isEmpty());
251 CPPUNIT_ASSERT_MESSAGE("remove-paste (stolen)",
252 m3p
.GetMetadataReference() == id1
);
254 // auto-detect stream
255 m5
.SetMetadataReference(id3e
);
256 CPPUNIT_ASSERT_MESSAGE("auto-detect (content)",
257 m5
.GetMetadataReference() == id3
);
258 m5
.m_bInContent
= false;
259 m5
.SetMetadataReference(id4e
);
260 CPPUNIT_ASSERT_MESSAGE("auto-detect (styles)",
261 m5
.GetMetadataReference() == id4
);
265 CPPUNIT_TEST_SUITE_REGISTRATION(MetadatableTest
);
269 CPPUNIT_PLUGIN_IMPLEMENT();
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */