/*
 * Copyright 2000-2014 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.popupview;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThan;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;

import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.PopupViewElement;
import com.vaadin.testbench.elements.VerticalLayoutElement;
import com.vaadin.tests.tb3.MultiBrowserTest;

/**
 * Resizing PopupView's popup component while it is visible should also resize
 * the drop shadow of the overlay.
 * 
 * @author Vaadin Ltd
 */
public class PopupViewResizeWhileOpenTest extends MultiBrowserTest {

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

        // Open PopupView
        $(PopupViewElement.class).first().click();

        // Shadow element
        WebElement shadow = findElement(By.className("v-shadow"));
        WebElement vl = $(VerticalLayoutElement.class).id("content-vl");

        // Sanity check
        assertShadowSize(vl, shadow);

        // Resize
        $(ButtonElement.class).first().click();
        assertShadowSize(vl, shadow);
    }

    private void assertShadowSize(WebElement layout, WebElement shadow) {
        Dimension shadowSize = shadow.getSize();
        Dimension layoutSize = layout.getSize();

        assertThat(shadowSize.height, is(greaterThan(layoutSize.height)));
        assertThat(shadowSize.height, is(lessThan(layoutSize.height + 10)));
        assertThat(shadowSize.width, is(greaterThan(layoutSize.width)));
        assertThat(shadowSize.width, is(lessThan(layoutSize.width + 10)));
    }

}
