Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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"
25 :
26 : #include <rtl/ustrbuf.hxx>
27 :
28 : #include <com/sun/star/util/DateTime.hpp>
29 : #include <com/sun/star/util/Date.hpp>
30 : #include <com/sun/star/util/Duration.hpp>
31 :
32 : #include <sfx2/Metadatable.hxx>
33 : #include <sfx2/XmlIdRegistry.hxx>
34 :
35 : #include <boost/scoped_ptr.hpp>
36 :
37 :
38 : using namespace ::com::sun::star;
39 :
40 :
41 : namespace {
42 :
43 3 : class MetadatableTest
44 : : public ::CppUnit::TestFixture
45 : {
46 : public:
47 : virtual void setUp();
48 : virtual void tearDown();
49 :
50 : void test();
51 :
52 2 : CPPUNIT_TEST_SUITE(MetadatableTest);
53 1 : CPPUNIT_TEST(test);
54 2 : CPPUNIT_TEST_SUITE_END();
55 :
56 : private:
57 : };
58 :
59 1 : void MetadatableTest::setUp()
60 : {
61 1 : }
62 :
63 1 : void MetadatableTest::tearDown()
64 : {
65 1 : }
66 :
67 :
68 11 : class MockMetadatable
69 : : public ::sfx2::Metadatable
70 : {
71 : private:
72 : ::sfx2::IXmlIdRegistry & m_rRegistry;
73 :
74 : public:
75 11 : MockMetadatable(::sfx2::IXmlIdRegistry & i_rReg,
76 : bool const i_isInClip = false)
77 : : m_rRegistry(i_rReg)
78 11 : , m_bInClipboard(i_isInClip), m_bInUndo(false), m_bInContent(true) {}
79 : bool m_bInClipboard;
80 : bool m_bInUndo;
81 : bool m_bInContent;
82 38 : virtual bool IsInClipboard() const { return m_bInClipboard; }
83 36 : virtual bool IsInUndo() const { return m_bInUndo; }
84 20 : virtual bool IsInContent() const { return m_bInContent; }
85 17 : virtual ::sfx2::IXmlIdRegistry& GetRegistry() { return m_rRegistry; }
86 : virtual ::com::sun::star::uno::Reference<
87 0 : ::com::sun::star::rdf::XMetadatable > MakeUnoObject() { return 0; }
88 : };
89 :
90 21 : static bool operator==(beans::StringPair p1, beans::StringPair p2)
91 : {
92 21 : return p1.First == p2.First && p1.Second == p2.Second;
93 : }
94 :
95 1 : void MetadatableTest::test()
96 : {
97 : OSL_TRACE("SwMetadatable test(): start");
98 : boost::scoped_ptr< ::sfx2::IXmlIdRegistry > const pReg(
99 1 : ::sfx2::createXmlIdRegistry(false) );
100 : boost::scoped_ptr< ::sfx2::IXmlIdRegistry > const pRegClip(
101 1 : ::sfx2::createXmlIdRegistry(true) );
102 :
103 1 : MockMetadatable m1(*pReg);
104 1 : MockMetadatable m2(*pReg);
105 1 : MockMetadatable m3(*pReg);
106 1 : MockMetadatable m4(*pReg);
107 1 : MockMetadatable m5(*pReg);
108 1 : ::rtl::OUString empty;
109 1 : ::rtl::OUString content( "content.xml" );
110 1 : ::rtl::OUString styles( "styles.xml" );
111 1 : ::rtl::OUString sid1( "id1" );
112 1 : ::rtl::OUString sid2( "id2" );
113 1 : ::rtl::OUString sid3( "id3" );
114 1 : ::rtl::OUString sid4( "id4" );
115 1 : beans::StringPair id1(content, sid1);
116 1 : beans::StringPair id2(content, sid2);
117 1 : beans::StringPair id3(content, sid3);
118 1 : beans::StringPair id4(styles, sid4);
119 1 : beans::StringPair id3e(empty, sid3);
120 1 : beans::StringPair id4e(empty, sid4);
121 1 : m1.SetMetadataReference(id1);
122 1 : CPPUNIT_ASSERT_MESSAGE("set failed", m1.GetMetadataReference() == id1);
123 : try {
124 1 : m2.SetMetadataReference(id1);
125 0 : CPPUNIT_ASSERT_MESSAGE("set duplicate succeeded", false);
126 1 : } catch (const lang::IllegalArgumentException &) { }
127 1 : m1.SetMetadataReference(id1);
128 2 : CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
129 1 : m1.GetMetadataReference() == id1);
130 1 : m1.EnsureMetadataReference();
131 2 : CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
132 1 : m1.GetMetadataReference() == id1);
133 :
134 1 : m2.EnsureMetadataReference();
135 1 : beans::StringPair m2id(m2.GetMetadataReference());
136 1 : CPPUNIT_ASSERT_MESSAGE("ensure failed", !m2id.Second.isEmpty());
137 1 : m2.EnsureMetadataReference();
138 2 : CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
139 1 : m2.GetMetadataReference() == m2id);
140 :
141 1 : m1.m_bInUndo = true;
142 2 : CPPUNIT_ASSERT_MESSAGE("move to undo failed",
143 1 : m1.GetMetadataReference().Second.isEmpty());
144 :
145 1 : m1.m_bInUndo = false;
146 2 : CPPUNIT_ASSERT_MESSAGE("move from undo failed",
147 1 : m1.GetMetadataReference() == id1);
148 :
149 1 : m1.m_bInUndo = true;
150 : try {
151 1 : m2.SetMetadataReference(id1); // steal!
152 0 : } catch (lang::IllegalArgumentException &) {
153 0 : CPPUNIT_FAIL("set duplicate to undo failed");
154 : }
155 1 : m1.m_bInUndo = false;
156 2 : CPPUNIT_ASSERT_MESSAGE("move from undo: duplicate",
157 1 : m1.GetMetadataReference().Second.isEmpty());
158 :
159 1 : m3.RegisterAsCopyOf(m2);
160 1 : CPPUNIT_ASSERT_MESSAGE("copy: source", m2.GetMetadataReference() == id1);
161 2 : CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
162 1 : m3.GetMetadataReference().Second.isEmpty());
163 1 : m4.RegisterAsCopyOf(m3);
164 1 : CPPUNIT_ASSERT_MESSAGE("copy: source", m2.GetMetadataReference() == id1);
165 2 : CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
166 1 : m3.GetMetadataReference().Second.isEmpty());
167 2 : CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
168 1 : m4.GetMetadataReference().Second.isEmpty());
169 1 : m2.m_bInUndo = true;
170 2 : CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
171 1 : m3.GetMetadataReference() == id1);
172 2 : CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
173 1 : m2.GetMetadataReference().Second.isEmpty());
174 1 : m2.m_bInUndo = false;
175 2 : CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
176 1 : m2.GetMetadataReference() == id1);
177 2 : CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
178 1 : m3.GetMetadataReference().Second.isEmpty());
179 :
180 1 : m4.EnsureMetadataReference(); // new!
181 1 : beans::StringPair m4id(m4.GetMetadataReference());
182 2 : CPPUNIT_ASSERT_MESSAGE("ensure on duplicate",
183 1 : !m4id.Second.isEmpty() && !(m4id == id1));
184 :
185 1 : MockMetadatable mc1(*pRegClip, true); // in clipboard
186 1 : MockMetadatable mc2(*pRegClip, true);
187 1 : MockMetadatable mc3(*pRegClip, true);
188 1 : MockMetadatable mc4(*pRegClip, true);
189 1 : MockMetadatable m2p(*pReg);
190 1 : MockMetadatable m3p(*pReg);
191 :
192 1 : mc1.SetMetadataReference(id2);
193 1 : CPPUNIT_ASSERT_MESSAGE("set failed", mc1.GetMetadataReference() == id2);
194 : try {
195 1 : mc2.SetMetadataReference(id2);
196 0 : CPPUNIT_FAIL("set duplicate succeeded");
197 1 : } catch (const lang::IllegalArgumentException &) { }
198 1 : mc1.SetMetadataReference(id2);
199 2 : CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
200 1 : mc1.GetMetadataReference() == id2);
201 1 : mc1.EnsureMetadataReference();
202 2 : CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
203 1 : mc1.GetMetadataReference() == id2);
204 1 : mc2.EnsureMetadataReference();
205 1 : beans::StringPair mc2id(mc2.GetMetadataReference());
206 1 : CPPUNIT_ASSERT_MESSAGE("ensure failed", !mc2id.Second.isEmpty());
207 1 : mc2.EnsureMetadataReference();
208 2 : CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
209 1 : mc2.GetMetadataReference() == mc2id);
210 1 : mc2.RemoveMetadataReference();
211 2 : CPPUNIT_ASSERT_MESSAGE("remove failed",
212 1 : mc2.GetMetadataReference().Second.isEmpty());
213 :
214 : // set up mc2 as copy of m2 and mc3 as copy of m3
215 1 : mc3.RegisterAsCopyOf(m3);
216 2 : CPPUNIT_ASSERT_MESSAGE("copy to clipboard (latent)",
217 1 : mc3.GetMetadataReference().Second.isEmpty() );
218 1 : mc2.RegisterAsCopyOf(m2);
219 2 : CPPUNIT_ASSERT_MESSAGE("copy to clipboard (non-latent)",
220 1 : mc2.GetMetadataReference() == id1);
221 : // paste mc2 to m2p and mc3 to m3p
222 1 : m2p.RegisterAsCopyOf(mc2);
223 2 : CPPUNIT_ASSERT_MESSAGE("paste from clipboard (non-latent)",
224 1 : m2p.GetMetadataReference().Second.isEmpty() );
225 1 : m3p.RegisterAsCopyOf(mc3);
226 2 : CPPUNIT_ASSERT_MESSAGE("paste from clipboard (latent)",
227 1 : m3p.GetMetadataReference().Second.isEmpty() );
228 : // delete m2, m2p, m3
229 1 : m2.RemoveMetadataReference();
230 2 : CPPUNIT_ASSERT_MESSAGE("remove failed",
231 1 : m2.GetMetadataReference().Second.isEmpty());
232 2 : CPPUNIT_ASSERT_MESSAGE("paste-remove (non-latent)",
233 1 : m2p.GetMetadataReference() == id1);
234 1 : m2p.RemoveMetadataReference();
235 2 : CPPUNIT_ASSERT_MESSAGE("remove failed",
236 1 : m2p.GetMetadataReference().Second.isEmpty());
237 2 : CPPUNIT_ASSERT_MESSAGE("paste-remove2 (non-latent)",
238 1 : m3.GetMetadataReference() == id1);
239 1 : m3.RemoveMetadataReference();
240 2 : CPPUNIT_ASSERT_MESSAGE("remove failed",
241 1 : m3.GetMetadataReference().Second.isEmpty());
242 2 : CPPUNIT_ASSERT_MESSAGE("paste-remove (latent)",
243 1 : m3p.GetMetadataReference() == id1);
244 : // delete mc2
245 1 : mc2.SetMetadataReference(beans::StringPair());
246 2 : CPPUNIT_ASSERT_MESSAGE("in clipboard becomes non-latent",
247 1 : mc3.GetMetadataReference().Second.isEmpty() );
248 : // paste mc2
249 1 : m2p.RegisterAsCopyOf(mc2);
250 2 : CPPUNIT_ASSERT_MESSAGE("remove-paste",
251 1 : m2p.GetMetadataReference().Second.isEmpty());
252 2 : CPPUNIT_ASSERT_MESSAGE("remove-paste (stolen)",
253 1 : m3p.GetMetadataReference() == id1);
254 :
255 : // auto-detect stream
256 1 : m5.SetMetadataReference(id3e);
257 2 : CPPUNIT_ASSERT_MESSAGE("auto-detect (content)",
258 1 : m5.GetMetadataReference() == id3);
259 1 : m5.m_bInContent = false;
260 1 : m5.SetMetadataReference(id4e);
261 2 : CPPUNIT_ASSERT_MESSAGE("auto-detect (styles)",
262 1 : m5.GetMetadataReference() == id4);
263 :
264 1 : OSL_TRACE("sfx2::Metadatable test(): finished");
265 1 : }
266 :
267 :
268 1 : CPPUNIT_TEST_SUITE_REGISTRATION(MetadatableTest);
269 :
270 : }
271 :
272 4 : CPPUNIT_PLUGIN_IMPLEMENT();
273 :
274 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|