1 /*
2  * Copyright (C) 2022 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.flags
18 
19 import android.test.suitebuilder.annotation.SmallTest
20 import android.testing.AndroidTestingRunner
21 import com.android.systemui.SysuiTestCase
22 import com.android.systemui.util.DeviceConfigProxyFake
23 import com.android.systemui.util.concurrency.FakeExecutor
24 import com.android.systemui.util.mockito.any
25 import com.android.systemui.util.time.FakeSystemClock
26 import org.junit.Before
27 import org.junit.Test
28 import org.junit.runner.RunWith
29 import org.mockito.Mock
30 import org.mockito.Mockito.anyString
31 import org.mockito.Mockito.never
32 import org.mockito.Mockito.verify
33 import org.mockito.MockitoAnnotations
34 
35 @SmallTest
36 @RunWith(AndroidTestingRunner::class)
37 class ServerFlagReaderImplTest : SysuiTestCase() {
38 
39     private val NAMESPACE = "test"
40 
41     @Mock private lateinit var changeListener: ServerFlagReader.ChangeListener
42 
43     private lateinit var serverFlagReader: ServerFlagReaderImpl
44     private val deviceConfig = DeviceConfigProxyFake()
45     private val executor = FakeExecutor(FakeSystemClock())
46 
47     @Before
48     fun setup() {
49         MockitoAnnotations.initMocks(this)
50 
51         serverFlagReader = ServerFlagReaderImpl(NAMESPACE, deviceConfig, executor, false)
52     }
53 
54     @Test
55     fun testChange_alertsListener() {
56         val flag = ReleasedFlag("flag_1", "test")
57         serverFlagReader.listenForChanges(listOf(flag), changeListener)
58 
59         deviceConfig.setProperty(NAMESPACE, "flag_1", "1", false)
60         executor.runAllReady()
61 
62         verify(changeListener).onChange(flag, "1")
63     }
64 
65     @Test
66     fun testChange_ignoresListenersDuringTest() {
67         val serverFlagReader = ServerFlagReaderImpl(NAMESPACE, deviceConfig, executor, true)
68         val flag = ReleasedFlag("1", "   test")
69         serverFlagReader.listenForChanges(listOf(flag), changeListener)
70 
71         deviceConfig.setProperty(NAMESPACE, "flag_override_1", "1", false)
72         executor.runAllReady()
73 
74         verify(changeListener, never()).onChange(any(), anyString())
75     }
76 }
77