View Javadoc
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.spi;
27  
28  import jakarta.annotation.Nonnull;
29  import java.util.ArrayList;
30  import java.util.Collection;
31  import java.util.List;
32  import java.util.function.Supplier;
33  import it.tidalwave.ui.core.role.UserAction;
34  import lombok.Getter;
35  import lombok.RequiredArgsConstructor;
36  import org.testng.annotations.Test;
37  import static it.tidalwave.ui.core.role.Displayable._Displayable_;
38  import static org.mockito.Mockito.*;
39  import static org.hamcrest.CoreMatchers.is;
40  import static org.hamcrest.MatcherAssert.assertThat;
41  
42  class BinderMock2
43    {
44    }
45  
46  @Getter @RequiredArgsConstructor
47  class ToolbarMock
48    {
49      @Nonnull
50      private final List<ButtonMock> items = new ArrayList<>();
51    }
52  
53  @Getter @RequiredArgsConstructor
54  class ButtonMock
55    {
56      @Nonnull
57      private final String text;
58    }
59  
60  class UnderTest extends ToolBarControlSupport<BinderMock2, ToolbarMock, ButtonMock>
61    {
62      public UnderTest (@Nonnull final Supplier<Collection<? extends UserAction>> userActionsSupplier)
63        {
64          super(userActionsSupplier);
65        }
66  
67      @Override @Nonnull
68      protected ButtonMock createButton (@Nonnull final BinderMock2 binder, @Nonnull final UserAction action)
69        {
70          return new ButtonMock(action.as(_Displayable_).getDisplayName());
71        }
72  
73      @Override
74      protected void addButtonsToToolBar (@Nonnull final ToolbarMock toolBar, @Nonnull final List<ButtonMock> buttons)
75        {
76          toolBar.getItems().addAll(buttons);
77        }
78    }
79  
80  /***************************************************************************************************************************************************************
81   *
82   * @author  Fabrizio Giudici
83   *
84   **************************************************************************************************************************************************************/
85  public class ToolBarControlSupportTest
86    {
87      private final TestUserActions a = new TestUserActions();
88  
89      @Test
90      public void test_populate()
91        {
92          // given
93          final var underTest = new UnderTest(() -> List.of(a.actionFileOpen, a.actionFileClose, a.actionFileCloseAll));
94          final var binder = mock(BinderMock2.class);
95          final var toolBar = new ToolbarMock();
96          // when
97          underTest.populate(binder, toolBar);
98          // then
99          final var buttons = toolBar.getItems();
100         assertThat(buttons.size(), is(3));
101         final var button1 = buttons.get(0);
102         final var button2 = buttons.get(1);
103         final var button3 = buttons.get(2);
104 
105         assertThat(button1.getText(), is("Open"));
106         assertThat(button2.getText(), is("Close"));
107         assertThat(button3.getText(), is("Close all"));
108       }
109   }