michael@0: package org.mozilla.gecko.tests; michael@0: michael@0: import java.util.ArrayList; michael@0: michael@0: import org.mozilla.gecko.Actions; michael@0: import org.mozilla.gecko.AppConstants; michael@0: import org.mozilla.gecko.GeckoProfile; michael@0: michael@0: import android.content.ContentResolver; michael@0: import android.content.ContentValues; michael@0: import android.database.Cursor; michael@0: import android.net.Uri; michael@0: import android.provider.Browser; michael@0: michael@0: /** michael@0: * This test covers the Import from Android feature michael@0: * The test will save the existing bookmarks and history then will do an Import michael@0: * After the import it will check that the bookmarks and history items from Android are imported michael@0: * Then it will test that the old data from Firefox is not lost michael@0: * At the end will test that a second import will not duplicate information michael@0: */ michael@0: michael@0: public class testImportFromAndroid extends AboutHomeTest { michael@0: private static final int MAX_WAIT_TIMEOUT = 15000; michael@0: ArrayList androidData = new ArrayList(); michael@0: ArrayList firefoxHistory = new ArrayList(); michael@0: michael@0: public void testImportFromAndroid() { michael@0: ArrayList firefoxBookmarks = new ArrayList(); michael@0: ArrayList oldFirefoxHistory = new ArrayList(); michael@0: ArrayList oldFirefoxBookmarks = new ArrayList(); michael@0: blockForGeckoReady(); michael@0: michael@0: // Get the Android history michael@0: androidData = getAndroidUrls("history"); michael@0: michael@0: // Add some overlapping data from the Android Stock Browser to Firefox before import michael@0: addData(); michael@0: michael@0: // Get the initial bookmarks and history michael@0: oldFirefoxBookmarks = mDatabaseHelper.getBrowserDBUrls(DatabaseHelper.BrowserDataType.BOOKMARKS); michael@0: oldFirefoxHistory = mDatabaseHelper.getBrowserDBUrls(DatabaseHelper.BrowserDataType.HISTORY); michael@0: michael@0: // Import the bookmarks and history michael@0: importDataFromAndroid(); michael@0: michael@0: // Get the Android history and the Firefox bookmarks and history lists michael@0: firefoxHistory = mDatabaseHelper.getBrowserDBUrls(DatabaseHelper.BrowserDataType.HISTORY); michael@0: firefoxBookmarks = mDatabaseHelper.getBrowserDBUrls(DatabaseHelper.BrowserDataType.BOOKMARKS); michael@0: michael@0: /** michael@0: * Add a delay to make sure the imported items are added to the array lists michael@0: * if there are a lot of history items in the Android Browser database michael@0: */ michael@0: boolean success = waitForTest(new BooleanTest() { michael@0: @Override michael@0: public boolean test() { michael@0: if (androidData.size() <= firefoxHistory.size()) { michael@0: return true; michael@0: } else { michael@0: return false; michael@0: } michael@0: } michael@0: }, MAX_WAIT_MS); michael@0: michael@0: /** michael@0: * Verify the history and bookmarks are imported michael@0: * Android history also contains the android bookmarks so we don't need to get them separately here michael@0: */ michael@0: for (String url:androidData) { michael@0: mAsserter.ok(firefoxHistory.contains(url)||firefoxBookmarks.contains(url), "Checking if Android" + (firefoxBookmarks.contains(url) ? " Bookmark" : " History item") + " is present", url + " was imported"); michael@0: } michael@0: michael@0: // Verify the original Firefox Bookmarks are not deleted michael@0: for (String url:oldFirefoxBookmarks) { michael@0: mAsserter.ok(firefoxBookmarks.contains(url), "Checking if original Firefox Bookmark is present", " Firefox Bookmark " + url + " was not removed"); michael@0: } michael@0: michael@0: // Verify the original Firefox History is not deleted michael@0: for (String url:oldFirefoxHistory) { michael@0: mAsserter.ok(firefoxHistory.contains(url), "Checking original Firefox History item is present", " Firefox History item " + url + " was not removed"); michael@0: } michael@0: michael@0: // Import data again and make sure bookmarks are not duplicated michael@0: importDataFromAndroid(); michael@0: michael@0: // Verify bookmarks are not duplicated michael@0: ArrayList verifiedBookmarks = new ArrayList(); michael@0: firefoxBookmarks = mDatabaseHelper.getBrowserDBUrls(DatabaseHelper.BrowserDataType.BOOKMARKS); michael@0: for (String url:firefoxBookmarks) { michael@0: if (verifiedBookmarks.contains(url)) { michael@0: mAsserter.ok(false, "Bookmark " + url + " should not be duplicated", "Bookmark is duplicated"); michael@0: } else { michael@0: verifiedBookmarks.add(url); michael@0: mAsserter.ok(true, "Bookmark " + url + " was not duplicated", "Bookmark is unique"); michael@0: } michael@0: } michael@0: michael@0: // Verify history count is not increased after the second import michael@0: mAsserter.ok(firefoxHistory.size() == mDatabaseHelper.getBrowserDBUrls(DatabaseHelper.BrowserDataType.HISTORY).size(), "The number of history entries was not increased", "None of the items were duplicated"); michael@0: } michael@0: michael@0: private void addData() { michael@0: ArrayList androidBookmarks = getAndroidUrls("bookmarks"); michael@0: michael@0: // Add a few Bookmarks from Android to Firefox Mobile michael@0: for (String url:androidBookmarks) { michael@0: // Add every 3rd bookmark to Firefox Mobile michael@0: if ((androidBookmarks.indexOf(url) % 3) == 0) { michael@0: mDatabaseHelper.addOrUpdateMobileBookmark("Bookmark Number" + String.valueOf(androidBookmarks.indexOf(url)), url); michael@0: } michael@0: } michael@0: michael@0: // Add a few history items in Firefox Mobile michael@0: ContentResolver resolver = getActivity().getContentResolver(); michael@0: Uri uri = Uri.parse("content://" + AppConstants.ANDROID_PACKAGE_NAME + ".db.browser/history"); michael@0: uri = uri.buildUpon().appendQueryParameter("profile", GeckoProfile.DEFAULT_PROFILE) michael@0: .appendQueryParameter("sync", "true").build(); michael@0: for (String url:androidData) { michael@0: // Add every 3rd website from Android History to Firefox Mobile michael@0: if ((androidData.indexOf(url) % 3) == 0) { michael@0: ContentValues values = new ContentValues(); michael@0: values.put("title", "Page" + url); michael@0: values.put("url", url); michael@0: values.put("date", System.currentTimeMillis()); michael@0: values.put("visits", androidData.indexOf(url)); michael@0: resolver.insert(uri, values); michael@0: } michael@0: } michael@0: } michael@0: michael@0: private void importDataFromAndroid() { michael@0: waitForText("Enter Search or Address"); michael@0: selectSettingsItem(StringHelper.CUSTOMIZE_SECTION_LABEL, StringHelper.IMPORT_FROM_ANDROID_LABEL); michael@0: michael@0: // 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 michael@0: waitForText("Cancel"); michael@0: mSolo.clickOnButton("Import"); michael@0: michael@0: // 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 michael@0: boolean importComplete = waitForTest(new BooleanTest() { michael@0: public boolean test() { michael@0: return !mSolo.searchText("Please wait..."); michael@0: } michael@0: }, MAX_WAIT_TIMEOUT); michael@0: michael@0: mAsserter.ok(importComplete, "Waiting for import to finish and the pop-up to be dismissed", "Import was completed and the pop-up was dismissed"); michael@0: michael@0: // Import has finished. Waiting to get back to the Settings Menu and looking for the Import&Export subsection michael@0: if ("phone".equals(mDevice.type)) { michael@0: // Phones don't have headers like tablets, so we need to pop up one more level. michael@0: waitForText(StringHelper.IMPORT_FROM_ANDROID_LABEL); michael@0: mActions.sendSpecialKey(Actions.SpecialKey.BACK); michael@0: } michael@0: waitForText("Privacy"); // Settings is a header for the settings menu page. Waiting for Privacy ensures we are back in the top Settings view michael@0: mActions.sendSpecialKey(Actions.SpecialKey.BACK); // Exit Settings michael@0: // Make sure the settings menu has been closed. michael@0: mAsserter.ok(mSolo.waitForText("Enter Search or Address"), "Waiting for search bar", "Search bar found"); michael@0: michael@0: } michael@0: michael@0: public ArrayList getAndroidUrls(String data) { michael@0: // Return bookmarks or history depending on what the user asks for michael@0: ArrayList urls = new ArrayList(); michael@0: ContentResolver resolver = getActivity().getContentResolver(); michael@0: Browser mBrowser = new Browser(); michael@0: Cursor cursor = null; michael@0: try { michael@0: if (data.equals("history")) { michael@0: cursor = mBrowser.getAllVisitedUrls(resolver); michael@0: } else if (data.equals("bookmarks")) { michael@0: cursor = mBrowser.getAllBookmarks(resolver); michael@0: } michael@0: if (cursor != null) { michael@0: cursor.moveToFirst(); michael@0: for (int i = 0; i < cursor.getCount(); i++ ) { michael@0: urls.add(cursor.getString(cursor.getColumnIndex("url"))); michael@0: if(!cursor.isLast()) { michael@0: cursor.moveToNext(); michael@0: } michael@0: } michael@0: } michael@0: } finally { michael@0: if (cursor != null) { michael@0: cursor.close(); michael@0: } michael@0: } michael@0: return urls; michael@0: } michael@0: michael@0: public void deleteImportedData() { michael@0: // Bookmarks michael@0: ArrayList androidBookmarks = getAndroidUrls("bookmarks"); michael@0: for (String url:androidBookmarks) { michael@0: mDatabaseHelper.deleteBookmark(url); michael@0: } michael@0: // History michael@0: for (String url:androidData) { michael@0: mDatabaseHelper.deleteHistoryItem(url); michael@0: } michael@0: } michael@0: michael@0: public void tearDown() throws Exception { michael@0: deleteImportedData(); michael@0: super.tearDown(); michael@0: } michael@0: }