diff options
author | Michael Stahl <mstahl@redhat.com> | 2012-12-03 18:15:49 +0100 |
---|---|---|
committer | Michael Stahl <mstahl@redhat.com> | 2012-12-03 18:31:57 +0100 |
commit | affd2b5c4fba727c1b119bdcbdb71325c10ef954 (patch) | |
tree | 1f421361dac1147f7b9b1d1bb23bce3efc8705ef /sw/qa | |
parent | aa61177f1d339422acb3322c8851962cd1ca7466 (diff) |
SwXDocumentIndex: implement css.util.XRefreshable
... as has been promised since the OOo initial import in deprecation
notices in XDocumentIndex :-/
Change-Id: I5dd7e482e0e6d60dcad3de883d5815f729e6b80f
Diffstat (limited to 'sw/qa')
-rw-r--r-- | sw/qa/complex/writer/CheckIndex.java | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/sw/qa/complex/writer/CheckIndex.java b/sw/qa/complex/writer/CheckIndex.java new file mode 100644 index 000000000000..327944fed661 --- /dev/null +++ b/sw/qa/complex/writer/CheckIndex.java @@ -0,0 +1,167 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package complex.writer; + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; +import com.sun.star.lang.EventObject; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.beans.XPropertySet; +import com.sun.star.util.XCloseable; +import com.sun.star.util.XRefreshable; +import com.sun.star.util.XRefreshListener; +import com.sun.star.text.ControlCharacter; +import com.sun.star.text.XDocumentIndex; +import com.sun.star.text.XParagraphCursor; +import com.sun.star.text.XText; +import com.sun.star.text.XTextContent; +import com.sun.star.text.XTextDocument; +import com.sun.star.text.XTextRange; +import com.sun.star.text.XTextCursor; + +import org.openoffice.test.OfficeConnection; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +import java.util.Set; +import java.util.HashSet; + + +public class CheckIndex +{ + private static final OfficeConnection connection = new OfficeConnection(); + + @BeforeClass public static void setUpConnection() throws Exception { + connection.setUp(); + } + + @AfterClass public static void tearDownConnection() + throws InterruptedException, com.sun.star.uno.Exception + { + connection.tearDown(); + } + + private XMultiServiceFactory m_xMSF = null; + private XComponentContext m_xContext = null; + private XTextDocument m_xDoc = null; + + @Before public void before() throws Exception + { + m_xMSF = UnoRuntime.queryInterface( + XMultiServiceFactory.class, + connection.getComponentContext().getServiceManager()); + m_xContext = connection.getComponentContext(); + assertNotNull("could not get component context.", m_xContext); + m_xDoc = util.WriterTools.createTextDoc(m_xMSF); + } + + @After public void after() + { + util.DesktopTools.closeDoc(m_xDoc); + } + + class RefreshListener implements XRefreshListener + { + public boolean m_bDisposed = false; + public boolean m_bRefreshed = false; + public void disposing(EventObject event) + { + m_bDisposed = true; + } + public void refreshed(EventObject event) + { + m_bRefreshed = true; + } + public void assertRefreshed() + { + assertTrue(m_bRefreshed); + m_bRefreshed = false; + } + } + + @Test + public void test_refresh() throws Exception + { + XMultiServiceFactory xDocFactory = + UnoRuntime.queryInterface(XMultiServiceFactory.class, m_xDoc); + Object xIndex = + xDocFactory.createInstance("com.sun.star.text.ContentIndex"); + + XText xBodyText = m_xDoc.getText(); + XParagraphCursor xCursor = UnoRuntime.queryInterface( + XParagraphCursor.class, xBodyText.createTextCursor()); + XPropertySet xCursorSet = + UnoRuntime.queryInterface(XPropertySet.class, xCursor); + XTextContent xIndexContent = + UnoRuntime.queryInterface(XTextContent.class, xIndex); + XPropertySet xIndexSet = + UnoRuntime.queryInterface(XPropertySet.class, xIndex); + xIndexSet.setPropertyValue("CreateFromOutline", true); + xBodyText.insertTextContent(xCursor, xIndexContent, true); + + XRefreshable xRefreshable = + UnoRuntime.queryInterface(XRefreshable.class, xIndex); + + // test that refresh calls listener + RefreshListener listener = new RefreshListener(); + xRefreshable.addRefreshListener(listener); + assertFalse(listener.m_bRefreshed); + xRefreshable.refresh(); + listener.assertRefreshed(); + + // insert some heading + xCursor.gotoEnd(false); + xBodyText.insertControlCharacter(xCursor, + ControlCharacter.PARAGRAPH_BREAK, false); + xCursor.gotoEnd(false); + xCursor.setString("a heading"); + xCursor.gotoStartOfParagraph(true); + xCursorSet.setPropertyValue("ParaStyleName", "Heading 1"); + + xRefreshable.refresh(); + listener.assertRefreshed(); + // hope text is in last paragraph... + xCursor.gotoRange(xIndexContent.getAnchor().getEnd(), false); + xCursor.gotoStartOfParagraph(true); + String text = xCursor.getString(); + assertTrue(text.contains("a heading")); + + // insert some more headings + xCursor.gotoEnd(false); + xBodyText.insertControlCharacter(xCursor, + ControlCharacter.PARAGRAPH_BREAK, false); + xCursor.gotoEnd(false); + xCursor.setString("yet another heading"); + xCursor.gotoStartOfParagraph(true); + xCursorSet.setPropertyValue("ParaStyleName", "Heading 1"); + + // try again with update + XDocumentIndex xIndexIndex = + UnoRuntime.queryInterface(XDocumentIndex.class, xIndex); + xIndexIndex.update(); + listener.assertRefreshed(); + xCursor.gotoRange(xIndexContent.getAnchor().getEnd(), false); + xCursor.gotoStartOfParagraph(true); + text = xCursor.getString(); + assertTrue(text.contains("yet another heading")); + + // dispose must call the listener + assertFalse(listener.m_bDisposed); + xIndexIndex.dispose(); + assertTrue(listener.m_bDisposed); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |