bump product version to 7.2.5.1
[LibreOffice.git] / sfx2 / qa / cppunit / test_metadatable.cxx
blob459f635d0056959004b9a9b3780ef89794046abf
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 <com/sun/star/lang/IllegalArgumentException.hpp>
28 #include <sfx2/Metadatable.hxx>
29 #include <sfx2/XmlIdRegistry.hxx>
31 #include <memory>
34 using namespace ::com::sun::star;
37 namespace {
39 class MetadatableTest
40 : public ::CppUnit::TestFixture
42 public:
43 void test();
45 CPPUNIT_TEST_SUITE(MetadatableTest);
46 CPPUNIT_TEST(test);
47 CPPUNIT_TEST_SUITE_END();
49 private:
52 class MockMetadatable
53 : public ::sfx2::Metadatable
55 private:
56 ::sfx2::IXmlIdRegistry & m_rRegistry;
58 public:
59 MockMetadatable(::sfx2::IXmlIdRegistry & i_rReg,
60 bool const i_isInClip = false)
61 : m_rRegistry(i_rReg)
62 , m_bInClipboard(i_isInClip), m_bInUndo(false), m_bInContent(true) {}
63 bool m_bInClipboard;
64 bool m_bInUndo;
65 bool m_bInContent;
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);
85 OUString empty;
86 OUString content( "content.xml" );
87 OUString sid3( "id3" );
88 OUString sid4( "id4" );
89 beans::StringPair id1(content, "id1");
90 beans::StringPair id2(content, "id2");
91 beans::StringPair id3(content, sid3);
92 beans::StringPair id4("styles.xml", sid4);
93 beans::StringPair id3e(empty, sid3);
94 beans::StringPair id4e(empty, sid4);
95 m1.SetMetadataReference(id1);
96 CPPUNIT_ASSERT_MESSAGE("set failed", bool(m1.GetMetadataReference() == id1));
97 try {
98 m2.SetMetadataReference(id1);
99 CPPUNIT_ASSERT_MESSAGE("set duplicate succeeded", false);
100 } catch (const lang::IllegalArgumentException &) { }
101 m1.SetMetadataReference(id1);
102 CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
103 bool(m1.GetMetadataReference() == id1));
104 m1.EnsureMetadataReference();
105 CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
106 bool(m1.GetMetadataReference() == id1));
108 m2.EnsureMetadataReference();
109 beans::StringPair m2id(m2.GetMetadataReference());
110 CPPUNIT_ASSERT_MESSAGE("ensure failed", !m2id.Second.isEmpty());
111 m2.EnsureMetadataReference();
112 CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
113 bool(m2.GetMetadataReference() == m2id));
115 m1.m_bInUndo = true;
116 CPPUNIT_ASSERT_MESSAGE("move to undo failed",
117 m1.GetMetadataReference().Second.isEmpty());
119 m1.m_bInUndo = false;
120 CPPUNIT_ASSERT_MESSAGE("move from undo failed",
121 bool(m1.GetMetadataReference() == id1));
123 m1.m_bInUndo = true;
124 try {
125 m2.SetMetadataReference(id1); // steal!
126 } catch (lang::IllegalArgumentException &) {
127 CPPUNIT_FAIL("set duplicate to undo failed");
129 m1.m_bInUndo = false;
130 CPPUNIT_ASSERT_MESSAGE("move from undo: duplicate",
131 m1.GetMetadataReference().Second.isEmpty());
133 m3.RegisterAsCopyOf(m2);
134 CPPUNIT_ASSERT_MESSAGE("copy: source", bool(m2.GetMetadataReference() == id1));
135 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
136 m3.GetMetadataReference().Second.isEmpty());
137 m4.RegisterAsCopyOf(m3);
138 CPPUNIT_ASSERT_MESSAGE("copy: source", bool(m2.GetMetadataReference() == id1));
139 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
140 m3.GetMetadataReference().Second.isEmpty());
141 CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
142 m4.GetMetadataReference().Second.isEmpty());
143 m2.m_bInUndo = true;
144 CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
145 bool(m3.GetMetadataReference() == id1));
146 CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
147 m2.GetMetadataReference().Second.isEmpty());
148 m2.m_bInUndo = false;
149 CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
150 bool(m2.GetMetadataReference() == id1));
151 CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
152 m3.GetMetadataReference().Second.isEmpty());
154 m4.EnsureMetadataReference(); // new!
155 beans::StringPair m4id(m4.GetMetadataReference());
156 CPPUNIT_ASSERT_MESSAGE("ensure on duplicate",
157 !m4id.Second.isEmpty());
158 CPPUNIT_ASSERT_MESSAGE("ensure on duplicate",
159 !(m4id == id1));
161 MockMetadatable mc1(*pRegClip, true); // in clipboard
162 MockMetadatable mc2(*pRegClip, true);
163 MockMetadatable mc3(*pRegClip, true);
164 MockMetadatable mc4(*pRegClip, true);
165 MockMetadatable m2p(*pReg);
166 MockMetadatable m3p(*pReg);
168 mc1.SetMetadataReference(id2);
169 CPPUNIT_ASSERT_MESSAGE("set failed", bool(mc1.GetMetadataReference() == id2));
170 try {
171 mc2.SetMetadataReference(id2);
172 CPPUNIT_FAIL("set duplicate succeeded");
173 } catch (const lang::IllegalArgumentException &) { }
174 mc1.SetMetadataReference(id2);
175 CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
176 bool(mc1.GetMetadataReference() == id2));
177 mc1.EnsureMetadataReference();
178 CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
179 bool(mc1.GetMetadataReference() == id2));
180 mc2.EnsureMetadataReference();
181 beans::StringPair mc2id(mc2.GetMetadataReference());
182 CPPUNIT_ASSERT_MESSAGE("ensure failed", !mc2id.Second.isEmpty());
183 mc2.EnsureMetadataReference();
184 CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
185 bool(mc2.GetMetadataReference() == mc2id));
186 mc2.RemoveMetadataReference();
187 CPPUNIT_ASSERT_MESSAGE("remove failed",
188 mc2.GetMetadataReference().Second.isEmpty());
190 // set up mc2 as copy of m2 and mc3 as copy of m3
191 mc3.RegisterAsCopyOf(m3);
192 CPPUNIT_ASSERT_MESSAGE("copy to clipboard (latent)",
193 mc3.GetMetadataReference().Second.isEmpty() );
194 mc2.RegisterAsCopyOf(m2);
195 CPPUNIT_ASSERT_MESSAGE("copy to clipboard (non-latent)",
196 bool(mc2.GetMetadataReference() == id1));
197 // paste mc2 to m2p and mc3 to m3p
198 m2p.RegisterAsCopyOf(mc2);
199 CPPUNIT_ASSERT_MESSAGE("paste from clipboard (non-latent)",
200 m2p.GetMetadataReference().Second.isEmpty() );
201 m3p.RegisterAsCopyOf(mc3);
202 CPPUNIT_ASSERT_MESSAGE("paste from clipboard (latent)",
203 m3p.GetMetadataReference().Second.isEmpty() );
204 // delete m2, m2p, m3
205 m2.RemoveMetadataReference();
206 CPPUNIT_ASSERT_MESSAGE("remove failed",
207 m2.GetMetadataReference().Second.isEmpty());
208 CPPUNIT_ASSERT_MESSAGE("paste-remove (non-latent)",
209 bool(m2p.GetMetadataReference() == id1));
210 m2p.RemoveMetadataReference();
211 CPPUNIT_ASSERT_MESSAGE("remove failed",
212 m2p.GetMetadataReference().Second.isEmpty());
213 CPPUNIT_ASSERT_MESSAGE("paste-remove2 (non-latent)",
214 bool(m3.GetMetadataReference() == id1));
215 m3.RemoveMetadataReference();
216 CPPUNIT_ASSERT_MESSAGE("remove failed",
217 m3.GetMetadataReference().Second.isEmpty());
218 CPPUNIT_ASSERT_MESSAGE("paste-remove (latent)",
219 bool(m3p.GetMetadataReference() == id1));
220 // delete mc2
221 mc2.SetMetadataReference(beans::StringPair());
222 CPPUNIT_ASSERT_MESSAGE("in clipboard becomes non-latent",
223 mc3.GetMetadataReference().Second.isEmpty() );
224 // paste mc2
225 m2p.RegisterAsCopyOf(mc2);
226 CPPUNIT_ASSERT_MESSAGE("remove-paste",
227 m2p.GetMetadataReference().Second.isEmpty());
228 CPPUNIT_ASSERT_MESSAGE("remove-paste (stolen)",
229 bool(m3p.GetMetadataReference() == id1));
231 // auto-detect stream
232 m5.SetMetadataReference(id3e);
233 CPPUNIT_ASSERT_MESSAGE("auto-detect (content)",
234 bool(m5.GetMetadataReference() == id3));
235 m5.m_bInContent = false;
236 m5.SetMetadataReference(id4e);
237 CPPUNIT_ASSERT_MESSAGE("auto-detect (styles)",
238 bool(m5.GetMetadataReference() == id4));
242 CPPUNIT_TEST_SUITE_REGISTRATION(MetadatableTest);
246 CPPUNIT_PLUGIN_IMPLEMENT();
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */