1/*
2 * Copyright (C) 2016 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
17package com.android.server.wifi;
18
19import static org.junit.Assert.*;
20import static org.mockito.Mockito.*;
21
22import android.os.IBinder;
23import android.os.RemoteException;
24import android.support.test.filters.SmallTest;
25
26import com.android.internal.app.IBatteryStats;
27
28import org.junit.Before;
29import org.junit.Test;
30import org.mockito.Mock;
31import org.mockito.MockitoAnnotations;
32
33/**
34 * Unit tests for {@link com.android.server.wifi.WifiConfigStoreData}.
35 */
36@SmallTest
37public class WifiMulticastLockManagerTest {
38    @Mock WifiMulticastLockManager.FilterController mHandler;
39    @Mock IBatteryStats mBatteryStats;
40    WifiMulticastLockManager mManager;
41
42    /**
43     * Initialize |WifiMulticastLockManager| instance before each test.
44     */
45    @Before
46    public void setUp() throws Exception {
47        MockitoAnnotations.initMocks(this);
48        mManager = new WifiMulticastLockManager(mHandler, mBatteryStats);
49    }
50
51    /**
52     * Test behavior when no locks are held.
53     */
54    @Test
55    public void noLocks() {
56        assertFalse(mManager.isMulticastEnabled());
57        mManager.initializeFiltering();
58        verify(mHandler, times(1)).startFilteringMulticastPackets();
59    }
60
61    /**
62     * Test behavior when one lock is aquired then released.
63     */
64    @Test
65    public void oneLock() throws RemoteException {
66        IBinder binder = mock(IBinder.class);
67        mManager.acquireLock(binder, "Test");
68        assertTrue(mManager.isMulticastEnabled());
69        verify(mHandler).stopFilteringMulticastPackets();
70        mManager.initializeFiltering();
71        verify(mHandler, times(0)).startFilteringMulticastPackets();
72        verify(mBatteryStats).noteWifiMulticastEnabled(anyInt());
73        verify(mBatteryStats, times(0)).noteWifiMulticastDisabled(anyInt());
74
75        mManager.releaseLock();
76        verify(mBatteryStats).noteWifiMulticastDisabled(anyInt());
77        assertFalse(mManager.isMulticastEnabled());
78    }
79}
80
81