1 /*
2 * *************************************************************************************************************************************************************
3 *
4 * SteelBlue: DCI User Interfaces
5 * http://tidalwave.it/projects/steelblue
6 *
7 * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8 *
9 * *************************************************************************************************************************************************************
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18 *
19 * *************************************************************************************************************************************************************
20 *
21 * git clone https://bitbucket.org/tidalwave/steelblue-src
22 * git clone https://github.com/tidalwave-it/steelblue-src
23 *
24 * *************************************************************************************************************************************************************
25 */
26 package it.tidalwave.ui.core.test;
27
28 import jakarta.annotation.Nonnull;
29 import java.util.Arrays;
30 import java.util.function.Consumer;
31 import it.tidalwave.ui.core.UserNotification;
32 import it.tidalwave.ui.core.UserNotificationWithFeedback;
33 import lombok.NoArgsConstructor;
34 import lombok.extern.slf4j.Slf4j;
35 import org.mockito.invocation.InvocationOnMock;
36 import org.mockito.stubbing.Answer;
37 import static lombok.AccessLevel.PRIVATE;
38
39 /***************************************************************************************************************************************************************
40 *
41 * A factory class to create testing matchers.
42 *
43 * @since 2.0-ALPHA-2
44 * @author Fabrizio Giudici
45 *
46 **************************************************************************************************************************************************************/
47 @NoArgsConstructor(access = PRIVATE) @Slf4j
48 public final class UserNotificationWithFeedbackTestHelper
49 {
50 /***********************************************************************************************************************************************************
51 * Creates a {@link UserNotification} matcher for the given caption and text.
52 * @param captionRegex the regular expression that should match the caption
53 * @param textRegex the regular expression that should match the text
54 * @return the matcher
55 **********************************************************************************************************************************************************/
56 @Nonnull
57 public static UserNotificationMatcher notification (@Nonnull final String captionRegex, @Nonnull final String textRegex)
58 {
59 return new UserNotificationMatcher(captionRegex, textRegex);
60 }
61
62 /***********************************************************************************************************************************************************
63 * Creates a {@link UserNotificationWithFeedback} matcher for the given caption and text.
64 * @param captionRegex the regular expression that should match the caption
65 * @param textRegex the regular expression that should match the text
66 * @return the matcher
67 **********************************************************************************************************************************************************/
68 @Nonnull
69 public static UserNotificationWithFeedbackMatcher notificationWithFeedback (@Nonnull final String captionRegex, @Nonnull final String textRegex)
70 {
71 return new UserNotificationWithFeedbackMatcher(captionRegex, textRegex);
72 }
73
74 /***********************************************************************************************************************************************************
75 * An {@link Answer} that triggers a confirmation to a {@link UserNotificationWithFeedback}.
76 * @return the {@code Answer}
77 **********************************************************************************************************************************************************/
78 public static Answer<Void> confirm()
79 {
80 return CONFIRM;
81 }
82
83 /***********************************************************************************************************************************************************
84 * An {@link Answer} that does a thing and then triggers a confirmation to a {@link UserNotificationWithFeedback}.
85 * @param thingToDo the thing to do
86 * @return the {@code Answer}
87 * @since 3.2-ALPHA-24
88 **********************************************************************************************************************************************************/
89 public static Answer<Void> doAndConfirm (@Nonnull final Consumer<? super InvocationOnMock> thingToDo)
90 {
91 return invocationOnMock ->
92 {
93 thingToDo.accept(invocationOnMock);
94 return CONFIRM.answer(invocationOnMock);
95 };
96 }
97
98 /***********************************************************************************************************************************************************
99 * An {@link Answer} that triggers a cancellation to a {@link UserNotificationWithFeedback}.
100 * @return the {@code Answer}
101 **********************************************************************************************************************************************************/
102 public static Answer<Void> cancel()
103 {
104 return CANCEL;
105 }
106
107 /***********************************************************************************************************************************************************
108 *
109 **********************************************************************************************************************************************************/
110 private static final Answer<Void> CONFIRM = invocation ->
111 {
112 final var notification = findNotification(invocation);
113 log.info(">>>> mock UI confirming {}...", notification);
114 notification.confirm();
115 return null;
116 };
117
118 /***********************************************************************************************************************************************************
119 *
120 **********************************************************************************************************************************************************/
121 private static final Answer<Void> CANCEL = invocation ->
122 {
123 final var notification = findNotification(invocation);
124 log.info(">>>> mock UI cancelling {}...", notification);
125 notification.cancel();
126 return null;
127 };
128
129 /***********************************************************************************************************************************************************
130 *
131 **********************************************************************************************************************************************************/
132 @Nonnull
133 private static UserNotificationWithFeedback findNotification (@Nonnull final InvocationOnMock invocation)
134 {
135 return (UserNotificationWithFeedback)Arrays.stream(invocation.getArguments())
136 .filter(a -> a instanceof UserNotificationWithFeedback)
137 .findFirst()
138 .orElseThrow();
139 }
140 }