1 /*
2  * Copyright (C) 2019 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 android.net.wifi;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 
22 import android.annotation.NonNull;
23 import android.net.Uri;
24 import android.util.SparseArray;
25 
26 import androidx.test.filters.SmallTest;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 
31 /**
32  * Unit tests for {@link android.net.wifi.EasyConnectStatusCallbackTest}.
33  */
34 @SmallTest
35 public class EasyConnectStatusCallbackTest {
36     private EasyConnectStatusCallback mEasyConnectStatusCallback = new EasyConnectStatusCallback() {
37         @Override
38         public void onEnrolleeSuccess(int newNetworkId) {
39 
40         }
41 
42         @Override
43         public void onConfiguratorSuccess(int code) {
44 
45         }
46 
47         @Override
48         public void onProgress(int code) {
49 
50         }
51 
52         @Override
53         public void onFailure(int code) {
54             mOnFailureR1EventReceived = true;
55             mLastCode = code;
56         }
57 
58         @Override
59         public void onBootstrapUriGenerated(@NonNull Uri dppUri) {
60 
61         }
62     };
63     private boolean mOnFailureR1EventReceived;
64     private int mLastCode;
65 
66     @Before
setUp()67     public void setUp() {
68         mOnFailureR1EventReceived = false;
69         mLastCode = 0;
70     }
71 
72     /**
73      * Test that the legacy R1 onFailure is called by default if the R2 onFailure is not overridden
74      * by the app.
75      */
76     @Test
testR1OnFailureCalled()77     public void testR1OnFailureCalled() {
78 
79         SparseArray<int[]> channelList = new SparseArray<>();
80         int[] channelArray = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
81 
82         channelList.append(81, channelArray);
83         mEasyConnectStatusCallback.onFailure(
84                 EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_CANNOT_FIND_NETWORK,
85                 "SomeSSID", channelList, new int[] {81});
86 
87         assertTrue(mOnFailureR1EventReceived);
88         assertEquals(mLastCode,
89                 EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_CANNOT_FIND_NETWORK);
90     }
91 }
92