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