001/* JUnit test the HTMLAbout mechanism
002
003 Copyright (c) 2011-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028
029package ptolemy.actor.gui.test.junit;
030
031import javax.swing.SwingUtilities;
032
033import ptolemy.actor.gui.ConfigurationApplication;
034import ptolemy.kernel.CompositeEntity;
035
036///////////////////////////////////////////////////////////////////
037//// HTMLAboutJUnitTest
038/**
039 * Test out HTMLAbout by starting vergil with various URLs.
040 *
041 * @author Christopher Brooks
042 * @version $Id$
043 * @since Ptolemy II 10.0
044 * @Pt.ProposedRating Red (cxh)
045 * @Pt.AcceptedRating Red (cxh)
046 */
047public class HTMLAboutJUnitTest {
048
049    /**
050     * Invoke about:allcopyrights, which pops up a window that lists
051     * the copyrights.
052     * @exception Exception If there is a problem reading or laying
053     * out a model.
054     */
055    @org.junit.Test
056    public void aboutAllCopyrights() throws Exception {
057        _openModel("about:allcopyrights");
058    }
059
060    /**
061     * Invoke about:configuration, which expands the actor tree on
062     * the left.
063     * @exception Exception If there is a problem reading or laying
064     * out a model.
065     */
066    @org.junit.Test
067    public void aboutConfiguration() throws Exception {
068        _openModel("about:configuration");
069    }
070
071    /**
072     * Invoke about:copyrights, which pops up a window that lists
073     * the copyrights.
074     * @exception Exception If there is a problem reading or laying
075     * out a model.
076     */
077    @org.junit.Test
078    public void aboutCopyrights() throws Exception {
079        _openModel("about:copyrights");
080    }
081
082    /** Test the HTMLAbout
083     *
084     *  <p>To run, use:</p>
085     *
086     *  <pre>
087     *   $PTII/bin/ptinvoke ptolemy.actor.gui.test.junit.HTMLAboutJUnitTest
088     *  </pre>
089     *  We use ptinvoke so that the classpath is set to include all the packages
090     *  used by Ptolemy II.
091     *
092     *  @param args Ignored.
093     */
094    public static void main(String args[]) {
095        org.junit.runner.JUnitCore
096                .main("ptolemy.actor.gui.test.junit.HTMLAboutJUnitTest");
097    }
098
099    ///////////////////////////////////////////////////////////////////
100    ////                         protected methods                 ////
101
102    /**
103     * Test HTMLAbout by opening a URL that starts with "about:".
104     *
105     * <p>This is the main entry point for HTMLAbout tests.</p>
106     *
107     * <p>The caller of this method need <b>not</b>be in the Swing
108     * Event Thread.</p>
109     *
110     * @param modelFileName The file name of the test model.
111     * @exception Exception If the file name cannot be read or laid out.
112     */
113    protected void _openModel(final String modelFileName) throws Exception {
114
115        // FIXME: this seem wrong:  The inner classes are in different
116        // threads and can only access final variables.  However, we
117        // use an array as a final variable, but we change the value
118        // of the element of the array.  Is this thread safe?
119        final CompositeEntity[] model = new CompositeEntity[1];
120        final Throwable[] throwable = new Throwable[1];
121        throwable[0] = null;
122        /////
123        // Open the model.
124        Runnable openModelAction = new Runnable() {
125            @Override
126            public void run() {
127                try {
128                    System.out.print(" " + modelFileName + " ");
129                    model[0] = ConfigurationApplication
130                            .openModelOrEntity(modelFileName);
131                } catch (Throwable throwableCause) {
132                    throwable[0] = throwableCause;
133                    throw new RuntimeException(throwableCause);
134                }
135            }
136        };
137        SwingUtilities.invokeAndWait(openModelAction);
138        _sleep();
139        if (throwable[0] != null /*|| model[0] == null*/) {
140            throw new Exception(
141                    "Failed to open " + modelFileName + throwable[0]);
142        }
143
144        /////
145        // Close the model.
146        Runnable closeAction = new Runnable() {
147            @Override
148            public void run() {
149                try {
150                    // FIXME: handle cases where model[0] is null.
151                    if (model[0] != null) {
152                        ConfigurationApplication
153                                .closeModelWithoutSavingOrExiting(model[0]);
154                    }
155                } catch (Exception ex) {
156                    throw new RuntimeException(ex);
157                }
158            }
159        };
160        SwingUtilities.invokeAndWait(closeAction);
161        _sleep();
162    }
163
164    /** Sleep the current thread, which is usually not the Swing Event
165     *  Dispatch Thread.
166     */
167    protected void _sleep() {
168        try {
169            Thread.sleep(1000);
170        } catch (Throwable ex) {
171            //Ignore
172        }
173    }
174}