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