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 package complex.broken_document;
24 
25 import com.sun.star.beans.PropertyValue;
26 // import com.sun.star.frame.FrameSearchFlag;
27 import com.sun.star.frame.XComponentLoader;
28 import com.sun.star.frame.XFrame;
29 import com.sun.star.lang.XMultiServiceFactory;
30 import com.sun.star.uno.UnoRuntime;
31 
32 
33 // ---------- junit imports -----------------
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.openoffice.test.OfficeConnection;
40 import static org.junit.Assert.*;
41 // ------------------------------------------
42 
43 /**
44  * Check, if message boxes appear when the Office is in "headless" mode. Tests
45  * bug i15809. This test uses the broken document dbf.dbf.emf.
46  */
47 public class LoadDocument {
48 
49     /** defect file to load **/
50     // private final String mFileName = "dbf.dbf.emf";
51 
52     /**
53      * Get all test methods.
54      * @return The test methods.
55      */
56 //    public String[] getTestMethodNames() {
57 //        return new String[]{"checkHeadlessState"};
58 //    }
59 
60     /**
61      * Start Office with "-headless" parameter, then
62      * load the broken document "dbf.dbf.emf", that brings a message box up in
63      * the ui, see if the headless mode of SOffice changes.
64      */
65     @Test public void checkHeadlessState()
66     {
67         XMultiServiceFactory xMSF = getMSF();
68         XFrame xDesktop = null;
69 
70         try {
71             xDesktop = UnoRuntime.queryInterface(XFrame.class, xMSF.createInstance("com.sun.star.frame.Desktop"));
72         }
73         catch(com.sun.star.uno.Exception e) {
74             fail("Could not create a desktop instance.");
75         }
76 
77         XComponentLoader xDesktopLoader = UnoRuntime.queryInterface(XComponentLoader.class, xDesktop);
78         System.out.println("xDektopLoader is null: " + (xDesktopLoader == null));
79         PropertyValue[] val = new PropertyValue[0];
80 
81         // String workingDir = (String)param.get("WorkingDir") + System.getProperty("file.separator") + mFileName;
82         // System.out.println("Working dir: " + workingDir);
83         String fileUrl = complex.broken_document.TestDocument.getUrl("dbf.dbf.emf");
84         System.out.println("File Url: " + fileUrl);
85 
86         try {
87             xDesktopLoader.loadComponentFromURL(fileUrl, "_blank", 0, val);
88         }
89         catch(com.sun.star.io.IOException e) {
90             fail("Could not load document");
91         }
92         catch(com.sun.star.lang.IllegalArgumentException e) {
93             fail("Could not load document");
94         }
95 
96         // try again: headless mode defect now?
97         try {
98             xDesktopLoader.loadComponentFromURL(fileUrl, "_blank", 0, val);
99         }
100         catch(com.sun.star.io.IOException e) {
101             fail("Could not load document");
102         }
103         catch(com.sun.star.lang.IllegalArgumentException e) {
104             fail("Could not load document");
105         }
106 
107     }
108 
109 
110 
111   private XMultiServiceFactory getMSF()
112     {
113         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
114         return xMSF1;
115     }
116 
117     // setup and close connections
118     @BeforeClass public static void setUpConnection() throws Exception {
119         System.out.println("setUpConnection()");
120         connection.setUp();
121     }
122 
123     @AfterClass public static void tearDownConnection()
124         throws InterruptedException, com.sun.star.uno.Exception
125     {
126         System.out.println("tearDownConnection()");
127         connection.tearDown();
128     }
129 
130     private static final OfficeConnection connection = new OfficeConnection();
131 
132 }
133