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.tester;
018
019import java.util.List;
020
021import org.apache.wicket.Page;
022import org.apache.wicket.extensions.ajax.markup.html.AjaxLazyLoadPanel;
023import org.apache.wicket.extensions.ajax.markup.html.AjaxLazyLoadPanel.AjaxLazyLoadTimer;
024import org.apache.wicket.util.tester.BaseWicketTester;
025import java.time.Duration;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029/**
030 * Class to help test {@link AjaxLazyLoadPanel}
031 * 
032 * @author Antony Stubbs
033 */
034public class AjaxLazyLoadPanelTester
035{
036        private static final Logger logger = LoggerFactory.getLogger(AjaxLazyLoadPanelTester.class);
037
038        /**
039         * Triggers loading of all {@link AjaxLazyLoadPanel}'s content in the last rendered page.
040         * 
041         * @param wt
042         *            the tester
043         */
044        public static void executeAjaxLazyLoadPanel(final BaseWicketTester wt)
045        {
046                executeAjaxLazyLoadPanel(wt, wt.getLastRenderedPage());
047        }
048
049        /**
050         * Triggers loading of all {@link AjaxLazyLoadPanel}'s content in a page.
051         * 
052         * @param wt
053         *            the tester
054         * @param page
055         *            contains the {@link AjaxLazyLoadPanel}s to trigger
056         */
057        public static void executeAjaxLazyLoadPanel(final BaseWicketTester wt, final Page page)
058        {
059                // get the AbstractAjaxBehaviour which is responsible for
060                // getting the contents of the lazy panel
061                List<AjaxLazyLoadTimer> behaviors = page.getBehaviors(AjaxLazyLoadTimer.class);
062                if (behaviors.size() == 0)
063                {
064                        logger.warn("No timer behavior for AjaxLazyLoadPanel found. A curious situation...");
065                        return;
066                }
067                else if (behaviors.size() > 1)
068                {
069                        logger.warn(
070                                "Multiple timer behavior for AjaxLazyLoadPanel found. A curious situation...");
071                }
072
073                wt.executeBehavior(behaviors.get(0));
074        }
075
076        /**
077         * Triggers loading of a single {@link AjaxLazyLoadPanel}.
078         * 
079         * @param wt
080         *            the tester
081         * @param panel
082         *            the panel
083         * @return      update duration or {@code null} of already loadedO
084         */
085        public static Duration loadAjaxLazyLoadPanel(final BaseWicketTester wt, final AjaxLazyLoadPanel<?> panel)
086        {
087                if (panel.isLoaded())
088                {
089                        return null;
090                }
091                else
092                {
093                        return panel.getUpdateInterval();
094                }
095        }
096}