1 package com.atlassian.pageobjects.elements.test.query;
2
3 import com.atlassian.pageobjects.elements.mock.MockCondition;
4 import com.atlassian.pageobjects.elements.mock.clock.QueryClocks;
5 import com.atlassian.pageobjects.elements.query.Poller;
6 import com.atlassian.pageobjects.elements.query.TimedCondition;
7 import org.junit.Test;
8
9 import static com.atlassian.pageobjects.elements.query.Poller.by;
10 import static com.atlassian.pageobjects.elements.query.Poller.byDefaultTimeout;
11 import static com.atlassian.pageobjects.elements.query.Poller.now;
12 import static junit.framework.Assert.assertEquals;
13 import static org.hamcrest.Matchers.is;
14
15
16
17
18
19 public class TestPollerConditionWaits
20 {
21 @Test
22 public void shouldPassForPassingCondition()
23 {
24 Poller.waitUntil(passingCondition(), is(true), byDefaultTimeout());
25 Poller.waitUntil(passingCondition(), is(true), by(1000));
26 Poller.waitUntil(passingCondition(), is(true), now());
27 }
28
29 @Test
30 public void shouldCallTestedConditionUntilItReturnsTrue()
31 {
32 MockCondition tested = new MockCondition(false, false, false, true)
33 .withClock(QueryClocks.forInterval(MockCondition.DEFAULT_INTERVAL));
34 Poller.waitUntil(tested, is(true), by(1000));
35 assertEquals(4, tested.callCount());
36 }
37
38 @Test
39 public void shouldCallTestedConditionUntilItReturnsFalse()
40 {
41 MockCondition tested = new MockCondition(true, true, true, false)
42 .withClock(QueryClocks.forInterval(MockCondition.DEFAULT_INTERVAL));
43 Poller.waitUntil(tested, is(false), by(1000));
44 assertEquals(4, tested.callCount());
45 }
46
47
48 @Test
49 public void shouldProduceMeaningfulErrorMessage()
50 {
51 try
52 {
53 Poller.waitUntil(failingCondition(), is(true), byDefaultTimeout());
54 throw new IllegalStateException("Should fail");
55 }
56 catch(AssertionError e)
57 {
58 assertEquals("Query <Failing Condition>\n"
59 + "Expected: is <true> by 500ms (default timeout)\n"
60 + "Got (last value): <false>", e.getMessage());
61 }
62 }
63
64 @Test
65 public void shouldProduceMeaningfulErrorMessageForNegatedAssertion()
66 {
67 try
68 {
69 Poller.waitUntil(passingCondition(), is(false), byDefaultTimeout());
70 throw new IllegalStateException("Should fail");
71 }
72 catch(AssertionError e)
73 {
74 assertEquals("Query <Passing Condition>\n"
75 + "Expected: is <false> by 500ms (default timeout)\n"
76 + "Got (last value): <true>", e.getMessage());
77 }
78 }
79
80
81 private MockCondition passingCondition()
82 {
83 return new MockCondition(true)
84 {
85 public String toString()
86 {
87 return "Passing Condition";
88 }
89 };
90 }
91
92 private TimedCondition failingCondition()
93 {
94 return new MockCondition(false)
95 {
96 public String toString()
97 {
98 return "Failing Condition";
99 }
100 };
101 }
102
103 }