Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit c00cf05

Browse files
committed
Changed Marker from a static utility class to a bean
1 parent 6f3224b commit c00cf05

File tree

6 files changed

+161
-32
lines changed

6 files changed

+161
-32
lines changed

webtester-core/src/main/java/info/novatec/testit/webtester/css/StyleChangerImpl.java renamed to webtester-core/src/main/java/info/novatec/testit/webtester/css/DefaultStyleChanger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import info.novatec.testit.webtester.pagefragments.PageFragment;
1111

1212
@Slf4j
13-
public class StyleChangerImpl implements StyleChanger {
13+
public class DefaultStyleChanger implements StyleChanger {
1414

1515
@Override
1616
public boolean changeStyleInformation(PageFragment pageFragment, String property, String value) {

webtester-core/src/main/java/info/novatec/testit/webtester/internal/ActionTemplate.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.function.Function;
77

88
import info.novatec.testit.webtester.browser.Browser;
9+
import info.novatec.testit.webtester.css.DefaultStyleChanger;
910
import info.novatec.testit.webtester.events.Event;
1011
import info.novatec.testit.webtester.events.EventSystem;
1112
import info.novatec.testit.webtester.markings.Marker;
@@ -100,8 +101,11 @@ protected EventSystem getEventSystem() {
100101

101102
public static class PageFragmentAction<T extends PageFragment> extends Action<T, PageFragmentAction<T>> {
102103

104+
private Marker marker;
105+
103106
public PageFragmentAction(T subject) {
104107
super(subject, subject.getBrowser());
108+
marker = new Marker(new DefaultStyleChanger());
105109
}
106110

107111
public PageFragmentAction<T> fireEvent(Function<T, Event> function) {
@@ -113,21 +117,24 @@ public PageFragmentAction<T> fireEvent(Function<T, Event> function) {
113117
}
114118

115119
public PageFragmentAction<T> markAsUsed() {
116-
Marker.markAsUsed(getSubject());
120+
marker.markAsUsed(getSubject());
117121
return this;
118122
}
119123

120124
public PageFragmentAction<T> markAsRead() {
121-
Marker.markAsRead(getSubject());
125+
marker.markAsRead(getSubject());
122126
return this;
123127
}
124128

125129
}
126130

127131
public static class PageFragmentsAction<T extends PageFragment> extends BiAction<T, PageFragmentsAction<T>> {
128132

133+
private Marker marker;
134+
129135
public PageFragmentsAction(T subject1, T subject2) {
130136
super(subject1, subject2, subject1.getBrowser());
137+
marker = new Marker(new DefaultStyleChanger());
131138
}
132139

133140
public PageFragmentsAction<T> fireEvent(BiFunction<T, T, Event> function) {
@@ -139,14 +146,14 @@ public PageFragmentsAction<T> fireEvent(BiFunction<T, T, Event> function) {
139146
}
140147

141148
public PageFragmentsAction<T> markAsUsed() {
142-
Marker.markAsUsed(getSubject1());
143-
Marker.markAsUsed(getSubject2());
149+
marker.markAsUsed(getSubject1());
150+
marker.markAsUsed(getSubject2());
144151
return this;
145152
}
146153

147154
public PageFragmentsAction<T> markAsRead() {
148-
Marker.markAsRead(getSubject1());
149-
Marker.markAsRead(getSubject2());
155+
marker.markAsRead(getSubject1());
156+
marker.markAsRead(getSubject2());
150157
return this;
151158
}
152159

webtester-core/src/main/java/info/novatec/testit/webtester/internal/proxies/befores/MarkOperation.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import lombok.extern.slf4j.Slf4j;
66

7+
import info.novatec.testit.webtester.css.DefaultStyleChanger;
78
import info.novatec.testit.webtester.pagefragments.annotations.As;
89
import info.novatec.testit.webtester.pagefragments.PageFragment;
910
import info.novatec.testit.webtester.pagefragments.annotations.Mark;
@@ -13,6 +14,12 @@
1314
@Slf4j
1415
public class MarkOperation implements BeforeOperation {
1516

17+
private Marker marker;
18+
19+
public MarkOperation() {
20+
this.marker = new Marker(new DefaultStyleChanger());
21+
}
22+
1623
@Override
1724
public boolean shouldBeInvokedFor(Method method) {
1825
return method.isAnnotationPresent(Mark.class);
@@ -26,9 +33,9 @@ public void invoke(Object proxy, Method method, Object[] args) {
2633

2734
private void markPageFragment(PageFragment proxy, As as) {
2835
if (As.READ == as) {
29-
Marker.markAsRead(proxy);
36+
marker.markAsRead(proxy);
3037
} else if (As.USED == as) {
31-
Marker.markAsUsed(proxy);
38+
marker.markAsUsed(proxy);
3239
}
3340
}
3441

webtester-core/src/main/java/info/novatec/testit/webtester/markings/Marker.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import info.novatec.testit.webtester.config.Configuration;
99
import info.novatec.testit.webtester.css.CssProperties;
1010
import info.novatec.testit.webtester.css.StyleChanger;
11-
import info.novatec.testit.webtester.css.StyleChangerImpl;
1211
import info.novatec.testit.webtester.pagefragments.PageFragment;
1312

1413

@@ -21,49 +20,53 @@
2120
* @see CssProperties
2221
* @since 2.0
2322
*/
24-
public final class Marker {
23+
public class Marker {
2524

26-
private static StyleChanger styleChanger = new StyleChangerImpl();
25+
private StyleChanger styleChanger;
26+
27+
public Marker(StyleChanger styleChanger) {
28+
this.styleChanger = styleChanger;
29+
}
2730

2831
/**
29-
* Marks the given {@link PageFragment page fragment} as 'used' using the configured colors from the page object's
32+
* Marks the given {@link PageFragment page fragment} as 'read' using the configured colors from the page object's
3033
* browser's {@link Configuration configuration}.
3134
*
32-
* @param pageFragment the page fragment to mark.
35+
* @param fragment the page fragment to mark.
3336
* @see StyleChanger
3437
* @see PageFragment
3538
* @see CssProperties
3639
* @since 2.0
3740
*/
38-
public static void markAsUsed(PageFragment pageFragment) {
39-
Configuration configuration = pageFragment.getBrowser().configuration();
41+
public void markAsRead(PageFragment fragment) {
42+
Configuration configuration = fragment.getBrowser().configuration();
4043
if (configuration.isMarkingsEnabled()) {
41-
Color backgroundColor = configuration.getMarkingsColorUsedBackground();
42-
Color outlineColor = configuration.getMarkingsColorUsedOutline();
43-
markElement(pageFragment, backgroundColor, outlineColor);
44+
Color backgroundColor = configuration.getMarkingsColorReadBackground();
45+
Color outlineColor = configuration.getMarkingsColorReadOutline();
46+
markElement(fragment, backgroundColor, outlineColor);
4447
}
4548
}
4649

4750
/**
48-
* Marks the given {@link PageFragment page fragment} as 'read' using the configured colors from the page object's
51+
* Marks the given {@link PageFragment page fragment} as 'used' using the configured colors from the page object's
4952
* browser's {@link Configuration configuration}.
5053
*
51-
* @param fragment the page fragment to mark.
54+
* @param pageFragment the page fragment to mark.
5255
* @see StyleChanger
5356
* @see PageFragment
5457
* @see CssProperties
5558
* @since 2.0
5659
*/
57-
public static void markAsRead(PageFragment fragment) {
58-
Configuration configuration = fragment.getBrowser().configuration();
60+
public void markAsUsed(PageFragment pageFragment) {
61+
Configuration configuration = pageFragment.getBrowser().configuration();
5962
if (configuration.isMarkingsEnabled()) {
60-
Color backgroundColor = configuration.getMarkingsColorReadBackground();
61-
Color outlineColor = configuration.getMarkingsColorReadOutline();
62-
markElement(fragment, backgroundColor, outlineColor);
63+
Color backgroundColor = configuration.getMarkingsColorUsedBackground();
64+
Color outlineColor = configuration.getMarkingsColorUsedOutline();
65+
markElement(pageFragment, backgroundColor, outlineColor);
6366
}
6467
}
6568

66-
private static void markElement(PageFragment fragment, Color backgroundColor, Color outlineColor) {
69+
private void markElement(PageFragment fragment, Color backgroundColor, Color outlineColor) {
6770

6871
Map<String, String> cssStyleAttributes = new HashMap<>();
6972
cssStyleAttributes.put(CssProperties.OUTLINE_STYLE, "solid");
@@ -75,7 +78,4 @@ private static void markElement(PageFragment fragment, Color backgroundColor, Co
7578

7679
}
7780

78-
private Marker() {
79-
}
80-
8181
}

webtester-core/src/test/java/info/novatec/testit/webtester/css/StyleChangerImplTest.java renamed to webtester-core/src/test/java/info/novatec/testit/webtester/css/DefaultStyleChangerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
@RunWith(MockitoJUnitRunner.class)
27-
public class StyleChangerImplTest {
27+
public class DefaultStyleChangerTest {
2828

2929
@Mock
3030
PageFragment fragment;
@@ -34,7 +34,7 @@ public class StyleChangerImplTest {
3434
JavaScriptExecutor javaScript;
3535

3636
@InjectMocks
37-
StyleChangerImpl cut;
37+
DefaultStyleChanger cut;
3838

3939
@Before
4040
public void init() {
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package info.novatec.testit.webtester.markings;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.mockito.Matchers.same;
5+
import static org.mockito.Mockito.doReturn;
6+
import static org.mockito.Mockito.verify;
7+
import static org.mockito.Mockito.verifyZeroInteractions;
8+
import static org.mockito.Mockito.when;
9+
10+
import java.util.Map;
11+
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
import org.junit.experimental.runners.Enclosed;
15+
import org.junit.runner.RunWith;
16+
import org.mockito.Answers;
17+
import org.mockito.ArgumentCaptor;
18+
import org.mockito.Captor;
19+
import org.mockito.InjectMocks;
20+
import org.mockito.Mock;
21+
import org.mockito.runners.MockitoJUnitRunner;
22+
import org.openqa.selenium.support.Color;
23+
24+
import info.novatec.testit.webtester.config.Configuration;
25+
import info.novatec.testit.webtester.css.CssProperties;
26+
import info.novatec.testit.webtester.css.StyleChanger;
27+
import info.novatec.testit.webtester.pagefragments.PageFragment;
28+
29+
30+
@RunWith(Enclosed.class)
31+
public class MarkerTest {
32+
33+
@RunWith(MockitoJUnitRunner.class)
34+
private static abstract class AbstractMarkerTest {
35+
36+
@Mock
37+
StyleChanger styleChanger;
38+
@InjectMocks
39+
Marker cut;
40+
41+
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
42+
PageFragment fragment;
43+
@Mock
44+
Configuration configuration;
45+
@Captor
46+
ArgumentCaptor<Map<String, String>> cssCaptor;
47+
48+
@Before
49+
public void configuration() {
50+
when(fragment.getBrowser().configuration()).thenReturn(configuration);
51+
}
52+
53+
}
54+
55+
public static class MarkAsRead extends AbstractMarkerTest {
56+
57+
@Test
58+
public void styleIsNotChangedIfMarkingsAreDeactivated() {
59+
doReturn(false).when(configuration).isMarkingsEnabled();
60+
cut.markAsRead(fragment);
61+
verifyZeroInteractions(styleChanger);
62+
}
63+
64+
@Test
65+
public void styleIsChangedIfMarkingsAreActivated() {
66+
67+
doReturn(Color.fromString("#aaaaaa")).when(configuration).getMarkingsColorReadBackground();
68+
doReturn(Color.fromString("#bbbbbb")).when(configuration).getMarkingsColorReadOutline();
69+
doReturn(true).when(configuration).isMarkingsEnabled();
70+
71+
cut.markAsRead(fragment);
72+
73+
verify(styleChanger).changeStyleInformation(same(fragment), cssCaptor.capture());
74+
Map<String, String> properties = cssCaptor.getValue();
75+
76+
assertThat(properties).containsEntry(CssProperties.OUTLINE_STYLE, "solid");
77+
assertThat(properties).containsEntry(CssProperties.OUTLINE_WIDTH, "2px");
78+
assertThat(properties).containsEntry(CssProperties.OUTLINE_COLOR, "#bbbbbb");
79+
assertThat(properties).containsEntry(CssProperties.BACKGROUND_COLOR, "#aaaaaa");
80+
81+
}
82+
83+
}
84+
85+
public static class MarkAsUsed extends AbstractMarkerTest {
86+
87+
@Test
88+
public void styleIsNotChangedIfMarkingsAreDeactivated() {
89+
doReturn(false).when(configuration).isMarkingsEnabled();
90+
cut.markAsUsed(fragment);
91+
verifyZeroInteractions(styleChanger);
92+
}
93+
94+
@Test
95+
public void styleIsChangedIfMarkingsAreActivated() {
96+
97+
doReturn(Color.fromString("#cccccc")).when(configuration).getMarkingsColorUsedBackground();
98+
doReturn(Color.fromString("#dddddd")).when(configuration).getMarkingsColorUsedOutline();
99+
doReturn(true).when(configuration).isMarkingsEnabled();
100+
101+
cut.markAsUsed(fragment);
102+
103+
verify(styleChanger).changeStyleInformation(same(fragment), cssCaptor.capture());
104+
Map<String, String> properties = cssCaptor.getValue();
105+
106+
assertThat(properties).containsEntry(CssProperties.OUTLINE_STYLE, "solid");
107+
assertThat(properties).containsEntry(CssProperties.OUTLINE_WIDTH, "2px");
108+
assertThat(properties).containsEntry(CssProperties.OUTLINE_COLOR, "#dddddd");
109+
assertThat(properties).containsEntry(CssProperties.BACKGROUND_COLOR, "#cccccc");
110+
111+
}
112+
113+
}
114+
115+
}

0 commit comments

Comments
 (0)