1 /*
2  * Copyright (C) 2023 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 package com.android.systemui.wmshell
17 
18 import android.content.ContentResolver
19 import android.content.Context
20 import android.content.SharedPreferences
21 import android.testing.AndroidTestingRunner
22 import android.testing.TestableLooper
23 import androidx.test.filters.SmallTest
24 import com.android.systemui.model.SysUiStateTest
25 import com.android.wm.shell.bubbles.Bubble
26 import com.android.wm.shell.bubbles.BubbleEducationController
27 import com.android.wm.shell.bubbles.PREF_MANAGED_EDUCATION
28 import com.android.wm.shell.bubbles.PREF_STACK_EDUCATION
29 import org.junit.Assert.assertEquals
30 import org.junit.Before
31 import org.junit.Test
32 import org.junit.runner.RunWith
33 import org.mockito.ArgumentMatchers.anyBoolean
34 import org.mockito.ArgumentMatchers.anyInt
35 import org.mockito.ArgumentMatchers.anyString
36 import org.mockito.Mockito
37 
38 @SmallTest
39 @RunWith(AndroidTestingRunner::class)
40 @TestableLooper.RunWithLooper(setAsMainLooper = true)
41 class BubbleEducationControllerTest : SysUiStateTest() {
42     private val sharedPrefsEditor = Mockito.mock(SharedPreferences.Editor::class.java)
43     private val sharedPrefs = Mockito.mock(SharedPreferences::class.java)
44     private val context = Mockito.mock(Context::class.java)
45     private lateinit var sut: BubbleEducationController
46 
47     @Before
48     fun setUp() {
49         Mockito.`when`(context.packageName).thenReturn("packageName")
50         Mockito.`when`(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs)
51         Mockito.`when`(context.contentResolver)
52             .thenReturn(Mockito.mock(ContentResolver::class.java))
53         Mockito.`when`(sharedPrefs.edit()).thenReturn(sharedPrefsEditor)
54         sut = BubbleEducationController(context)
55     }
56 
57     @Test
58     fun testSeenStackEducation_read() {
59         Mockito.`when`(sharedPrefs.getBoolean(anyString(), anyBoolean())).thenReturn(true)
60         assertEquals(sut.hasSeenStackEducation, true)
61         Mockito.verify(sharedPrefs).getBoolean(PREF_STACK_EDUCATION, false)
62     }
63 
64     @Test
65     fun testSeenStackEducation_write() {
66         sut.hasSeenStackEducation = true
67         Mockito.verify(sharedPrefsEditor).putBoolean(PREF_STACK_EDUCATION, true)
68     }
69 
70     @Test
71     fun testSeenManageEducation_read() {
72         Mockito.`when`(sharedPrefs.getBoolean(anyString(), anyBoolean())).thenReturn(true)
73         assertEquals(sut.hasSeenManageEducation, true)
74         Mockito.verify(sharedPrefs).getBoolean(PREF_MANAGED_EDUCATION, false)
75     }
76 
77     @Test
78     fun testSeenManageEducation_write() {
79         sut.hasSeenManageEducation = true
80         Mockito.verify(sharedPrefsEditor).putBoolean(PREF_MANAGED_EDUCATION, true)
81     }
82 
83     @Test
84     fun testShouldShowStackEducation() {
85         val bubble = Mockito.mock(Bubble::class.java)
86         // When bubble is null
87         assertEquals(sut.shouldShowStackEducation(null), false)
88         // When bubble is not conversation
89         Mockito.`when`(bubble.isConversation).thenReturn(false)
90         assertEquals(sut.shouldShowStackEducation(bubble), false)
91         // When bubble is conversation and has seen stack edu
92         Mockito.`when`(bubble.isConversation).thenReturn(true)
93         Mockito.`when`(sharedPrefs.getBoolean(anyString(), anyBoolean())).thenReturn(true)
94         assertEquals(sut.shouldShowStackEducation(bubble), false)
95         // When bubble is conversation and has not seen stack edu
96         Mockito.`when`(bubble.isConversation).thenReturn(true)
97         Mockito.`when`(sharedPrefs.getBoolean(anyString(), anyBoolean())).thenReturn(false)
98         assertEquals(sut.shouldShowStackEducation(bubble), true)
99     }
100 
101     @Test
102     fun testShouldShowManageEducation() {
103         val bubble = Mockito.mock(Bubble::class.java)
104         // When bubble is null
105         assertEquals(sut.shouldShowManageEducation(null), false)
106         // When bubble is not conversation
107         Mockito.`when`(bubble.isConversation).thenReturn(false)
108         assertEquals(sut.shouldShowManageEducation(bubble), false)
109         // When bubble is conversation and has seen stack edu
110         Mockito.`when`(bubble.isConversation).thenReturn(true)
111         Mockito.`when`(sharedPrefs.getBoolean(anyString(), anyBoolean())).thenReturn(true)
112         assertEquals(sut.shouldShowManageEducation(bubble), false)
113         // When bubble is conversation and has not seen stack edu
114         Mockito.`when`(bubble.isConversation).thenReturn(true)
115         Mockito.`when`(sharedPrefs.getBoolean(anyString(), anyBoolean())).thenReturn(false)
116         assertEquals(sut.shouldShowManageEducation(bubble), true)
117     }
118 }
119