Sundeep Ghuman | 7c32aa3 | 2017-06-26 17:13:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.settingslib.wifi; |
| 17 | |
| 18 | import static com.google.common.truth.Truth.assertThat; |
Eric Schwarzenbach | 6efd09a | 2017-08-22 10:33:00 -0700 | [diff] [blame] | 19 | import static org.mockito.ArgumentMatchers.anyInt; |
Fan Zhang | 60b850d | 2017-07-18 15:59:06 -0700 | [diff] [blame] | 20 | import static org.mockito.Mockito.mock; |
Eric Schwarzenbach | 6efd09a | 2017-08-22 10:33:00 -0700 | [diff] [blame] | 21 | import static org.mockito.Mockito.times; |
Fan Zhang | 60b850d | 2017-07-18 15:59:06 -0700 | [diff] [blame] | 22 | import static org.mockito.Mockito.when; |
Eric Schwarzenbach | 6efd09a | 2017-08-22 10:33:00 -0700 | [diff] [blame] | 23 | import static org.mockito.Mockito.verify; |
Sundeep Ghuman | 7c32aa3 | 2017-06-26 17:13:00 -0700 | [diff] [blame] | 24 | |
| 25 | import android.content.Context; |
| 26 | |
Eric Schwarzenbach | 6efd09a | 2017-08-22 10:33:00 -0700 | [diff] [blame] | 27 | import android.graphics.drawable.ColorDrawable; |
Tony Mantler | 4ca9ebd | 2017-07-31 10:54:20 -0700 | [diff] [blame] | 28 | import com.android.settingslib.SettingsLibRobolectricTestRunner; |
Sundeep Ghuman | 7c32aa3 | 2017-06-26 17:13:00 -0700 | [diff] [blame] | 29 | import com.android.settingslib.TestConfig; |
| 30 | |
Eric Schwarzenbach | 6efd09a | 2017-08-22 10:33:00 -0700 | [diff] [blame] | 31 | import org.junit.Before; |
Sundeep Ghuman | 7c32aa3 | 2017-06-26 17:13:00 -0700 | [diff] [blame] | 32 | import org.junit.Test; |
| 33 | import org.junit.runner.RunWith; |
Eric Schwarzenbach | 6efd09a | 2017-08-22 10:33:00 -0700 | [diff] [blame] | 34 | import org.mockito.Mock; |
| 35 | import org.mockito.MockitoAnnotations; |
Sundeep Ghuman | 7c32aa3 | 2017-06-26 17:13:00 -0700 | [diff] [blame] | 36 | import org.robolectric.RuntimeEnvironment; |
| 37 | import org.robolectric.annotation.Config; |
| 38 | |
Tony Mantler | 4ca9ebd | 2017-07-31 10:54:20 -0700 | [diff] [blame] | 39 | @RunWith(SettingsLibRobolectricTestRunner.class) |
Sundeep Ghuman | 7c32aa3 | 2017-06-26 17:13:00 -0700 | [diff] [blame] | 40 | @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) |
| 41 | public class AccessPointPreferenceTest { |
| 42 | |
| 43 | private Context mContext = RuntimeEnvironment.application; |
| 44 | |
Eric Schwarzenbach | 6efd09a | 2017-08-22 10:33:00 -0700 | [diff] [blame] | 45 | @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 Ghuman | 7c32aa3 | 2017-06-26 17:13:00 -0700 | [diff] [blame] | 61 | @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 Zhang | 60b850d | 2017-07-18 15:59:06 -0700 | [diff] [blame] | 86 | |
| 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 Schwarzenbach | 6efd09a | 2017-08-22 10:33:00 -0700 | [diff] [blame] | 104 | |
| 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 Ghuman | 7c32aa3 | 2017-06-26 17:13:00 -0700 | [diff] [blame] | 116 | } |