/*
 * Copyright (c) 2007-2012 The Broad Institute, Inc.
 * SOFTWARE COPYRIGHT NOTICE
 * This software and its documentation are the copyright of the Broad Institute, Inc. All rights are reserved.
 *
 * This software is supplied without any warranty or guaranteed support whatsoever. The Broad Institute is not responsible for its use, misuse, or functionality.
 *
 * This software is licensed under the terms of the GNU Lesser General Public License (LGPL),
 * Version 2.1 which is available at http://www.opensource.org/licenses/lgpl-2.1.php.
 */

package org.broad.igv.ui;

import org.broad.igv.AbstractHeadedTest;
import org.broad.igv.track.Track;
import org.broad.igv.ui.panel.FrameManager;
import org.broad.igv.util.TestUtils;
import org.junit.*;
import org.junit.rules.TestRule;
import org.junit.rules.Timeout;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * Test of class main. In general will use this to see that IGV
 * starts properly, given startup parameters. Since we will be starting
 * IGV in a non-standard way, do NOT inherit from AbstractHeadedTest
 * User: jacob
 * Date: 2012/05/04
 */
public class MainTest{

    @Rule
    public TestRule testTimeout = new Timeout((int) 1e5);

    @BeforeClass
    public static void setUpClass() throws Exception{
        AbstractHeadedTest.assumeNotHeadless();
        TestUtils.setUpTestEnvironment();
    }

    @AfterClass
    public static void tearDownClass() throws Exception{
        AbstractHeadedTest.tearDownClass();
    }
    /**
     * Test that loading IGV with a startup file and
     * locus loads that file and locus
     *
     * @throws Exception
     */
    @Test
    public void testFileLocusArgs() throws Exception {

        String trackName = "NA12878.SLX.bam";
        String filePath = "http://www.broadinstitute.org/igvdata/1KG/pilot2Bams/" + trackName;
        String genome = "hg18";
        String locus = "chr1:64,098,103-64,098,175";

        String[] args = new String[]{filePath, locus, "-g", genome};

        Main.main(args);

        IGV igv = IGV.getInstance();

        //Need to wait for IGV to load file, genome,  and move to locus
        assertTrue(igv.waitForNotify(60000));

        assertEquals(genome, igv.getGenomeManager().getGenomeId());
        boolean trackFound = false;
        for (Track track : igv.getAllTracks()) {
            trackFound |= track.getName().equals(trackName);
        }

        assertTrue("Expected " + trackName + " to be loaded, but it wasn't", trackFound);

        String actLocus = FrameManager.getDefaultFrame().getFormattedLocusString();
        assertEquals(actLocus, locus);
    }


}

@@ -610,16 +610,56 @@
 edTest;%0A
+import org.broad.igv.PreferenceManager;%0A
 import o
@@ -782,17 +782,102 @@
 g.junit.
-*
+AfterClass;%0Aimport org.junit.BeforeClass;%0Aimport org.junit.Rule;%0Aimport org.junit.Test
 ;%0Aimport
@@ -971,63 +971,9 @@
 ert.
-assertEquals;%0Aimport static org.junit.Assert.assertTrue
+*
 ;%0A%0A/
@@ -1240,16 +1240,17 @@
 MainTest
+ 
 %7B%0A%0A    @
@@ -1374,32 +1374,33 @@
 throws Exception
+ 
 %7B%0A        Abstra
@@ -1553,16 +1553,17 @@
 xception
+ 
 %7B%0A      
@@ -1606,16 +1606,17 @@
 ;%0A    %7D%0A
+%0A
     /**%0A
@@ -2114,72 +2114,8 @@
 %7D;%0A%0A
-        Main.main(args);%0A%0A        IGV igv = IGV.getInstance();%0A%0A
     
@@ -2162,17 +2162,16 @@
  genome,
- 
  and mov
@@ -2193,44 +2193,40 @@
     
-assertTrue(igv.waitForNotify(
+IGV igv = startMain(args, 
 60000)
-)
 ;%0A%0A 
@@ -2670,11 +2670,798 @@
 %0A    %7D%0A%0A
+    /**%0A     * Test loading a genome not in the display list%0A     *%0A     * @throws Exception%0A     */%0A    @Test%0A    public void testLoadGenome() throws Exception %7B%0A%0A        String genomeId = %22mm7%22;%0A        String%5B%5D genomes = PreferenceManager.getInstance().getGenomeIdDisplayList();%0A        for (String gen : genomes) %7B%0A            assertNotSame(%22Bad test setup, test genome in display list%22, gen, genomeId);%0A        %7D%0A%0A        String%5B%5D args = new String%5B%5D%7B%22-g%22, genomeId%7D;%0A        IGV igv = startMain(args, 10000);%0A%0A        assertEquals(igv.getGenomeManager().getGenomeId(), genomeId);%0A    %7D%0A%0A    private IGV startMain(String%5B%5D args, long timeout) %7B%0A        Main.main(args);%0A        IGV igv = IGV.getInstance();%0A        assertTrue(igv.waitForNotify(timeout));%0A        return igv;%0A    %7D%0A%0A
 %0A%7D%0A

Add test for loading genome from command line which is not in display list