1 package com.atlassian.webdriver.rule;
2
3 import com.atlassian.webdriver.testing.annotation.WindowSize;
4 import com.atlassian.webdriver.testing.rule.WindowSizeRule;
5 import org.junit.Before;
6 import org.junit.Test;
7 import org.junit.runner.Description;
8 import org.junit.runner.RunWith;
9 import org.junit.runners.model.Statement;
10 import org.mockito.Mock;
11 import org.mockito.runners.MockitoJUnitRunner;
12 import org.openqa.selenium.Dimension;
13 import org.openqa.selenium.Point;
14 import org.openqa.selenium.WebDriver;
15
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.verifyNoMoreInteractions;
19 import static org.mockito.Mockito.when;
20
21
22
23
24
25
26 @RunWith(MockitoJUnitRunner.class)
27 public class TestWindowSizeRule
28 {
29
30 @Mock private Statement mockTest;
31 @Mock private WebDriver webDriver;
32 @Mock private WebDriver.Options wedDriverOptions;
33 @Mock private WebDriver.Window wedDriverWindow;
34 @Mock private WindowSize mockWindowSize;
35
36 @Before
37 public void stubWebDriverOptions()
38 {
39 when(webDriver.manage()).thenReturn(wedDriverOptions);
40 when(wedDriverOptions.window()).thenReturn(wedDriverWindow);
41 }
42
43 @Test
44 public void shouldMaximizeWindowIfAnnotationNotPresent()
45 {
46 final Description description = Description.createTestDescription(TestWindowSizeRule.class, "testMethod");
47 new SafeStatementInvoker(createRule().apply(mockTest, description)).invokeSafely();
48 verify(wedDriverWindow).maximize();
49 }
50
51 @Test
52 public void shouldMaximizeWindowIfMaximizeIsTrueOnAnnotation()
53 {
54 when(mockWindowSize.annotationType()).thenReturn((Class)WindowSize.class);
55 when(mockWindowSize.maximize()).thenReturn(true);
56 when(mockWindowSize.width()).thenReturn(1024);
57 when(mockWindowSize.height()).thenReturn(768);
58 final Description description = Description.createTestDescription(TestWindowSizeRule.class, "testMethod", mockWindowSize);
59 new SafeStatementInvoker(createRule().apply(mockTest, description)).invokeSafely();
60 verify(wedDriverWindow).maximize();
61 verifyNoMoreInteractions(wedDriverWindow);
62 }
63
64 @Test
65 public void shouldSetWindowSizeIfMaximizeIsFalseOnAnnotation()
66 {
67 when(mockWindowSize.annotationType()).thenReturn((Class)WindowSize.class);
68 when(mockWindowSize.maximize()).thenReturn(false);
69 when(mockWindowSize.width()).thenReturn(1024);
70 when(mockWindowSize.height()).thenReturn(768);
71 final Description description = Description.createTestDescription(TestWindowSizeRule.class, "testMethod", mockWindowSize);
72 new SafeStatementInvoker(createRule().apply(mockTest, description)).invokeSafely();
73 verify(wedDriverWindow).setPosition(new Point(0, 0));
74 verify(wedDriverWindow, times(2)).setSize(new Dimension(1024, 768));
75 verifyNoMoreInteractions(wedDriverWindow);
76 }
77
78 @Test
79 public void shouldSetWindowSizeIfMaximizeIsFalseOnClassLevelAnnotation()
80 {
81 final Description description = Description.createTestDescription(ClassAnnotatedWithWindowSize.class, "testMethod");
82 new SafeStatementInvoker(createRule().apply(mockTest, description)).invokeSafely();
83 verify(wedDriverWindow).setPosition(new Point(0, 0));
84 verify(wedDriverWindow , times(2)).setSize(new Dimension(1024, 768));
85 verifyNoMoreInteractions(wedDriverWindow);
86 }
87
88 @Test
89 public void shouldPreferMethodLevelAnnotationOverClassLevel()
90 {
91 when(mockWindowSize.annotationType()).thenReturn((Class)WindowSize.class);
92 when(mockWindowSize.maximize()).thenReturn(true);
93 final Description description = Description.createTestDescription(ClassAnnotatedWithWindowSize.class, "testMethod", mockWindowSize);
94 new SafeStatementInvoker(createRule().apply(mockTest, description)).invokeSafely();
95 verify(wedDriverWindow).maximize();
96 verifyNoMoreInteractions(wedDriverWindow);
97 }
98
99 @Test(expected = IllegalStateException.class)
100 public void shouldThrowExceptionIfHeightInvalid() throws Throwable
101 {
102 when(mockWindowSize.annotationType()).thenReturn((Class)WindowSize.class);
103 when(mockWindowSize.maximize()).thenReturn(false);
104 when(mockWindowSize.width()).thenReturn(1024);
105 when(mockWindowSize.height()).thenReturn(0);
106 final Description description = Description.createTestDescription(TestWindowSizeRule.class, "testMethod", mockWindowSize);
107 createRule().apply(mockTest, description).evaluate();
108 }
109
110
111 private WindowSizeRule createRule()
112 {
113 return new WindowSizeRule(webDriver);
114 }
115
116 @WindowSize(width = 1024, height = 768)
117 public static class ClassAnnotatedWithWindowSize
118 {
119
120 }
121
122 }