/*
 * 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.ui.pillContainerAutoComplete;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.List;

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

public class BasePillContainerAutoComplete extends WebDriverTestCase {

	private final String CMP_URL;
	public static final String INPUT = "div[class*='uiInputTextForAutocomplete'] input";
    public static final String LISTCONTENT_LOCATOR = ".visible";
    
    public BasePillContainerAutoComplete(String urlPath) {
    	super(urlPath);
        this.CMP_URL = urlPath;
    }
    
    /*
     * ui:pillContainer dropdown continues to focus after clicking on any of the pill
     * Bug: W-2628705
     */
    public void testAutoCompleteListContentVisible() throws MalformedURLException, URISyntaxException, InterruptedException {
        open(CMP_URL);
        WebElement input = findDomElement(By.cssSelector(INPUT));
        input.sendKeys("khDmXpDDmALzDqhYeCvJgqEmjUPJqV");
        auraUITestingUtil.pressEnter(input);
        assertFalse("Auto complete List Content should not be visible", isListContentVisible());
        input.sendKeys("test");
        assertNotNull("Auto complete List Content should be visible", isListContentVisible());
        WebElement pill = findDomElement(By.cssSelector(".pill"));
        pill.click();
        assertTrue("Auto complete List Content should be still visible after clicking on pill", isListContentVisible());
    }
    
    /*
     * Test to prevent loss of focus from the auto complete input when clicking
     * on the option in autocomplete list
     * Bug: W-2641156
     */
    public void testLossOfFocusVerification() throws MalformedURLException, URISyntaxException, InterruptedException {
        open(CMP_URL);
        WebElement input = findDomElement(By.cssSelector(INPUT));
        input.sendKeys("khDmXpDDmALzDqhYeCvJgqEmjUPJqV");
        auraUITestingUtil.pressEnter(input);
        assertFalse("Auto complete List Content should not be visible", isListContentVisible());
        input.sendKeys("test");
        assertNotNull("Auto complete List Content should be visible", isListContentVisible());
        int listItem = 2;
        WebElement listItem2 = getAutoCompleteListOptions(listItem);
        listItem2.click();
        assertFalse("Auto complete List Content should not be visible after click on pill 3", isListContentVisible());
    }

	private WebElement getAutoCompleteListOptions(int optionNumber) {
		List<WebElement> listOfOptions = findDomElements(By.cssSelector("ul[class*='visible'] li"));
		return listOfOptions.get(optionNumber);
	}

	private boolean isListContentVisible() throws InterruptedException {
		waitFor(3);
        return isElementPresent(By.cssSelector(LISTCONTENT_LOCATOR));
	}
}
@@ -887,23 +887,118 @@
 nium.Web
-Element
+Driver;%0Aimport org.openqa.selenium.WebElement;%0Aimport org.openqa.selenium.support.ui.ExpectedCondition
 ;%0A%0Apubli
@@ -1819,35 +1819,53 @@
 t);%0A        
-assertFalse
+verifyAutoCompleteListPresent
 (%22Auto compl
@@ -1905,38 +1905,21 @@
 sible%22, 
-isListContentVisible()
+false
 );%0A     
@@ -1949,37 +1949,53 @@
 %22);%0A        
-assertNotNull
+verifyAutoCompleteListPresent
 (%22Auto compl
@@ -2031,38 +2031,20 @@
 sible%22, 
-isListContentVisible()
+true
 );%0A     
@@ -2139,18 +2139,37 @@
     
-assertTrue
+verifyAutoCompleteListPresent
 (%22Au
@@ -2242,38 +2242,20 @@
  pill%22, 
-isListContentVisible()
+true
 );%0A    %7D
@@ -2734,35 +2734,53 @@
 t);%0A        
-assertFalse
+verifyAutoCompleteListPresent
 (%22Auto compl
@@ -2820,38 +2820,21 @@
 sible%22, 
-isListContentVisible()
+false
 );%0A     
@@ -2872,21 +2872,37 @@
     
-assertNotNull
+verifyAutoCompleteListPresent
 (%22Au
@@ -2946,38 +2946,20 @@
 sible%22, 
-isListContentVisible()
+true
 );%0A     
@@ -3087,19 +3087,37 @@
     
-assertFalse
+verifyAutoCompleteListPresent
 (%22Au
@@ -3187,38 +3187,21 @@
 ill 3%22, 
-isListContentVisible()
+false
 );%0A    %7D
@@ -3415,91 +3415,360 @@
 %7D%0A%0A%09
-private boolean isListContentVisible() throws InterruptedException %7B%0A%09%09waitFor(3);%0A
+/**%0A%09 * Verify autocomplete list is expanded(Visible) %0A%09 * @param failureMessage%0A%09 * @param isElemPresent%0A%09 */%0A%09private void verifyAutoCompleteListPresent(String failureMessage, boolean isElemPresent) %7B%0A        auraUITestingUtil.waitUntil(new ExpectedCondition%3CBoolean%3E() %7B%0A            @Override%0A            public Boolean apply(WebDriver d) %7B%0A        
     
@@ -3831,14 +3831,76 @@
 OCATOR))
-;%0A%09
+ == isElemPresent;%0A            %7D%0A        %7D, failureMessage);%0A    
 %7D%0A%7D

Fix pills flapping test