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() SAL_OVERRIDE;
48 : virtual void tearDown() SAL_OVERRIDE;
49 :
50 : void test();
51 :
52 2 : CPPUNIT_TEST_SUITE(MetadatableTest);
53 1 : CPPUNIT_TEST(test);
54 5 : 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 SAL_OVERRIDE { return m_bInClipboard; }
83 36 : virtual bool IsInUndo() const SAL_OVERRIDE { return m_bInUndo; }
84 20 : virtual bool IsInContent() const SAL_OVERRIDE { return m_bInContent; }
85 17 : 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 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 : boost::scoped_ptr< ::sfx2::IXmlIdRegistry > const pReg(
98 1 : ::sfx2::createXmlIdRegistry(false) );
99 : boost::scoped_ptr< ::sfx2::IXmlIdRegistry > const pRegClip(
100 2 : ::sfx2::createXmlIdRegistry(true) );
101 :
102 2 : MockMetadatable m1(*pReg);
103 2 : MockMetadatable m2(*pReg);
104 2 : MockMetadatable m3(*pReg);
105 2 : MockMetadatable m4(*pReg);
106 2 : MockMetadatable m5(*pReg);
107 2 : OUString empty;
108 2 : OUString content( "content.xml" );
109 2 : OUString styles( "styles.xml" );
110 2 : OUString sid1( "id1" );
111 2 : OUString sid2( "id2" );
112 2 : OUString sid3( "id3" );
113 2 : OUString sid4( "id4" );
114 2 : beans::StringPair id1(content, sid1);
115 2 : beans::StringPair id2(content, sid2);
116 2 : beans::StringPair id3(content, sid3);
117 2 : beans::StringPair id4(styles, sid4);
118 2 : beans::StringPair id3e(empty, sid3);
119 2 : beans::StringPair id4e(empty, sid4);
120 1 : m1.SetMetadataReference(id1);
121 1 : CPPUNIT_ASSERT_MESSAGE("set failed", m1.GetMetadataReference() == id1);
122 : try {
123 1 : m2.SetMetadataReference(id1);
124 0 : CPPUNIT_ASSERT_MESSAGE("set duplicate succeeded", false);
125 1 : } catch (const lang::IllegalArgumentException &) { }
126 1 : m1.SetMetadataReference(id1);
127 2 : CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
128 1 : m1.GetMetadataReference() == id1);
129 1 : m1.EnsureMetadataReference();
130 2 : CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
131 1 : m1.GetMetadataReference() == id1);
132 :
133 1 : m2.EnsureMetadataReference();
134 2 : beans::StringPair m2id(m2.GetMetadataReference());
135 1 : CPPUNIT_ASSERT_MESSAGE("ensure failed", !m2id.Second.isEmpty());
136 1 : m2.EnsureMetadataReference();
137 2 : CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
138 1 : m2.GetMetadataReference() == m2id);
139 :
140 1 : m1.m_bInUndo = true;
141 2 : CPPUNIT_ASSERT_MESSAGE("move to undo failed",
142 1 : m1.GetMetadataReference().Second.isEmpty());
143 :
144 1 : m1.m_bInUndo = false;
145 2 : CPPUNIT_ASSERT_MESSAGE("move from undo failed",
146 1 : m1.GetMetadataReference() == id1);
147 :
148 1 : m1.m_bInUndo = true;
149 : try {
150 1 : m2.SetMetadataReference(id1); // steal!
151 0 : } catch (lang::IllegalArgumentException &) {
152 0 : CPPUNIT_FAIL("set duplicate to undo failed");
153 : }
154 1 : m1.m_bInUndo = false;
155 2 : CPPUNIT_ASSERT_MESSAGE("move from undo: duplicate",
156 1 : m1.GetMetadataReference().Second.isEmpty());
157 :
158 1 : m3.RegisterAsCopyOf(m2);
159 1 : CPPUNIT_ASSERT_MESSAGE("copy: source", m2.GetMetadataReference() == id1);
160 2 : CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
161 1 : m3.GetMetadataReference().Second.isEmpty());
162 1 : m4.RegisterAsCopyOf(m3);
163 1 : CPPUNIT_ASSERT_MESSAGE("copy: source", m2.GetMetadataReference() == id1);
164 2 : CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
165 1 : m3.GetMetadataReference().Second.isEmpty());
166 2 : CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
167 1 : m4.GetMetadataReference().Second.isEmpty());
168 1 : m2.m_bInUndo = true;
169 2 : CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
170 1 : m3.GetMetadataReference() == id1);
171 2 : CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
172 1 : m2.GetMetadataReference().Second.isEmpty());
173 1 : m2.m_bInUndo = false;
174 2 : CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
175 1 : m2.GetMetadataReference() == id1);
176 2 : CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
177 1 : m3.GetMetadataReference().Second.isEmpty());
178 :
179 1 : m4.EnsureMetadataReference(); // new!
180 2 : beans::StringPair m4id(m4.GetMetadataReference());
181 2 : CPPUNIT_ASSERT_MESSAGE("ensure on duplicate",
182 1 : !m4id.Second.isEmpty() && !(m4id == id1));
183 :
184 2 : MockMetadatable mc1(*pRegClip, true); // in clipboard
185 2 : MockMetadatable mc2(*pRegClip, true);
186 2 : MockMetadatable mc3(*pRegClip, true);
187 2 : MockMetadatable mc4(*pRegClip, true);
188 2 : MockMetadatable m2p(*pReg);
189 2 : MockMetadatable m3p(*pReg);
190 :
191 1 : mc1.SetMetadataReference(id2);
192 1 : CPPUNIT_ASSERT_MESSAGE("set failed", mc1.GetMetadataReference() == id2);
193 : try {
194 1 : mc2.SetMetadataReference(id2);
195 0 : CPPUNIT_FAIL("set duplicate succeeded");
196 1 : } catch (const lang::IllegalArgumentException &) { }
197 1 : mc1.SetMetadataReference(id2);
198 2 : CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
199 1 : mc1.GetMetadataReference() == id2);
200 1 : mc1.EnsureMetadataReference();
201 2 : CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
202 1 : mc1.GetMetadataReference() == id2);
203 1 : mc2.EnsureMetadataReference();
204 2 : beans::StringPair mc2id(mc2.GetMetadataReference());
205 1 : CPPUNIT_ASSERT_MESSAGE("ensure failed", !mc2id.Second.isEmpty());
206 1 : mc2.EnsureMetadataReference();
207 2 : CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
208 1 : mc2.GetMetadataReference() == mc2id);
209 1 : mc2.RemoveMetadataReference();
210 2 : CPPUNIT_ASSERT_MESSAGE("remove failed",
211 1 : mc2.GetMetadataReference().Second.isEmpty());
212 :
213 : // set up mc2 as copy of m2 and mc3 as copy of m3
214 1 : mc3.RegisterAsCopyOf(m3);
215 2 : CPPUNIT_ASSERT_MESSAGE("copy to clipboard (latent)",
216 1 : mc3.GetMetadataReference().Second.isEmpty() );
217 1 : mc2.RegisterAsCopyOf(m2);
218 2 : CPPUNIT_ASSERT_MESSAGE("copy to clipboard (non-latent)",
219 1 : mc2.GetMetadataReference() == id1);
220 : // paste mc2 to m2p and mc3 to m3p
221 1 : m2p.RegisterAsCopyOf(mc2);
222 2 : CPPUNIT_ASSERT_MESSAGE("paste from clipboard (non-latent)",
223 1 : m2p.GetMetadataReference().Second.isEmpty() );
224 1 : m3p.RegisterAsCopyOf(mc3);
225 2 : CPPUNIT_ASSERT_MESSAGE("paste from clipboard (latent)",
226 1 : m3p.GetMetadataReference().Second.isEmpty() );
227 : // delete m2, m2p, m3
228 1 : m2.RemoveMetadataReference();
229 2 : CPPUNIT_ASSERT_MESSAGE("remove failed",
230 1 : m2.GetMetadataReference().Second.isEmpty());
231 2 : CPPUNIT_ASSERT_MESSAGE("paste-remove (non-latent)",
232 1 : m2p.GetMetadataReference() == id1);
233 1 : m2p.RemoveMetadataReference();
234 2 : CPPUNIT_ASSERT_MESSAGE("remove failed",
235 1 : m2p.GetMetadataReference().Second.isEmpty());
236 2 : CPPUNIT_ASSERT_MESSAGE("paste-remove2 (non-latent)",
237 1 : m3.GetMetadataReference() == id1);
238 1 : m3.RemoveMetadataReference();
239 2 : CPPUNIT_ASSERT_MESSAGE("remove failed",
240 1 : m3.GetMetadataReference().Second.isEmpty());
241 2 : CPPUNIT_ASSERT_MESSAGE("paste-remove (latent)",
242 1 : m3p.GetMetadataReference() == id1);
243 : // delete mc2
244 1 : mc2.SetMetadataReference(beans::StringPair());
245 2 : CPPUNIT_ASSERT_MESSAGE("in clipboard becomes non-latent",
246 1 : mc3.GetMetadataReference().Second.isEmpty() );
247 : // paste mc2
248 1 : m2p.RegisterAsCopyOf(mc2);
249 2 : CPPUNIT_ASSERT_MESSAGE("remove-paste",
250 1 : m2p.GetMetadataReference().Second.isEmpty());
251 2 : CPPUNIT_ASSERT_MESSAGE("remove-paste (stolen)",
252 1 : m3p.GetMetadataReference() == id1);
253 :
254 : // auto-detect stream
255 1 : m5.SetMetadataReference(id3e);
256 2 : CPPUNIT_ASSERT_MESSAGE("auto-detect (content)",
257 1 : m5.GetMetadataReference() == id3);
258 1 : m5.m_bInContent = false;
259 1 : m5.SetMetadataReference(id4e);
260 2 : CPPUNIT_ASSERT_MESSAGE("auto-detect (styles)",
261 2 : m5.GetMetadataReference() == id4);
262 1 : }
263 :
264 :
265 1 : CPPUNIT_TEST_SUITE_REGISTRATION(MetadatableTest);
266 :
267 : }
268 :
269 4 : CPPUNIT_PLUGIN_IMPLEMENT();
270 :
271 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|