/*
 * Copyright (c) 2015 NECTEC
 *   National Electronics and Computer Technology Center, Thailand
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package th.or.nectec.tanrabad.survey.presenter;

import android.content.Intent;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import th.or.nectec.tanrabad.survey.R;
import th.or.nectec.tanrabad.survey.TanrabadEspressoTestBase;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.*;
import static org.hamcrest.Matchers.allOf;

@RunWith(AndroidJUnit4.class)
public class PlaceListInDatabaseTest extends TanrabadEspressoTestBase {
    public ActivityTestRule<PlaceListActivity> mActivityTestRule
            = new ActivityTestRule<>(PlaceListActivity.class);
    PlaceListActivity mActivity;

    @Before
    public void setUp() {
        Intent intent = new Intent();
        mActivity = mActivityTestRule.launchActivity(intent);
    }

    @Test
    public void openDefineSurveyPlaceShouldFoundListPlace8List() {
        onView(allOf(ViewMatchers.withId(R.id.place_count)
                , withContentDescription(R.string.number_place_list_in_database)))
                .check(matches(withText("8 รายการ")));
    }

    @Test
    public void chooseTypeVillageCommunityShouldFound2List() {
        clickPlaceTypeNotDefine();
        onView(withText(R.string.village_community))
                .perform(click());

        onView(allOf(withId(R.id.place_count)
                , withContentDescription(R.string.number_place_list_in_database)))
                .check(matches(withText("2 รายการ")));
        textDisplayed("หมู่บ้านพาลาซเซตโต้");
        textDisplayed("ชุมชนกอล์ฟวิว");
    }

    @Test
    public void chooseTypeFactoryShouldNotFoundListPlace() {
        clickPlaceTypeNotDefine();
        onView(withText(R.string.factory))
                .perform(click());

        textDisplayed(R.string.places_not_found);
    }

    private void clickPlaceTypeNotDefine() {
        onView(withText(R.string.not_define_place_type))
                .perform(click());
    }
}

@@ -734,16 +734,61 @@
 Intent;%0A
+import android.support.annotation.StringRes;%0A
 import a
@@ -2208,36 +2208,37 @@
 st() %7B%0A        c
-lick
+hange
 PlaceTypeNotDefi
@@ -2234,106 +2234,43 @@
 Type
-NotDefine();%0A        onView(withText(R.string.village_community))%0A                .perform(click()
+FilterTo(R.string.village_community
 );%0A%0A
@@ -2626,20 +2626,21 @@
        c
-lick
+hange
 PlaceTyp
@@ -2644,96 +2644,33 @@
 Type
-NotDefine();%0A        onView(withText(R.string.factory))%0A                .perform(click()
+FilterTo(R.string.factory
 );%0A%0A
@@ -2744,20 +2744,21 @@
 e void c
-lick
+hange
 PlaceTyp
@@ -2762,18 +2762,41 @@
 Type
-NotDefine(
+FilterTo(@StringRes int placeType
 ) %7B%0A
@@ -2883,20 +2883,92 @@
 m(click());%0A
+        onView(withText(placeType))%0A                .perform(click());%0A%0A
     %7D%0A%7D%0A

Refactor PlaceListInDatabaseTest