/*
 * JBoss, Home of Professional Open Source
 *
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * 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.keycloak.testsuite.console.realm;

import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.keycloak.testsuite.console.page.realm.TokenSettings;

import org.jboss.arquillian.graphene.page.Page;
import static org.keycloak.testsuite.util.URLAssert.assertCurrentUrlStartsWithLoginUrlOf;
import static org.keycloak.testsuite.util.URLAssert.assertCurrentUrlStartsWith;

/**
 *
 * @author Petr Mensik
 */
public class TokensTest extends AbstractRealmTest {

    @Page
    private TokenSettings tokenSettingsPage;

    private static final int TIMEOUT = 4;
    private static final TimeUnit TIME_UNIT = TimeUnit.SECONDS;

    @Before
    public void beforeTokensTest() {
//        configure().realmSettings();
//        tabs().tokens();
        tokenSettingsPage.navigateTo();
    }

    @Test
    public void testTimeoutForRealmSession() throws InterruptedException {
        tokenSettingsPage.form().setSessionTimeout(TIMEOUT, TIME_UNIT);
        tokenSettingsPage.form().save();

        loginToTestRealmConsoleAs(testUser);
        TIME_UNIT.sleep(TIMEOUT + 2);

        driver.navigate().refresh();

        log.debug(driver.getCurrentUrl());
        assertCurrentUrlStartsWithLoginUrlOf(testRealmPage);
    }

    @Test
    public void testLifespanOfRealmSession() throws InterruptedException {
        tokenSettingsPage.form().setSessionTimeoutLifespan(TIMEOUT, TIME_UNIT);
        tokenSettingsPage.form().save();

        loginToTestRealmConsoleAs(testUser);
        TIME_UNIT.sleep(TIMEOUT / 2);

        driver.navigate().refresh();
        assertCurrentUrlStartsWith(testRealmAdminConsolePage); // assert still logged in (within lifespan)

        TIME_UNIT.sleep(TIMEOUT / 2 + 2);
        driver.navigate().refresh();

        log.debug(driver.getCurrentUrl());
        assertCurrentUrlStartsWithLoginUrlOf(testRealmPage); // assert logged out (lifespan exceeded)
    }

}

@@ -1287,9 +1287,9 @@
 T = 
-4
+1
 ;%0A  
@@ -1345,14 +1345,14 @@
 nit.
-SECOND
+MINUTE
 S;%0A%0A
@@ -1761,39 +1761,38 @@
 r);%0A        
-TIME_UNIT.sleep
+waitForTimeout
 (TIMEOUT + 2
@@ -2200,39 +2200,38 @@
 r);%0A        
-TIME_UNIT.sleep
+waitForTimeout
 (TIMEOUT / 2
@@ -2391,23 +2391,22 @@
     
-TIME_UNIT.sleep
+waitForTimeout
 (TIM
@@ -2493,32 +2493,32 @@
 tCurrentUrl());%0A
-
         assertCu
@@ -2610,10 +2610,232 @@
 %0A    %7D%0A%0A
+    private void waitForTimeout (int timeout) throws InterruptedException %7B%0A        log.info(%22Wait for timeout: %22 + timeout + %22 %22 + TIME_UNIT);%0A        TIME_UNIT.sleep(timeout);%0A        log.info(%22Timeout reached%22);%0A    %7D%0A%0A
 %7D%0A

Fix TokensTest (cherry picked from commit 32e6bb0)