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; |
Fan Zhang | f7802ea | 2018-08-28 15:15:19 -0700 | [diff] [blame] | 19 | |
Eric Schwarzenbach | 6efd09a | 2017-08-22 10:33:00 -0700 | [diff] [blame] | 20 | import static org.mockito.ArgumentMatchers.anyInt; |
Fan Zhang | 60b850d | 2017-07-18 15:59:06 -0700 | [diff] [blame] | 21 | import static org.mockito.Mockito.mock; |
Eric Schwarzenbach | 6efd09a | 2017-08-22 10:33:00 -0700 | [diff] [blame] | 22 | import static org.mockito.Mockito.verify; |
Fan Zhang | b6cec0d | 2017-09-05 11:02:03 -0700 | [diff] [blame] | 23 | import static org.mockito.Mockito.when; |
Sundeep Ghuman | 7c32aa3 | 2017-06-26 17:13:00 -0700 | [diff] [blame] | 24 | |
| 25 | import android.content.Context; |
Eric Schwarzenbach | 6efd09a | 2017-08-22 10:33:00 -0700 | [diff] [blame] | 26 | import android.graphics.drawable.ColorDrawable; |
Fan Zhang | b6cec0d | 2017-09-05 11:02:03 -0700 | [diff] [blame] | 27 | |
Eric Schwarzenbach | 6efd09a | 2017-08-22 10:33:00 -0700 | [diff] [blame] | 28 | import org.junit.Before; |
Sundeep Ghuman | 7c32aa3 | 2017-06-26 17:13:00 -0700 | [diff] [blame] | 29 | import org.junit.Test; |
| 30 | import org.junit.runner.RunWith; |
Eric Schwarzenbach | 6efd09a | 2017-08-22 10:33:00 -0700 | [diff] [blame] | 31 | import org.mockito.Mock; |
| 32 | import org.mockito.MockitoAnnotations; |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 33 | import org.robolectric.RobolectricTestRunner; |
Sundeep Ghuman | 7c32aa3 | 2017-06-26 17:13:00 -0700 | [diff] [blame] | 34 | import org.robolectric.RuntimeEnvironment; |
Sundeep Ghuman | 7c32aa3 | 2017-06-26 17:13:00 -0700 | [diff] [blame] | 35 | |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 36 | @RunWith(RobolectricTestRunner.class) |
Sundeep Ghuman | 7c32aa3 | 2017-06-26 17:13:00 -0700 | [diff] [blame] | 37 | public class AccessPointPreferenceTest { |
| 38 | |
| 39 | private Context mContext = RuntimeEnvironment.application; |
| 40 | |
Fan Zhang | b6cec0d | 2017-09-05 11:02:03 -0700 | [diff] [blame] | 41 | @Mock |
| 42 | private AccessPoint mockAccessPoint; |
| 43 | @Mock |
| 44 | private AccessPointPreference.UserBadgeCache mockUserBadgeCache; |
| 45 | @Mock |
| 46 | private AccessPointPreference.IconInjector mockIconInjector; |
Eric Schwarzenbach | 6efd09a | 2017-08-22 10:33:00 -0700 | [diff] [blame] | 47 | |
| 48 | private AccessPointPreference createWithAccessPoint(AccessPoint accessPoint) { |
| 49 | return new AccessPointPreference(accessPoint, mContext, mockUserBadgeCache, |
| 50 | 0, true, null, -1, mockIconInjector); |
| 51 | } |
| 52 | |
| 53 | @Before |
| 54 | public void setUp() { |
| 55 | MockitoAnnotations.initMocks(this); |
| 56 | |
| 57 | when(mockIconInjector.getIcon(anyInt())).thenReturn(new ColorDrawable()); |
| 58 | } |
| 59 | |
Sundeep Ghuman | 7c32aa3 | 2017-06-26 17:13:00 -0700 | [diff] [blame] | 60 | @Test |
Fan Zhang | 60b850d | 2017-07-18 15:59:06 -0700 | [diff] [blame] | 61 | public void refresh_openNetwork_updateContentDescription() { |
| 62 | final String ssid = "ssid"; |
| 63 | final String summary = "connected"; |
| 64 | final int security = AccessPoint.SECURITY_WEP; |
| 65 | final AccessPoint ap = new TestAccessPointBuilder(mContext) |
| 66 | .setSsid(ssid) |
| 67 | .setSecurity(security) |
| 68 | .build(); |
| 69 | final AccessPointPreference pref = mock(AccessPointPreference.class); |
| 70 | when(pref.getTitle()).thenReturn(ssid); |
| 71 | when(pref.getSummary()).thenReturn(summary); |
| 72 | |
| 73 | assertThat(AccessPointPreference.buildContentDescription( |
| 74 | RuntimeEnvironment.application, pref, ap)) |
| 75 | .isEqualTo("ssid,connected,Wifi signal full.,Secure network"); |
| 76 | } |
Eric Schwarzenbach | 6efd09a | 2017-08-22 10:33:00 -0700 | [diff] [blame] | 77 | |
| 78 | @Test |
| 79 | public void refresh_shouldUpdateIcon() { |
| 80 | int level = 1; |
| 81 | when(mockAccessPoint.getSpeed()).thenReturn(0); |
| 82 | when(mockAccessPoint.getLevel()).thenReturn(level); |
| 83 | |
| 84 | AccessPointPreference pref = createWithAccessPoint(mockAccessPoint); |
| 85 | pref.refresh(); |
| 86 | |
| 87 | verify(mockIconInjector).getIcon(level); |
| 88 | } |
Fan Zhang | b6cec0d | 2017-09-05 11:02:03 -0700 | [diff] [blame] | 89 | |
| 90 | @Test |
| 91 | public void refresh_setTitle_shouldUseSsidString() { |
| 92 | final String ssid = "ssid"; |
| 93 | final String summary = "connected"; |
| 94 | final int security = AccessPoint.SECURITY_WEP; |
| 95 | final AccessPoint ap = new TestAccessPointBuilder(mContext) |
| 96 | .setSsid(ssid) |
| 97 | .setSecurity(security) |
| 98 | .build(); |
| 99 | final AccessPointPreference preference = mock(AccessPointPreference.class); |
| 100 | |
Quang Luong | 19429e4 | 2019-02-20 17:10:51 -0800 | [diff] [blame] | 101 | AccessPointPreference.setTitle(preference, ap); |
Fan Zhang | b6cec0d | 2017-09-05 11:02:03 -0700 | [diff] [blame] | 102 | verify(preference).setTitle(ssid); |
| 103 | } |
Sundeep Ghuman | 7c32aa3 | 2017-06-26 17:13:00 -0700 | [diff] [blame] | 104 | } |