Skip to content

Commit

Permalink
Add test for search appearance/function over CSVs
Browse files Browse the repository at this point in the history
  • Loading branch information
lognaturel committed Jun 5, 2019
1 parent 78b629b commit 33d66af
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name,label
artichoke,Artichoke
apple,Apple
banana,Banana
blueberry,Blueberry
cherimoya,Cherimoya
carrot,Carrot
41 changes: 41 additions & 0 deletions collect_app/src/androidTest/assets/forms/external-csv-search.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:odk="http://www.opendatakit.org/xforms" xmlns:orx="http://openrosa.org/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<h:head>
<h:title>external-csv-search</h:title>
<model>
<instance>
<external-csv-search id="external-csv-search">
<multi_produce/>
<produce_search/>
<produce/>
<meta>
<instanceID/>
</meta>
</external-csv-search>
</instance>
<bind nodeset="/external-csv-search/multi_produce" type="select"/>
<bind nodeset="/external-csv-search/produce_search" type="string"/>
<bind nodeset="/external-csv-search/produce" type="select1"/>
<bind calculate="concat('uuid:', uuid())" nodeset="/external-csv-search/meta/instanceID" readonly="true()" type="string"/>
</model>
</h:head>
<h:body>
<select appearance="search('external-csv-search-produce')" ref="/external-csv-search/multi_produce">
<label>Multiple produce</label>
<item>
<label>label</label>
<value>name</value>
</item>
</select>
<input ref="/external-csv-search/produce_search">
<label>Produce search</label>
</input>
<select1 appearance="search('external-csv-search-produce', 'contains', 'name', /external-csv-search/produce_search )" ref="/external-csv-search/produce">
<label>Produce</label>
<item>
<label>label</label>
<value>name</value>
</item>
</select1>
</h:body>
</h:html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.odk.collect.android.formentry;

import android.Manifest;

import androidx.test.espresso.intent.rule.IntentsTestRule;
import androidx.test.rule.GrantPermissionRule;

import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.odk.collect.android.activities.FormEntryActivity;
import org.odk.collect.android.test.FormLoadingUtils;

import java.io.IOException;
import java.util.Collections;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.replaceText;
import static androidx.test.espresso.action.ViewActions.swipeLeft;
import static androidx.test.espresso.action.ViewActions.swipeRight;
import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withClassName;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.CoreMatchers.endsWith;

