summaryrefslogtreecommitdiffstats
path: root/unotest/source/java/org
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
commited5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch)
tree7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /unotest/source/java/org
parentInitial commit. (diff)
downloadlibreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.tar.xz
libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.zip
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'unotest/source/java/org')
-rw-r--r--unotest/source/java/org/openoffice/test/Argument.java27
-rw-r--r--unotest/source/java/org/openoffice/test/FileHelper.java46
-rw-r--r--unotest/source/java/org/openoffice/test/OfficeConnection.java285
-rw-r--r--unotest/source/java/org/openoffice/test/OfficeFileUrl.java33
-rw-r--r--unotest/source/java/org/openoffice/test/UnoApiTest.java47
-rw-r--r--unotest/source/java/org/openoffice/test/tools/DocumentType.java37
-rw-r--r--unotest/source/java/org/openoffice/test/tools/OfficeDocument.java198
-rw-r--r--unotest/source/java/org/openoffice/test/tools/OfficeDocumentView.java104
-rw-r--r--unotest/source/java/org/openoffice/test/tools/SpreadsheetDocument.java61
-rw-r--r--unotest/source/java/org/openoffice/test/tools/SpreadsheetView.java32
10 files changed, 870 insertions, 0 deletions
diff --git a/unotest/source/java/org/openoffice/test/Argument.java b/unotest/source/java/org/openoffice/test/Argument.java
new file mode 100644
index 000000000..294e14489
--- /dev/null
+++ b/unotest/source/java/org/openoffice/test/Argument.java
@@ -0,0 +1,27 @@
+/*
+ * 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/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+package org.openoffice.test;
+
+public final class Argument {
+ public static String get(String name) {
+ return System.getProperty("org.openoffice.test.arg." + name);
+ }
+
+ private Argument() {}
+}
diff --git a/unotest/source/java/org/openoffice/test/FileHelper.java b/unotest/source/java/org/openoffice/test/FileHelper.java
new file mode 100644
index 000000000..93907ed92
--- /dev/null
+++ b/unotest/source/java/org/openoffice/test/FileHelper.java
@@ -0,0 +1,46 @@
+/*
+ * 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/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+package org.openoffice.test;
+
+/*
+ * Helper Functions for File handling
+ */
+public class FileHelper
+{
+ /*
+ * Concat a _sRelativePathToAdd to a _sPath and append a '/' to the _sPath only if need.
+ *
+ * @return a right concatenated path
+ */
+ public static String appendPath(String _sPath, String _sRelativePathToAdd)
+ {
+ String sNewPath = _sPath;
+ String fs = System.getProperty("file.separator");
+ if (_sPath.startsWith("file:"))
+ {
+ fs = "/"; // we use a file URL so only '/' is allowed.
+ }
+ if (! (sNewPath.endsWith("/") || sNewPath.endsWith("\\") ) )
+ {
+ sNewPath += fs;
+ }
+ sNewPath += _sRelativePathToAdd;
+ return sNewPath;
+ }
+}
diff --git a/unotest/source/java/org/openoffice/test/OfficeConnection.java b/unotest/source/java/org/openoffice/test/OfficeConnection.java
new file mode 100644
index 000000000..0801da6e0
--- /dev/null
+++ b/unotest/source/java/org/openoffice/test/OfficeConnection.java
@@ -0,0 +1,285 @@
+/*
+ * 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/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+package org.openoffice.test;
+
+import com.sun.star.bridge.UnoUrlResolver;
+import com.sun.star.bridge.XUnoUrlResolver;
+import com.sun.star.comp.helper.Bootstrap;
+import com.sun.star.connection.NoConnectException;
+import com.sun.star.frame.XDesktop;
+import com.sun.star.lang.DisposedException;
+import com.sun.star.lang.XMultiComponentFactory;
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.uno.XComponentContext;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintStream;
+import java.util.Map;
+import java.util.UUID;
+import static org.junit.Assert.*;
+
+/** Start up and shut down an OOo instance.
+
+ Details about the OOo instance are tunneled in via
+ org.openoffice.test.arg... system properties.
+*/
+
+
+public final class OfficeConnection {
+ /** Start up an OOo instance.
+ */
+ public void setUp() throws Exception {
+ String sofficeArg = Argument.get("soffice");
+ if (sofficeArg.startsWith("path:")) {
+ description = "pipe,name=oootest" + UUID.randomUUID();
+ ProcessBuilder pb = new ProcessBuilder(
+ sofficeArg.substring("path:".length()), "--quickstart=no",
+ "--norestore", "--nologo", "--headless",
+ "--accept=" + description + ";urp",
+ "-env:UserInstallation=" + Argument.get("user"),
+ "-env:UNO_JAVA_JFW_ENV_JREHOME=true");
+ String workdirArg = Argument.get("workdir");
+ if (workdirArg != null) {
+ pb.directory(new File(workdirArg));
+ }
+ String envArg = Argument.get("env");
+ if (envArg != null) {
+ Map<String, String> env = pb.environment();
+ int i = envArg.indexOf('=');
+ if (i == -1) {
+ env.remove(envArg);
+ } else {
+ env.put(envArg.substring(0, i), envArg.substring(i + 1));
+ }
+ }
+ process = pb.start();
+ outForward = new Forward(process.getInputStream(), System.out);
+ outForward.start();
+ errForward = new Forward(process.getErrorStream(), System.err);
+ errForward.start();
+ } else if (sofficeArg.startsWith("connect:")) {
+ description = sofficeArg.substring("connect:".length());
+ } else {
+ fail(
+ "\"soffice\" argument \"" + sofficeArg +
+ " starts with neither \"path:\" nor \"connect:\"");
+ }
+ XUnoUrlResolver resolver = UnoUrlResolver.create(
+ Bootstrap.createInitialComponentContext(null));
+ for (;;) {
+ try {
+ context = UnoRuntime.queryInterface(
+ XComponentContext.class,
+ resolver.resolve(
+ "uno:" + description +
+ ";urp;StarOffice.ComponentContext"));
+ break;
+ } catch (NoConnectException e) {}
+ if (process != null) {
+ assertNull(waitForProcess(process, 1000)); // 1 sec
+ }
+ }
+ }
+
+ /** Shut down the OOo instance.
+ */
+ public void tearDown()
+ throws InterruptedException, com.sun.star.uno.Exception
+ {
+ boolean cleanTermination = false;
+ int code = 0;
+ try {
+ if (process != null) {
+ if (context != null) {
+ XDesktop desktop = null;
+ try {
+ XMultiComponentFactory factory =
+ context.getServiceManager();
+ assertNotNull(factory);
+ desktop = UnoRuntime.queryInterface(XDesktop.class,
+ factory.createInstanceWithContext(
+ "com.sun.star.frame.Desktop", context));
+ } catch (DisposedException e) {
+ // it can happen that the Java bridge was disposed
+ // already, we want to ensure soffice.bin is killed
+ process.destroy();
+ }
+ context = null;
+ if (desktop != null) {
+ try {
+ boolean desktopTerminated = desktop.terminate();
+ if (!desktopTerminated) {
+ // in case terminate() fails we would wait
+ // forever for the process to die, so kill it
+ process.destroy();
+ }
+ assertTrue(desktopTerminated);
+ } catch (DisposedException e) {}
+ // it appears that DisposedExceptions can already happen
+ // while receiving the response of the terminate call
+ }
+ desktop = null;
+ } else {
+ process.destroy();
+ }
+ }
+ if (process != null) {
+ code = process.waitFor();
+ }
+ boolean outTerminated = outForward == null
+ || outForward.terminated();
+ boolean errTerminated = errForward == null
+ || errForward.terminated();
+ assertEquals(0, code);
+ cleanTermination = true;
+ assertTrue(outTerminated);
+ assertTrue(errTerminated);
+ } finally {
+ if (!cleanTermination) {
+ try {
+ String sofficeArg = Argument.get("soffice");
+ String workdir = Argument.get("workdir");
+ String postprocesscommand = Argument.get(
+ "postprocesscommand");
+ if (sofficeArg.startsWith("path:") && workdir != null
+ && postprocesscommand != null)
+ {
+ ProcessBuilder pb = new ProcessBuilder(
+ postprocesscommand,
+ sofficeArg.substring("path:".length()) + ".bin",
+ workdir, String.valueOf(code));
+ Process postprocess = pb.start();
+ Forward ppoutForward = new Forward(
+ postprocess.getInputStream(), System.out);
+ ppoutForward.start();
+ Forward pperrForward = new Forward(
+ postprocess.getErrorStream(), System.err);
+ pperrForward.start();
+ code = postprocess.waitFor();
+ if (code != 0) {
+ throw new PostprocessFailedException(code);
+ }
+ }
+ }
+ catch (IOException e) {
+ throw new PostprocessFailedException(e);
+ }
+ }
+ }
+ }
+
+ /** Obtain the component context of the running OOo instance.
+ */
+ public XComponentContext getComponentContext() {
+ return context;
+ }
+
+ //TODO: get rid of this hack for legacy qa/unoapi tests
+ public String getDescription() {
+ return description;
+ }
+
+ private static Integer waitForProcess(Process process, final int millis)
+ throws InterruptedException
+ {
+ final Thread t1 = Thread.currentThread();
+ Thread t2 = new Thread("waitForProcess") {
+ @Override
+ public void run() {
+ util.utils.pause(millis);
+ t1.interrupt();
+ }
+ };
+ boolean old = Thread.interrupted();
+ // clear interrupted status, get old status
+ t2.start();
+ int n = 0;
+ boolean done = false;
+ try {
+ n = process.waitFor();
+ done = true;
+ } catch (InterruptedException e) {}
+ t2.interrupt();
+ try {
+ t2.join();
+ } catch (InterruptedException e) {
+ t2.join();
+ }
+ Thread.interrupted(); // clear interrupted status
+ if (old) {
+ t1.interrupt(); // reset old status
+ }
+ return done ? Integer.valueOf(n) : null;
+ }
+
+ private static final class Forward extends Thread {
+ public Forward(InputStream in, PrintStream out) {
+ super("process output forwarder");
+ this.in = in;
+ this.out = out;
+ }
+
+ @Override
+ public void run() {
+ for (;;) {
+ byte[] buf = new byte[1024];
+ int n;
+ try {
+ n = in.read(buf);
+ } catch (IOException e) {
+ throw new RuntimeException("wrapping", e);
+ }
+ if (n == -1) {
+ break;
+ }
+ out.write(buf, 0, n);
+ }
+ done = true;
+ }
+
+ public boolean terminated() throws InterruptedException {
+ join();
+ return done;
+ }
+
+ private final InputStream in;
+ private final PrintStream out;
+ private boolean done = false;
+ }
+
+ private static final class PostprocessFailedException
+ extends RuntimeException
+ {
+ PostprocessFailedException(int exitCode) {
+ super("postprocessing failed with exit code " + exitCode);
+ }
+
+ PostprocessFailedException(IOException cause) {
+ super("postprocessing failed with IOException " + cause, cause);
+ }
+ }
+
+ private String description;
+ private Process process = null;
+ private Forward outForward = null;
+ private Forward errForward = null;
+ private XComponentContext context = null;
+}
+// vim:set et sw=4 sts=4:
diff --git a/unotest/source/java/org/openoffice/test/OfficeFileUrl.java b/unotest/source/java/org/openoffice/test/OfficeFileUrl.java
new file mode 100644
index 000000000..f908f3642
--- /dev/null
+++ b/unotest/source/java/org/openoffice/test/OfficeFileUrl.java
@@ -0,0 +1,33 @@
+/*
+ * 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/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+package org.openoffice.test;
+
+import java.io.File;
+
+/** Obtain the office-internal absolute file URL of a given file.
+ */
+public final class OfficeFileUrl {
+ public static String getAbsolute(File file) {
+ return file.getAbsoluteFile().toURI().toString().replaceFirst(
+ "\\A[Ff][Ii][Ll][Ee]:/(?=[^/]|\\z)", "file:///");
+ // file:/path -> file:///path
+ }
+
+ private OfficeFileUrl() {}
+}
diff --git a/unotest/source/java/org/openoffice/test/UnoApiTest.java b/unotest/source/java/org/openoffice/test/UnoApiTest.java
new file mode 100644
index 000000000..44dd47231
--- /dev/null
+++ b/unotest/source/java/org/openoffice/test/UnoApiTest.java
@@ -0,0 +1,47 @@
+/*
+ * 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/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+package org.openoffice.test;
+
+import org.openoffice.Runner;
+import static org.junit.Assert.*;
+import org.junit.*;
+
+public final class UnoApiTest {
+ @Before public void setUp() throws Exception {
+ connection.setUp();
+ }
+
+ @After public void tearDown()
+ throws InterruptedException, com.sun.star.uno.Exception
+ {
+ connection.tearDown();
+ }
+
+ @Test public void test() throws Exception {
+ assertTrue("org.openoffice.Runner failed with params: -sce " + Argument.get("sce")
+ + "-xcl " + Argument.get("xcl")
+ + " -tdoc " + Argument.get("tdoc")
+ + " -cs " + connection.getDescription(),
+ Runner.run(
+ "-sce", Argument.get("sce"), "-xcl", Argument.get("xcl"), "-tdoc",
+ Argument.get("tdoc"), "-cs", connection.getDescription()));
+ }
+
+ private final OfficeConnection connection = new OfficeConnection();
+}
diff --git a/unotest/source/java/org/openoffice/test/tools/DocumentType.java b/unotest/source/java/org/openoffice/test/tools/DocumentType.java
new file mode 100644
index 000000000..bb4745f7b
--- /dev/null
+++ b/unotest/source/java/org/openoffice/test/tools/DocumentType.java
@@ -0,0 +1,37 @@
+/*
+ * 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/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+package org.openoffice.test.tools;
+
+/** a helper "enumeration class" for classifying a document type
+*/
+public class DocumentType extends com.sun.star.uno.Enum
+{
+ private DocumentType( int value )
+ {
+ super( value );
+ }
+
+ public static final DocumentType WRITER = new DocumentType(0);
+ public static final DocumentType CALC = new DocumentType(1);
+ public static final DocumentType DRAWING = new DocumentType(2);
+ public static final DocumentType XMLFORM = new DocumentType(3);
+ public static final DocumentType PRESENTATION = new DocumentType(4);
+ public static final DocumentType FORMULA = new DocumentType(5);
+ public static final DocumentType UNKNOWN = new DocumentType(-1);
+
+}
diff --git a/unotest/source/java/org/openoffice/test/tools/OfficeDocument.java b/unotest/source/java/org/openoffice/test/tools/OfficeDocument.java
new file mode 100644
index 000000000..bd996c011
--- /dev/null
+++ b/unotest/source/java/org/openoffice/test/tools/OfficeDocument.java
@@ -0,0 +1,198 @@
+/*
+ * 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/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+package org.openoffice.test.tools;
+
+import com.sun.star.beans.PropertyState;
+import com.sun.star.beans.PropertyValue;
+import com.sun.star.document.MacroExecMode;
+import com.sun.star.frame.XComponentLoader;
+import com.sun.star.frame.XController;
+import com.sun.star.frame.XModel;
+import com.sun.star.lang.XComponent;
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.lang.XServiceInfo;
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.uno.XInterface;
+import com.sun.star.util.CloseVetoException;
+import com.sun.star.util.XCloseable;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**************************************************************************/
+
+/**************************************************************************/
+/** provides a small wrapper around a document
+*/
+public class OfficeDocument
+{
+ /* ================================================================== */
+ /* ------------------------------------------------------------------ */
+ public OfficeDocument( XMultiServiceFactory orb, XComponent document )
+ {
+ m_orb = orb;
+ m_documentComponent = document;
+ }
+
+ /* ------------------------------------------------------------------ */
+ protected static XComponent implLoadAsComponent( XMultiServiceFactory orb, String documentOrFactoryURL ) throws com.sun.star.uno.Exception
+ {
+ return implLoadAsComponent( orb, documentOrFactoryURL, new PropertyValue[0] );
+ }
+
+ /* ------------------------------------------------------------------ */
+ private static XComponent implLoadAsComponent( XMultiServiceFactory orb, String documentOrFactoryURL, final PropertyValue[] i_args ) throws com.sun.star.uno.Exception
+ {
+ XComponentLoader aLoader = UnoRuntime.queryInterface( XComponentLoader.class,
+ orb.createInstance( "com.sun.star.frame.Desktop" ) );
+
+ XComponent document = UnoRuntime.queryInterface( XComponent.class,
+ aLoader.loadComponentFromURL( documentOrFactoryURL, "_blank", 0, i_args )
+ );
+ return document;
+ }
+
+ /* ------------------------------------------------------------------ */
+ private static OfficeDocument implLoadDocument( XMultiServiceFactory orb, String documentOrFactoryURL, final PropertyValue[] i_args ) throws com.sun.star.uno.Exception
+ {
+ XComponent document = implLoadAsComponent( orb, documentOrFactoryURL, i_args );
+
+ XServiceInfo xSI = UnoRuntime.queryInterface( XServiceInfo.class, document );
+ if ( xSI.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
+ return new SpreadsheetDocument( orb, document );
+ return new OfficeDocument( orb, document );
+ }
+
+ /* ------------------------------------------------------------------ */
+ public static OfficeDocument blankTextDocument( XMultiServiceFactory orb ) throws com.sun.star.uno.Exception
+ {
+ return blankDocument( orb, DocumentType.WRITER );
+ }
+
+ /* ------------------------------------------------------------------ */
+ public static OfficeDocument blankDocument( XMultiServiceFactory orb, DocumentType eType ) throws com.sun.star.uno.Exception
+ {
+ final PropertyValue[] args = new PropertyValue[] {
+ new PropertyValue( "MacroExecutionMode", -1, MacroExecMode.ALWAYS_EXECUTE, PropertyState.DIRECT_VALUE )
+ };
+ return implLoadDocument( orb, getDocumentFactoryURL( eType ), args );
+ }
+
+ /* ------------------------------------------------------------------ */
+ public boolean close()
+ {
+ try
+ {
+ XCloseable closeDoc = UnoRuntime.queryInterface( XCloseable.class, m_documentComponent );
+ closeDoc.close( true );
+ return true;
+ }
+ catch ( CloseVetoException e )
+ {
+ Logger.getLogger( OfficeDocument.class.getName() ).log( Level.SEVERE, "closing the document was vetoed", e );
+ }
+ return false;
+ }
+
+ /* ================================================================== */
+ /* ------------------------------------------------------------------ */
+ public XComponent getDocument( )
+ {
+ return m_documentComponent;
+ }
+
+ /* ------------------------------------------------------------------ */
+ /** retrieves the current view of the document
+ @return
+ the view component, queried for the interface described by aInterfaceClass
+ */
+ public OfficeDocumentView getCurrentView( )
+ {
+ // get the model interface for the document
+ XModel xDocModel = UnoRuntime.queryInterface( XModel.class, m_documentComponent );
+ // get the current controller for the document - as a controller is tied to a view,
+ // this gives us the currently active view for the document.
+ XController xController = xDocModel.getCurrentController();
+
+ if ( classify() == DocumentType.CALC )
+ return new SpreadsheetView( m_orb, xController );
+
+ return new OfficeDocumentView( m_orb, xController );
+ }
+
+ /* ------------------------------------------------------------------ */
+ /** returns a URL which can be used to create a document of a certain type
+ */
+ private static String getDocumentFactoryURL( DocumentType eType )
+ {
+ if ( eType == DocumentType.WRITER )
+ return "private:factory/swriter";
+ if ( eType == DocumentType.CALC )
+ return "private:factory/scalc";
+ if ( eType == DocumentType.DRAWING )
+ return "private:factory/sdraw";
+ if ( eType == DocumentType.XMLFORM )
+ return "private:factory/swriter?slot=21053";
+ if ( eType == DocumentType.PRESENTATION )
+ return "private:factory/simpress";
+ if ( eType == DocumentType.FORMULA )
+ return "private:factory/smath";
+ return "private:factory/swriter";
+ }
+
+ /* ------------------------------------------------------------------ */
+ /** classifies a document
+ */
+ private DocumentType classify( )
+ {
+ XServiceInfo xSI = UnoRuntime.queryInterface( XServiceInfo.class, m_documentComponent );
+
+ if ( xSI.supportsService( "com.sun.star.text.TextDocument" ) )
+ return DocumentType.WRITER;
+ else if ( xSI.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
+ return DocumentType.CALC;
+ else if ( xSI.supportsService( "com.sun.star.drawing.DrawingDocument" ) )
+ return DocumentType.DRAWING;
+ else if ( xSI.supportsService( "com.sun.star.presentation.PresentationDocument" ) )
+ return DocumentType.PRESENTATION;
+ else if ( xSI.supportsService( "com.sun.star.formula.FormulaProperties" ) )
+ return DocumentType.FORMULA;
+
+ return DocumentType.UNKNOWN;
+ }
+
+ /* ------------------------------------------------------------------ */
+ /** creates a component at the service factory provided by the document
+ */
+ public XInterface createInstance( String serviceSpecifier ) throws com.sun.star.uno.Exception
+ {
+ XMultiServiceFactory xORB = UnoRuntime.queryInterface( XMultiServiceFactory.class, m_documentComponent );
+ return (XInterface)xORB.createInstance( serviceSpecifier );
+ }
+
+ /* ------------------------------------------------------------------ */
+ /** creates a component at the service factory provided by the document, queried for a given interface type
+ */
+ public <T> T createInstance( String i_serviceSpecifier, Class<T> i_interfaceClass ) throws com.sun.star.uno.Exception
+ {
+ return UnoRuntime.queryInterface( i_interfaceClass, createInstance( i_serviceSpecifier ) );
+ }
+
+ private final XMultiServiceFactory m_orb;
+ private XComponent m_documentComponent;
+}
+
diff --git a/unotest/source/java/org/openoffice/test/tools/OfficeDocumentView.java b/unotest/source/java/org/openoffice/test/tools/OfficeDocumentView.java
new file mode 100644
index 000000000..c78537b71
--- /dev/null
+++ b/unotest/source/java/org/openoffice/test/tools/OfficeDocumentView.java
@@ -0,0 +1,104 @@
+/*
+ * 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/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+package org.openoffice.test.tools;
+
+/**************************************************************************/
+
+import com.sun.star.beans.PropertyValue;
+import com.sun.star.frame.XController;
+import com.sun.star.frame.XDispatch;
+import com.sun.star.frame.XDispatchProvider;
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.util.URL;
+import com.sun.star.util.XURLTransformer;
+
+/**************************************************************************/
+/** provides a small wrapper around a document view
+*/
+public class OfficeDocumentView
+{
+ private final XMultiServiceFactory m_orb;
+ private final XController m_controller;
+
+ /* ------------------------------------------------------------------ */
+ final public XController getController()
+ {
+ return m_controller;
+ }
+
+ /* ------------------------------------------------------------------ */
+ public OfficeDocumentView( XMultiServiceFactory orb, XController controller )
+ {
+ m_orb = orb;
+ m_controller = controller;
+ }
+
+ /* ------------------------------------------------------------------ */
+ /** retrieves a dispatcher for the given URL, obtained at the current view of the document
+ @param aURL
+ a one-element array. The first element must contain a valid
+ <member scope="com.sun.star.util">URL::Complete</member> value. Upon return, the URL is correctly
+ parsed.
+ @return
+ the dispatcher for the URL in question
+ */
+ private XDispatch getDispatcher( URL[] aURL ) throws com.sun.star.uno.Exception
+ {
+ XDispatch xReturn = null;
+
+ // go get the dispatch provider of its frame
+ XDispatchProvider xProvider = UnoRuntime.queryInterface( XDispatchProvider.class, m_controller.getFrame() );
+ if ( null != xProvider )
+ {
+ // need a URLTransformer
+ XURLTransformer xTransformer = UnoRuntime.queryInterface( XURLTransformer.class,
+ m_orb.createInstance( "com.sun.star.util.URLTransformer" ) );
+ xTransformer.parseStrict( aURL );
+
+ xReturn = xProvider.queryDispatch( aURL[0], "", 0 );
+ }
+ return xReturn;
+ }
+
+
+ /* ------------------------------------------------------------------ */
+ /** dispatches the given URL into the view, if there's a dispatcher for it
+
+ @return
+ <TRUE/> if the URL was successfully dispatched
+ */
+ public boolean dispatch( String i_url ) throws com.sun.star.uno.Exception
+ {
+ return dispatch( i_url, new PropertyValue[0] );
+ }
+
+ /* ------------------------------------------------------------------ */
+ private boolean dispatch( final String i_url, final PropertyValue[] i_arguments ) throws com.sun.star.uno.Exception
+ {
+ URL[] completeURL = new URL[] { new URL() };
+ completeURL[0].Complete = i_url;
+ XDispatch dispatcher = getDispatcher( completeURL );
+ if ( dispatcher == null )
+ return false;
+
+ dispatcher.dispatch( completeURL[0], i_arguments );
+ return true;
+ }
+
+}
diff --git a/unotest/source/java/org/openoffice/test/tools/SpreadsheetDocument.java b/unotest/source/java/org/openoffice/test/tools/SpreadsheetDocument.java
new file mode 100644
index 000000000..7db429103
--- /dev/null
+++ b/unotest/source/java/org/openoffice/test/tools/SpreadsheetDocument.java
@@ -0,0 +1,61 @@
+/*
+ * 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/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+package org.openoffice.test.tools;
+
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.lang.XComponent;
+import com.sun.star.table.XCellRange;
+import com.sun.star.container.XIndexAccess;
+import com.sun.star.sheet.XSpreadsheetDocument;
+import com.sun.star.sheet.XSpreadsheets;
+import com.sun.star.uno.UnoRuntime;
+
+public class SpreadsheetDocument extends OfficeDocument
+{
+ /** Creates a new blank spreadsheet document */
+ /* ------------------------------------------------------------------ */
+ public SpreadsheetDocument( XMultiServiceFactory orb ) throws com.sun.star.uno.Exception
+ {
+ super( orb, implLoadAsComponent( orb, "private:factory/scalc" ) );
+ }
+
+ /* ------------------------------------------------------------------ */
+ public SpreadsheetDocument( XMultiServiceFactory orb, XComponent document )
+ {
+ super( orb, document );
+ }
+
+ /* ------------------------------------------------------------------ */
+ /** returns the sheets collection
+ */
+ private XSpreadsheets getSheets()
+ {
+ XSpreadsheetDocument spreadsheetDoc = UnoRuntime.queryInterface( XSpreadsheetDocument.class, getDocument() );
+ return spreadsheetDoc.getSheets();
+ }
+
+ /* ------------------------------------------------------------------ */
+ /** returns the sheet with the given index
+ */
+ public XCellRange getSheet( int index ) throws com.sun.star.uno.Exception
+ {
+ XIndexAccess sheets = UnoRuntime.queryInterface( XIndexAccess.class, getSheets() );
+ return UnoRuntime.queryInterface( XCellRange.class, sheets.getByIndex( index ) );
+ }
+}
diff --git a/unotest/source/java/org/openoffice/test/tools/SpreadsheetView.java b/unotest/source/java/org/openoffice/test/tools/SpreadsheetView.java
new file mode 100644
index 000000000..21d3d23e4
--- /dev/null
+++ b/unotest/source/java/org/openoffice/test/tools/SpreadsheetView.java
@@ -0,0 +1,32 @@
+/*
+ * 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/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+package org.openoffice.test.tools;
+
+import com.sun.star.frame.XController;
+import com.sun.star.lang.XMultiServiceFactory;
+
+public class SpreadsheetView extends OfficeDocumentView
+{
+
+ /** Creates a new instance of SpreadsheetView */
+ public SpreadsheetView( XMultiServiceFactory orb, XController controller )
+ {
+ super( orb, controller );
+ }
+
+}