1 /* 2 * Copyright (c) 2007, 2017 Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package org.jemmy.timing; 26 27 import java.util.HashMap; 28 import java.util.LinkedList; 29 import java.util.TreeMap; 30 import org.jemmy.JemmyException; 31 import org.jemmy.action.Action; 32 import org.jemmy.action.GetAction; 33 import org.jemmy.timing.TimedCriteria; 34 import org.jemmy.control.Wrap; 35 import org.jemmy.env.Environment; 36 37 /** 38 * 39 * @author shura 40 */ 41 public class Timeline<T> { 42 43 private LinkedList<TimedAction> list = new LinkedList<TimedAction>(); 44 private Environment env; 45 private T control; 46 private long startTime = 0; 47 private long tic = 10; 48 49 public Timeline(long startTime, Environment env, T control) { 50 this.startTime = startTime; 51 this.env = env; 52 this.control = control; 53 } 54 55 public Timeline(long startTime, Wrap<? extends T> wrap) { 56 this(startTime, wrap.getEnvironment(), wrap.getControl()); 57 } 58 59 public Timeline(Environment env, T control) { 60 this(0, env, control); 61 } 62 63 public Timeline(Wrap<T> wrap) { 64 this(wrap.getEnvironment(), wrap.getControl()); 65 } 66 67 public synchronized final Timeline<T> schedule(long when, TimedCriteria<T> frame) { 68 if (when >= 0) { 69 if (list.size() > 0) { 70 for (int i = 0; i < list.size(); i++) { 71 if (list.get(i).when >= when) { 72 list.add(i, new TimedAction(frame, when)); 73 return this; 74 } 75 } 76 list.add(new TimedAction(frame, when)); 77 } else { 78 list.add(new TimedAction(frame, when)); 79 } 80 } 81 return this; 82 } 83 84 public synchronized final Timeline<T> schedule(long start, long until, long delta, TimedCriteria<T> frame) { 85 long when = start; 86 do { 87 schedule(when, frame); 88 } while ((when += delta) <= until); 89 return this; 90 } 91 92 synchronized TimedAction next(long until) { 93 if (list.get(0).when <= until) { 94 return remove(); 95 } 96 return null; 97 } 98 99 synchronized TimedAction remove() { 100 return list.removeFirst(); 101 } 102 103 public void start() { 104 if (startTime > 0) { 105 long before = System.currentTimeMillis(); 106 while (System.currentTimeMillis() < startTime + before) { 107 try { 108 Thread.sleep(tic); 109 } catch (InterruptedException ex) { 110 throw new JemmyException("Sleep interrupted.", ex); 111 } 112 } 113 } 114 long start = System.currentTimeMillis(); 115 while (hasMore()) { 116 long now = System.currentTimeMillis() - start; 117 TimedAction next = next(now); 118 if (next != null) { 119 env.getExecutor().execute(env, 120 true, next, Long.valueOf(now)); 121 if (!next.getResult()) { 122 throw new JemmyException("Check failed on " + now + ":", next.criteria); 123 } 124 } 125 try { 126 Thread.sleep(tic); 127 } catch (InterruptedException ex) { 128 throw new JemmyException("Sleep interrupted.", ex); 129 } 130 } 131 } 132 133 public boolean hasMore() { 134 return list.size() > 0; 135 } 136 137 private class TimedAction extends GetAction<Boolean> { 138 139 long when; 140 TimedCriteria<T> criteria; 141 142 public TimedAction(TimedCriteria<T> criteria, long when) { 143 this.criteria = criteria; 144 this.when = when; 145 } 146 147 @Override 148 public void run(Object... parameters) { 149 setResult(criteria.check(control, (Long) parameters[0])); 150 } 151 } 152 }