/*
 * Copyright 2000-2013 Vaadin Ltd.
 * 
 * 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 com.vaadin.tests.components.table;

import static com.vaadin.tests.components.table.SelectAllRows.COUNT_OF_SELECTED_ROWS_LABEL;
import static com.vaadin.tests.components.table.SelectAllRows.COUNT_SELECTED_BUTTON;
import static com.vaadin.tests.components.table.SelectAllRows.TABLE;
import static com.vaadin.tests.components.table.SelectAllRows.TOTAL_NUMBER_OF_ROWS;
import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;

import com.vaadin.tests.tb3.MultiBrowserTest;

public class SelectAllRowsTest extends MultiBrowserTest {

    private final static String TABLE_ROW = "v-table-row";

    @Override
    public List<DesiredCapabilities> getBrowsersToTest() {
        // Pressing Shift modifier key does not work with TestBench and IE
        // (#8621)
        return Arrays.asList(Browser.FIREFOX.getDesiredCapabilities(),
                Browser.CHROME.getDesiredCapabilities());
    }

    @Test
    public void testAllRowsAreSelected() {
        openTestURL();

        selectAllRowsInTable();
        int selectedRows = countSelectedItems();

        assertEquals(TOTAL_NUMBER_OF_ROWS, selectedRows);
    }

    private int countSelectedItems() {
        WebElement countButton = vaadinElementById(COUNT_SELECTED_BUTTON);
        countButton.click();
        WebElement countOfSelectedRows = vaadinElementById(COUNT_OF_SELECTED_ROWS_LABEL);
        String count = countOfSelectedRows.getText();
        return Integer.parseInt(count);
    }

    private void selectAllRowsInTable() {
        clickFirstRow();
        scrollTableToBottom();
        new Actions(getDriver()).keyDown(Keys.SHIFT).perform();
        clickLastRow();
        new Actions(getDriver()).keyUp(Keys.SHIFT).perform();
    }

    private void clickLastRow() {
        List<WebElement> rows = allVisibleTableRows();
        WebElement lastRow = rows.get(rows.size() - 1);
        lastRow.click();
    }

    private void clickFirstRow() {
        WebElement firstRow = allVisibleTableRows().get(0);
        firstRow.click();
    }

    private void scrollTableToBottom() {
        WebElement table = vaadinElementById(TABLE);
        testBenchElement(table.findElement(By.className("v-scrollable")))
                .scroll(TOTAL_NUMBER_OF_ROWS * 30);

        // Wait for scrolling to complete. Otherwise, clicking last row will
        // fail with Chrome
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private List<WebElement> allVisibleTableRows() {
        WebElement table = vaadinElementById(TABLE);
        List<WebElement> rows = table.findElements(By
                .cssSelector(".v-table-table tr"));
        return rows;
    }
}

@@ -1034,17 +1034,20 @@
 il.Array
-s
+List
 ;%0Aimport
@@ -1461,16 +1461,239 @@
 -row%22;%0A%0A
+    @Override%0A    protected DesiredCapabilities getDesiredCapabilities() %7B%0A        DesiredCapabilities cap = super.getDesiredCapabilities();%0A        cap.setCapability(%22requireWindowFocus%22, true);%0A        return cap;%0A    %7D%0A%0A
     @Ove
@@ -1819,92 +1819,80 @@
 ith 
-TestBench and IE%0A        // (#8621)%0A        return Arrays.asList(Browser.FIREFOX.get
+Firefox%0A        ArrayList%3CDesiredCapabilities%3E browsers = new ArrayList%3C
 Desi
@@ -1910,11 +1910,10 @@
 ties
+%3E
 (
-),
 %0A   
@@ -1929,22 +1929,75 @@
     
-Browser.CHROME
+super.getBrowsersToTest());%0A        browsers.remove(Browser.FIREFOX
 .get
@@ -2016,24 +2016,49 @@
 ilities());%0A
+        return browsers;%0A
     %7D%0A%0A    @
@@ -2765,32 +2765,17 @@
 FT).
-perform();%0A        
 click
+(get
 Last
@@ -2779,26 +2779,31 @@
 astRow()
-;%0A
+)%0A     
         
 new Acti
@@ -2798,32 +2798,11 @@
     
-new Actions(getDriver())
+   
 .key
@@ -2846,26 +2846,30 @@
 private 
-void click
+WebElement get
 LastRow(
@@ -2995,23 +2995,22 @@
     
+return 
 lastRow
-.click()
 ;%0A  

Make test work in IE. Disable Firefox which doesn't work