001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.extensions.ajax.markup.html;
018
019import java.util.List;
020
021import org.apache.wicket.Page;
022import org.apache.wicket.extensions.ajax.markup.html.AjaxLazyLoadPanel.AjaxLazyLoadTimer;
023import org.apache.wicket.util.tester.BaseWicketTester;
024import java.time.Duration;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028/**
029 * Class to help test {@link AjaxLazyLoadPanel}
030 * 
031 * @author Antony Stubbs
032 */
033public class AjaxLazyLoadPanelTester
034{
035        private static final Logger logger = LoggerFactory.getLogger(AjaxLazyLoadPanelTester.class);
036
037        /**
038         * Triggers loading of all {@link AjaxLazyLoadPanel}'s content in the last rendered page.
039         * 
040         * @param wt
041         *            the tester
042         */
043        public static void executeAjaxLazyLoadPanel(final BaseWicketTester wt)
044        {
045                executeAjaxLazyLoadPanel(wt, wt.getLastRenderedPage());
046        }
047
048        /**
049         * Triggers loading of all {@link AjaxLazyLoadPanel}'s content in a page.
050         * 
051         * @param wt
052         *            the tester
053         * @param page
054         *            contains the {@link AjaxLazyLoadPanel}s to trigger
055         */
056        public static void executeAjaxLazyLoadPanel(final BaseWicketTester wt, final Page page)
057        {
058                // get the AbstractAjaxBehaviour which is responsible for
059                // getting the contents of the lazy panel
060                List<AjaxLazyLoadTimer> behaviors = page.getBehaviors(AjaxLazyLoadTimer.class);
061                if (behaviors.size() == 0)
062                {
063                        logger.warn("No timer behavior for AjaxLazyLoadPanel found. A curious situation...");
064                        return;
065                }
066                else if (behaviors.size() > 1)
067                {
068                        logger.warn(
069                                "Multiple timer behavior for AjaxLazyLoadPanel found. A curious situation...");
070                }
071
072                wt.executeBehavior(behaviors.get(0));
073        }
074
075        /**
076         * Triggers loading of a single {@link AjaxLazyLoadPanel}.
077         * 
078         * @param wt
079         *            the tester
080         * @param panel
081         *            the panel
082         * @return      update duration or {@code null} of already loadedO
083         */
084        public static Duration loadAjaxLazyLoadPanel(final BaseWicketTester wt, final AjaxLazyLoadPanel<?> panel)
085        {
086                if (panel.isLoaded())
087                {
088                        return null;
089                }
090                else
091                {
092                        return panel.getUpdateInterval();
093                }
094        }
095}