ru.stqa.selenium.decorated.DecoratedWindowTest.java Source code

Java tutorial

Introduction

Here is the source code for ru.stqa.selenium.decorated.DecoratedWindowTest.java

Source

/*
 * Copyright 2016 Alexei Barantsev
 *
 * 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 ru.stqa.selenium.decorated;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;

import java.util.function.Consumer;
import java.util.function.Function;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.*;

class DecoratedWindowTest {

    private static class Fixture {
        WebDriver mockedDriver;
        DecoratedWebDriver decoratedDriver;
        WebDriver.Window mocked;
        DecoratedWindow decorated;

        public Fixture() {
            mockedDriver = mock(WebDriver.class);
            decoratedDriver = new DecoratedWebDriver(mockedDriver);
            mocked = mock(WebDriver.Window.class);
            decorated = new DecoratedWindow(mocked, decoratedDriver);
        }
    }

    @Test
    void testConstructor() {
        Fixture fixture = new Fixture();
        assertThat(fixture.mocked, sameInstance(fixture.decorated.getOriginal()));
        assertThat(fixture.decoratedDriver, sameInstance(fixture.decorated.getTopmostDecorated()));
    }

    private void verifyFunction(Consumer<WebDriver.Window> f) {
        Fixture fixture = new Fixture();
        f.accept(fixture.decorated);
        f.accept(verify(fixture.mocked, times(1)));
        verifyNoMoreInteractions(fixture.mocked);
    }

    private <R> void verifyFunction(Function<WebDriver.Window, R> f, R result) {
        Fixture fixture = new Fixture();
        when(f.apply(fixture.mocked)).thenReturn(result);
        assertThat(f.apply(fixture.decorated), equalTo(result));
        f.apply(verify(fixture.mocked, times(1)));
        verifyNoMoreInteractions(fixture.mocked);
    }

    @Test
    void testSetSize() {
        verifyFunction($ -> $.setSize(new Dimension(100, 200)));
    }

    @Test
    void testSetPosition() {
        verifyFunction($ -> $.setPosition(new Point(10, 20)));
    }

    @Test
    void testGetSize() {
        verifyFunction(WebDriver.Window::getSize, new Dimension(100, 200));
    }

    @Test
    void testGetPosition() {
        verifyFunction(WebDriver.Window::getPosition, new Point(10, 20));
    }

    @Test
    void testMaximize() {
        verifyFunction(WebDriver.Window::maximize);
    }

    @Test
    void testFullscreen() {
        verifyFunction(WebDriver.Window::fullscreen);
    }

}