1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/tests/testImportFromAndroid.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,207 @@ 1.4 +package org.mozilla.gecko.tests; 1.5 + 1.6 +import java.util.ArrayList; 1.7 + 1.8 +import org.mozilla.gecko.Actions; 1.9 +import org.mozilla.gecko.AppConstants; 1.10 +import org.mozilla.gecko.GeckoProfile; 1.11 + 1.12 +import android.content.ContentResolver; 1.13 +import android.content.ContentValues; 1.14 +import android.database.Cursor; 1.15 +import android.net.Uri; 1.16 +import android.provider.Browser; 1.17 + 1.18 +/** 1.19 + * This test covers the Import from Android feature 1.20 + * The test will save the existing bookmarks and history then will do an Import 1.21 + * After the import it will check that the bookmarks and history items from Android are imported 1.22 + * Then it will test that the old data from Firefox is not lost 1.23 + * At the end will test that a second import will not duplicate information 1.24 + */ 1.25 + 1.26 +public class testImportFromAndroid extends AboutHomeTest { 1.27 + private static final int MAX_WAIT_TIMEOUT = 15000; 1.28 + ArrayList<String> androidData = new ArrayList<String>(); 1.29 + ArrayList<String> firefoxHistory = new ArrayList<String>(); 1.30 + 1.31 + public void testImportFromAndroid() { 1.32 + ArrayList<String> firefoxBookmarks = new ArrayList<String>(); 1.33 + ArrayList<String> oldFirefoxHistory = new ArrayList<String>(); 1.34 + ArrayList<String> oldFirefoxBookmarks = new ArrayList<String>(); 1.35 + blockForGeckoReady(); 1.36 + 1.37 + // Get the Android history 1.38 + androidData = getAndroidUrls("history"); 1.39 + 1.40 + // Add some overlapping data from the Android Stock Browser to Firefox before import 1.41 + addData(); 1.42 + 1.43 + // Get the initial bookmarks and history 1.44 + oldFirefoxBookmarks = mDatabaseHelper.getBrowserDBUrls(DatabaseHelper.BrowserDataType.BOOKMARKS); 1.45 + oldFirefoxHistory = mDatabaseHelper.getBrowserDBUrls(DatabaseHelper.BrowserDataType.HISTORY); 1.46 + 1.47 + // Import the bookmarks and history 1.48 + importDataFromAndroid(); 1.49 + 1.50 + // Get the Android history and the Firefox bookmarks and history lists 1.51 + firefoxHistory = mDatabaseHelper.getBrowserDBUrls(DatabaseHelper.BrowserDataType.HISTORY); 1.52 + firefoxBookmarks = mDatabaseHelper.getBrowserDBUrls(DatabaseHelper.BrowserDataType.BOOKMARKS); 1.53 + 1.54 + /** 1.55 + * Add a delay to make sure the imported items are added to the array lists 1.56 + * if there are a lot of history items in the Android Browser database 1.57 + */ 1.58 + boolean success = waitForTest(new BooleanTest() { 1.59 + @Override 1.60 + public boolean test() { 1.61 + if (androidData.size() <= firefoxHistory.size()) { 1.62 + return true; 1.63 + } else { 1.64 + return false; 1.65 + } 1.66 + } 1.67 + }, MAX_WAIT_MS); 1.68 + 1.69 + /** 1.70 + * Verify the history and bookmarks are imported 1.71 + * Android history also contains the android bookmarks so we don't need to get them separately here 1.72 + */ 1.73 + for (String url:androidData) { 1.74 + mAsserter.ok(firefoxHistory.contains(url)||firefoxBookmarks.contains(url), "Checking if Android" + (firefoxBookmarks.contains(url) ? " Bookmark" : " History item") + " is present", url + " was imported"); 1.75 + } 1.76 + 1.77 + // Verify the original Firefox Bookmarks are not deleted 1.78 + for (String url:oldFirefoxBookmarks) { 1.79 + mAsserter.ok(firefoxBookmarks.contains(url), "Checking if original Firefox Bookmark is present", " Firefox Bookmark " + url + " was not removed"); 1.80 + } 1.81 + 1.82 + // Verify the original Firefox History is not deleted 1.83 + for (String url:oldFirefoxHistory) { 1.84 + mAsserter.ok(firefoxHistory.contains(url), "Checking original Firefox History item is present", " Firefox History item " + url + " was not removed"); 1.85 + } 1.86 + 1.87 + // Import data again and make sure bookmarks are not duplicated 1.88 + importDataFromAndroid(); 1.89 + 1.90 + // Verify bookmarks are not duplicated 1.91 + ArrayList<String> verifiedBookmarks = new ArrayList<String>(); 1.92 + firefoxBookmarks = mDatabaseHelper.getBrowserDBUrls(DatabaseHelper.BrowserDataType.BOOKMARKS); 1.93 + for (String url:firefoxBookmarks) { 1.94 + if (verifiedBookmarks.contains(url)) { 1.95 + mAsserter.ok(false, "Bookmark " + url + " should not be duplicated", "Bookmark is duplicated"); 1.96 + } else { 1.97 + verifiedBookmarks.add(url); 1.98 + mAsserter.ok(true, "Bookmark " + url + " was not duplicated", "Bookmark is unique"); 1.99 + } 1.100 + } 1.101 + 1.102 + // Verify history count is not increased after the second import 1.103 + mAsserter.ok(firefoxHistory.size() == mDatabaseHelper.getBrowserDBUrls(DatabaseHelper.BrowserDataType.HISTORY).size(), "The number of history entries was not increased", "None of the items were duplicated"); 1.104 + } 1.105 + 1.106 + private void addData() { 1.107 + ArrayList<String> androidBookmarks = getAndroidUrls("bookmarks"); 1.108 + 1.109 + // Add a few Bookmarks from Android to Firefox Mobile 1.110 + for (String url:androidBookmarks) { 1.111 + // Add every 3rd bookmark to Firefox Mobile 1.112 + if ((androidBookmarks.indexOf(url) % 3) == 0) { 1.113 + mDatabaseHelper.addOrUpdateMobileBookmark("Bookmark Number" + String.valueOf(androidBookmarks.indexOf(url)), url); 1.114 + } 1.115 + } 1.116 + 1.117 + // Add a few history items in Firefox Mobile 1.118 + ContentResolver resolver = getActivity().getContentResolver(); 1.119 + Uri uri = Uri.parse("content://" + AppConstants.ANDROID_PACKAGE_NAME + ".db.browser/history"); 1.120 + uri = uri.buildUpon().appendQueryParameter("profile", GeckoProfile.DEFAULT_PROFILE) 1.121 + .appendQueryParameter("sync", "true").build(); 1.122 + for (String url:androidData) { 1.123 + // Add every 3rd website from Android History to Firefox Mobile 1.124 + if ((androidData.indexOf(url) % 3) == 0) { 1.125 + ContentValues values = new ContentValues(); 1.126 + values.put("title", "Page" + url); 1.127 + values.put("url", url); 1.128 + values.put("date", System.currentTimeMillis()); 1.129 + values.put("visits", androidData.indexOf(url)); 1.130 + resolver.insert(uri, values); 1.131 + } 1.132 + } 1.133 + } 1.134 + 1.135 + private void importDataFromAndroid() { 1.136 + waitForText("Enter Search or Address"); 1.137 + selectSettingsItem(StringHelper.CUSTOMIZE_SECTION_LABEL, StringHelper.IMPORT_FROM_ANDROID_LABEL); 1.138 + 1.139 + // Wait for the Import form Android pop-up to be opened. It has the same title as the option so waiting for the "Cancel" button 1.140 + waitForText("Cancel"); 1.141 + mSolo.clickOnButton("Import"); 1.142 + 1.143 + // Wait until the import pop-up is dismissed. This depending on the number of items in the android history can take up to a few seconds 1.144 + boolean importComplete = waitForTest(new BooleanTest() { 1.145 + public boolean test() { 1.146 + return !mSolo.searchText("Please wait..."); 1.147 + } 1.148 + }, MAX_WAIT_TIMEOUT); 1.149 + 1.150 + mAsserter.ok(importComplete, "Waiting for import to finish and the pop-up to be dismissed", "Import was completed and the pop-up was dismissed"); 1.151 + 1.152 + // Import has finished. Waiting to get back to the Settings Menu and looking for the Import&Export subsection 1.153 + if ("phone".equals(mDevice.type)) { 1.154 + // Phones don't have headers like tablets, so we need to pop up one more level. 1.155 + waitForText(StringHelper.IMPORT_FROM_ANDROID_LABEL); 1.156 + mActions.sendSpecialKey(Actions.SpecialKey.BACK); 1.157 + } 1.158 + waitForText("Privacy"); // Settings is a header for the settings menu page. Waiting for Privacy ensures we are back in the top Settings view 1.159 + mActions.sendSpecialKey(Actions.SpecialKey.BACK); // Exit Settings 1.160 + // Make sure the settings menu has been closed. 1.161 + mAsserter.ok(mSolo.waitForText("Enter Search or Address"), "Waiting for search bar", "Search bar found"); 1.162 + 1.163 + } 1.164 + 1.165 + public ArrayList<String> getAndroidUrls(String data) { 1.166 + // Return bookmarks or history depending on what the user asks for 1.167 + ArrayList<String> urls = new ArrayList<String>(); 1.168 + ContentResolver resolver = getActivity().getContentResolver(); 1.169 + Browser mBrowser = new Browser(); 1.170 + Cursor cursor = null; 1.171 + try { 1.172 + if (data.equals("history")) { 1.173 + cursor = mBrowser.getAllVisitedUrls(resolver); 1.174 + } else if (data.equals("bookmarks")) { 1.175 + cursor = mBrowser.getAllBookmarks(resolver); 1.176 + } 1.177 + if (cursor != null) { 1.178 + cursor.moveToFirst(); 1.179 + for (int i = 0; i < cursor.getCount(); i++ ) { 1.180 + urls.add(cursor.getString(cursor.getColumnIndex("url"))); 1.181 + if(!cursor.isLast()) { 1.182 + cursor.moveToNext(); 1.183 + } 1.184 + } 1.185 + } 1.186 + } finally { 1.187 + if (cursor != null) { 1.188 + cursor.close(); 1.189 + } 1.190 + } 1.191 + return urls; 1.192 + } 1.193 + 1.194 + public void deleteImportedData() { 1.195 + // Bookmarks 1.196 + ArrayList<String> androidBookmarks = getAndroidUrls("bookmarks"); 1.197 + for (String url:androidBookmarks) { 1.198 + mDatabaseHelper.deleteBookmark(url); 1.199 + } 1.200 + // History 1.201 + for (String url:androidData) { 1.202 + mDatabaseHelper.deleteHistoryItem(url); 1.203 + } 1.204 + } 1.205 + 1.206 + public void tearDown() throws Exception { 1.207 + deleteImportedData(); 1.208 + super.tearDown(); 1.209 + } 1.210 +}