<commit_before>package org.projectbuendia.client.smoke;

import android.support.test.annotation.UiThreadTest;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.ViewAction;
import android.support.test.espresso.action.GeneralClickAction;
import android.support.test.espresso.action.GeneralLocation;
import android.support.test.espresso.action.Press;
import android.support.test.espresso.action.Tap;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.NumberPicker;
import android.widget.RadioButton;
import android.widget.TextView;

import com.android.volley.toolbox.RequestFuture;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.joda.time.DateTime;
import org.junit.Test;
import org.odk.collect.android.views.MediaLayout;
import org.odk.collect.android.views.ODKView;
import org.odk.collect.android.widgets2.group.TableWidgetGroup;
import org.odk.collect.android.widgets2.selectone.ButtonsSelectOneWidget;
import org.projectbuendia.client.App;
import org.projectbuendia.client.R;
import org.projectbuendia.client.json.ConceptType;
import org.projectbuendia.client.json.JsonEncounter;
import org.projectbuendia.client.json.JsonUser;
import org.projectbuendia.client.models.ConceptUuids;
import org.projectbuendia.client.models.Encounter;
import org.projectbuendia.client.models.Location;
import org.projectbuendia.client.models.LocationForest;
import org.projectbuendia.client.models.Obs;
import org.projectbuendia.client.sync.SyncManager;
import org.projectbuendia.client.ui.FunctionalTestCase;
import org.projectbuendia.client.ui.chart.PatientChartController;
import org.projectbuendia.client.utils.Logger;
import org.projectbuendia.client.utils.Utils;
import org.w3c.dom.Document;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import androidx.test.filters.MediumTest;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.hamcrest.Matchers.greaterThan;
import static org.projectbuendia.client.acceptance.ListItemCountAssertion.hasItemCount;
import static org.projectbuendia.client.utils.Utils.eq;

/** A quick test suite that exercises all basic functionality. */
@MediumTest public class SmokeTest extends FunctionalTestCase {
    public static final String HOSTNAME = "ping.buendia.org";
    public static final String OPENMRS_USERNAME = "tester";
    public static final String OPENMRS_PASSWORD = "<PASSWORD>";
    public static final Logger LOG = Logger.create();

    /** A test that exercises all the API methods and endpoints. */
    @Test @UiThreadTest public void testApi() {
        screenshot("Start");
        expect("Guest User");
        initSettings();  // step 0
        screenshot("Loaded users");
        final String id = "" + getNextAvailableId(R.id.users);
        addUser("Test" + id, "User" + id);  // step 1
        screenshot("Added new user");
        signInFullSync("Test" + id + " User" + id);  // step 2
        screenshot("Signed in");
        int suspectedCount = getPatientCount("Suspected");
        addPatient(id, "Given" + id, "Family" + id, 11);  // step 3
        screenshot("Added new patient");
        expect("Triage");
        String patientUuid = PatientChartController.currentPatientUuid;
        movePatient("Suspected");
        back();  // back to location list
        screenshot("Moved patient away");
        expectPatientCount("Suspected", suspectedCount + 1);
        int dischargedCount = getPatientCount("Discharged");
        internalMovePatient(patientUuid, internalGetLocationUuid("Discharged"));
        internalIncrementalSync();  // step 4
        screenshot("Moved patient back");
        expectPatientCount("Suspected", suspectedCount);
        expectPatientCount("Discharged", dischargedCount + 1);
        goToPatientById(id, "Given" + id, "Family" + id);  // step 5
        screenshot("Opened chart by ID");
        editPatientAge(22);  // step 6
        screenshot("Edited patient age");
        rewindAdmissionDate(2, "Day 3");  // step 7
        screenshot("Edited admission date");
        openForm("Patient attributes");  // step 8
        screenshot("Opened form");
        answerMultipleCodedQuestion("Attributes", "Oxygen mask");
        submitForm(); // step 9
        screenshot("Submitted form");
        waitFor(viewThat(hasId(R.id.special_labels), hasText("Oxygen")));
        addOrder("Sunshine", "25 rays", "Get outside!");  // step 10
        screenshot("Added new order");
        editOrder("Sunshine", "Exercise!");  // step 11
        screenshot("Edited order");
        deleteOrder("Sunshine");  // step 12
        screenshot("Deleted order");
        toast("Smoke test passed!");
    }

