1 /* 2 * Copyright (C) 2018 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.settings.datetime.timezone; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver; 22 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 import java.util.Locale; 29 import java.util.concurrent.CountDownLatch; 30 import java.util.concurrent.TimeUnit; 31 import org.robolectric.RobolectricTestRunner; 32 33 @RunWith(RobolectricTestRunner.class) 34 public class BaseTimeZoneAdapterTest { 35 36 @Test testFilter()37 public void testFilter() throws InterruptedException { 38 TestItem US = new TestItem("United States"); 39 TestItem HK = new TestItem("Hong Kong"); 40 TestItem UK = new TestItem("United Kingdom", new String[] { "United Kingdom", 41 "Great Britain"}); 42 TestItem secretCountry = new TestItem("no name", new String[] { "Secret"}); 43 List<TestItem> items = new ArrayList<>(); 44 items.add(US); 45 items.add(HK); 46 items.add(UK); 47 items.add(secretCountry); 48 49 TestTimeZoneAdapter adapter = new TestTimeZoneAdapter(items); 50 assertSearch(adapter, "", items.toArray(new TestItem[0])); 51 assertSearch(adapter, "Unit", US, UK); 52 assertSearch(adapter, "kon", HK); 53 assertSearch(adapter, "brit", UK); 54 assertSearch(adapter, "sec", secretCountry); 55 } 56 assertSearch(TestTimeZoneAdapter adapter , String searchText, TestItem... items)57 private void assertSearch(TestTimeZoneAdapter adapter , String searchText, TestItem... items) 58 throws InterruptedException { 59 Observer observer = new Observer(adapter); 60 adapter.getFilter().filter(searchText); 61 observer.await(); 62 assertThat(adapter.getItemCount()).isEqualTo(items.length); 63 for (int i = 0; i < items.length; i++) { 64 assertThat(adapter.getDataItem(i)).isEqualTo(items[i]); 65 } 66 } 67 68 private static class Observer extends AdapterDataObserver { 69 70 private final CountDownLatch mLatch = new CountDownLatch(1); 71 private final TestTimeZoneAdapter mAdapter; 72 Observer(TestTimeZoneAdapter adapter)73 private Observer(TestTimeZoneAdapter adapter) { 74 mAdapter = adapter; 75 mAdapter.registerAdapterDataObserver(this); 76 } 77 78 @Override onChanged()79 public void onChanged() { 80 mAdapter.unregisterAdapterDataObserver(this); 81 mLatch.countDown(); 82 } 83 await()84 private void await() throws InterruptedException { 85 mLatch.await(2L, TimeUnit.SECONDS); 86 } 87 } 88 89 private static class TestTimeZoneAdapter extends BaseTimeZoneAdapter<TestItem> { 90 TestTimeZoneAdapter(List<TestItem> items)91 private TestTimeZoneAdapter(List<TestItem> items) { 92 super(items, position -> {}, Locale.US, false /* showItemSummary */, 93 null /* headerText */); 94 } 95 } 96 97 private static class TestItem implements BaseTimeZoneAdapter.AdapterItem { 98 99 private final String mTitle; 100 private final String[] mSearchKeys; 101 TestItem(String title)102 TestItem(String title) { 103 this(title, new String[] { title }); 104 } 105 TestItem(String title, String[] searchKeys)106 TestItem(String title, String[] searchKeys) { 107 mTitle = title; 108 mSearchKeys = searchKeys; 109 } 110 111 @Override getTitle()112 public CharSequence getTitle() { 113 return mTitle; 114 } 115 116 @Override getSummary()117 public CharSequence getSummary() { 118 return null; 119 } 120 121 @Override getIconText()122 public String getIconText() { 123 return null; 124 } 125 126 @Override getCurrentTime()127 public String getCurrentTime() { 128 return null; 129 } 130 131 @Override getItemId()132 public long getItemId() { 133 return 0; 134 } 135 136 @Override getSearchKeys()137 public String[] getSearchKeys() { 138 return mSearchKeys; 139 } 140 } 141 } 142