1 /* 2 * Copyright (C) 2010 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.browser; 18 19 import android.app.SearchManager; 20 import android.content.ContentValues; 21 import android.database.Cursor; 22 import android.net.Uri; 23 import android.test.AndroidTestCase; 24 import android.test.suitebuilder.annotation.MediumTest; 25 26 import java.util.ArrayList; 27 import java.util.Arrays; 28 29 /** 30 * Unit tests for {@link BrowserProvider}. 31 */ 32 @MediumTest 33 public class BrowserProviderTests extends AndroidTestCase { 34 35 private ArrayList<Uri> mDeleteUris; 36 37 @Override 38 protected void setUp() throws Exception { 39 mDeleteUris = new ArrayList<Uri>(); 40 super.setUp(); 41 } 42 43 @Override 44 protected void tearDown() throws Exception { 45 for (Uri uri : mDeleteUris) { 46 deleteUri(uri); 47 } 48 super.tearDown(); 49 } 50 51 public void testHasDefaultBookmarks() { 52 Cursor c = getBookmarksSuggest(""); 53 try { 54 assertTrue("No default bookmarks", c.getCount() > 0); 55 } finally { 56 c.close(); 57 } 58 } 59 60 public void testPartialFirstTitleWord() { 61 assertInsertQuery("http://www.example.com/rasdfe", "nfgjra sdfywe", "nfgj"); 62 } 63 64 public void testFullFirstTitleWord() { 65 assertInsertQuery("http://www.example.com/", "nfgjra dfger", "nfgjra"); 66 } 67 68 public void testFullFirstTitleWordPartialSecond() { 69 assertInsertQuery("http://www.example.com/", "nfgjra dfger", "nfgjra df"); 70 } 71 72 public void testFullTitle() { 73 assertInsertQuery("http://www.example.com/", "nfgjra dfger", "nfgjra dfger"); 74 } 75 76 // Not implemented in BrowserProvider 77 // public void testFullSecondTitleWord() { 78 // assertInsertQuery("http://www.example.com/rasdfe", "nfgjra sdfywe", "sdfywe"); 79 // } 80 81 public void testFullTitleJapanese() { 82 String title = "\u30ae\u30e3\u30e9\u30ea\u30fc\u30fcGoogle\u691c\u7d22"; 83 assertInsertQuery("http://www.example.com/sdaga", title, title); 84 } 85 86 public void testPartialTitleJapanese() { 87 String title = "\u30ae\u30e3\u30e9\u30ea\u30fc\u30fcGoogle\u691c\u7d22"; 88 String query = "\u30ae\u30e3\u30e9\u30ea\u30fc"; 89 assertInsertQuery("http://www.example.com/sdaga", title, query); 90 } 91 92 // Test for http://b/issue?id=2152749 93 public void testSoundmarkTitleJapanese() { 94 String title = "\u30ae\u30e3\u30e9\u30ea\u30fc\u30fcGoogle\u691c\u7d22"; 95 String query = "\u30ad\u30e3\u30e9\u30ea\u30fc"; 96 assertInsertQuery("http://www.example.com/sdaga", title, query); 97 } 98 99 // 100 // Utilities 101 // 102 103 private void assertInsertQuery(String url, String title, String query) { 104 addBookmark(url, title); 105 assertQueryReturns(url, title, query); 106 } 107 108 private void assertQueryReturns(String url, String title, String query) { 109 Cursor c = getBookmarksSuggest(query); 110 try { 111 assertTrue(title + " not matched by " + query, c.getCount() > 0); 112 assertTrue("More than one result for " + query, c.getCount() == 1); 113 while (c.moveToNext()) { 114 String text1 = getCol(c, SearchManager.SUGGEST_COLUMN_TEXT_1); 115 assertNotNull(text1); 116 assertEquals("Bad title", title, text1); 117 String text2 = getCol(c, SearchManager.SUGGEST_COLUMN_TEXT_2); 118 assertNotNull(text2); 119 String data = getCol(c, SearchManager.SUGGEST_COLUMN_INTENT_DATA); 120 assertNotNull(data); 121 assertEquals("Bad URL", url, data); 122 } 123 } finally { 124 c.close(); 125 } 126 } 127 128 private Cursor getBookmarksSuggest(String query) { 129 Uri suggestUri = Uri.parse("content://browser/bookmarks/search_suggest_query"); 130 String[] selectionArgs = { query }; 131 Cursor c = getContext().getContentResolver().query(suggestUri, null, "url LIKE ?", 132 selectionArgs, null); 133 assertNotNull(c); 134 return c; 135 } 136 137 private void addBookmark(String url, String title) { 138 Uri uri = insertBookmark(url, title); 139 assertNotNull(uri); 140 assertFalse(android.provider.Browser.BOOKMARKS_URI.equals(uri)); 141 mDeleteUris.add(uri); 142 } 143 144 private Uri insertBookmark(String url, String title) { 145 ContentValues values = new ContentValues(); 146 values.put("title", title); 147 values.put("url", url); 148 values.put("visits", 0); 149 values.put("date", 0); 150 values.put("created", 0); 151 values.put("bookmark", 1); 152 return getContext().getContentResolver().insert(android.provider.Browser.BOOKMARKS_URI, 153 values); 154 } 155 156 private void deleteUri(Uri uri) { 157 int count = getContext().getContentResolver().delete(uri, null, null); 158 assertEquals("Failed to delete " + uri, 1, count); 159 } 160 161 private static String getCol(Cursor c, String name) { 162 int col = c.getColumnIndex(name); 163 String msg = "Column " + name + " not found, columns: " 164 + Arrays.toString(c.getColumnNames()); 165 assertTrue(msg, col >= 0); 166 return c.getString(col); 167 } 168 } 169