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.scanner;
18
19import static com.android.server.wifi.ScanTestUtil.channelsToNativeSettings;
20import static com.android.server.wifi.ScanTestUtil.channelsToSpec;
21import static com.android.server.wifi.ScanTestUtil.createRequest;
22
23import static org.junit.Assert.assertEquals;
24import static org.mockito.Mockito.any;
25import static org.mockito.Mockito.doCallRealMethod;
26import static org.mockito.Mockito.mock;
27import static org.mockito.Mockito.verify;
28
29import android.net.wifi.WifiScanner;
30import android.test.suitebuilder.annotation.SmallTest;
31
32import com.android.server.wifi.WifiNative;
33
34import org.junit.Before;
35import org.junit.Test;
36import org.junit.experimental.runners.Enclosed;
37import org.junit.runner.RunWith;
38
39/**
40 * Unit tests for {@link com.android.server.wifi.scanner.ChannelHelper}.
41 */
42@RunWith(Enclosed.class) // WARNING: tests cannot be declared in the outer class
43public class ChannelHelperTest {
44
45    /**
46     * Unit tests for
47     * {@link com.android.server.wifi.scanner.ChannelHelper#toString}.
48     */
49    @SmallTest
50    public static class ToStringTest {
51        /**
52         * Compute a string representing the channels in a ScanSettings with a band set.
53         */
54        @Test
55        public void scanSettings_band() {
56            WifiScanner.ScanSettings scanSettings = createRequest(WifiScanner.WIFI_BAND_BOTH,
57                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
58            assertEquals("24Ghz & 5Ghz (no DFS)", ChannelHelper.toString(scanSettings));
59        }
60
61        /**
62         * Compute a string representing the channels in a ScanSettings with a list of channels set.
63         */
64        @Test
65        public void scanSettings_channels() {
66            WifiScanner.ScanSettings scanSettings = createRequest(channelsToSpec(5150, 2400),
67                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
68            assertEquals("[5150,2400]", ChannelHelper.toString(scanSettings));
69        }
70
71        /**
72         * Compute a string representing the channels in a BucketSettings with a band set.
73         */
74        @Test
75        public void bucketSettings_band() {
76            WifiScanner.ScanSettings scanSettings = createRequest(WifiScanner.WIFI_BAND_5_GHZ,
77                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
78            assertEquals("5Ghz (no DFS)", ChannelHelper.toString(scanSettings));
79        }
80
81        /**
82         * Compute a string representing the channels in a BucketSettings with a list of channels
83         * set.
84         */
85        @Test
86        public void bucketSettings_channels() {
87            WifiScanner.ScanSettings scanSettings = createRequest(channelsToSpec(2400, 5100),
88                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
89            assertEquals("[2400,5100]", ChannelHelper.toString(scanSettings));
90        }
91    }
92
93    /**
94     * Unit tests for
95     * {@link com.android.server.wifi.scanner.ChannelHelper.ChannelCollection}.
96     */
97    @SmallTest
98    public static class ChannelCollectionTest {
99        ChannelHelper.ChannelCollection mChannelCollection;
100
101        /**
102         * Called before each test
103         * Create an instance of ChannelCollection that calls all real methods, but allows mocking
104         * of abstract methods.
105         */
106        @Before
107        public void setUp() throws Exception {
108            mChannelCollection = mock(ChannelHelper.ChannelCollection.class);
109            doCallRealMethod().when(mChannelCollection)
110                    .addChannels(any(WifiScanner.ScanSettings.class));
111            doCallRealMethod().when(mChannelCollection)
112                    .addChannels(any(WifiNative.BucketSettings.class));
113        }
114
115        /**
116         * Verify adding channels from a WifiScanner.ScanSettings with channels
117         */
118        @Test
119        public void addChannelsWifiScanner_channels() {
120            WifiScanner.ScanSettings testSettings = createRequest(channelsToSpec(5150, 2400),
121                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
122
123            mChannelCollection.addChannels(testSettings);
124
125            verify(mChannelCollection).addChannel(2400);
126            verify(mChannelCollection).addChannel(5150);
127        }
128
129        /**
130         * Verify adding channels from a WifiScanner.ScanSettings with bands
131         */
132        @Test
133        public void addChannelsWifiScanner_band() {
134            WifiScanner.ScanSettings testSettings = createRequest(WifiScanner.WIFI_BAND_BOTH,
135                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
136
137            mChannelCollection.addChannels(testSettings);
138
139            verify(mChannelCollection).addBand(WifiScanner.WIFI_BAND_BOTH);
140        }
141
142        /**
143         * Verify adding channels from a WifiNative.BucketSetting with channels
144         */
145        @Test
146        public void addChannelsWifiNativeBucket_channels() {
147            WifiNative.BucketSettings testBucket = new WifiNative.BucketSettings();
148            testBucket.band = WifiScanner.WIFI_BAND_UNSPECIFIED;
149            testBucket.num_channels = 2;
150            testBucket.channels = channelsToNativeSettings(2450, 5100);
151
152            mChannelCollection.addChannels(testBucket);
153
154            verify(mChannelCollection).addChannel(2450);
155            verify(mChannelCollection).addChannel(5100);
156        }
157
158        /**
159         * Verify adding channels from a WifiNative.BucketSetting with bands
160         */
161        @Test
162        public void addChannelsWifiNativeBucket_band() {
163            WifiNative.BucketSettings testBucket = new WifiNative.BucketSettings();
164            testBucket.band = WifiScanner.WIFI_BAND_5_GHZ;
165
166            mChannelCollection.addChannels(testBucket);
167
168            verify(mChannelCollection).addBand(WifiScanner.WIFI_BAND_5_GHZ);
169        }
170    }
171}
172