public class ExternalCsvSearchTest {
private static final String EXTERNAL_CSV_SEARCH_FORM = "external-csv-search.xml";

@Rule
public IntentsTestRule<FormEntryActivity> activityTestRule = FormLoadingUtils.getFormActivityTestRuleFor(EXTERNAL_CSV_SEARCH_FORM);

@Rule
public GrantPermissionRule permissionRule = GrantPermissionRule.grant(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE);

@BeforeClass
public static void copyFormToSdCard() throws IOException {
FormLoadingUtils.copyFormToSdCard(EXTERNAL_CSV_SEARCH_FORM, "forms",
Collections.singletonList("external-csv-search-produce.csv"));
}

@Test
public void simpleSearchStatement_ShouldDisplayAllCsvChoices() {
onView(withText("Multiple produce")).check(matches(isDisplayed()));

onView(withText("Artichoke")).check(matches(isDisplayed()));
onView(withText("Apple")).check(matches(isDisplayed()));
onView(withText("Banana")).check(matches(isDisplayed()));
onView(withText("Blueberry")).check(matches(isDisplayed()));
onView(withText("Cherimoya")).check(matches(isDisplayed()));
onView(withText("Carrot")).check(matches(isDisplayed()));
}

@Test
// Regression: https://github.com/opendatakit/collect/issues/3132
public void searchStatementWithContainsFilter_ShouldUpdateOnSearchChange() {
onView(withText("Multiple produce")).perform(swipeLeft());

onView(withText("Produce search")).check(matches(isDisplayed()));
onView(withClassName(endsWith("EditText"))).perform(replaceText("A"));
onView(withText("Produce search")).perform(swipeLeft());
onView(withText("Artichoke")).check(matches(isDisplayed()));
onView(withText("Apple")).check(matches(isDisplayed()));
onView(withText("Banana")).check(matches(isDisplayed()));
onView(withText("Blueberry")).check(doesNotExist());
onView(withText("Cherimoya")).check(matches(isDisplayed()));
onView(withText("Carrot")).check(matches(isDisplayed()));

onView(withText("Produce")).perform(swipeRight());
onView(withClassName(endsWith("EditText"))).perform(replaceText("B"));
onView(withText("Produce search")).perform(swipeLeft());
onView(withText("Artichoke")).check(doesNotExist());
onView(withText("Apple")).check(doesNotExist());
onView(withText("Banana")).check(matches(isDisplayed()));
onView(withText("Blueberry")).check(matches(isDisplayed()));
onView(withText("Cherimoya")).check(doesNotExist());
onView(withText("Carrot")).check(doesNotExist());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.odk.collect.android.formfilling;
package org.odk.collect.android.formentry;

import android.Manifest;
import android.app.Activity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.odk.collect.android.formfilling;
package org.odk.collect.android.formentry;

import android.Manifest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.odk.collect.android.formfilling;
package org.odk.collect.android.formentry;

import android.Manifest;
import android.app.Activity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@
import android.content.Intent;
import android.content.res.AssetManager;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.espresso.intent.rule.IntentsTestRule;
import androidx.test.platform.app.InstrumentationRegistry;

import org.apache.commons.io.IOUtils;
import org.odk.collect.android.activities.FormEntryActivity;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.tasks.FormLoaderTask;
import org.odk.collect.android.utilities.FileUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.espresso.intent.rule.IntentsTestRule;
import androidx.test.platform.app.InstrumentationRegistry;
import java.util.List;

import static org.odk.collect.android.activities.FormEntryActivity.EXTRA_TESTING_PATH;

Expand All @@ -44,27 +46,47 @@ private FormLoadingUtils() {
}

/**
* Copies a form with the given file name from the given assets folder to the SD Card where it
* will be loaded by {@link FormLoaderTask}.
* Copies a form with the given file name and given associated media from the given assets
* folder to the SD Card where it will be loaded by {@link FormLoaderTask}.
*/
public static void copyFormToSdCard(String formFilename, String formAssetPath) throws IOException {
public static void copyFormToSdCard(String formFilename, String formAssetPath, List<String> mediaFilenames) throws IOException {
String pathname = Collect.FORMS_PATH + formFilename;

AssetManager assetManager = InstrumentationRegistry.getInstrumentation().getContext().getAssets();
InputStream inputStream = assetManager.open(formAssetPath + formFilename);
InputStream inputStream = assetManager.open(formAssetPath + File.separator + formFilename);

File outFile = new File(pathname);
OutputStream outputStream = new FileOutputStream(outFile);

IOUtils.copy(inputStream, outputStream);

if (mediaFilenames != null) {
String mediaPathName = Collect.FORMS_PATH + formFilename.replace(".xml", "") + FileUtils.MEDIA_SUFFIX + "/";

for (String mediaFilename: mediaFilenames) {
InputStream mediaInputStream = assetManager.open(formAssetPath + File.separator + mediaFilename);
File mediaOutFile = new File(mediaPathName + mediaFilename);
OutputStream mediaOutputStream = new FileOutputStream(mediaOutFile);

IOUtils.copy(mediaInputStream, mediaOutputStream);
}
}
}

/**
* Copies a form with the given file name from the from the given assets folder to the SD Card
* where it will be loaded by {@link FormLoaderTask}.
*/
public static void copyFormToSdCard(String formFilename, String formAssetPath) throws IOException {
copyFormToSdCard(formFilename, formAssetPath, null);
}

/**
* Copies a form with the given file name from the assets root to the SD Card where it
* will be loaded by {@link FormLoaderTask}.
*/
public static void copyFormToSdCard(String formFilename) throws IOException {
copyFormToSdCard(formFilename, "");
copyFormToSdCard(formFilename, "", null);
}

public static IntentsTestRule<FormEntryActivity> getFormActivityTestRuleFor(String formFilename) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class FileUtils {
public static final String AUTO_SEND = "autoSend";

/** Suffix for the form media directory. */
private static final String MEDIA_SUFFIX = "-media";
public static final String MEDIA_SUFFIX = "-media";

/** Filename of the last-saved instance data. */
public static final String LAST_SAVED_FILENAME = "last-saved.xml";
Expand Down

0 comments on commit 33d66af

Please sign in to comment.