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