Jason Chang | fe43b0d | 2018-12-25 19:16:42 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.documentsui.queries; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import android.content.Context; |
Ivan Chiang | c955ac0 | 2019-11-14 19:04:28 +0800 | [diff] [blame] | 22 | |
Jason Chang | fe43b0d | 2018-12-25 19:16:42 +0800 | [diff] [blame] | 23 | import androidx.test.InstrumentationRegistry; |
| 24 | import androidx.test.filters.MediumTest; |
| 25 | import androidx.test.runner.AndroidJUnit4; |
| 26 | |
Jason Chang | fe43b0d | 2018-12-25 19:16:42 +0800 | [diff] [blame] | 27 | import org.junit.After; |
Ivan Chiang | c955ac0 | 2019-11-14 19:04:28 +0800 | [diff] [blame] | 28 | import org.junit.Before; |
| 29 | import org.junit.Ignore; |
Jason Chang | fe43b0d | 2018-12-25 19:16:42 +0800 | [diff] [blame] | 30 | import org.junit.Test; |
| 31 | import org.junit.runner.RunWith; |
| 32 | |
Ivan Chiang | c955ac0 | 2019-11-14 19:04:28 +0800 | [diff] [blame] | 33 | import java.util.List; |
| 34 | import java.util.concurrent.CountDownLatch; |
| 35 | import java.util.concurrent.TimeUnit; |
| 36 | |
Jason Chang | fe43b0d | 2018-12-25 19:16:42 +0800 | [diff] [blame] | 37 | @RunWith(AndroidJUnit4.class) |
| 38 | @MediumTest |
| 39 | public final class SearchHistoryManagerTest { |
| 40 | |
| 41 | private Context mContext; |
| 42 | private CountDownLatch mLatch ; |
| 43 | private SearchHistoryManager mManager; |
| 44 | private SearchHistoryManager.DatabaseChangedListener mListener; |
| 45 | private int mIntResult; |
| 46 | private long mLongResult; |
| 47 | |
| 48 | @Before |
| 49 | public void setUp() throws Exception { |
| 50 | mContext = InstrumentationRegistry.getTargetContext(); |
| 51 | mManager = SearchHistoryManager.getInstance(mContext); |
Ivan Chiang | c955ac0 | 2019-11-14 19:04:28 +0800 | [diff] [blame] | 52 | //clearData(); |
Jason Chang | fe43b0d | 2018-12-25 19:16:42 +0800 | [diff] [blame] | 53 | mIntResult = -1; |
| 54 | mLongResult = -1; |
| 55 | } |
| 56 | |
| 57 | @After |
| 58 | public void tearDown() { |
| 59 | mListener = null; |
Ivan Chiang | c955ac0 | 2019-11-14 19:04:28 +0800 | [diff] [blame] | 60 | //clearData(); |
Jason Chang | fe43b0d | 2018-12-25 19:16:42 +0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | private void clearData() { |
| 64 | final List<String> list = mManager.getHistoryList(null); |
| 65 | for (int i = 0; i < list.size(); i++) { |
| 66 | mManager.deleteHistory(list.get(i)); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | @Test |
Ivan Chiang | c955ac0 | 2019-11-14 19:04:28 +0800 | [diff] [blame] | 71 | @Ignore |
Jason Chang | fe43b0d | 2018-12-25 19:16:42 +0800 | [diff] [blame] | 72 | public void testAddHistory() throws Exception { |
| 73 | mLatch = new CountDownLatch(2); |
| 74 | mListener = new SearchHistoryManager.DatabaseChangedListener() { |
| 75 | @Override |
| 76 | public void onAddChangedListener(long longResult) { |
| 77 | mLongResult = longResult; |
| 78 | mLatch.countDown(); |
| 79 | } |
| 80 | @Override |
| 81 | public void onDeleteChangedListener(int intResult) { } |
| 82 | @Override |
| 83 | public void onPostExecute() { } |
| 84 | |
| 85 | }; |
| 86 | mManager.setDatabaseListener(mListener); |
| 87 | mManager.addHistory("testKeyword"); |
| 88 | mLatch.await(1, TimeUnit.SECONDS); |
| 89 | |
| 90 | assertThat(mLongResult).isGreaterThan(0L); |
| 91 | } |
| 92 | |
| 93 | @Test |
Ivan Chiang | c955ac0 | 2019-11-14 19:04:28 +0800 | [diff] [blame] | 94 | @Ignore |
Jason Chang | fe43b0d | 2018-12-25 19:16:42 +0800 | [diff] [blame] | 95 | public void testDeleteHistory() throws Exception { |
| 96 | mLatch = new CountDownLatch(2); |
| 97 | mListener = new SearchHistoryManager.DatabaseChangedListener() { |
| 98 | @Override |
| 99 | public void onAddChangedListener(long longResult) { |
| 100 | mLongResult = longResult; |
| 101 | mLatch.countDown(); |
| 102 | } |
| 103 | @Override |
| 104 | public void onPostExecute() { } |
| 105 | |
| 106 | @Override public void onDeleteChangedListener(int intResult) { |
| 107 | mIntResult = intResult; |
| 108 | mLatch.countDown(); |
| 109 | } |
| 110 | }; |
| 111 | mManager.setDatabaseListener(mListener); |
| 112 | |
| 113 | mManager.addHistory("testDeleteKeyword"); |
| 114 | mLatch.await(1, TimeUnit.SECONDS); |
| 115 | assertThat(mLongResult).isGreaterThan(0L); |
| 116 | |
| 117 | // TODO: Solving this tricky usage of new CountDownLatch(2) count with bg/127610355 |
| 118 | // Using this tricky way is for making sure the result synchronization of public APIs |
| 119 | // getHistoryList()/addHistory()/deleteHistory() with database processing. |
| 120 | // From design contract and non-blocking UI design, not necessarily doing synchronization |
| 121 | // from code level, therefore doing this tricky usage of new CountDownLatch in test case for |
| 122 | // guarantee the synchronization. |
| 123 | mLatch = new CountDownLatch(2); |
| 124 | mManager.deleteHistory("testDeleteKeyword"); |
| 125 | mLatch.await(1, TimeUnit.SECONDS); |
| 126 | assertThat(mIntResult).isGreaterThan(0); |
| 127 | } |
| 128 | |
| 129 | @Test |
Ivan Chiang | c955ac0 | 2019-11-14 19:04:28 +0800 | [diff] [blame] | 130 | @Ignore |
Jason Chang | fe43b0d | 2018-12-25 19:16:42 +0800 | [diff] [blame] | 131 | public void testGetHistoryList() throws Exception { |
| 132 | mLatch = new CountDownLatch(2); |
| 133 | mListener = new SearchHistoryManager.DatabaseChangedListener() { |
| 134 | @Override |
| 135 | public void onAddChangedListener(long longResult) { } |
| 136 | @Override |
| 137 | public void onDeleteChangedListener(int intResult) { } |
| 138 | @Override |
| 139 | public void onPostExecute() { |
| 140 | mLatch.countDown(); |
| 141 | } |
| 142 | }; |
| 143 | mManager.setDatabaseListener(mListener); |
| 144 | |
| 145 | mManager.addHistory("abcdefghijk"); |
| 146 | mLatch.await(1, TimeUnit.SECONDS); |
| 147 | |
| 148 | mLatch = new CountDownLatch(2); |
| 149 | mManager.addHistory("lmnop"); |
| 150 | mLatch.await(1, TimeUnit.SECONDS); |
| 151 | |
| 152 | mLatch = new CountDownLatch(2); |
| 153 | mManager.addHistory("qrstuv"); |
| 154 | mLatch.await(1, TimeUnit.SECONDS); |
| 155 | |
| 156 | InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 157 | assertThat(mManager.getHistoryList(null).size()).isEqualTo(3); |
| 158 | |
| 159 | // Test the last adding history should be the first item in the list. |
| 160 | assertThat(mManager.getHistoryList(null).get(0)).contains("qrstuv"); |
| 161 | |
| 162 | assertThat(mManager.getHistoryList(null).get(2)).contains("abcdefghijk"); |
| 163 | } |
| 164 | } |