blob: 7f89fd5d079c5835ea3096bd09b701f1d4bb5a61 [file] [log] [blame]
Sundeep Ghuman7c32aa32017-06-26 17:13:00 -07001/*
2 * Copyright (C) 2017 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 */
16package com.android.settingslib.wifi;
17
18import static com.google.common.truth.Truth.assertThat;
Eric Schwarzenbach6efd09a2017-08-22 10:33:00 -070019import static org.mockito.ArgumentMatchers.anyInt;
Fan Zhang60b850d2017-07-18 15:59:06 -070020import static org.mockito.Mockito.mock;
Eric Schwarzenbach6efd09a2017-08-22 10:33:00 -070021import static org.mockito.Mockito.times;
Fan Zhang60b850d2017-07-18 15:59:06 -070022import static org.mockito.Mockito.when;
Eric Schwarzenbach6efd09a2017-08-22 10:33:00 -070023import static org.mockito.Mockito.verify;
Sundeep Ghuman7c32aa32017-06-26 17:13:00 -070024
25import android.content.Context;
26
Eric Schwarzenbach6efd09a2017-08-22 10:33:00 -070027import android.graphics.drawable.ColorDrawable;
Tony Mantler4ca9ebd2017-07-31 10:54:20 -070028import com.android.settingslib.SettingsLibRobolectricTestRunner;
Sundeep Ghuman7c32aa32017-06-26 17:13:00 -070029import com.android.settingslib.TestConfig;
30
Eric Schwarzenbach6efd09a2017-08-22 10:33:00 -070031import org.junit.Before;
Sundeep Ghuman7c32aa32017-06-26 17:13:00 -070032import org.junit.Test;
33import org.junit.runner.RunWith;
Eric Schwarzenbach6efd09a2017-08-22 10:33:00 -070034import org.mockito.Mock;
35import org.mockito.MockitoAnnotations;
Sundeep Ghuman7c32aa32017-06-26 17:13:00 -070036import org.robolectric.RuntimeEnvironment;
37import org.robolectric.annotation.Config;
38
Tony Mantler4ca9ebd2017-07-31 10:54:20 -070039@RunWith(SettingsLibRobolectricTestRunner.class)
Sundeep Ghuman7c32aa32017-06-26 17:13:00 -070040@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
41public class AccessPointPreferenceTest {
42
43 private Context mContext = RuntimeEnvironment.application;
44
Eric Schwarzenbach6efd09a2017-08-22 10:33:00 -070045 @Mock private AccessPoint mockAccessPoint;
46 @Mock private AccessPointPreference.UserBadgeCache mockUserBadgeCache;
47 @Mock private AccessPointPreference.IconInjector mockIconInjector;
48
49 private AccessPointPreference createWithAccessPoint(AccessPoint accessPoint) {
50 return new AccessPointPreference(accessPoint, mContext, mockUserBadgeCache,
51 0, true, null, -1, mockIconInjector);
52 }
53
54 @Before
55 public void setUp() {
56 MockitoAnnotations.initMocks(this);
57
58 when(mockIconInjector.getIcon(anyInt())).thenReturn(new ColorDrawable());
59 }
60
Sundeep Ghuman7c32aa32017-06-26 17:13:00 -070061 @Test
62 public void generatePreferenceKey_shouldReturnSsidPlusSecurity() {
63 String ssid = "ssid";
64 int security = AccessPoint.SECURITY_WEP;
65 String expectedKey = ssid + ',' + security;
66
67 TestAccessPointBuilder builder = new TestAccessPointBuilder(mContext);
68 builder.setSsid(ssid).setSecurity(security);
69
70 assertThat(AccessPointPreference.generatePreferenceKey(builder.build()))
71 .isEqualTo(expectedKey);
72 }
73
74 @Test
75 public void generatePreferenceKey_shouldReturnBssidPlusSecurity() {
76 String bssid = "bssid";
77 int security = AccessPoint.SECURITY_WEP;
78 String expectedKey = bssid + ',' + security;
79
80 TestAccessPointBuilder builder = new TestAccessPointBuilder(mContext);
81 builder.setBssid(bssid).setSecurity(security);
82
83 assertThat(AccessPointPreference.generatePreferenceKey(builder.build()))
84 .isEqualTo(expectedKey);
85 }
Fan Zhang60b850d2017-07-18 15:59:06 -070086
87 @Test
88 public void refresh_openNetwork_updateContentDescription() {
89 final String ssid = "ssid";
90 final String summary = "connected";
91 final int security = AccessPoint.SECURITY_WEP;
92 final AccessPoint ap = new TestAccessPointBuilder(mContext)
93 .setSsid(ssid)
94 .setSecurity(security)
95 .build();
96 final AccessPointPreference pref = mock(AccessPointPreference.class);
97 when(pref.getTitle()).thenReturn(ssid);
98 when(pref.getSummary()).thenReturn(summary);
99
100 assertThat(AccessPointPreference.buildContentDescription(
101 RuntimeEnvironment.application, pref, ap))
102 .isEqualTo("ssid,connected,Wifi signal full.,Secure network");
103 }
Eric Schwarzenbach6efd09a2017-08-22 10:33:00 -0700104
105 @Test
106 public void refresh_shouldUpdateIcon() {
107 int level = 1;
108 when(mockAccessPoint.getSpeed()).thenReturn(0);
109 when(mockAccessPoint.getLevel()).thenReturn(level);
110
111 AccessPointPreference pref = createWithAccessPoint(mockAccessPoint);
112 pref.refresh();
113
114 verify(mockIconInjector).getIcon(level);
115 }
Sundeep Ghuman7c32aa32017-06-26 17:13:00 -0700116}