summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/test/java/com/smartdevicelink/managers/video/HapticInterfaceManagerTest.java
blob: 7c854cbe414e13f1db8887f411ae94a571abf091 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/***************************************************************************************************
 * Copyright © 2017 Xevo Inc.
 * Redistribution and use in source and binary forms, with or without modification, are permitted
 * provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
 *    and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific prior written
 *    permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 **************************************************************************************************/
package com.smartdevicelink.managers.video;

import android.view.View;
import android.view.ViewGroup;

import com.smartdevicelink.proxy.interfaces.ISdl;
import com.smartdevicelink.proxy.rpc.HapticRect;
import com.smartdevicelink.proxy.rpc.Rectangle;
import com.smartdevicelink.proxy.rpc.SendHapticData;
import com.smartdevicelink.proxy.rpc.VideoStreamingCapability;
import com.smartdevicelink.proxy.rpc.enums.SystemCapabilityType;

import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;

import java.util.ArrayList;
import java.util.List;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Created on 9/26/2017.
 */
@RunWith(MockitoJUnitRunner.Strict.class)
public class HapticInterfaceManagerTest extends TestCase {
    @Mock
    private ISdl mockProxy;

    @Captor
    private ArgumentCaptor<SendHapticData> captor;

    private HapticInterfaceManager hapticMgr;

    @Before
    public void setUp() throws Exception {
        hapticMgr = new HapticInterfaceManager(mockProxy);
    }

    @After
    public void tearDown() throws Exception {
        hapticMgr = null;
    }

    @Test
    public void testSetHapticData() throws Exception {
        List<HapticRect> rects = new ArrayList<>();
        Rectangle rect = new Rectangle();
        rect.setX(10f);
        rect.setY(10f);
        rect.setWidth(50f);
        rect.setHeight(20f);
        HapticRect hRect = new HapticRect();
        hRect.setRect(rect);
        rects.add(hRect);
        hapticMgr.setHapticData(rects);
        verify(mockProxy).sendRPCRequest(any(SendHapticData.class));
    }

    @Test
    public void testRefreshHapticData() throws Exception {
        View root = createViews();
        hapticMgr.refreshHapticData(root);
        verify(mockProxy).sendRPC(captor.capture());
        SendHapticData data = captor.getValue();
        assertNotNull("SendHapticData RPC", data);
        List<HapticRect> list = data.getHapticRectData();
        assertNotNull("List", list);
        assertEquals("Haptic Rects", 4, list.size());
    }

    @Test
    public void testRefreshHapticDataNull() throws Exception {
        hapticMgr.refreshHapticData(null);
        verify(mockProxy).sendRPC(captor.capture());
        SendHapticData data = captor.getValue();
        assertNotNull("SendHapticData RPC", data);
        List<HapticRect> list = data.getHapticRectData();
        assertNull("List", list);
    }

    @Test
    public void testRefreshHapticData_Scale_2_0() {
        double scale = 2.0;

        final int buttonX = 60;
        final int buttonY = 60;
        final int buttonWidth = 150;
        final int buttonHeight = 70;

        Rectangle expected = new Rectangle();
        expected.setX(120.0f);
        expected.setY(120.0f);
        expected.setWidth(300.0f);
        expected.setHeight(140.0f);

        VideoStreamingCapability capability = new VideoStreamingCapability();
        capability.setScale(scale);

        assertViewWithCapability(buttonX, buttonY, buttonWidth, buttonHeight, expected, capability);
    }


    @Test
    public void testRefreshHapticData_Scale_0_5() {
        double scale = 0.5;

        final int buttonX = 60;
        final int buttonY = 60;
        final int buttonWidth = 150;
        final int buttonHeight = 70;

        Rectangle expected = new Rectangle();
        expected.setX(30.0f);
        expected.setY(30.0f);
        expected.setWidth(75.0f);
        expected.setHeight(35.0f);

        VideoStreamingCapability capability = new VideoStreamingCapability();
        capability.setScale(scale);

        assertViewWithCapability(buttonX, buttonY, buttonWidth, buttonHeight, expected, capability);
    }