    private void initSettings() {
        click(R.id.settings);
        waitFor("Periodic sync disabled");
        clickIfUnchecked(viewThat(
            isA(CheckBox.class),
            whoseParent(hasSiblingThat(hasChildThat(hasText("Periodic sync disabled"))))
        ));
        enterSetting("Buendia server", HOSTNAME, "Apply and clear local data");
        enterSetting("OpenMRS username", OPENMRS_USERNAME, "Apply and clear local data");
        enterSetting("OpenMRS password", OPENMRS_PASSWORD, "Apply and clear local data");
        back();
    }

    private void enterSetting(String title, Object value) {
        enterSetting(title, value, "OK");
    }

    private void enterSetting(String title, Object value, String buttonText) {
        click(title);
        EditText editText = (EditText) getViewThat(
            isA(EditText.class),
            hasSiblingThat(hasText(""))
        );
        if (!eq(editText.getText().toString(), value)) {
            clearAndType(value, viewThat(
                isA(EditText.class),
                hasSiblingThat(hasText(""))
            ));
        }
        click(buttonText);
    }

    private void addUser(String given, String family) {
        click(R.id.action_new_user);
        type(given, R.id.add_user_given_name_tv);
        type(family, R.id.add_user_family_name_tv);
        click("OK");
        expect(given + " " + family);
    }

    private void signInFullSync(String name) {
        click(name);
        waitFor("Discharged");
    }

    private void addPatient(String id, String given, String family, int age) {
        click(R.id.action_new_patient);
        expect("New patient");

        type(id, R.id.patient_id);
        type(given, R.id.patient_given_name);
        type(family, R.id.patient_family_name);
        type(age, R.id.patient_age_years);
        click("OK");

        expect(id + ". " + given + " " + family);
        expect(age + " y");
    }

    private void movePatient(String location) {
        click(R.id.patient_placement);
        click(location);
        click("OK");
        waitFor(viewThat(hasId(R.id.patient_placement),
            hasDescendantThat(hasId(R.id.view_attribute_name), hasText("Location")),
            hasDescendantThat(hasId(R.id.view_attribute_value), hasText(location))
        ));
    }

    private String internalGetLocationUuid(String name) {
        LocationForest forest = App.getModel().getForest();
        for (Location location : forest.allNodes()) {
            if (eq(location.name, name)) return location.uuid;
        }
        throw new RuntimeException("No location named \"" + name + "\" could be found");
    }<commit_msg>Small fixes to SmokeTest.<commit_after>package org.projectbuendia.client.smoke;

import android.support.test.annotation.UiThreadTest;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.ViewAction;
import android.support.test.espresso.action.GeneralClickAction;
import android.support.test.espresso.action.GeneralLocation;
import android.support.test.espresso.action.Press;
import android.support.test.espresso.action.Tap;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.NumberPicker;
import android.widget.RadioButton;
import android.widget.TextView;

import com.android.volley.toolbox.RequestFuture;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.joda.time.DateTime;
import org.junit.Test;
import org.odk.collect.android.views.MediaLayout;
import org.odk.collect.android.views.ODKView;
import org.odk.collect.android.widgets2.group.TableWidgetGroup;
import org.odk.collect.android.widgets2.selectone.ButtonsSelectOneWidget;
import org.projectbuendia.client.App;
import org.projectbuendia.client.R;
import org.projectbuendia.client.json.ConceptType;
import org.projectbuendia.client.json.JsonEncounter;
import org.projectbuendia.client.json.JsonUser;
import org.projectbuendia.client.models.ConceptUuids;
import org.projectbuendia.client.models.Encounter;
import org.projectbuendia.client.models.Location;
import org.projectbuendia.client.models.LocationForest;
import org.projectbuendia.client.models.Obs;
import org.projectbuendia.client.sync.SyncManager;
import org.projectbuendia.client.ui.FunctionalTestCase;
import org.projectbuendia.client.ui.chart.PatientChartController;
import org.projectbuendia.client.utils.Logger;
import org.projectbuendia.client.utils.Utils;
import org.w3c.dom.Document;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import androidx.test.filters.MediumTest;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.hamcrest.Matchers.greaterThan;
import static org.projectbuendia.client.acceptance.ListItemCountAssertion.hasItemCount;
import static org.projectbuendia.client.utils.Utils.eq;

/** A quick test suite that exercises all basic functionality. */
@MediumTest public class SmokeTest extends FunctionalTestCase {
    public static final String HOSTNAME = "ping.buendia.org";
    public static final String OPENMRS_USERNAME = "tester";
    public static final String OPENMRS_PASSWORD = "<PASSWORD>";
    public static final Logger LOG = Logger.create();

