@ -0,0 +1,194 @@
|
||||
/*
|
||||
* Copyright (c) 2024, The MiCode Open Source Community (www.micode.net)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.micode.notes.search;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* SearchHistory的单元测试类
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SearchHistoryTest {
|
||||
@Mock
|
||||
private Context mMockContext;
|
||||
private SearchHistory mSearchHistory;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
// 初始化SearchHistory
|
||||
mSearchHistory = SearchHistory.getInstance(mMockContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试单例模式
|
||||
*/
|
||||
@Test
|
||||
public void testSingleton() {
|
||||
SearchHistory instance1 = SearchHistory.getInstance(mMockContext);
|
||||
SearchHistory instance2 = SearchHistory.getInstance(mMockContext);
|
||||
assertSame("SearchHistory should be a singleton", instance1, instance2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试添加和获取搜索历史
|
||||
*/
|
||||
@Test
|
||||
public void testAddAndGetSearchHistory() {
|
||||
// 清除现有历史记录
|
||||
mSearchHistory.clearSearchHistory();
|
||||
|
||||
// 添加搜索历史
|
||||
String keyword1 = "test1";
|
||||
String keyword2 = "test2";
|
||||
String keyword3 = "test3";
|
||||
|
||||
mSearchHistory.addSearchHistory(keyword1);
|
||||
mSearchHistory.addSearchHistory(keyword2);
|
||||
mSearchHistory.addSearchHistory(keyword3);
|
||||
|
||||
// 获取搜索历史
|
||||
List<String> history = mSearchHistory.getRecentSearches(10);
|
||||
assertNotNull("Search history should not be null", history);
|
||||
|
||||
// 验证历史记录顺序(最新的在前面)
|
||||
assertEquals("Latest search should be first", keyword3, history.get(0));
|
||||
assertEquals("Second latest search should be second", keyword2, history.get(1));
|
||||
assertEquals("Oldest search should be last", keyword1, history.get(2));
|
||||
|
||||
// 测试添加重复关键词
|
||||
mSearchHistory.addSearchHistory(keyword1);
|
||||
history = mSearchHistory.getRecentSearches(10);
|
||||
assertEquals("Duplicate keyword should be moved to front", keyword1, history.get(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试获取匹配的搜索历史
|
||||
*/
|
||||
@Test
|
||||
public void testGetMatchingSearches() {
|
||||
// 清除现有历史记录
|
||||
mSearchHistory.clearSearchHistory();
|
||||
|
||||
// 添加搜索历史
|
||||
mSearchHistory.addSearchHistory("test1");
|
||||
mSearchHistory.addSearchHistory("test2");
|
||||
mSearchHistory.addSearchHistory("example");
|
||||
mSearchHistory.addSearchHistory("test3");
|
||||
|
||||
// 获取匹配的搜索历史
|
||||
List<String> matching = mSearchHistory.getMatchingSearches("test", 5);
|
||||
assertNotNull("Matching searches should not be null", matching);
|
||||
assertTrue("Should find matching searches", matching.size() >= 3);
|
||||
|
||||
// 验证所有匹配的关键词都包含"test"
|
||||
for (String keyword : matching) {
|
||||
assertTrue("Matching keyword should contain 'test'", keyword.contains("test"));
|
||||
}
|
||||
|
||||
// 测试不匹配的关键词
|
||||
matching = mSearchHistory.getMatchingSearches("nonexistent", 5);
|
||||
assertTrue("Should not find matching searches for nonexistent keyword", matching.isEmpty());
|
||||
|
||||
// 测试空关键词
|
||||
matching = mSearchHistory.getMatchingSearches("", 5);
|
||||
assertTrue("Should return empty list for empty keyword", matching.isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试删除搜索历史
|
||||
*/
|
||||
@Test
|
||||
public void testDeleteSearchHistory() {
|
||||
// 清除现有历史记录
|
||||
mSearchHistory.clearSearchHistory();
|
||||
|
||||
// 添加搜索历史
|
||||
String keyword1 = "test1";
|
||||
String keyword2 = "test2";
|
||||
|
||||
mSearchHistory.addSearchHistory(keyword1);
|
||||
mSearchHistory.addSearchHistory(keyword2);
|
||||
|
||||
// 删除一个关键词
|
||||
mSearchHistory.deleteSearchHistory(keyword1);
|
||||
List<String> history = mSearchHistory.getRecentSearches(10);
|
||||
assertEquals("Should have one less item after deletion", 1, history.size());
|
||||
assertFalse("Deleted keyword should not be in history", history.contains(keyword1));
|
||||
assertTrue("Remaining keyword should be in history", history.contains(keyword2));
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试清除搜索历史
|
||||
*/
|
||||
@Test
|
||||
public void testClearSearchHistory() {
|
||||
// 添加搜索历史
|
||||
mSearchHistory.addSearchHistory("test1");
|
||||
mSearchHistory.addSearchHistory("test2");
|
||||
|
||||
// 清除搜索历史
|
||||
mSearchHistory.clearSearchHistory();
|
||||
List<String> history = mSearchHistory.getRecentSearches(10);
|
||||
assertNotNull("Search history should not be null after clearing", history);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试最大历史记录数限制
|
||||
*/
|
||||
@Test
|
||||
public void testMaxHistoryCount() {
|
||||
// 清除现有历史记录
|
||||
mSearchHistory.clearSearchHistory();
|
||||
|
||||
// 设置最大历史记录数
|
||||
int maxCount = 5;
|
||||
mSearchHistory.setMaxHistoryCount(maxCount);
|
||||
|
||||
// 添加超过最大数量的搜索历史
|
||||
for (int i = 0; i < maxCount + 3; i++) {
|
||||
mSearchHistory.addSearchHistory("test" + i);
|
||||
}
|
||||
|
||||
// 获取搜索历史
|
||||
List<String> history = mSearchHistory.getRecentSearches(10);
|
||||
assertTrue("Search history should not exceed max count", history.size() <= maxCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试添加空关键词
|
||||
*/
|
||||
@Test
|
||||
public void testAddEmptyKeyword() {
|
||||
// 清除现有历史记录
|
||||
mSearchHistory.clearSearchHistory();
|
||||
|
||||
// 添加空关键词
|
||||
mSearchHistory.addSearchHistory("");
|
||||
List<String> history = mSearchHistory.getRecentSearches(10);
|
||||
assertTrue("Empty keyword should not be added to history", history.isEmpty());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,146 @@
|
||||
/*
|
||||
* Copyright (c) 2024, The MiCode Open Source Community (www.micode.net)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.micode.notes.search;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
|
||||
import net.micode.notes.data.Notes;
|
||||
import net.micode.notes.data.Notes.DataColumns;
|
||||
import net.micode.notes.data.Notes.NoteColumns;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* SearchManager的单元测试类
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SearchManagerTest {
|
||||
@Mock
|
||||
private Context mMockContext;
|
||||
@Mock
|
||||
private ContentResolver mMockContentResolver;
|
||||
@Mock
|
||||
private Cursor mMockCursor;
|
||||
|
||||
private SearchManager mSearchManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
// 配置Context和ContentResolver的mock行为
|
||||
when(mMockContext.getApplicationContext()).thenReturn(mMockContext);
|
||||
when(mMockContext.getContentResolver()).thenReturn(mMockContentResolver);
|
||||
|
||||
// 初始化SearchManager
|
||||
mSearchManager = SearchManager.getInstance(mMockContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试单例模式
|
||||
*/
|
||||
@Test
|
||||
public void testSingleton() {
|
||||
SearchManager instance1 = SearchManager.getInstance(mMockContext);
|
||||
SearchManager instance2 = SearchManager.getInstance(mMockContext);
|
||||
assertSame("SearchManager should be a singleton", instance1, instance2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试空关键词搜索
|
||||
*/
|
||||
@Test
|
||||
public void testEmptyKeywordSearch() {
|
||||
List<SearchResult> results = mSearchManager.search("", SearchManager.SortBy.RELEVANCE, 10);
|
||||
assertTrue("Search with empty keyword should return empty list", results.isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试搜索建议功能
|
||||
*/
|
||||
@Test
|
||||
public void testSearchSuggestions() {
|
||||
// 测试空关键词建议
|
||||
List<String> suggestions = mSearchManager.getSearchSuggestions("", 5);
|
||||
assertNotNull("Search suggestions should not be null", suggestions);
|
||||
|
||||
// 测试非空关键词建议
|
||||
suggestions = mSearchManager.getSearchSuggestions("test", 5);
|
||||
assertNotNull("Search suggestions should not be null", suggestions);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试搜索历史功能
|
||||
*/
|
||||
@Test
|
||||
public void testSearchHistory() {
|
||||
// 测试添加搜索历史
|
||||
mSearchManager.getSearchHistory(10);
|
||||
|
||||
// 测试获取搜索历史
|
||||
List<String> history = mSearchManager.getSearchHistory(10);
|
||||
assertNotNull("Search history should not be null", history);
|
||||
|
||||
// 测试清除搜索历史
|
||||
mSearchManager.clearSearchHistory();
|
||||
history = mSearchManager.getSearchHistory(10);
|
||||
assertNotNull("Search history should not be null after clearing", history);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试关键词高亮功能
|
||||
*/
|
||||
@Test
|
||||
public void testHighlightKeyword() {
|
||||
String text = "This is a test text for highlighting keyword test";
|
||||
String keyword = "test";
|
||||
String highlighted = mSearchManager.highlightKeyword(text, keyword);
|
||||
|
||||
// 验证高亮结果
|
||||
assertNotNull("Highlighted text should not be null", highlighted);
|
||||
assertTrue("Highlighted text should contain HTML tags", highlighted.contains("<font"));
|
||||
assertTrue("Highlighted text should contain keyword", highlighted.contains(keyword));
|
||||
|
||||
// 测试空文本高亮
|
||||
highlighted = mSearchManager.highlightKeyword(null, keyword);
|
||||
assertNull("Highlighted text should be null for null input", highlighted);
|
||||
|
||||
// 测试空关键词高亮
|
||||
highlighted = mSearchManager.highlightKeyword(text, null);
|
||||
assertEquals("Highlighted text should be same as input for null keyword", text, highlighted);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试排序方式枚举
|
||||
*/
|
||||
@Test
|
||||
public void testSortByEnum() {
|
||||
assertEquals("RELEVANCE should be 0", 0, SearchManager.SortBy.RELEVANCE.ordinal());
|
||||
assertEquals("CREATED_DATE should be 1", 1, SearchManager.SortBy.CREATED_DATE.ordinal());
|
||||
assertEquals("MODIFIED_DATE should be 2", 2, SearchManager.SortBy.MODIFIED_DATE.ordinal());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,137 @@
|
||||
/*
|
||||
* Copyright (c) 2024, The MiCode Open Source Community (www.micode.net)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.micode.notes.search;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* SearchResult的单元测试类
|
||||
*/
|
||||
public class SearchResultTest {
|
||||
private SearchResult mSearchResult;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mSearchResult = new SearchResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试相关度得分计算
|
||||
*/
|
||||
@Test
|
||||
public void testRelevanceScoreCalculation() {
|
||||
// 设置测试数据
|
||||
mSearchResult.setTitle("Test Title");
|
||||
mSearchResult.setContent("This is a test content for testing relevance score calculation");
|
||||
mSearchResult.setSnippet("Test snippet");
|
||||
|
||||
// 测试完全匹配标题
|
||||
mSearchResult.calculateRelevanceScore("Test Title");
|
||||
float score = mSearchResult.getRelevanceScore();
|
||||
assertTrue("Exact title match should have high score", score > 9.0f);
|
||||
|
||||
// 测试部分匹配标题
|
||||
mSearchResult.calculateRelevanceScore("Test");
|
||||
float partialTitleScore = mSearchResult.getRelevanceScore();
|
||||
assertTrue("Partial title match should have high score", partialTitleScore > 4.0f);
|
||||
|
||||
// 测试内容匹配
|
||||
mSearchResult.calculateRelevanceScore("content");
|
||||
float contentScore = mSearchResult.getRelevanceScore();
|
||||
assertTrue("Content match should have moderate score", contentScore > 0.0f);
|
||||
|
||||
// 测试多次匹配
|
||||
mSearchResult.calculateRelevanceScore("test");
|
||||
float multipleMatchScore = mSearchResult.getRelevanceScore();
|
||||
assertTrue("Multiple matches should have higher score", multipleMatchScore > contentScore);
|
||||
|
||||
// 测试不匹配
|
||||
mSearchResult.calculateRelevanceScore("nonexistent");
|
||||
float noMatchScore = mSearchResult.getRelevanceScore();
|
||||
assertEquals("No match should have zero score", 0.0f, noMatchScore, 0.001f);
|
||||
|
||||
// 测试空关键词
|
||||
mSearchResult.calculateRelevanceScore("");
|
||||
float emptyKeywordScore = mSearchResult.getRelevanceScore();
|
||||
assertEquals("Empty keyword should have zero score", 0.0f, emptyKeywordScore, 0.001f);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试SearchResult的getter和setter方法
|
||||
*/
|
||||
@Test
|
||||
public void testGetterSetterMethods() {
|
||||
// 测试NoteId
|
||||
long noteId = 12345;
|
||||
mSearchResult.setNoteId(noteId);
|
||||
assertEquals("NoteId should be set correctly", noteId, mSearchResult.getNoteId());
|
||||
|
||||
// 测试Title
|
||||
String title = "Test Title";
|
||||
mSearchResult.setTitle(title);
|
||||
assertEquals("Title should be set correctly", title, mSearchResult.getTitle());
|
||||
|
||||
// 测试Content
|
||||
String content = "Test Content";
|
||||
mSearchResult.setContent(content);
|
||||
assertEquals("Content should be set correctly", content, mSearchResult.getContent());
|
||||
|
||||
// 测试Snippet
|
||||
String snippet = "Test Snippet";
|
||||
mSearchResult.setSnippet(snippet);
|
||||
assertEquals("Snippet should be set correctly", snippet, mSearchResult.getSnippet());
|
||||
|
||||
// 测试CreatedDate
|
||||
long createdDate = System.currentTimeMillis();
|
||||
mSearchResult.setCreatedDate(createdDate);
|
||||
assertEquals("CreatedDate should be set correctly", createdDate, mSearchResult.getCreatedDate());
|
||||
|
||||
// 测试ModifiedDate
|
||||
long modifiedDate = System.currentTimeMillis();
|
||||
mSearchResult.setModifiedDate(modifiedDate);
|
||||
assertEquals("ModifiedDate should be set correctly", modifiedDate, mSearchResult.getModifiedDate());
|
||||
|
||||
// 测试BgColorId
|
||||
int bgColorId = 1;
|
||||
mSearchResult.setBgColorId(bgColorId);
|
||||
assertEquals("BgColorId should be set correctly", bgColorId, mSearchResult.getBgColorId());
|
||||
|
||||
// 测试RelevanceScore
|
||||
float relevanceScore = 8.5f;
|
||||
mSearchResult.setRelevanceScore(relevanceScore);
|
||||
assertEquals("RelevanceScore should be set correctly", relevanceScore, mSearchResult.getRelevanceScore(), 0.001f);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试toString方法
|
||||
*/
|
||||
@Test
|
||||
public void testToString() {
|
||||
mSearchResult.setNoteId(123);
|
||||
mSearchResult.setTitle("Test Title");
|
||||
mSearchResult.setRelevanceScore(5.5f);
|
||||
|
||||
String resultString = mSearchResult.toString();
|
||||
assertNotNull("toString should not return null", resultString);
|
||||
assertTrue("toString should contain noteId", resultString.contains("123"));
|
||||
assertTrue("toString should contain title", resultString.contains("Test Title"));
|
||||
assertTrue("toString should contain relevanceScore", resultString.contains("5.5"));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue