Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | package org.mozilla.gecko.tests; |
michael@0 | 2 | |
michael@0 | 3 | import java.util.ArrayList; |
michael@0 | 4 | |
michael@0 | 5 | import org.mozilla.gecko.Actions; |
michael@0 | 6 | import org.mozilla.gecko.AppConstants; |
michael@0 | 7 | import org.mozilla.gecko.GeckoProfile; |
michael@0 | 8 | |
michael@0 | 9 | import android.content.ContentResolver; |
michael@0 | 10 | import android.content.ContentValues; |
michael@0 | 11 | import android.database.Cursor; |
michael@0 | 12 | import android.net.Uri; |
michael@0 | 13 | import android.provider.Browser; |
michael@0 | 14 | |
michael@0 | 15 | /** |
michael@0 | 16 | * This test covers the Import from Android feature |
michael@0 | 17 | * The test will save the existing bookmarks and history then will do an Import |
michael@0 | 18 | * After the import it will check that the bookmarks and history items from Android are imported |
michael@0 | 19 | * Then it will test that the old data from Firefox is not lost |
michael@0 | 20 | * At the end will test that a second import will not duplicate information |
michael@0 | 21 | */ |
michael@0 | 22 | |
michael@0 | 23 | public class testImportFromAndroid extends AboutHomeTest { |
michael@0 | 24 | private static final int MAX_WAIT_TIMEOUT = 15000; |
michael@0 | 25 | ArrayList<String> androidData = new ArrayList<String>(); |
michael@0 | 26 | ArrayList<String> firefoxHistory = new ArrayList<String>(); |
michael@0 | 27 | |
michael@0 | 28 | public void testImportFromAndroid() { |
michael@0 | 29 | ArrayList<String> firefoxBookmarks = new ArrayList<String>(); |
michael@0 | 30 | ArrayList<String> oldFirefoxHistory = new ArrayList<String>(); |
michael@0 | 31 | ArrayList<String> oldFirefoxBookmarks = new ArrayList<String>(); |
michael@0 | 32 | blockForGeckoReady(); |
michael@0 | 33 | |
michael@0 | 34 | // Get the Android history |
michael@0 | 35 | androidData = getAndroidUrls("history"); |
michael@0 | 36 | |
michael@0 | 37 | // Add some overlapping data from the Android Stock Browser to Firefox before import |
michael@0 | 38 | addData(); |
michael@0 | 39 | |
michael@0 | 40 | // Get the initial bookmarks and history |
michael@0 | 41 | oldFirefoxBookmarks = mDatabaseHelper.getBrowserDBUrls(DatabaseHelper.BrowserDataType.BOOKMARKS); |
michael@0 | 42 | oldFirefoxHistory = mDatabaseHelper.getBrowserDBUrls(DatabaseHelper.BrowserDataType.HISTORY); |
michael@0 | 43 | |
michael@0 | 44 | // Import the bookmarks and history |
michael@0 | 45 | importDataFromAndroid(); |
michael@0 | 46 | |
michael@0 | 47 | // Get the Android history and the Firefox bookmarks and history lists |
michael@0 | 48 | firefoxHistory = mDatabaseHelper.getBrowserDBUrls(DatabaseHelper.BrowserDataType.HISTORY); |
michael@0 | 49 | firefoxBookmarks = mDatabaseHelper.getBrowserDBUrls(DatabaseHelper.BrowserDataType.BOOKMARKS); |
michael@0 | 50 | |
michael@0 | 51 | /** |
michael@0 | 52 | * Add a delay to make sure the imported items are added to the array lists |
michael@0 | 53 | * if there are a lot of history items in the Android Browser database |
michael@0 | 54 | */ |
michael@0 | 55 | boolean success = waitForTest(new BooleanTest() { |
michael@0 | 56 | @Override |
michael@0 | 57 | public boolean test() { |
michael@0 | 58 | if (androidData.size() <= firefoxHistory.size()) { |
michael@0 | 59 | return true; |
michael@0 | 60 | } else { |
michael@0 | 61 | return false; |
michael@0 | 62 | } |
michael@0 | 63 | } |
michael@0 | 64 | }, MAX_WAIT_MS); |
michael@0 | 65 | |
michael@0 | 66 | /** |
michael@0 | 67 | * Verify the history and bookmarks are imported |
michael@0 | 68 | * Android history also contains the android bookmarks so we don't need to get them separately here |
michael@0 | 69 | */ |
michael@0 | 70 | for (String url:androidData) { |
michael@0 | 71 | mAsserter.ok(firefoxHistory.contains(url)||firefoxBookmarks.contains(url), "Checking if Android" + (firefoxBookmarks.contains(url) ? " Bookmark" : " History item") + " is present", url + " was imported"); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | // Verify the original Firefox Bookmarks are not deleted |
michael@0 | 75 | for (String url:oldFirefoxBookmarks) { |
michael@0 | 76 | mAsserter.ok(firefoxBookmarks.contains(url), "Checking if original Firefox Bookmark is present", " Firefox Bookmark " + url + " was not removed"); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | // Verify the original Firefox History is not deleted |
michael@0 | 80 | for (String url:oldFirefoxHistory) { |
michael@0 | 81 | mAsserter.ok(firefoxHistory.contains(url), "Checking original Firefox History item is present", " Firefox History item " + url + " was not removed"); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | // Import data again and make sure bookmarks are not duplicated |
michael@0 | 85 | importDataFromAndroid(); |
michael@0 | 86 | |
michael@0 | 87 | // Verify bookmarks are not duplicated |
michael@0 | 88 | ArrayList<String> verifiedBookmarks = new ArrayList<String>(); |
michael@0 | 89 | firefoxBookmarks = mDatabaseHelper.getBrowserDBUrls(DatabaseHelper.BrowserDataType.BOOKMARKS); |
michael@0 | 90 | for (String url:firefoxBookmarks) { |
michael@0 | 91 | if (verifiedBookmarks.contains(url)) { |
michael@0 | 92 | mAsserter.ok(false, "Bookmark " + url + " should not be duplicated", "Bookmark is duplicated"); |
michael@0 | 93 | } else { |
michael@0 | 94 | verifiedBookmarks.add(url); |
michael@0 | 95 | mAsserter.ok(true, "Bookmark " + url + " was not duplicated", "Bookmark is unique"); |
michael@0 | 96 | } |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | // Verify history count is not increased after the second import |
michael@0 | 100 | 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 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | private void addData() { |
michael@0 | 104 | ArrayList<String> androidBookmarks = getAndroidUrls("bookmarks"); |
michael@0 | 105 | |
michael@0 | 106 | // Add a few Bookmarks from Android to Firefox Mobile |
michael@0 | 107 | for (String url:androidBookmarks) { |
michael@0 | 108 | // Add every 3rd bookmark to Firefox Mobile |
michael@0 | 109 | if ((androidBookmarks.indexOf(url) % 3) == 0) { |
michael@0 | 110 | mDatabaseHelper.addOrUpdateMobileBookmark("Bookmark Number" + String.valueOf(androidBookmarks.indexOf(url)), url); |
michael@0 | 111 | } |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | // Add a few history items in Firefox Mobile |
michael@0 | 115 | ContentResolver resolver = getActivity().getContentResolver(); |
michael@0 | 116 | Uri uri = Uri.parse("content://" + AppConstants.ANDROID_PACKAGE_NAME + ".db.browser/history"); |
michael@0 | 117 | uri = uri.buildUpon().appendQueryParameter("profile", GeckoProfile.DEFAULT_PROFILE) |
michael@0 | 118 | .appendQueryParameter("sync", "true").build(); |
michael@0 | 119 | for (String url:androidData) { |
michael@0 | 120 | // Add every 3rd website from Android History to Firefox Mobile |
michael@0 | 121 | if ((androidData.indexOf(url) % 3) == 0) { |
michael@0 | 122 | ContentValues values = new ContentValues(); |
michael@0 | 123 | values.put("title", "Page" + url); |
michael@0 | 124 | values.put("url", url); |
michael@0 | 125 | values.put("date", System.currentTimeMillis()); |
michael@0 | 126 | values.put("visits", androidData.indexOf(url)); |
michael@0 | 127 | resolver.insert(uri, values); |
michael@0 | 128 | } |
michael@0 | 129 | } |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | private void importDataFromAndroid() { |
michael@0 | 133 | waitForText("Enter Search or Address"); |
michael@0 | 134 | selectSettingsItem(StringHelper.CUSTOMIZE_SECTION_LABEL, StringHelper.IMPORT_FROM_ANDROID_LABEL); |
michael@0 | 135 | |
michael@0 | 136 | // 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 | 137 | waitForText("Cancel"); |
michael@0 | 138 | mSolo.clickOnButton("Import"); |
michael@0 | 139 | |
michael@0 | 140 | // 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 | 141 | boolean importComplete = waitForTest(new BooleanTest() { |
michael@0 | 142 | public boolean test() { |
michael@0 | 143 | return !mSolo.searchText("Please wait..."); |
michael@0 | 144 | } |
michael@0 | 145 | }, MAX_WAIT_TIMEOUT); |
michael@0 | 146 | |
michael@0 | 147 | 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 | 148 | |
michael@0 | 149 | // Import has finished. Waiting to get back to the Settings Menu and looking for the Import&Export subsection |
michael@0 | 150 | if ("phone".equals(mDevice.type)) { |
michael@0 | 151 | // Phones don't have headers like tablets, so we need to pop up one more level. |
michael@0 | 152 | waitForText(StringHelper.IMPORT_FROM_ANDROID_LABEL); |
michael@0 | 153 | mActions.sendSpecialKey(Actions.SpecialKey.BACK); |
michael@0 | 154 | } |
michael@0 | 155 | 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 | 156 | mActions.sendSpecialKey(Actions.SpecialKey.BACK); // Exit Settings |
michael@0 | 157 | // Make sure the settings menu has been closed. |
michael@0 | 158 | mAsserter.ok(mSolo.waitForText("Enter Search or Address"), "Waiting for search bar", "Search bar found"); |
michael@0 | 159 | |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | public ArrayList<String> getAndroidUrls(String data) { |
michael@0 | 163 | // Return bookmarks or history depending on what the user asks for |
michael@0 | 164 | ArrayList<String> urls = new ArrayList<String>(); |
michael@0 | 165 | ContentResolver resolver = getActivity().getContentResolver(); |
michael@0 | 166 | Browser mBrowser = new Browser(); |
michael@0 | 167 | Cursor cursor = null; |
michael@0 | 168 | try { |
michael@0 | 169 | if (data.equals("history")) { |
michael@0 | 170 | cursor = mBrowser.getAllVisitedUrls(resolver); |
michael@0 | 171 | } else if (data.equals("bookmarks")) { |
michael@0 | 172 | cursor = mBrowser.getAllBookmarks(resolver); |
michael@0 | 173 | } |
michael@0 | 174 | if (cursor != null) { |
michael@0 | 175 | cursor.moveToFirst(); |
michael@0 | 176 | for (int i = 0; i < cursor.getCount(); i++ ) { |
michael@0 | 177 | urls.add(cursor.getString(cursor.getColumnIndex("url"))); |
michael@0 | 178 | if(!cursor.isLast()) { |
michael@0 | 179 | cursor.moveToNext(); |
michael@0 | 180 | } |
michael@0 | 181 | } |
michael@0 | 182 | } |
michael@0 | 183 | } finally { |
michael@0 | 184 | if (cursor != null) { |
michael@0 | 185 | cursor.close(); |
michael@0 | 186 | } |
michael@0 | 187 | } |
michael@0 | 188 | return urls; |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | public void deleteImportedData() { |
michael@0 | 192 | // Bookmarks |
michael@0 | 193 | ArrayList<String> androidBookmarks = getAndroidUrls("bookmarks"); |
michael@0 | 194 | for (String url:androidBookmarks) { |
michael@0 | 195 | mDatabaseHelper.deleteBookmark(url); |
michael@0 | 196 | } |
michael@0 | 197 | // History |
michael@0 | 198 | for (String url:androidData) { |
michael@0 | 199 | mDatabaseHelper.deleteHistoryItem(url); |
michael@0 | 200 | } |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | public void tearDown() throws Exception { |
michael@0 | 204 | deleteImportedData(); |
michael@0 | 205 | super.tearDown(); |
michael@0 | 206 | } |
michael@0 | 207 | } |