bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sfx2 / qa / cppunit / test_metadatable.cxx
blob5073f87202b780ff078097710406bcaa6b14fbe6
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>
27 #include <com/sun/star/lang/IllegalArgumentException.hpp>
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 <memory>
38 using namespace ::com::sun::star;
41 namespace {
43 class MetadatableTest
44 : public ::CppUnit::TestFixture
46 public:
47 void test();
49 CPPUNIT_TEST_SUITE(MetadatableTest);
50 CPPUNIT_TEST(test);
51 CPPUNIT_TEST_SUITE_END();
53 private:
56 class MockMetadatable
57 : public ::sfx2::Metadatable
59 private:
60 ::sfx2::IXmlIdRegistry & m_rRegistry;
62 public:
63 MockMetadatable(::sfx2::IXmlIdRegistry & i_rReg,
64 bool const i_isInClip = false)
65 : m_rRegistry(i_rReg)
66 , m_bInClipboard(i_isInClip), m_bInUndo(false), m_bInContent(true) {}
67 bool const m_bInClipboard;
68 bool m_bInUndo;
69 bool m_bInContent;
70 virtual bool IsInClipboard() const override { return m_bInClipboard; }
71 virtual bool IsInUndo() const override { return m_bInUndo; }
72 virtual bool IsInContent() const override { return m_bInContent; }
73 virtual ::sfx2::IXmlIdRegistry& GetRegistry() override { return m_rRegistry; }
74 virtual css::uno::Reference< css::rdf::XMetadatable > MakeUnoObject() override { return nullptr; }
77 void MetadatableTest::test()
79 std::unique_ptr< ::sfx2::IXmlIdRegistry > const pReg(
80 ::sfx2::createXmlIdRegistry(false) );
81 std::unique_ptr< ::sfx2::IXmlIdRegistry > const pRegClip(
82 ::sfx2::createXmlIdRegistry(true) );
84 MockMetadatable m1(*pReg);
85 MockMetadatable m2(*pReg);
86 MockMetadatable m3(*pReg);
87 MockMetadatable m4(*pReg);
88 MockMetadatable m5(*pReg);
89 OUString empty;
90 OUString content( "content.xml" );
91 OUString const styles( "styles.xml" );
92 OUString const sid1( "id1" );
93 OUString const sid2( "id2" );
94 OUString sid3( "id3" );
95 OUString sid4( "id4" );
96 beans::StringPair id1(content, sid1);
97 beans::StringPair id2(content, sid2);
98 beans::StringPair id3(content, sid3);
99 beans::StringPair id4(styles, sid4);
100 beans::StringPair id3e(empty, sid3);
101 beans::StringPair id4e(empty, sid4);
102 m1.SetMetadataReference(id1);
103 CPPUNIT_ASSERT_MESSAGE("set failed", bool(m1.GetMetadataReference() == id1));
104 try {
105 m2.SetMetadataReference(id1);
106 CPPUNIT_ASSERT_MESSAGE("set duplicate succeeded", false);
107 } catch (const lang::IllegalArgumentException &) { }
108 m1.SetMetadataReference(id1);
109 CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
110 bool(m1.GetMetadataReference() == id1));
111 m1.EnsureMetadataReference();
112 CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
113 bool(m1.GetMetadataReference() == id1));
115 m2.EnsureMetadataReference();
116 beans::StringPair m2id(m2.GetMetadataReference());
117 CPPUNIT_ASSERT_MESSAGE("ensure failed", !m2id.Second.isEmpty());
118 m2.EnsureMetadataReference();
119 CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
120 bool(m2.GetMetadataReference() == m2id));
122 m1.m_bInUndo = true;
123 CPPUNIT_ASSERT_MESSAGE("move to undo failed",
124 m1.GetMetadataReference().Second.isEmpty());
126 m1.m_bInUndo = false;
127 CPPUNIT_ASSERT_MESSAGE("move from undo failed",
128 bool(m1.GetMetadataReference() == id1));
130 m1.m_bInUndo = true;
131 try {
132 m2.SetMetadataReference(id1); // steal!
133 } catch (lang::IllegalArgumentException &) {
134 CPPUNIT_FAIL("set duplicate to undo failed");
136 m1.m_bInUndo = false;
137 CPPUNIT_ASSERT_MESSAGE("move from undo: duplicate",
138 m1.GetMetadataReference().Second.isEmpty());
140 m3.RegisterAsCopyOf(m2);
141 CPPUNIT_ASSERT_MESSAGE("copy: source", bool(m2.GetMetadataReference() == id1));
142 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
143 m3.GetMetadataReference().Second.isEmpty());
144 m4.RegisterAsCopyOf(m3);
145 CPPUNIT_ASSERT_MESSAGE("copy: source", bool(m2.GetMetadataReference() == id1));
146 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
147 m3.GetMetadataReference().Second.isEmpty());
148 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
149 m4.GetMetadataReference().Second.isEmpty());
150 m2.m_bInUndo = true;
151 CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
152 bool(m3.GetMetadataReference() == id1));
153 CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
154 m2.GetMetadataReference().Second.isEmpty());
155 m2.m_bInUndo = false;
156 CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
157 bool(m2.GetMetadataReference() == id1));
158 CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
159 m3.GetMetadataReference().Second.isEmpty());
161 m4.EnsureMetadataReference(); // new!
162 beans::StringPair m4id(m4.GetMetadataReference());
163 CPPUNIT_ASSERT_MESSAGE("ensure on duplicate",
164 !m4id.Second.isEmpty() && !(m4id == id1));
166 MockMetadatable mc1(*pRegClip, true); // in clipboard
167 MockMetadatable mc2(*pRegClip, true);
168 MockMetadatable mc3(*pRegClip, true);
169 MockMetadatable mc4(*pRegClip, true);
170 MockMetadatable m2p(*pReg);
171 MockMetadatable m3p(*pReg);
173 mc1.SetMetadataReference(id2);
174 CPPUNIT_ASSERT_MESSAGE("set failed", bool(mc1.GetMetadataReference() == id2));
175 try {
176 mc2.SetMetadataReference(id2);
177 CPPUNIT_FAIL("set duplicate succeeded");
178 } catch (const lang::IllegalArgumentException &) { }
179 mc1.SetMetadataReference(id2);
180 CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
181 bool(mc1.GetMetadataReference() == id2));
182 mc1.EnsureMetadataReference();
183 CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
184 bool(mc1.GetMetadataReference() == id2));
185 mc2.EnsureMetadataReference();
186 beans::StringPair mc2id(mc2.GetMetadataReference());
187 CPPUNIT_ASSERT_MESSAGE("ensure failed", !mc2id.Second.isEmpty());
188 mc2.EnsureMetadataReference();
189 CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
190 bool(mc2.GetMetadataReference() == mc2id));
191 mc2.RemoveMetadataReference();
192 CPPUNIT_ASSERT_MESSAGE("remove failed",
193 mc2.GetMetadataReference().Second.isEmpty());
195 // set up mc2 as copy of m2 and mc3 as copy of m3
196 mc3.RegisterAsCopyOf(m3);
197 CPPUNIT_ASSERT_MESSAGE("copy to clipboard (latent)",
198 mc3.GetMetadataReference().Second.isEmpty() );
199 mc2.RegisterAsCopyOf(m2);
200 CPPUNIT_ASSERT_MESSAGE("copy to clipboard (non-latent)",
201 bool(mc2.GetMetadataReference() == id1));
202 // paste mc2 to m2p and mc3 to m3p
203 m2p.RegisterAsCopyOf(mc2);
204 CPPUNIT_ASSERT_MESSAGE("paste from clipboard (non-latent)",
205 m2p.GetMetadataReference().Second.isEmpty() );
206 m3p.RegisterAsCopyOf(mc3);
207 CPPUNIT_ASSERT_MESSAGE("paste from clipboard (latent)",
208 m3p.GetMetadataReference().Second.isEmpty() );
209 // delete m2, m2p, m3
210 m2.RemoveMetadataReference();
211 CPPUNIT_ASSERT_MESSAGE("remove failed",
212 m2.GetMetadataReference().Second.isEmpty());
213 CPPUNIT_ASSERT_MESSAGE("paste-remove (non-latent)",
214 bool(m2p.GetMetadataReference() == id1));
215 m2p.RemoveMetadataReference();
216 CPPUNIT_ASSERT_MESSAGE("remove failed",
217 m2p.GetMetadataReference().Second.isEmpty());
218 CPPUNIT_ASSERT_MESSAGE("paste-remove2 (non-latent)",
219 bool(m3.GetMetadataReference() == id1));
220 m3.RemoveMetadataReference();
221 CPPUNIT_ASSERT_MESSAGE("remove failed",
222 m3.GetMetadataReference().Second.isEmpty());
223 CPPUNIT_ASSERT_MESSAGE("paste-remove (latent)",
224 bool(m3p.GetMetadataReference() == id1));
225 // delete mc2
226 mc2.SetMetadataReference(beans::StringPair());
227 CPPUNIT_ASSERT_MESSAGE("in clipboard becomes non-latent",
228 mc3.GetMetadataReference().Second.isEmpty() );
229 // paste mc2
230 m2p.RegisterAsCopyOf(mc2);
231 CPPUNIT_ASSERT_MESSAGE("remove-paste",
232 m2p.GetMetadataReference().Second.isEmpty());
233 CPPUNIT_ASSERT_MESSAGE("remove-paste (stolen)",
234 bool(m3p.GetMetadataReference() == id1));
236 // auto-detect stream
237 m5.SetMetadataReference(id3e);
238 CPPUNIT_ASSERT_MESSAGE("auto-detect (content)",
239 bool(m5.GetMetadataReference() == id3));
240 m5.m_bInContent = false;
241 m5.SetMetadataReference(id4e);
242 CPPUNIT_ASSERT_MESSAGE("auto-detect (styles)",
243 bool(m5.GetMetadataReference() == id4));
247 CPPUNIT_TEST_SUITE_REGISTRATION(MetadatableTest);
251 CPPUNIT_PLUGIN_IMPLEMENT();
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */