summaryrefslogtreecommitdiffstats
path: root/tests/wifitests/src/com/android/server/wifi/CandidateScorerTest.java
blob: 3db50c5f28d35bd311fd8a88caa04194512d63fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.wifi;

import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import androidx.test.filters.SmallTest;

import com.android.server.wifi.WifiCandidates.Candidate;
import com.android.server.wifi.WifiCandidates.CandidateScorer;
import com.android.server.wifi.WifiCandidates.ScoredCandidate;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import java.util.ArrayList;
import java.util.List;

/**
 * Unit tests for implementations of
 * {@link com.android.server.wifi.WifiCandidates.CandidateScorer}.
 *
 * Runs tests that any reasonable CandidateScorer should pass.
 * Individual scorers may have additional tests of their own.
 */
@SmallTest
@RunWith(Parameterized.class)
public class CandidateScorerTest {

    @Parameters(name = "{index}: {0}")
    public static List<Object[]> listOfObjectArraysBecauseJUnitMadeUs() {
        ScoringParams sp;
        ArrayList<Object[]> ans = new ArrayList<>();

        sp = new ScoringParams();
        ans.add(new Object[]{
                "Compatibility Scorer",
                CompatibilityScorer.COMPATIBILITY_SCORER_DEFAULT_EXPID,
                new CompatibilityScorer(sp),
                sp});

        sp = new ScoringParams();
        ans.add(new Object[]{
                "Score Card Based Scorer",
                ScoreCardBasedScorer.SCORE_CARD_BASED_SCORER_DEFAULT_EXPID,
                new ScoreCardBasedScorer(sp),
                sp});

        sp = new ScoringParams();
        ans.add(new Object[]{
                "Bubble Function Scorer",
                BubbleFunScorer.BUBBLE_FUN_SCORER_DEFAULT_EXPID,
                new BubbleFunScorer(sp),
                sp});

        return ans;
    }

    @Parameter(0)
    public String mTitleForUseInGeneratedParameterNames;

    @Parameter(1)
    public int mExpectedExpId;

    @Parameter(2)
    public CandidateScorer mCandidateScorer;

    @Parameter(3)
    public ScoringParams mScoringParams;

    private static final double TOL = 1e-6; // for assertEquals(double, double, tolerance)

    private ConcreteCandidate mCandidate1;
    private ConcreteCandidate mCandidate2;

    /**
     * Sets up for unit test
     */
    @Before
    public void setUp() throws Exception {
        mScoringParams.update("");
        mCandidate1 = new ConcreteCandidate().setEvaluatorId(0).setEvaluatorScore(66)
                .setScanRssi(-50).setFrequency(5180);
        mCandidate2 = new ConcreteCandidate().setEvaluatorId(0).setEvaluatorScore(99)
                .setScanRssi(-50).setFrequency(5180);
    }

    /**
     * Test that the expected expid is computed with the built-in defaults.
     */
    @Test
    public void testExpid() throws Exception {
        String identifier = mCandidateScorer.getIdentifier();
        assertEquals(identifier,
                mExpectedExpId,
                WifiNetworkSelector.experimentIdFromIdentifier(identifier));
    }

    /**
     * Utility function to build and evaluate a candidate.
     */
    private double evaluate(ConcreteCandidate candidate) {
        ArrayList<Candidate> candidates = new ArrayList<>(1);
        candidates.add(candidate);
        ScoredCandidate choice = mCandidateScorer.scoreCandidates(candidates);
        return Math.max(-999999999.0, choice.value);
    }

    /**
     * Evaluating equal inputs should give the same result.
     */
    @Test
    public void testEqualInputsShouldGiveTheSameResult() throws Exception {
        assertEquals(evaluate(mCandidate1), evaluate(mCandidate2), TOL);
    }

    /**
     * Prefer 5 GHz over 2.4 GHz in non-fringe conditions, similar rssi.
     */
    @Test
    public void testPrefer5GhzOver2GhzInNonFringeConditionsSimilarRssi() throws Exception {
        assertThat(evaluate(mCandidate1.setFrequency(5180).setScanRssi(-44)),
                greaterThan(evaluate(mCandidate2.setFrequency(2024).setScanRssi(-44))));
    }

    /**
     * Prefer higher rssi.
     */
    @Test
    public void testPreferHigherRssi() throws Exception {
        assertThat(evaluate(mCandidate1.setScanRssi(-63)),
                greaterThan(evaluate(mCandidate2.setScanRssi(-64))));
    }

    /**
     * Prefer a secure network over an open one.
     */
    @Test
    public void testPreferASecureNetworkOverAnOpenOne() throws Exception {
        assertThat(evaluate(mCandidate1),
                greaterThan(evaluate(mCandidate2.setOpenNetwork(true))));
    }

    /**
     * Prefer the current network, even if rssi difference is significant.
     */
    @Test
    public void testPreferTheCurrentNetworkEvenIfRssiDifferenceIsSignificant() throws Exception {
        assertThat(evaluate(mCandidate1.setScanRssi(-77).setCurrentNetwork(true)),
                greaterThan(evaluate(mCandidate2.setScanRssi(-68))));
    }

    /**
     * Above saturation, don't switch from current even with a large rssi difference.
     */
    @Test
    public void testAboveSaturationDoNotSwitchAwayEvenWithALargeRssiDifference() throws Exception {
        int goodRssi = mScoringParams.getGoodRssi(mCandidate1.getFrequency());
        int unbelievablyGoodRssi = -1;
        assertThat(evaluate(mCandidate1.setScanRssi(goodRssi).setCurrentNetwork(true)),
                greaterThan(evaluate(mCandidate2.setScanRssi(unbelievablyGoodRssi))));
    }

}