Commit a97da2ea authored by DESKTOP-NFGF3PG\zxa01's avatar DESKTOP-NFGF3PG\zxa01

update test

parent 69a4f4b9
......@@ -46,5 +46,12 @@ dependencies {
// retrofit2
implementation "com.squareup.retrofit2:retrofit:2.1.0"
implementation "com.squareup.retrofit2:converter-gson:2.1.0"
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
exclude group: 'com.android.support', module: 'support-v4'
exclude group: 'com.android.support', module: 'design'
exclude group: 'com.android.support', module: 'recyclerview-v7'
}
}
package com.example.zxa01.iotclient.component.login;
import android.support.test.espresso.ViewInteraction;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import com.example.zxa01.iotclient.R;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.scrollTo;
import static android.support.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withClassName;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.is;
@LargeTest
@RunWith(AndroidJUnit4.class)
public class IOTClientTest {
@Rule
public ActivityTestRule<LoginActivity> mActivityTestRule = new ActivityTestRule<>(LoginActivity.class);
private static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
@Test
public void iOTClientTest() {
ViewInteraction appCompatButton = onView(
allOf(withId(R.id.sign_in_button), withText("登入"),
childAtPosition(
childAtPosition(
withClassName(is("android.widget.ScrollView")),
0),
4)));
appCompatButton.perform(scrollTo(), click());
ViewInteraction floatingActionButton = onView(
allOf(withId(R.id.fab),
childAtPosition(
childAtPosition(
withId(R.id.home_fragment_layout),
0),
2),
isDisplayed()));
floatingActionButton.perform(click());
ViewInteraction appCompatButton2 = onView(
allOf(withId(android.R.id.button2), withText("返回"),
childAtPosition(
childAtPosition(
withClassName(is("android.widget.ScrollView")),
0),
2)));
appCompatButton2.perform(scrollTo(), click());
ViewInteraction recyclerView = onView(
allOf(withId(R.id.device_recycler_view),
childAtPosition(
withClassName(is("android.widget.FrameLayout")),
0)));
recyclerView.perform(actionOnItemAtPosition(0, click()));
ViewInteraction appCompatButton3 = onView(
allOf(withId(R.id.buttonPrivacy), withText("隱私選項設定"),
childAtPosition(
childAtPosition(
withClassName(is("android.widget.TableRow")),
0),
1),
isDisplayed()));
appCompatButton3.perform(click());
ViewInteraction switch_ = onView(
allOf(withId(R.id.switch1), withText("拒絕/同意"),
childAtPosition(
childAtPosition(
withClassName(is("android.widget.LinearLayout")),
0),
1),
isDisplayed()));
switch_.perform(click());
// Added a sleep statement to match the app's execution delay.
// The recommended way to handle such scenarios is to use Espresso idling resources:
// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
ViewInteraction appCompatImageButton = onView(
allOf(withContentDescription("向上瀏覽"),
childAtPosition(
allOf(withId(R.id.action_bar),
childAtPosition(
withId(R.id.action_bar_container),
0)),
1),
isDisplayed()));
appCompatImageButton.perform(click());
ViewInteraction appCompatImageButton2 = onView(
allOf(withContentDescription("向上瀏覽"),
childAtPosition(
allOf(withId(R.id.action_bar),
childAtPosition(
withId(R.id.action_bar_container),
0)),
1),
isDisplayed()));
appCompatImageButton2.perform(click());
ViewInteraction bottomNavigationItemView = onView(
allOf(withId(R.id.navigation_setting), withContentDescription("Setting"),
childAtPosition(
childAtPosition(
withId(R.id.navigation),
0),
2),
isDisplayed()));
bottomNavigationItemView.perform(click());
ViewInteraction bottomNavigationItemView2 = onView(
allOf(withId(R.id.navigation_privacy), withContentDescription("Record"),
childAtPosition(
childAtPosition(
withId(R.id.navigation),
0),
1),
isDisplayed()));
bottomNavigationItemView2.perform(click());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment