Robert Snoeberger | 70d0d6b | 2020-05-14 16:47:02 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.media; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import static org.mockito.Mockito.any; |
| 22 | import static org.mockito.Mockito.eq; |
| 23 | import static org.mockito.Mockito.mock; |
| 24 | import static org.mockito.Mockito.never; |
| 25 | import static org.mockito.Mockito.verify; |
| 26 | |
| 27 | import android.graphics.Color; |
| 28 | import android.testing.AndroidTestingRunner; |
| 29 | import android.testing.TestableLooper; |
| 30 | |
| 31 | import androidx.test.filters.SmallTest; |
| 32 | |
| 33 | import com.android.systemui.SysuiTestCase; |
| 34 | |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | import org.mockito.ArgumentCaptor; |
| 39 | import org.mockito.Mock; |
| 40 | |
| 41 | import java.util.ArrayList; |
| 42 | |
| 43 | @SmallTest |
| 44 | @RunWith(AndroidTestingRunner.class) |
| 45 | @TestableLooper.RunWithLooper |
| 46 | public class MediaDataCombineLatestTest extends SysuiTestCase { |
| 47 | |
| 48 | private static final String KEY = "TEST_KEY"; |
| 49 | private static final String APP = "APP"; |
| 50 | private static final String PACKAGE = "PKG"; |
| 51 | private static final int BG_COLOR = Color.RED; |
| 52 | private static final String ARTIST = "ARTIST"; |
| 53 | private static final String TITLE = "TITLE"; |
| 54 | private static final String DEVICE_NAME = "DEVICE_NAME"; |
| 55 | |
| 56 | private MediaDataCombineLatest mManager; |
| 57 | |
| 58 | @Mock private MediaDataManager mDataSource; |
| 59 | @Mock private MediaDeviceManager mDeviceSource; |
| 60 | @Mock private MediaDataManager.Listener mListener; |
| 61 | |
| 62 | private MediaDataManager.Listener mDataListener; |
| 63 | private MediaDeviceManager.Listener mDeviceListener; |
| 64 | |
| 65 | private MediaData mMediaData; |
| 66 | private MediaDeviceData mDeviceData; |
| 67 | |
| 68 | @Before |
| 69 | public void setUp() { |
| 70 | mDataSource = mock(MediaDataManager.class); |
| 71 | mDeviceSource = mock(MediaDeviceManager.class); |
| 72 | mListener = mock(MediaDataManager.Listener.class); |
| 73 | |
| 74 | mManager = new MediaDataCombineLatest(mDataSource, mDeviceSource); |
| 75 | |
| 76 | mDataListener = captureDataListener(); |
| 77 | mDeviceListener = captureDeviceListener(); |
| 78 | |
| 79 | mManager.addListener(mListener); |
| 80 | |
| 81 | mMediaData = new MediaData(true, BG_COLOR, APP, null, ARTIST, TITLE, null, |
Robert Snoeberger | f05aa68f | 2020-06-18 16:14:29 -0400 | [diff] [blame] | 82 | new ArrayList<>(), new ArrayList<>(), PACKAGE, null, null, null, true, null, false, |
| 83 | KEY, false); |
Robert Snoeberger | 23e3689 | 2020-05-21 00:33:24 -0400 | [diff] [blame] | 84 | mDeviceData = new MediaDeviceData(true, null, DEVICE_NAME); |
Robert Snoeberger | 70d0d6b | 2020-05-14 16:47:02 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void eventNotEmittedWithoutDevice() { |
| 89 | // WHEN data source emits an event without device data |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 90 | mDataListener.onMediaDataLoaded(KEY, null, mMediaData); |
Robert Snoeberger | 70d0d6b | 2020-05-14 16:47:02 -0400 | [diff] [blame] | 91 | // THEN an event isn't emitted |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 92 | verify(mListener, never()).onMediaDataLoaded(eq(KEY), any(), any()); |
Robert Snoeberger | 70d0d6b | 2020-05-14 16:47:02 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void eventNotEmittedWithoutMedia() { |
| 97 | // WHEN device source emits an event without media data |
| 98 | mDeviceListener.onMediaDeviceChanged(KEY, mDeviceData); |
| 99 | // THEN an event isn't emitted |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 100 | verify(mListener, never()).onMediaDataLoaded(eq(KEY), any(), any()); |
Robert Snoeberger | 70d0d6b | 2020-05-14 16:47:02 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void emitEventAfterDeviceFirst() { |
| 105 | // GIVEN that a device event has already been received |
| 106 | mDeviceListener.onMediaDeviceChanged(KEY, mDeviceData); |
| 107 | // WHEN media event is received |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 108 | mDataListener.onMediaDataLoaded(KEY, null, mMediaData); |
Robert Snoeberger | 70d0d6b | 2020-05-14 16:47:02 -0400 | [diff] [blame] | 109 | // THEN the listener receives a combined event |
| 110 | ArgumentCaptor<MediaData> captor = ArgumentCaptor.forClass(MediaData.class); |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 111 | verify(mListener).onMediaDataLoaded(eq(KEY), any(), captor.capture()); |
Robert Snoeberger | 70d0d6b | 2020-05-14 16:47:02 -0400 | [diff] [blame] | 112 | assertThat(captor.getValue().getDevice()).isNotNull(); |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void emitEventAfterMediaFirst() { |
| 117 | // GIVEN that media event has already been received |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 118 | mDataListener.onMediaDataLoaded(KEY, null, mMediaData); |
Robert Snoeberger | 70d0d6b | 2020-05-14 16:47:02 -0400 | [diff] [blame] | 119 | // WHEN device event is received |
| 120 | mDeviceListener.onMediaDeviceChanged(KEY, mDeviceData); |
| 121 | // THEN the listener receives a combined event |
| 122 | ArgumentCaptor<MediaData> captor = ArgumentCaptor.forClass(MediaData.class); |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 123 | verify(mListener).onMediaDataLoaded(eq(KEY), any(), captor.capture()); |
Robert Snoeberger | 70d0d6b | 2020-05-14 16:47:02 -0400 | [diff] [blame] | 124 | assertThat(captor.getValue().getDevice()).isNotNull(); |
| 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void mediaDataRemoved() { |
| 129 | // WHEN media data is removed without first receiving device or data |
| 130 | mDataListener.onMediaDataRemoved(KEY); |
| 131 | // THEN a removed event isn't emitted |
| 132 | verify(mListener, never()).onMediaDataRemoved(eq(KEY)); |
| 133 | } |
| 134 | |
| 135 | @Test |
| 136 | public void mediaDataRemovedAfterMediaEvent() { |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 137 | mDataListener.onMediaDataLoaded(KEY, null, mMediaData); |
Robert Snoeberger | 70d0d6b | 2020-05-14 16:47:02 -0400 | [diff] [blame] | 138 | mDataListener.onMediaDataRemoved(KEY); |
| 139 | verify(mListener).onMediaDataRemoved(eq(KEY)); |
| 140 | } |
| 141 | |
| 142 | @Test |
| 143 | public void mediaDataRemovedAfterDeviceEvent() { |
| 144 | mDeviceListener.onMediaDeviceChanged(KEY, mDeviceData); |
| 145 | mDataListener.onMediaDataRemoved(KEY); |
| 146 | verify(mListener).onMediaDataRemoved(eq(KEY)); |
| 147 | } |
| 148 | |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 149 | @Test |
| 150 | public void mediaDataKeyUpdated() { |
| 151 | // GIVEN that device and media events have already been received |
| 152 | mDataListener.onMediaDataLoaded(KEY, null, mMediaData); |
| 153 | mDeviceListener.onMediaDeviceChanged(KEY, mDeviceData); |
| 154 | // WHEN the key is changed |
| 155 | mDataListener.onMediaDataLoaded("NEW_KEY", KEY, mMediaData); |
| 156 | // THEN the listener gets a load event with the correct keys |
| 157 | ArgumentCaptor<MediaData> captor = ArgumentCaptor.forClass(MediaData.class); |
| 158 | verify(mListener).onMediaDataLoaded(eq("NEW_KEY"), any(), captor.capture()); |
| 159 | } |
| 160 | |
Robert Snoeberger | 70d0d6b | 2020-05-14 16:47:02 -0400 | [diff] [blame] | 161 | private MediaDataManager.Listener captureDataListener() { |
| 162 | ArgumentCaptor<MediaDataManager.Listener> captor = ArgumentCaptor.forClass( |
| 163 | MediaDataManager.Listener.class); |
| 164 | verify(mDataSource).addListener(captor.capture()); |
| 165 | return captor.getValue(); |
| 166 | } |
| 167 | |
| 168 | private MediaDeviceManager.Listener captureDeviceListener() { |
| 169 | ArgumentCaptor<MediaDeviceManager.Listener> captor = ArgumentCaptor.forClass( |
| 170 | MediaDeviceManager.Listener.class); |
| 171 | verify(mDeviceSource).addListener(captor.capture()); |
| 172 | return captor.getValue(); |
| 173 | } |
| 174 | } |