View Javadoc

1   package com.atlassian.pageobjects.elements.test.query;
2   
3   import com.atlassian.pageobjects.elements.mock.MockTimedQuery;
4   import com.atlassian.pageobjects.elements.mock.clock.StrictMockClock;
5   import com.atlassian.pageobjects.elements.query.*;
6   import com.atlassian.pageobjects.elements.query.util.Clock;
7   import com.atlassian.pageobjects.elements.query.util.LinearBackoff;
8   import com.atlassian.pageobjects.elements.util.IncrementalClock;
9   import org.junit.Before;
10  import org.junit.Test;
11  import org.openqa.selenium.StaleElementReferenceException;
12  
13  import java.util.ArrayList;
14  import java.util.Arrays;
15  import java.util.List;
16  import java.util.Random;
17  import java.util.concurrent.Callable;
18  import java.util.concurrent.TimeUnit;
19  import java.util.concurrent.atomic.AtomicReference;
20  
21  import static com.atlassian.pageobjects.elements.query.Poller.by;
22  import static com.atlassian.pageobjects.elements.query.Poller.byDefaultTimeout;
23  import static com.atlassian.pageobjects.elements.query.Poller.now;
24  import static org.hamcrest.Matchers.*;
25  import static org.junit.Assert.*;
26  import static org.mockito.Mockito.mock;
27  import static org.mockito.Mockito.when;
28  
29  /**
30   * Test case for {@link com.atlassian.pageobjects.elements.query.Poll} with various timed queries.
31   * This is the only test that should have a dependancy on the clock. All other would be time dependant tests
32   * should mock this class instead.
33   */
34  
35  public class TestPollerQueryWaits
36  {
37      private static final int DEFAULT_INTERVAL = 100;
38  
39      private static final Callable<String> query = new Callable<String>()
40      {
41          @Override
42          public String call() throws Exception
43          {
44              return "Hello world";
45          }
46      };
47  
48      Poll<String> p;
49      IncrementalClock c;
50  
51      @Before
52      public void setup() {
53          c = new IncrementalClock(50);
54          p = createDefaultPoller(query, c);
55      }
56  
57      public Poll<String> createDefaultPoller(Callable<String> query, Clock c) {
58          return new Poll(query, c)
59                  .every(50, TimeUnit.MILLISECONDS)
60                  .until(equalTo("Hello world"))
61                  .withTimeout(10000, TimeUnit.MILLISECONDS)
62                  .onFailure(ExpirationHandler.RETURN_NULL);
63      }
64  
65      @Test
66      public void pollShouldPassForPassingQuery()
67      {
68          String result = p.call();
69          assertNotNull(result);
70          assertThat(c.currentTime(), lessThan(10000L));
71      }
72  
73      @Test
74      public void pollShouldFailForFailingQuery_afterAFashion()
75      {
76          String result = p.until(equalTo("wrong answer!")).call();
77          assertNull(result);
78          assertThat(c.currentTime(), greaterThan(10000L));
79      }
80  
81      @Test
82      public void pollShouldSucceed_WhenQueryValueBecomesCorrect()
83      {
84          /**
85           * At a random time before the timeout, change the value to the one expected (i.e. the query passes)
86           */
87          Random rand = new Random();
88          final long x = (long)rand.nextInt(9000);
89          final AtomicReference<String> str = new AtomicReference<String>("Hello world!");
90  
91          Callable<String> query = new Callable<String>()
92          {
93              @Override
94              public String call() throws Exception
95              {
96                  return str.get();
97              }
98          };
99  
100         c = new IncrementalClock(50) {
101             public long currentTime() {
102                 long t = super.currentTime();
103                 if(t > x) {
104                     str.set("right answer!");
105                 }
106                 return t;
107             }
108         };
109         p = createDefaultPoller(query, c);
110 
111         String result = p.until(equalTo("right answer!")).call();
112         assertNotNull(result);
113         assertThat(c.currentTime(), lessThanOrEqualTo(10000L));
114     }
115 
116     @Test
117     public void pollShouldSucceed_WhenQueryValueBecomesCorrect_evenAtTheLastMilliSec()
118     {
119         /**
120          * Right before the timeout, change the value to the one expected (i.e. the query passes)
121          */
122         final long x = 10000;
123         final AtomicReference<String> str = new AtomicReference<String>("Hello world!");
124 
125         Callable<String> query = new Callable<String>()
126         {
127             @Override
128             public String call() throws Exception
129             {
130                 return str.get();
131             }
132         };
133 
134         c = new IncrementalClock(50) {
135             public long currentTime() {
136                 long t = super.currentTime();
137                 if(t > x) {
138                     str.set("right answer!");
139                 }
140                 return t;
141             }
142         };
143         p = createDefaultPoller(query, c);
144 
145         String result = p.until(equalTo("right answer!")).call();
146         assertNotNull(result);
147         assertThat(c.currentTime(), greaterThanOrEqualTo(10000L));
148     }
149 
150 }