    /** A test that exercises all the API methods and endpoints. */
    @Test @UiThreadTest public void testApi() {
        screenshot("Start");
        expect("Guest User");
        initSettings();  // step 0
        screenshot("Loaded users");
        final String id = "" + getNextAvailableId(R.id.users);
        addUser("Test" + id, "User" + id);  // step 1
        screenshot("Added new user");
        signInFullSync("Test" + id + " User" + id);  // step 2
        screenshot("Signed in");
        int suspectedCount = getPatientCount("Suspected");
        addPatient(id, "Given" + id, "Family" + id, 11);  // step 3
        screenshot("Added new patient");
        expect("Triage");
        String patientUuid = PatientChartController.currentPatientUuid;
        movePatient("Suspected");
        back();  // back to location list
        screenshot("Moved patient away");
        expectPatientCount("Suspected", suspectedCount + 1);
        int dischargedCount = getPatientCount("Discharged");
        internalMovePatient(patientUuid, internalGetLocationUuid("Discharged"));
        internalIncrementalSync();  // step 4
        screenshot("Moved patient back");
        expectPatientCount("Suspected", suspectedCount);
        expectPatientCount("Discharged", dischargedCount + 1);
        goToPatientById(id, "Given" + id, "Family" + id);  // step 5
        screenshot("Opened chart by ID");
        editPatientAge(22);  // step 6
        screenshot("Edited patient age");
        rewindAdmissionDate(2, "Day 3");  // step 7
        screenshot("Edited admission date");
        openForm("Patient attributes");  // step 8
        screenshot("Opened form");
        answerMultipleCodedQuestion("Attributes", "Pregnant");
        submitForm(); // step 9
        screenshot("Submitted form");
        waitFor(viewContainingText("pregnant, 22 y"));
        addOrder("Sunshine", "25 rays", "Get outside!");  // step 10
        screenshot("Added new order");
        editOrder("Sunshine", "Exercise!");  // step 11
        screenshot("Edited order");
        deleteOrder("Sunshine");  // step 12
        screenshot("Deleted order");
        toast("Smoke test passed!");
    }

    private void initSettings() {
        click(R.id.settings);
        waitFor("Periodic sync disabled");
        clickIfUnchecked(viewThat(
            isA(CheckBox.class),
            whoseParent(hasSiblingThat(hasChildThat(hasText("Periodic sync disabled"))))
        ));
        enterSetting("Buendia server", HOSTNAME, "Apply and clear local data");
        enterSetting("OpenMRS username", OPENMRS_USERNAME, "Apply and clear local data");
        enterSetting("OpenMRS password", OPENMRS_PASSWORD, "Apply and clear local data");
        back();
    }

    private void enterSetting(String title, Object value) {
        enterSetting(title, value, "OK");
    }

    private void enterSetting(String title, Object value, String buttonText) {
        click(title);
        EditText editText = (EditText) getViewThat(
            isA(EditText.class),
            hasSiblingThat(hasText(""))
        );
        if (!eq(editText.getText().toString(), value)) {
            clearAndType(value, viewThat(
                isA(EditText.class),
                hasSiblingThat(hasText(""))
            ));
        }
        click(buttonText);
    }

    private void addUser(String given, String family) {
        click(R.id.action_new_user);
        type(given, R.id.add_user_given_name_tv);
        type(family, R.id.add_user_family_name_tv);
        click("OK");
        expect(given + " " + family);
    }

    private void signInFullSync(String name) {
        click(name);
        waitFor("Discharged");
    }

    private void addPatient(String id, String given, String family, int age) {
        click(R.id.action_new_patient);
        expect("New patient");

        type(id, R.id.patient_id);
        type(given, R.id.patient_given_name);
        type(family, R.id.patient_family_name);
        type(age, R.id.patient_age_years);
        click("OK");

        expect(id + ". " + given + " " + family);
        expect(age + " y");
    }

    private void movePatient(String location) {
        click(firstViewWithText("Location"));
        click(location);
        click("OK");
        waitFor(viewThat(hasId(R.id.patient_placement),
            hasDescendantThat(hasId(R.id.view_attribute_name), hasText("Location")),
            hasDescendantThat(hasId(R.id.view_attribute_value), hasText(location))
        ));
    }

    private String internalGetLocationUuid(String name) {
        LocationForest forest = App.getModel().getForest();
        for (Location location : forest.allNodes()) {
            if (eq(location.name, name)) return location.uuid;
        }
        throw new RuntimeException("No location named \"" + name + "\" could be found");
    }
{'fraction_non_alphanumeric': 0.11843406050220787, 'fraction_numerical': 0.0039543926711922495, 'mean_word_length': 3.3893549320219845, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1891865', 'n_tokens_mistral': 4498, 'n_tokens_neox': 4352, 'n_words': 909}