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 <com/sun/star/lang/IllegalArgumentException.hpp>
28 #include <sfx2/Metadatable.hxx>
29 #include <sfx2/XmlIdRegistry.hxx>
34 using namespace ::com::sun::star
;
40 : public ::CppUnit::TestFixture
45 CPPUNIT_TEST_SUITE(MetadatableTest
);
47 CPPUNIT_TEST_SUITE_END();
53 : public ::sfx2::Metadatable
56 ::sfx2::IXmlIdRegistry
& m_rRegistry
;
59 MockMetadatable(::sfx2::IXmlIdRegistry
& i_rReg
,
60 bool const i_isInClip
= false)
62 , m_bInClipboard(i_isInClip
), m_bInUndo(false), m_bInContent(true) {}
63 bool const m_bInClipboard
;
66 virtual bool IsInClipboard() const override
{ return m_bInClipboard
; }
67 virtual bool IsInUndo() const override
{ return m_bInUndo
; }
68 virtual bool IsInContent() const override
{ return m_bInContent
; }
69 virtual ::sfx2::IXmlIdRegistry
& GetRegistry() override
{ return m_rRegistry
; }
70 virtual css::uno::Reference
< css::rdf::XMetadatable
> MakeUnoObject() override
{ return nullptr; }
73 void MetadatableTest::test()
75 std::unique_ptr
< ::sfx2::IXmlIdRegistry
> const pReg(
76 ::sfx2::createXmlIdRegistry(false) );
77 std::unique_ptr
< ::sfx2::IXmlIdRegistry
> const pRegClip(
78 ::sfx2::createXmlIdRegistry(true) );
80 MockMetadatable
m1(*pReg
);
81 MockMetadatable
m2(*pReg
);
82 MockMetadatable
m3(*pReg
);
83 MockMetadatable
m4(*pReg
);
84 MockMetadatable
m5(*pReg
);
86 OUString
content( "content.xml" );
87 OUString
const styles( "styles.xml" );
88 OUString
const sid1( "id1" );
89 OUString
const sid2( "id2" );
90 OUString
sid3( "id3" );
91 OUString
sid4( "id4" );
92 beans::StringPair
id1(content
, sid1
);
93 beans::StringPair
id2(content
, sid2
);
94 beans::StringPair
id3(content
, sid3
);
95 beans::StringPair
id4(styles
, sid4
);
96 beans::StringPair
id3e(empty
, sid3
);
97 beans::StringPair
id4e(empty
, sid4
);
98 m1
.SetMetadataReference(id1
);
99 CPPUNIT_ASSERT_MESSAGE("set failed", bool(m1
.GetMetadataReference() == id1
));
101 m2
.SetMetadataReference(id1
);
102 CPPUNIT_ASSERT_MESSAGE("set duplicate succeeded", false);
103 } catch (const lang::IllegalArgumentException
&) { }
104 m1
.SetMetadataReference(id1
);
105 CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
106 bool(m1
.GetMetadataReference() == id1
));
107 m1
.EnsureMetadataReference();
108 CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
109 bool(m1
.GetMetadataReference() == id1
));
111 m2
.EnsureMetadataReference();
112 beans::StringPair
m2id(m2
.GetMetadataReference());
113 CPPUNIT_ASSERT_MESSAGE("ensure failed", !m2id
.Second
.isEmpty());
114 m2
.EnsureMetadataReference();
115 CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
116 bool(m2
.GetMetadataReference() == m2id
));
119 CPPUNIT_ASSERT_MESSAGE("move to undo failed",
120 m1
.GetMetadataReference().Second
.isEmpty());
122 m1
.m_bInUndo
= false;
123 CPPUNIT_ASSERT_MESSAGE("move from undo failed",
124 bool(m1
.GetMetadataReference() == id1
));
128 m2
.SetMetadataReference(id1
); // steal!
129 } catch (lang::IllegalArgumentException
&) {
130 CPPUNIT_FAIL("set duplicate to undo failed");
132 m1
.m_bInUndo
= false;
133 CPPUNIT_ASSERT_MESSAGE("move from undo: duplicate",
134 m1
.GetMetadataReference().Second
.isEmpty());
136 m3
.RegisterAsCopyOf(m2
);
137 CPPUNIT_ASSERT_MESSAGE("copy: source", bool(m2
.GetMetadataReference() == id1
));
138 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
139 m3
.GetMetadataReference().Second
.isEmpty());
140 m4
.RegisterAsCopyOf(m3
);
141 CPPUNIT_ASSERT_MESSAGE("copy: source", bool(m2
.GetMetadataReference() == id1
));
142 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
143 m3
.GetMetadataReference().Second
.isEmpty());
144 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
145 m4
.GetMetadataReference().Second
.isEmpty());
147 CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
148 bool(m3
.GetMetadataReference() == id1
));
149 CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
150 m2
.GetMetadataReference().Second
.isEmpty());
151 m2
.m_bInUndo
= false;
152 CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
153 bool(m2
.GetMetadataReference() == id1
));
154 CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
155 m3
.GetMetadataReference().Second
.isEmpty());
157 m4
.EnsureMetadataReference(); // new!
158 beans::StringPair
m4id(m4
.GetMetadataReference());
159 CPPUNIT_ASSERT_MESSAGE("ensure on duplicate",
160 !m4id
.Second
.isEmpty() && !(m4id
== id1
));
162 MockMetadatable
mc1(*pRegClip
, true); // in clipboard
163 MockMetadatable
mc2(*pRegClip
, true);
164 MockMetadatable
mc3(*pRegClip
, true);
165 MockMetadatable
mc4(*pRegClip
, true);
166 MockMetadatable
m2p(*pReg
);
167 MockMetadatable
m3p(*pReg
);
169 mc1
.SetMetadataReference(id2
);
170 CPPUNIT_ASSERT_MESSAGE("set failed", bool(mc1
.GetMetadataReference() == id2
));
172 mc2
.SetMetadataReference(id2
);
173 CPPUNIT_FAIL("set duplicate succeeded");
174 } catch (const lang::IllegalArgumentException
&) { }
175 mc1
.SetMetadataReference(id2
);
176 CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
177 bool(mc1
.GetMetadataReference() == id2
));
178 mc1
.EnsureMetadataReference();
179 CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
180 bool(mc1
.GetMetadataReference() == id2
));
181 mc2
.EnsureMetadataReference();
182 beans::StringPair
mc2id(mc2
.GetMetadataReference());
183 CPPUNIT_ASSERT_MESSAGE("ensure failed", !mc2id
.Second
.isEmpty());
184 mc2
.EnsureMetadataReference();
185 CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
186 bool(mc2
.GetMetadataReference() == mc2id
));
187 mc2
.RemoveMetadataReference();
188 CPPUNIT_ASSERT_MESSAGE("remove failed",
189 mc2
.GetMetadataReference().Second
.isEmpty());
191 // set up mc2 as copy of m2 and mc3 as copy of m3
192 mc3
.RegisterAsCopyOf(m3
);
193 CPPUNIT_ASSERT_MESSAGE("copy to clipboard (latent)",
194 mc3
.GetMetadataReference().Second
.isEmpty() );
195 mc2
.RegisterAsCopyOf(m2
);
196 CPPUNIT_ASSERT_MESSAGE("copy to clipboard (non-latent)",
197 bool(mc2
.GetMetadataReference() == id1
));
198 // paste mc2 to m2p and mc3 to m3p
199 m2p
.RegisterAsCopyOf(mc2
);
200 CPPUNIT_ASSERT_MESSAGE("paste from clipboard (non-latent)",
201 m2p
.GetMetadataReference().Second
.isEmpty() );
202 m3p
.RegisterAsCopyOf(mc3
);
203 CPPUNIT_ASSERT_MESSAGE("paste from clipboard (latent)",
204 m3p
.GetMetadataReference().Second
.isEmpty() );
205 // delete m2, m2p, m3
206 m2
.RemoveMetadataReference();
207 CPPUNIT_ASSERT_MESSAGE("remove failed",
208 m2
.GetMetadataReference().Second
.isEmpty());
209 CPPUNIT_ASSERT_MESSAGE("paste-remove (non-latent)",
210 bool(m2p
.GetMetadataReference() == id1
));
211 m2p
.RemoveMetadataReference();
212 CPPUNIT_ASSERT_MESSAGE("remove failed",
213 m2p
.GetMetadataReference().Second
.isEmpty());
214 CPPUNIT_ASSERT_MESSAGE("paste-remove2 (non-latent)",
215 bool(m3
.GetMetadataReference() == id1
));
216 m3
.RemoveMetadataReference();
217 CPPUNIT_ASSERT_MESSAGE("remove failed",
218 m3
.GetMetadataReference().Second
.isEmpty());
219 CPPUNIT_ASSERT_MESSAGE("paste-remove (latent)",
220 bool(m3p
.GetMetadataReference() == id1
));
222 mc2
.SetMetadataReference(beans::StringPair());
223 CPPUNIT_ASSERT_MESSAGE("in clipboard becomes non-latent",
224 mc3
.GetMetadataReference().Second
.isEmpty() );
226 m2p
.RegisterAsCopyOf(mc2
);
227 CPPUNIT_ASSERT_MESSAGE("remove-paste",
228 m2p
.GetMetadataReference().Second
.isEmpty());
229 CPPUNIT_ASSERT_MESSAGE("remove-paste (stolen)",
230 bool(m3p
.GetMetadataReference() == id1
));
232 // auto-detect stream
233 m5
.SetMetadataReference(id3e
);
234 CPPUNIT_ASSERT_MESSAGE("auto-detect (content)",
235 bool(m5
.GetMetadataReference() == id3
));
236 m5
.m_bInContent
= false;
237 m5
.SetMetadataReference(id4e
);
238 CPPUNIT_ASSERT_MESSAGE("auto-detect (styles)",
239 bool(m5
.GetMetadataReference() == id4
));
243 CPPUNIT_TEST_SUITE_REGISTRATION(MetadatableTest
);
247 CPPUNIT_PLUGIN_IMPLEMENT();
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */