summaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/urifragments/FragmentHandlingAndAsynchUIUpdateTest.java
blob: 01ea890cec4b94418a57917a3a65cd95ac1f50be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * 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.urifragments;

import static com.vaadin.tests.urifragments.FragmentHandlingAndAsynchUIUpdate.FRAG_NAME_TPL;
import static com.vaadin.tests.urifragments.FragmentHandlingAndAsynchUIUpdate.START_FRAG_ID;

import org.junit.Test;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;

import com.vaadin.testbench.By;
import com.vaadin.tests.tb3.MultiBrowserTest;

/**
 * Back and Forward buttons in browser should work correctly during UI update
 * 
 * @author Vaadin Ltd
 */
public class FragmentHandlingAndAsynchUIUpdateTest extends MultiBrowserTest {

    /**
     * The case when we successively set 10 fragments, go back 9 times and then
     * go forward 9 times
     */
    @Test
    public void testBackwardForwardHistoryWithWaitingForSettingFrag()
            throws Exception {
        openTestURL();

        for (int i = 0; i < 10; i++) {
            // here we wait for setting fragment in URI. If not to do it -
            // history will be "loss"
            getDriver().findElement(
                    By.id(FragmentHandlingAndAsynchUIUpdate.BUTTON_ID)).click();
            assertFragment(String.format(FRAG_NAME_TPL, START_FRAG_ID + i));
        }

        for (int i = 8; i >= 0; i--) {
            ((JavascriptExecutor) driver).executeScript("history.back()");
            assertFragment(String.format(FRAG_NAME_TPL, START_FRAG_ID + i));
        }

        for (int i = 1; i < 10; i++) {
            ((JavascriptExecutor) driver).executeScript("history.forward()");
            assertFragment(String.format(FRAG_NAME_TPL, START_FRAG_ID + i));
        }
    }

    /**
     * The case when it seems than history is loss
     */
    @Test
    public void testBackwardForwardHistoryWithoutWaitingForSettingFrag()
            throws Exception {
        openTestURL();

        // begin to wait for setting 3th fragment and then click backward
        // history and then wait for 9th fragment
        new Thread(new Runnable() {

            @Override
            public void run() {
                // wait for setting 3th fragment
                assertFragment(String.format(FRAG_NAME_TPL, START_FRAG_ID + 3));
                // go back one time after we wait for 3th fragment and then we
                // will see the "loss" of "forward history"
                ((JavascriptExecutor) driver).executeScript("history.back()");

                // now at some time new fragments will be set
                // wait for last fragment setting..
                // of course forward history is empty now..
                assertFragment(String.format(FRAG_NAME_TPL, START_FRAG_ID + 9));
            }

        }).start();

        // not wait for setting fragment in URI
        // (simulated situation when users clicks some times in row on
        // button)
        new Thread(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    getDriver().findElement(
                            By.id(FragmentHandlingAndAsynchUIUpdate.BUTTON_ID))
                            .click();
                }

            }

        }).start();

    }

    private void assertFragment(String fragment) {
        final String expectedText = fragment;

        waitUntil(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver input) {
                String currentURL = getDriver().getCurrentUrl();
                String currentURIFragment = "";
                if (currentURL.contains("#") && !currentURL.endsWith("#")) {
                    currentURIFragment = currentURL.split("#")[1];
                }
                return expectedText.equals(currentURIFragment);
            }
        }, 20);
    }

}