1*d119d52dSAndrew Rist /**************************************************************
2*d119d52dSAndrew Rist  *
3*d119d52dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d119d52dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d119d52dSAndrew Rist  * distributed with this work for additional information
6*d119d52dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d119d52dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d119d52dSAndrew Rist  * "License"); you may not use this file except in compliance
9*d119d52dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*d119d52dSAndrew Rist  *
11*d119d52dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*d119d52dSAndrew Rist  *
13*d119d52dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d119d52dSAndrew Rist  * software distributed under the License is distributed on an
15*d119d52dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d119d52dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*d119d52dSAndrew Rist  * specific language governing permissions and limitations
18*d119d52dSAndrew Rist  * under the License.
19*d119d52dSAndrew Rist  *
20*d119d52dSAndrew Rist  *************************************************************/
21*d119d52dSAndrew Rist 
22*d119d52dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <sal/cppunit.h>
25cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <com/sun/star/util/DateTime.hpp>
28cdf0e10cSrcweir #include <com/sun/star/util/Date.hpp>
29cdf0e10cSrcweir #include <com/sun/star/util/Duration.hpp>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <sfx2/Metadatable.hxx>
32cdf0e10cSrcweir #include <sfx2/XmlIdRegistry.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace ::com::sun::star;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 
38cdf0e10cSrcweir namespace {
39cdf0e10cSrcweir 
40cdf0e10cSrcweir class MetadatableTest
41cdf0e10cSrcweir     : public ::CppUnit::TestFixture
42cdf0e10cSrcweir {
43cdf0e10cSrcweir public:
44cdf0e10cSrcweir     virtual void setUp();
45cdf0e10cSrcweir     virtual void tearDown();
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     void test();
48cdf0e10cSrcweir 
49cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(MetadatableTest);
50cdf0e10cSrcweir     CPPUNIT_TEST(test);
51cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
52cdf0e10cSrcweir 
53cdf0e10cSrcweir private:
54cdf0e10cSrcweir };
55cdf0e10cSrcweir 
setUp()56cdf0e10cSrcweir void MetadatableTest::setUp()
57cdf0e10cSrcweir {
58cdf0e10cSrcweir }
59cdf0e10cSrcweir 
tearDown()60cdf0e10cSrcweir void MetadatableTest::tearDown()
61cdf0e10cSrcweir {
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 
65cdf0e10cSrcweir class MockMetadatable
66cdf0e10cSrcweir     : public ::sfx2::Metadatable
67cdf0e10cSrcweir {
68cdf0e10cSrcweir private:
69cdf0e10cSrcweir     ::sfx2::IXmlIdRegistry & m_rRegistry;
70cdf0e10cSrcweir 
71cdf0e10cSrcweir public:
MockMetadatable(::sfx2::IXmlIdRegistry & i_rReg,bool const i_isInClip=false)72cdf0e10cSrcweir     MockMetadatable(::sfx2::IXmlIdRegistry & i_rReg,
73cdf0e10cSrcweir             bool const i_isInClip = false)
74cdf0e10cSrcweir         : m_rRegistry(i_rReg)
75cdf0e10cSrcweir         , m_bInClipboard(i_isInClip), m_bInUndo(false), m_bInContent(true) {}
76cdf0e10cSrcweir     bool m_bInClipboard;
77cdf0e10cSrcweir     bool m_bInUndo;
78cdf0e10cSrcweir     bool m_bInContent;
IsInClipboard() const79cdf0e10cSrcweir     virtual bool IsInClipboard() const { return m_bInClipboard; }
IsInUndo() const80cdf0e10cSrcweir     virtual bool IsInUndo() const { return m_bInUndo; }
IsInContent() const81cdf0e10cSrcweir     virtual bool IsInContent() const { return m_bInContent; }
GetRegistry()82cdf0e10cSrcweir     virtual ::sfx2::IXmlIdRegistry& GetRegistry() { return m_rRegistry; }
83cdf0e10cSrcweir     virtual ::com::sun::star::uno::Reference<
MakeUnoObject()84cdf0e10cSrcweir         ::com::sun::star::rdf::XMetadatable > MakeUnoObject() { return 0; }
85cdf0e10cSrcweir };
86cdf0e10cSrcweir 
operator ==(beans::StringPair p1,beans::StringPair p2)87cdf0e10cSrcweir static bool operator==(beans::StringPair p1, beans::StringPair p2)
88cdf0e10cSrcweir {
89cdf0e10cSrcweir     return p1.First == p2.First && p1.Second == p2.Second;
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
test()92cdf0e10cSrcweir void MetadatableTest::test()
93cdf0e10cSrcweir {
94cdf0e10cSrcweir     OSL_TRACE("SwMetadatable test(): start\n");
95cdf0e10cSrcweir     ::std::auto_ptr< ::sfx2::IXmlIdRegistry > const pReg(
96cdf0e10cSrcweir         ::sfx2::createXmlIdRegistry(false) );
97cdf0e10cSrcweir     ::std::auto_ptr< ::sfx2::IXmlIdRegistry > const pRegClip(
98cdf0e10cSrcweir         ::sfx2::createXmlIdRegistry(true) );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     MockMetadatable m1(*pReg);
101cdf0e10cSrcweir     MockMetadatable m2(*pReg);
102cdf0e10cSrcweir     MockMetadatable m3(*pReg);
103cdf0e10cSrcweir     MockMetadatable m4(*pReg);
104cdf0e10cSrcweir     MockMetadatable m5(*pReg);
105cdf0e10cSrcweir     ::rtl::OUString empty;
106cdf0e10cSrcweir     ::rtl::OUString content( ::rtl::OUString::createFromAscii("content.xml") );
107cdf0e10cSrcweir     ::rtl::OUString styles ( ::rtl::OUString::createFromAscii("styles.xml") );
108cdf0e10cSrcweir     ::rtl::OUString sid1( ::rtl::OUString::createFromAscii("id1") );
109cdf0e10cSrcweir     ::rtl::OUString sid2( ::rtl::OUString::createFromAscii("id2") );
110cdf0e10cSrcweir     ::rtl::OUString sid3( ::rtl::OUString::createFromAscii("id3") );
111cdf0e10cSrcweir     ::rtl::OUString sid4( ::rtl::OUString::createFromAscii("id4") );
112cdf0e10cSrcweir     beans::StringPair id1(content, sid1);
113cdf0e10cSrcweir     beans::StringPair id2(content, sid2);
114cdf0e10cSrcweir     beans::StringPair id3(content, sid3);
115cdf0e10cSrcweir     beans::StringPair id4(styles,  sid4);
116cdf0e10cSrcweir     beans::StringPair id3e(empty,  sid3);
117cdf0e10cSrcweir     beans::StringPair id4e(empty,  sid4);
118cdf0e10cSrcweir     m1.SetMetadataReference(id1);
119cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("set failed", m1.GetMetadataReference() == id1);
120cdf0e10cSrcweir     try {
121cdf0e10cSrcweir         m2.SetMetadataReference(id1);
122cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("set duplicate succeeded", false);
123cdf0e10cSrcweir     } catch (lang::IllegalArgumentException) { }
124cdf0e10cSrcweir     m1.SetMetadataReference(id1);
125cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
126cdf0e10cSrcweir             m1.GetMetadataReference() == id1);
127cdf0e10cSrcweir     m1.EnsureMetadataReference();
128cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
129cdf0e10cSrcweir             m1.GetMetadataReference() == id1);
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     m2.EnsureMetadataReference();
132cdf0e10cSrcweir     beans::StringPair m2id(m2.GetMetadataReference());
133cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("ensure failed", m2id.Second.getLength());
134cdf0e10cSrcweir     m2.EnsureMetadataReference();
135cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
136cdf0e10cSrcweir             m2.GetMetadataReference() == m2id);
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     m1.m_bInUndo = true;
139cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("move to undo failed",
140cdf0e10cSrcweir             !m1.GetMetadataReference().Second.getLength());
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     m1.m_bInUndo = false;
143cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("move from undo failed",
144cdf0e10cSrcweir             m1.GetMetadataReference() == id1);
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     m1.m_bInUndo = true;
147cdf0e10cSrcweir     try {
148cdf0e10cSrcweir         m2.SetMetadataReference(id1); // steal!
149cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
150cdf0e10cSrcweir         CPPUNIT_FAIL("set duplicate to undo failed");
151cdf0e10cSrcweir     }
152cdf0e10cSrcweir     m1.m_bInUndo = false;
153cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("move from undo: duplicate",
154cdf0e10cSrcweir             !m1.GetMetadataReference().Second.getLength());
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     m3.RegisterAsCopyOf(m2);
157cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("copy: source", m2.GetMetadataReference() == id1);
158cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
159cdf0e10cSrcweir             !m3.GetMetadataReference().Second.getLength());
160cdf0e10cSrcweir     m4.RegisterAsCopyOf(m3);
161cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("copy: source", m2.GetMetadataReference() == id1);
162cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
163cdf0e10cSrcweir             !m3.GetMetadataReference().Second.getLength());
164cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
165cdf0e10cSrcweir             !m4.GetMetadataReference().Second.getLength());
166cdf0e10cSrcweir     m2.m_bInUndo = true;
167cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
168cdf0e10cSrcweir             m3.GetMetadataReference() == id1);
169cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
170cdf0e10cSrcweir             !m2.GetMetadataReference().Second.getLength());
171cdf0e10cSrcweir     m2.m_bInUndo = false;
172cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
173cdf0e10cSrcweir             m2.GetMetadataReference() == id1);
174cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
175cdf0e10cSrcweir             !m3.GetMetadataReference().Second.getLength());
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     m4.EnsureMetadataReference(); // new!
178cdf0e10cSrcweir     beans::StringPair m4id(m4.GetMetadataReference());
179cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("ensure on duplicate",
180cdf0e10cSrcweir             m4id.Second.getLength() && !(m4id == id1));
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     MockMetadatable mc1(*pRegClip, true); // in clipboard
183cdf0e10cSrcweir     MockMetadatable mc2(*pRegClip, true);
184cdf0e10cSrcweir     MockMetadatable mc3(*pRegClip, true);
185cdf0e10cSrcweir     MockMetadatable mc4(*pRegClip, true);
186cdf0e10cSrcweir     MockMetadatable m2p(*pReg);
187cdf0e10cSrcweir     MockMetadatable m3p(*pReg);
188cdf0e10cSrcweir 
189cdf0e10cSrcweir     mc1.SetMetadataReference(id2);
190cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("set failed", mc1.GetMetadataReference() == id2);
191cdf0e10cSrcweir     try {
192cdf0e10cSrcweir         mc2.SetMetadataReference(id2);
193cdf0e10cSrcweir         CPPUNIT_FAIL("set duplicate succeeded");
194cdf0e10cSrcweir     } catch (lang::IllegalArgumentException) { }
195cdf0e10cSrcweir     mc1.SetMetadataReference(id2);
196cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
197cdf0e10cSrcweir             mc1.GetMetadataReference() == id2);
198cdf0e10cSrcweir     mc1.EnsureMetadataReference();
199cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
200cdf0e10cSrcweir             mc1.GetMetadataReference() == id2);
201cdf0e10cSrcweir     mc2.EnsureMetadataReference();
202cdf0e10cSrcweir     beans::StringPair mc2id(mc2.GetMetadataReference());
203cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("ensure failed", mc2id.Second.getLength());
204cdf0e10cSrcweir     mc2.EnsureMetadataReference();
205cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
206cdf0e10cSrcweir             mc2.GetMetadataReference() == mc2id);
207cdf0e10cSrcweir     mc2.RemoveMetadataReference();
208cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("remove failed",
209cdf0e10cSrcweir             !mc2.GetMetadataReference().Second.getLength());
210cdf0e10cSrcweir 
211cdf0e10cSrcweir     // set up mc2 as copy of m2 and mc3 as copy of m3
212cdf0e10cSrcweir     mc3.RegisterAsCopyOf(m3);
213cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("copy to clipboard (latent)",
214cdf0e10cSrcweir             !mc3.GetMetadataReference().Second.getLength() );
215cdf0e10cSrcweir     mc2.RegisterAsCopyOf(m2);
216cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("copy to clipboard (non-latent)",
217cdf0e10cSrcweir             mc2.GetMetadataReference() == id1);
218cdf0e10cSrcweir     // paste mc2 to m2p and mc3 to m3p
219cdf0e10cSrcweir     m2p.RegisterAsCopyOf(mc2);
220cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("paste from clipboard (non-latent)",
221cdf0e10cSrcweir             !m2p.GetMetadataReference().Second.getLength() );
222cdf0e10cSrcweir     m3p.RegisterAsCopyOf(mc3);
223cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("paste from clipboard (latent)",
224cdf0e10cSrcweir             !m3p.GetMetadataReference().Second.getLength() );
225cdf0e10cSrcweir     // delete m2, m2p, m3
226cdf0e10cSrcweir     m2.RemoveMetadataReference();
227cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("remove failed",
228cdf0e10cSrcweir             !m2.GetMetadataReference().Second.getLength());
229cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("paste-remove (non-latent)",
230cdf0e10cSrcweir             m2p.GetMetadataReference() == id1);
231cdf0e10cSrcweir     m2p.RemoveMetadataReference();
232cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("remove failed",
233cdf0e10cSrcweir             !m2p.GetMetadataReference().Second.getLength());
234cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("paste-remove2 (non-latent)",
235cdf0e10cSrcweir             m3.GetMetadataReference() == id1);
236cdf0e10cSrcweir     m3.RemoveMetadataReference();
237cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("remove failed",
238cdf0e10cSrcweir             !m3.GetMetadataReference().Second.getLength());
239cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("paste-remove (latent)",
240cdf0e10cSrcweir             m3p.GetMetadataReference() == id1);
241cdf0e10cSrcweir     // delete mc2
242cdf0e10cSrcweir     mc2.SetMetadataReference(beans::StringPair());
243cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("in clipboard becomes non-latent",
244cdf0e10cSrcweir             !mc3.GetMetadataReference().Second.getLength() );
245cdf0e10cSrcweir     // paste mc2
246cdf0e10cSrcweir     m2p.RegisterAsCopyOf(mc2);
247cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("remove-paste",
248cdf0e10cSrcweir             !m2p.GetMetadataReference().Second.getLength());
249cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("remove-paste (stolen)",
250cdf0e10cSrcweir             m3p.GetMetadataReference() == id1);
251cdf0e10cSrcweir 
252cdf0e10cSrcweir     // auto-detect stream
253cdf0e10cSrcweir     m5.SetMetadataReference(id3e);
254cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("auto-detect (content)",
255cdf0e10cSrcweir             m5.GetMetadataReference() == id3);
256cdf0e10cSrcweir     m5.m_bInContent = false;
257cdf0e10cSrcweir     m5.SetMetadataReference(id4e);
258cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("auto-detect (styles)",
259cdf0e10cSrcweir             m5.GetMetadataReference() == id4);
260cdf0e10cSrcweir 
261cdf0e10cSrcweir     OSL_TRACE("sfx2::Metadatable test(): finished\n");
262cdf0e10cSrcweir }
263cdf0e10cSrcweir 
264cdf0e10cSrcweir 
265cdf0e10cSrcweir CPPUNIT_TEST_SUITE_REGISTRATION(MetadatableTest);
266cdf0e10cSrcweir 
267cdf0e10cSrcweir }
268cdf0e10cSrcweir 
269cdf0e10cSrcweir CPPUNIT_PLUGIN_IMPLEMENT();
270cdf0e10cSrcweir 
271