1 /* 2 * Copyright (C) 2020 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 com.android.deskclock.worldclock; 18 19 import android.widget.ListAdapter; 20 import android.widget.ListView; 21 22 import androidx.appcompat.widget.SearchView; 23 import androidx.test.InstrumentationRegistry; 24 import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner; 25 import androidx.test.rule.ActivityTestRule; 26 27 import com.android.deskclock.R; 28 import com.android.deskclock.data.City; 29 import com.android.deskclock.data.DataModel; 30 31 import org.junit.After; 32 import org.junit.Before; 33 import org.junit.Rule; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 import java.util.Collections; 38 import java.util.Locale; 39 40 import static org.junit.Assert.assertEquals; 41 import static org.junit.Assert.assertNotNull; 42 43 /** 44 * Exercise the user interface that adjusts the selected cities. 45 */ 46 @RunWith(AndroidJUnit4ClassRunner.class) 47 public class CitySelectionActivityTest { 48 49 private CitySelectionActivity activity; 50 private ListView cities; 51 private ListAdapter citiesAdapter; 52 private SearchView searchView; 53 private Locale defaultLocale; 54 55 @Rule 56 public ActivityTestRule<CitySelectionActivity> rule = 57 new ActivityTestRule<>(CitySelectionActivity.class, true); 58 59 @Before setUp()60 public void setUp() { 61 defaultLocale = Locale.getDefault(); 62 Locale.setDefault(new Locale("en", "US")); 63 Runnable setUpRunnable = () -> { 64 final City city = DataModel.getDataModel().getAllCities().get(0); 65 DataModel.getDataModel().setSelectedCities(Collections.singletonList(city)); 66 activity = rule.getActivity(); 67 cities = activity.findViewById(R.id.cities_list); 68 citiesAdapter = (ListAdapter) cities.getAdapter(); 69 searchView = activity.findViewById(R.id.menu_item_search); 70 }; 71 InstrumentationRegistry.getInstrumentation().runOnMainSync(setUpRunnable); 72 } 73 74 @After tearDown()75 public void tearDown() { 76 activity = null; 77 cities = null; 78 searchView = null; 79 Locale.setDefault(defaultLocale); 80 } 81 82 @Test validateDefaultState()83 public void validateDefaultState() { 84 Runnable testRunable = () -> { 85 assertNotNull(searchView); 86 assertNotNull(cities); 87 assertNotNull(citiesAdapter); 88 assertEquals(340, citiesAdapter.getCount()); 89 }; 90 InstrumentationRegistry.getInstrumentation().runOnMainSync(testRunable); 91 } 92 93 @Test searchCities()94 public void searchCities() { 95 Runnable testRunable = () -> { 96 // Search for cities starting with Z. 97 searchView.setQuery("Z", true); 98 assertEquals(2, citiesAdapter.getCount()); 99 assertItemContent(0, "Zagreb"); 100 assertItemContent(1, "Zurich"); 101 102 // Clear the filter query. 103 searchView.setQuery("", true); 104 assertEquals(340, citiesAdapter.getCount()); 105 }; 106 InstrumentationRegistry.getInstrumentation().runOnMainSync(testRunable); 107 } 108 assertItemContent(int index, String cityName)109 private void assertItemContent(int index, String cityName) { 110 final City city = (City) citiesAdapter.getItem(index); 111 assertEquals(cityName, city.getName()); 112 } 113 } 114