/*
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * Licensed 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
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.auraframework.components.auradocs;

import org.auraframework.system.AuraContext.Mode;
import org.auraframework.test.util.WebDriverTestCase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

/**
 * A webdriver test for auradoc examples.
 */
public class AuradocsExampleUITest extends WebDriverTestCase {

    public AuradocsExampleUITest(String name) {
        super(name);
    }

    @Override
    public void perBrowserSetUp() {
        super.perBrowserSetUp();
        // This page, especially the reference tab, loads several components and
        // needs extra time in slower environments
        auraUITestingUtil.setTimeoutInSecs(60);
    }

    public void testBaseProd() throws Exception {
        doBase(Mode.PROD);
    }

    public void testBaseDev() throws Exception {
        doBase(Mode.DEV);
    }

    public void testReferenceProd() throws Exception {
        doReference(Mode.PROD);
    }

    public void testReferenceDev() throws Exception {
        doReference(Mode.DEV);
    }

    public void testComponentProd() throws Exception {
        doComponent(Mode.PROD);
    }

    public void testComponentDev() throws Exception {
        doComponent(Mode.DEV);
    }

    public void testAPIProd() throws Exception {
        doAPI(Mode.PROD);
    }

    public void testAPIDev() throws Exception {
        doAPI(Mode.DEV);
    }

    private long doBase(Mode mode) throws Exception {
        long start = System.currentTimeMillis();
        open("/auradocs", mode);
        WebElement content = getAuraUITestingUtil().findDomElement(
                By.cssSelector(".content"));
        assertNotNull("Should have content showing", content);
        assertTrue("Should have content displayed", content.isDisplayed());
        return System.currentTimeMillis() - start;
    }

    private long doReference(Mode mode) throws Exception {
        long start = System.currentTimeMillis();
        open("/auradocs#reference", mode);
        WebElement sidebar = getAuraUITestingUtil().findDomElement(
                By.xpath("//ol[contains(@class,'auradocsSidebar')]"));
        assertEquals("We expect 8 sidebar menu items", 8,
                sidebar.findElements(By.xpath("li")).size());
        return System.currentTimeMillis() - start;
    }

    private long doComponent(Mode mode) throws Exception {
        long start = System.currentTimeMillis();
        open("/auradocs#reference?descriptor=aura:component&defType=component",
                mode);
        WebElement tabset = getAuraUITestingUtil().findDomElement(
                By.xpath("//ul[contains(@class,'tabs__nav')]"));
        assertEquals("We expect 6 tabs in the component help", 6, tabset
                .findElements(By.xpath("li")).size());
        return System.currentTimeMillis() - start;
    }

    private long doAPI(Mode mode) throws Exception {
        long start = System.currentTimeMillis();
        open("/auradocs#reference?topic=api:Aura");
        // TODO: this should test more.
        WebElement content = getAuraUITestingUtil().findDomElement(
                By.cssSelector(".content"));
        assertNotNull("Should have content showing", content);
        assertTrue("Should have content displayed", content.isDisplayed());
        return System.currentTimeMillis() - start;
    }
}

@@ -818,23 +818,118 @@
 nium.Web
-Element
+Driver;%0Aimport org.openqa.selenium.WebElement;%0Aimport org.openqa.selenium.support.ui.ExpectedCondition
 ;%0A%0A/**%0A 
@@ -2680,32 +2680,187 @@
  mode);%0A        
+%0A        auraUITestingUtil.waitUntil(new ExpectedCondition%3CBoolean%3E() %7B%0A            @Override%0A            public Boolean apply(WebDriver d) %7B%0A            %09
 WebElement sideb
@@ -2895,32 +2895,40 @@
 findDomElement(%0A
+        
                 
@@ -2994,73 +2994,36 @@
     
-assertEquals(%22We expect 8 sidebar menu i
+        int totalMenuI
 tems
-%22, 8,%0A              
  
+=
  sid
@@ -3054,35 +3054,144 @@
 th(%22li%22)).size()
+;%0A            %09return totalMenuItems == 8;%0A            %7D%0A        %7D, %22We expect 8 sidebar menu items%22
 );%0A
+        %0A
         return S
@@ -3443,32 +3443,178 @@
  mode);%0A        
+auraUITestingUtil.waitUntil(new ExpectedCondition%3CBoolean%3E() %7B%0A            @Override%0A            public Boolean apply(WebDriver d) %7B%0A            %09
 WebElement tabse
@@ -3664,32 +3664,40 @@
                 
+        
 By.xpath(%22//ul%5Bc
@@ -3741,127 +3741,188 @@
     
-assertEquals(%22We expect 6 tabs in the component help%22, 6, tabset%0A                .findElements(By.xpath(%22li%22)).size());
+        int totalTabs = tabset.findElements(By.xpath(%22li%22)).size();%0A            %09return totalTabs == 6;%0A            %7D%0A        %7D, %22We expect 6 tabs in the component help%22);%0A        
 %0A   

Test fix by adding waitUnitl