    @Test
    public void testRefreshHapticData_NullCapability() {
        final int buttonX = 60;
        final int buttonY = 60;
        final int buttonWidth = 150;
        final int buttonHeight = 70;

        Rectangle expected = new Rectangle();
        expected.setX(60.0f);
        expected.setY(60.0f);
        expected.setWidth(150.0f);
        expected.setHeight(70.0f);

        assertViewWithCapability(buttonX, buttonY, buttonWidth, buttonHeight, expected, null);
    }

    @Test
    public void testRefreshHapticData_NullScale() {
        final int buttonX = 60;
        final int buttonY = 60;
        final int buttonWidth = 150;
        final int buttonHeight = 70;

        Rectangle expected = new Rectangle();
        expected.setX(60.0f);
        expected.setY(60.0f);
        expected.setWidth(150.0f);
        expected.setHeight(70.0f);

        VideoStreamingCapability capability = new VideoStreamingCapability();
        capability.setScale(null);

        assertViewWithCapability(buttonX, buttonY, buttonWidth, buttonHeight, expected, capability);
    }

    @Test
    public void testRefreshWithUserData() throws Exception {
        List<HapticRect> rects = new ArrayList<>();
        Rectangle rect = new Rectangle();
        rect.setX(10f);
        rect.setY(10f);
        rect.setWidth(50f);
        rect.setHeight(20f);
        HapticRect hRect = new HapticRect();
        hRect.setRect(rect);
        rects.add(hRect);
        hapticMgr.setHapticData(rects);
        verify(mockProxy).sendRPCRequest(any(SendHapticData.class));

        View root = createViews();
        hapticMgr.refreshHapticData(root);
        verify(mockProxy, times(1)).sendRPCRequest(any(SendHapticData.class));
    }

    private View createViews() {

        View view = mock(View.class);

        ViewGroup parent1 = mock(ViewGroup.class);
        ViewGroup parent2 = mock(ViewGroup.class);

        when(parent1.getChildCount()).thenReturn(5);

        when(parent1.getChildAt(0)).thenReturn(view);
        when(parent1.getChildAt(1)).thenReturn(view);
        when(parent1.getChildAt(2)).thenReturn(view);
        when(parent1.getChildAt(3)).thenReturn(parent2);
        when(parent1.getChildAt(4)).thenReturn(view);

        when(parent2.getChildCount()).thenReturn(2);
        when(parent2.getChildAt(0)).thenReturn(view);
        when(parent2.getChildAt(1)).thenReturn(view);

        when(view.isFocusable()).then(new Answer<Boolean>() {
            private int count = 0;

            @Override
            public Boolean answer(InvocationOnMock invocation) throws Throwable {
                int curCount = count++;
                return (curCount == 1) || (curCount == 2) || (curCount == 3);
            }
        });
        when(view.isClickable()).then(new Answer<Boolean>() {
            private int count = 0;

            @Override
            public Boolean answer(InvocationOnMock invocation) throws Throwable {
                int curCount = count++;
                return (curCount == 0) || (curCount == 3);
            }
        });

        return parent1;
    }


    private View createView(final int x, final int y, int w, int h) {
        View button = mock(View.class);
        when(button.isFocusable()).thenReturn(true);
        doAnswer(new Answer() {
            @Override
            public int[] answer(InvocationOnMock invocation) throws Throwable {
                int[] args = (int[])(invocation.getArguments()[0]);
                args[0] = x;
                args[1] = y;
                return args;
            }
        }).when(button).getLocationOnScreen(any(int[].class));

        when(button.getWidth()).thenReturn(w);
        when(button.getHeight()).thenReturn(h);
        return button;
    }

    private void assertViewWithCapability(int x, int y, int w, int h, Rectangle expected, VideoStreamingCapability capability) {
        when(mockProxy.getCapability(SystemCapabilityType.VIDEO_STREAMING)).thenReturn(capability);

        View button = createView(x, y, w, h);


        hapticMgr.refreshHapticData(button);
        verify(mockProxy).sendRPC(captor.capture());

        SendHapticData data = captor.getValue();
        List<HapticRect> list = data.getHapticRectData();
        assertEquals(1, list.size());
        Rectangle current = list.get(0).getRect();

        assertEquals(expected.getX(), current.getX(), 0.0005);
        assertEquals(expected.getY(), current.getY(), 0.0005);
        assertEquals(expected.getWidth(), current.getWidth(), 0.0005);
        assertEquals(expected.getHeight(), current.getHeight(), 0.0005);
    }
}