blob: 3c216c7a70b446842084f2c4ac7ce7c8cd28a667 [file] [log] [blame]
Jason Changfe43b0d2018-12-25 19:16:42 +08001/*
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
17package com.android.documentsui.queries;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import android.content.Context;
Ivan Chiangc955ac02019-11-14 19:04:28 +080022
Jason Changfe43b0d2018-12-25 19:16:42 +080023import androidx.test.InstrumentationRegistry;
24import androidx.test.filters.MediumTest;
25import androidx.test.runner.AndroidJUnit4;
26
Jason Changfe43b0d2018-12-25 19:16:42 +080027import org.junit.After;
Ivan Chiangc955ac02019-11-14 19:04:28 +080028import org.junit.Before;
29import org.junit.Ignore;
Jason Changfe43b0d2018-12-25 19:16:42 +080030import org.junit.Test;
31import org.junit.runner.RunWith;
32
Ivan Chiangc955ac02019-11-14 19:04:28 +080033import java.util.List;
34import java.util.concurrent.CountDownLatch;
35import java.util.concurrent.TimeUnit;
36
Jason Changfe43b0d2018-12-25 19:16:42 +080037@RunWith(AndroidJUnit4.class)
38@MediumTest
39public 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 Chiangc955ac02019-11-14 19:04:28 +080052 //clearData();
Jason Changfe43b0d2018-12-25 19:16:42 +080053 mIntResult = -1;
54 mLongResult = -1;
55 }
56
57 @After
58 public void tearDown() {
59 mListener = null;
Ivan Chiangc955ac02019-11-14 19:04:28 +080060 //clearData();
Jason Changfe43b0d2018-12-25 19:16:42 +080061 }
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 Chiangc955ac02019-11-14 19:04:28 +080071 @Ignore
Jason Changfe43b0d2018-12-25 19:16:42 +080072 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 Chiangc955ac02019-11-14 19:04:28 +080094 @Ignore
Jason Changfe43b0d2018-12-25 19:16:42 +080095 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 Chiangc955ac02019-11-14 19:04:28 +0800130 @Ignore
Jason Changfe43b0d2018-12-25 19:16:42 +0800131 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}