KnownBandsChannelHelperTest.java revision 712ef6246834caeac3d5b06bea08e85d6b29cd7a
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.bandIs;
20import static com.android.server.wifi.ScanTestUtil.channelsAre;
21
22import static org.hamcrest.MatcherAssert.assertThat;
23
24import android.net.wifi.WifiScanner;
25import android.test.suitebuilder.annotation.SmallTest;
26
27import com.android.server.wifi.WifiNative;
28
29import org.junit.Before;
30import org.junit.Test;
31import org.junit.experimental.runners.Enclosed;
32import org.junit.runner.RunWith;
33
34/**
35 * Unit tests for {@link com.android.server.wifi.scanner.KnownBandsChannelHelper}.
36 */
37@SmallTest
38@RunWith(Enclosed.class) // WARNING: tests cannot be declared in the outer class
39public class KnownBandsChannelHelperTest {
40
41    /**
42     * Unit tests for
43     * {@link com.android.server.wifi.scanner.KnownBandsChannelHelper.KnownBandsChannelCollection}.
44     */
45    public static class KnownBandsChannelCollectionTest {
46        ChannelHelper.ChannelCollection mChannelCollection;
47
48        /**
49         * Called before each test
50         * Create a collection to use for each test
51         */
52        @Before
53        public void setUp() throws Exception {
54            KnownBandsChannelHelper channelHelper = new KnownBandsChannelHelper(
55                    new int[]{2400, 2450},
56                    new int[]{5150, 5175},
57                    new int[]{5600, 5650, 5660});
58            mChannelCollection = channelHelper.createChannelCollection();
59        }
60
61        /**
62         * Create an empty collection
63         */
64        @Test
65        public void empty() {
66            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
67            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
68
69            assertThat(bucketSettings, channelsAre());
70        }
71
72        /**
73         * Add something to a collection and then clear it and make sure nothing is in it
74         */
75        @Test
76        public void clear() {
77            mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
78            mChannelCollection.clear();
79
80            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
81            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
82            assertThat(bucketSettings, channelsAre());
83        }
84
85        /**
86         * Add a single band to the collection
87         */
88        @Test
89        public void addBand() {
90            mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
91
92            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
93            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
94            assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_24_GHZ));
95        }
96
97        /**
98         * Add a single channel to the collection
99         */
100        @Test
101        public void addChannel_single() {
102            mChannelCollection.addChannel(2400);
103
104            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
105            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
106            assertThat(bucketSettings, channelsAre(2400));
107        }
108
109        /**
110         * Add a multiple channels to the collection
111         */
112        @Test
113        public void addChannel_multiple() {
114            mChannelCollection.addChannel(2400);
115            mChannelCollection.addChannel(2450);
116
117            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
118            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
119            assertThat(bucketSettings, channelsAre(2400, 2450));
120        }
121
122        /**
123         * Add a band and channel that is on that band
124         */
125        @Test
126        public void addChannel_and_addBand_sameBand() {
127            mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
128            mChannelCollection.addChannel(2400);
129
130            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
131            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
132            assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_24_GHZ));
133        }
134
135        /**
136         * Add a band and channel that is not that band
137         */
138        @Test
139        public void addChannel_and_addBand_withDifferentBandChannel() {
140            mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
141            mChannelCollection.addChannel(5150);
142
143            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
144            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
145            assertThat(bucketSettings, channelsAre(2400, 2450, 5150));
146        }
147
148        /**
149         * Add enough channels on a single band that the max channels is exceeded
150         */
151        @Test
152        public void addChannel_exceedMaxChannels() {
153            mChannelCollection.addChannel(5600);
154            mChannelCollection.addChannel(5650);
155            mChannelCollection.addChannel(5660);
156
157            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
158            mChannelCollection.fillBucketSettings(bucketSettings, 2);
159            assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_5_GHZ_DFS_ONLY));
160        }
161
162        /**
163         * Add enough channels across multiple bands that the max channels is exceeded
164         */
165        @Test
166        public void addChannel_exceedMaxChannelsOnMultipleBands() {
167            mChannelCollection.addChannel(2400);
168            mChannelCollection.addChannel(2450);
169            mChannelCollection.addChannel(5150);
170
171            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
172            mChannelCollection.fillBucketSettings(bucketSettings, 2);
173            assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_BOTH));
174        }
175    }
176}
177