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