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