1 /*
<lambda>null2  * Copyright (C) 2024 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.google.android.msdl.logging
18 
19 import android.os.VibrationAttributes
20 import com.google.android.msdl.data.model.MSDLToken
21 import com.google.android.msdl.domain.InteractionProperties
22 import com.google.common.truth.Truth.assertThat
23 import kotlin.random.Random
24 import org.junit.Test
25 import org.junit.runner.RunWith
26 import org.junit.runners.JUnit4
27 
28 @RunWith(JUnit4::class)
29 class MSDLHistoryLoggerImplTest {
30 
31     private val properties =
32         object : InteractionProperties {
33             override val vibrationAttributes: VibrationAttributes =
34                 VibrationAttributes.createForUsage(VibrationAttributes.USAGE_HARDWARE_FEEDBACK)
35         }
36     private val random = Random(0)
37 
38     private val logger = MSDLHistoryLoggerImpl(MSDLHistoryLogger.HISTORY_SIZE)
39 
40     @Test
41     fun addEvent_eventIsAddedToHistory() {
42         val token = getRandomToken()
43         val event = MSDLEvent(token, properties)
44         logger.addEvent(event)
45 
46         assertThat(logger.getHistory().containsEvent(event)).isTrue()
47     }
48 
49     @Test
50     fun addEvent_beyondLimit_keepsHistorySize() {
51         val tokens = getRandomTokens(MSDLHistoryLogger.HISTORY_SIZE + 10)
52         tokens.forEach { logger.addEvent(MSDLEvent(it, properties)) }
53 
54         assertThat(logger.getHistory().size).isEqualTo(MSDLHistoryLogger.HISTORY_SIZE)
55     }
56 
57     @Test
58     fun addEvent_beyondLimit_keepsLatestEvents() {
59         val localHistory = arrayListOf<MSDLEvent>()
60         val tokens = getRandomTokens(MSDLHistoryLogger.HISTORY_SIZE + 10)
61         var event: MSDLEvent
62         tokens.forEach {
63             event = MSDLEvent(it, properties)
64             logger.addEvent(event)
65             localHistory.add(event)
66         }
67 
68         val latestLocalHistory = localHistory.takeLast(MSDLHistoryLogger.HISTORY_SIZE)
69         val loggerHistory = logger.getHistory()
70         assertThat(latestLocalHistory.isEqualTo(loggerHistory)).isTrue()
71     }
72 
73     private fun getRandomToken(): MSDLToken =
74         MSDLToken.entries[random.nextInt(0, MSDLToken.entries.size)]
75 
76     private fun getRandomTokens(n: Int): List<MSDLToken> = List(n) { getRandomToken() }
77 
78     /**
79      * Check if the list is equal to another by making sure it has the same elements in the same
80      * order. Events are compared by their token name and interaction properties.
81      *
82      * @param[other] The other list to compare to.
83      */
84     private fun List<MSDLEvent>.isEqualTo(other: List<MSDLEvent>): Boolean {
85         assert(other.size == this.size) { "Both lists must be of the same size" }
86         this.forEachIndexed { i, event ->
87             if (event.tokenName != other[i].tokenName || event.properties != other[i].properties) {
88                 return false
89             }
90         }
91         return true
92     }
93 
94     /**
95      * Check if the list contains an event. Events are compared by their token name and interaction
96      * properties.
97      *
98      * @param[other] The event to find.
99      */
100     private fun List<MSDLEvent>.containsEvent(other: MSDLEvent): Boolean {
101         this.forEach { event ->
102             if (event.tokenName == other.tokenName && event.properties == other.properties) {
103                 return true
104             }
105         }
106         return false
107     }
108 }
109