瀏覽代碼

Feature: Add Unit Test sample and android UI Test sample.

tubinyan 5 年之前
父節點
當前提交
95a416eaa0

+ 1 - 0
app/build.gradle

@@ -69,4 +69,5 @@ dependencies {
     testImplementation 'junit:junit:4.+'
     testImplementation 'junit:junit:4.+'
     androidTestImplementation 'androidx.test.ext:junit:1.1.2'
     androidTestImplementation 'androidx.test.ext:junit:1.1.2'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
+    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.3.0'
 }
 }

+ 48 - 0
app/src/androidTest/java/cn/seecoder/courselearningdemo/SimpleTextSearchTest.java

@@ -0,0 +1,48 @@
+package cn.seecoder.courselearningdemo;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import androidx.test.espresso.contrib.RecyclerViewActions;
+import androidx.test.espresso.matcher.ViewMatchers;
+import androidx.test.ext.junit.rules.ActivityScenarioRule;
+import androidx.test.filters.LargeTest;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import static androidx.test.espresso.Espresso.onView;
+import static androidx.test.espresso.action.ViewActions.pressImeActionButton;
+import static androidx.test.espresso.action.ViewActions.typeText;
+import static androidx.test.espresso.matcher.ViewMatchers.hasDescendant;
+import static androidx.test.espresso.matcher.ViewMatchers.withId;
+import static androidx.test.espresso.matcher.ViewMatchers.withText;
+
+import cn.seecoder.courselearningdemo.ui.activity.MainActivity;
+
+@RunWith(AndroidJUnit4.class)
+@LargeTest
+public class SimpleTextSearchTest {
+
+    @Rule
+    public ActivityScenarioRule rule = new ActivityScenarioRule<>(MainActivity.class);
+
+    @Test
+    public void testSearch() throws InterruptedException {
+        // 在HomeFragment顶部的搜索栏里 输入“Java”
+        onView(withId(R.id.search_course)).perform(typeText("Java"));
+        Thread.sleep(1000);
+        // 按下搜索按钮后 等待后端返回数据
+        onView(withId(R.id.search_course)).perform(pressImeActionButton());
+        Thread.sleep(2000);
+        // 数据加载完成后,开始判断当前的 RecyclerView内列表元素项 是否符合预期
+        // 尝试将屏幕滚动至包含预期的字符串的元素项
+        // 当匹配不到相应的元素项时,scrollTo操作会失败
+        onView(ViewMatchers.withId(R.id.course_list_rv))
+                .perform(RecyclerViewActions.scrollTo(
+                        hasDescendant(withText(R.string.course_name_for_test_1))
+                ));
+        onView(ViewMatchers.withId(R.id.course_list_rv))
+                .perform(RecyclerViewActions.scrollTo(
+                        hasDescendant(withText(R.string.course_name_for_test_2))
+                ));
+    }
+}

+ 3 - 13
app/src/main/res/values/strings.xml

@@ -4,19 +4,6 @@
     <string name="logo_text">SEECODER</string>
     <string name="logo_text">SEECODER</string>
 
 
     <string name="text_search_tips">搜索课程</string>
     <string name="text_search_tips">搜索课程</string>
-
-    <string name="sample_description">Account Kit Demo</string>
-
-    <string name="sample_account_description">Account Usage Process</string>
-
-    <string name="sign_in">ID-Token Mode Signln</string>
-
-    <string name="sign_out">SignOut</string>
-
-    <string name="revokeAccess">AuthorizationCode Mode Signln</string>
-
-    <string name="silent_sign_in">Silent SignIn</string>
-
     <string name="cancel_authorization">Cancel Authorization</string>
     <string name="cancel_authorization">Cancel Authorization</string>
     <string name="text_home">首页</string>
     <string name="text_home">首页</string>
     <string name="text_study">课程学习</string>
     <string name="text_study">课程学习</string>
@@ -32,6 +19,7 @@
     <string name="course_intro_example">本课程内容涉及以下知识:Python编程,Java编程,Lambda演算,Java虚拟机,面向对象编程..</string>
     <string name="course_intro_example">本课程内容涉及以下知识:Python编程,Java编程,Lambda演算,Java虚拟机,面向对象编程..</string>
     <string name="course_buy_button_example">购买课程 28</string>
     <string name="course_buy_button_example">购买课程 28</string>
     <string name="course_show_button_example">浏览课程</string>
     <string name="course_show_button_example">浏览课程</string>
+    <string name="course_order">课程订单</string>
     <string name="recharge_text">钻石充值</string>
     <string name="recharge_text">钻石充值</string>
     <string name="user_name_text">用户昵称</string>
     <string name="user_name_text">用户昵称</string>
     <string name="user_balance_text_title">余额:</string>
     <string name="user_balance_text_title">余额:</string>
@@ -40,4 +28,6 @@
     <string name="login_warn_text">你还没有登录!</string>
     <string name="login_warn_text">你还没有登录!</string>
     <string name="none_bought_text">你还没有购买过任何课程</string>
     <string name="none_bought_text">你还没有购买过任何课程</string>
     <string name="account_courses_refresh_button">刷新列表</string>
     <string name="account_courses_refresh_button">刷新列表</string>
+    <string name="course_name_for_test_1">软件工程与计算 I</string>
+    <string name="course_name_for_test_2">基于Java的面向对象编程范式</string>
 </resources>
 </resources>

+ 42 - 0
app/src/test/java/cn/seecoder/courselearningdemo/DateUtilUnitTest.java

@@ -0,0 +1,42 @@
+package cn.seecoder.courselearningdemo;
+
+import org.junit.Test;
+
+import java.text.ParseException;
+import java.util.Calendar;
+import java.util.Date;
+
+import cn.seecoder.courselearningdemo.utils.DateUtil;
+
+import static org.junit.Assert.assertEquals;
+
+public class DateUtilUnitTest {
+
+    @Test
+    public void testGetDateString1() {
+        Calendar calendar = Calendar.getInstance();
+        calendar.set(Calendar.YEAR, 2021);
+        calendar.set(Calendar.MONTH, Calendar.JANUARY);
+        calendar.set(Calendar.DATE, 18);
+        calendar.set(Calendar.HOUR_OF_DAY, 20);
+        calendar.set(Calendar.MINUTE, 45);
+        calendar.set(Calendar.SECOND, 30);
+        Date date = calendar.getTime();
+        assertEquals("2021-01-18 20:45:30", DateUtil.getDateString(date));
+    }
+
+    @Test
+    public void testGetDateString2() {
+        long time = new Long("1610973930000");
+        Date date = new Date(time);
+        assertEquals("2021-01-18 20:45:30", DateUtil.getDateString(date));
+    }
+
+    @Test
+    public void testToDate() throws ParseException {
+        String dateString = "2021-01-18 20:45:30";
+        Date date = DateUtil.toDate(dateString);
+        long expected = new Long("1610973930000");
+        assertEquals(expected, date.getTime());
+    }
+}