Skip to content
This repository was archived by the owner on Jul 2, 2025. It is now read-only.

Commit 0f1f91a

Browse files
Test
1 parent 588851a commit 0f1f91a

File tree

6 files changed

+386
-62
lines changed

6 files changed

+386
-62
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.google.samples.quickstart.canonical
2+
3+
import androidx.test.espresso.Espresso.onData
4+
import androidx.test.espresso.Espresso.onView
5+
import androidx.test.espresso.action.ViewActions.click
6+
import androidx.test.espresso.assertion.ViewAssertions
7+
import androidx.test.espresso.assertion.ViewAssertions.matches
8+
import androidx.test.espresso.matcher.RootMatchers.isDialog
9+
import androidx.test.espresso.matcher.ViewMatchers
10+
import androidx.test.ext.junit.runners.AndroidJUnit4
11+
import androidx.test.platform.app.InstrumentationRegistry
12+
import androidx.test.rule.ActivityTestRule
13+
import androidx.test.uiautomator.UiDevice
14+
import androidx.test.uiautomator.UiSelector
15+
import org.hamcrest.Matchers
16+
import org.junit.After
17+
import org.junit.Assert.*
18+
import org.junit.Before
19+
import org.junit.Rule
20+
import org.junit.Test
21+
import org.junit.runner.RunWith
22+
23+
@RunWith(AndroidJUnit4::class)
24+
class DatabaseInteractionTest {
25+
@get:Rule
26+
var activityRule: ActivityTestRule<MainActivity>
27+
= ActivityTestRule(MainActivity::class.java)
28+
29+
lateinit var device : UiDevice
30+
31+
@Before
32+
fun setup() {
33+
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
34+
onView(ViewMatchers.withId(R.id.sign_in_button))
35+
.perform(click())
36+
val googleSignInDialog = device.findObject(UiSelector().text("yl4324@columbia.edu"))
37+
googleSignInDialog.clickAndWaitForNewWindow()
38+
// Make sure that:
39+
// 1. Your google account must have signed out before test.
40+
// 2. You should have at lest one google account for your device,
41+
// which means, when you click sign in button, you have at least
42+
// one account to choose
43+
}
44+
45+
@Test
46+
fun submitRecord() {
47+
onView(ViewMatchers.withId(R.id.start_pause_btn))
48+
.perform(click())
49+
Thread.sleep(5000)
50+
51+
onView(ViewMatchers.withId(R.id.submit_btn))
52+
.perform(click())
53+
Thread.sleep(2000)
54+
onView(ViewMatchers.withText("Submission Confirm"))
55+
.inRoot(isDialog())
56+
.check(matches(ViewMatchers.isDisplayed()))
57+
onView(ViewMatchers.withText("Confirm"))
58+
.inRoot(isDialog())
59+
.perform(click())
60+
Thread.sleep(1000)
61+
62+
onView(ViewMatchers.withId(R.id.bottom_navigation_item_profile))
63+
.perform(click())
64+
65+
onView(ViewMatchers.withId(R.id.run_history_list_view))
66+
.check(matches(ViewMatchers.isDisplayed()))
67+
onData(Matchers.anything())
68+
.inAdapterView(ViewMatchers.withId(R.id.run_history_list_view))
69+
.atPosition(0)
70+
.onChildView(ViewMatchers.withId(R.id.single_run_time))
71+
.check(matches(ViewMatchers.withText("00:00:05")))
72+
.perform(click())
73+
}
74+
75+
@After
76+
fun logoutUser() {
77+
onView(ViewMatchers.withId(R.id.bottom_navigation_item_profile))
78+
.perform(click())
79+
onView(ViewMatchers.withId(R.id.logout_button))
80+
.perform(click())
81+
Thread.sleep(1000)
82+
}
83+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.google.samples.quickstart.canonical
2+
3+
import androidx.test.espresso.Espresso
4+
import androidx.test.espresso.action.ViewActions
5+
import androidx.test.espresso.assertion.ViewAssertions
6+
import androidx.test.espresso.matcher.RootMatchers
7+
import androidx.test.espresso.matcher.ViewMatchers
8+
import androidx.test.ext.junit.runners.AndroidJUnit4
9+
import androidx.test.platform.app.InstrumentationRegistry
10+
import androidx.test.rule.ActivityTestRule
11+
import androidx.test.uiautomator.UiDevice
12+
import androidx.test.uiautomator.UiSelector
13+
import org.hamcrest.core.IsNot
14+
import org.junit.After
15+
import org.junit.Before
16+
import org.junit.Rule
17+
import org.junit.Test
18+
import org.junit.runner.RunWith
19+
20+
@RunWith(AndroidJUnit4::class)
21+
class LoginFragmentTest {
22+
@get:Rule
23+
var activityRule: ActivityTestRule<MainActivity>
24+
= ActivityTestRule(MainActivity::class.java)
25+
26+
lateinit var device : UiDevice
27+
28+
@Before
29+
fun setup() {
30+
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
31+
Espresso.onView(ViewMatchers.withId(R.id.sign_in_button))
32+
.perform(ViewActions.click())
33+
val googleSignInDialog = device.findObject(UiSelector().text("sjtuly1996@gmail.com"))
34+
googleSignInDialog.clickAndWaitForNewWindow()
35+
// Make sure that:
36+
// 1. Your google account must have signed out before test.
37+
// 2. You should have at lest one google account for your device,
38+
// which means, when you click sign in button, you have at least
39+
// one account to choose
40+
}
41+
42+
43+
@Test
44+
fun logoutAndSignIn() {
45+
// Logout test
46+
Espresso.onView(ViewMatchers.withId(R.id.bottom_navigation_item_profile))
47+
.perform(ViewActions.click())
48+
Espresso.onView(ViewMatchers.withId(R.id.logout_button))
49+
.perform(ViewActions.click())
50+
Thread.sleep(1000)
51+
52+
// Sign in test
53+
Espresso.onView(ViewMatchers.withId(R.id.sign_in_button))
54+
.perform(ViewActions.click())
55+
val googleSignInDialog = device.findObject(UiSelector().text("sjtuly1996@gmail.com"))
56+
googleSignInDialog.clickAndWaitForNewWindow()
57+
}
58+
59+
@Test
60+
fun signInFailureHandle() {
61+
// Logout
62+
Espresso.onView(ViewMatchers.withId(R.id.bottom_navigation_item_profile))
63+
.perform(ViewActions.click())
64+
Espresso.onView(ViewMatchers.withId(R.id.logout_button))
65+
.perform(ViewActions.click())
66+
Thread.sleep(1000)
67+
68+
// Sign in failure test
69+
Espresso.onView(ViewMatchers.withId(R.id.sign_in_button))
70+
.perform(ViewActions.click())
71+
device.pressBack()
72+
Espresso.onView(ViewMatchers.withText(R.string.login_failed))
73+
.inRoot(RootMatchers.withDecorView(IsNot.not(activityRule.activity.window.decorView)))
74+
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
75+
76+
// Sign in
77+
Espresso.onView(ViewMatchers.withId(R.id.sign_in_button))
78+
.perform(ViewActions.click())
79+
val googleSignInDialog = device.findObject(UiSelector().text("sjtuly1996@gmail.com"))
80+
googleSignInDialog.clickAndWaitForNewWindow()
81+
82+
}
83+
84+
85+
@After
86+
fun logoutUser() {
87+
Espresso.onView(ViewMatchers.withId(R.id.bottom_navigation_item_profile))
88+
.perform(ViewActions.click())
89+
Espresso.onView(ViewMatchers.withId(R.id.logout_button))
90+
.perform(ViewActions.click())
91+
Thread.sleep(1000)
92+
}
93+
}

android/canonical/app/src/androidTest/java/com/google/samples/quickstart/canonical/MapsFragmentTest.kt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.google.samples.quickstart.canonical
22

3+
import androidx.test.espresso.Espresso
34
import androidx.test.espresso.Espresso.onView
45
import androidx.test.espresso.action.ViewActions
56
import androidx.test.espresso.matcher.ViewMatchers
@@ -9,6 +10,7 @@ import androidx.test.rule.ActivityTestRule
910
import androidx.test.rule.GrantPermissionRule
1011
import androidx.test.uiautomator.UiDevice
1112
import androidx.test.uiautomator.UiSelector
13+
import org.junit.After
1214
import org.junit.Before
1315
import org.junit.Rule
1416
import org.junit.Test
@@ -30,11 +32,15 @@ class MapsFragmentTest {
3032
@Before
3133
fun setup() {
3234
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
33-
// Your google account must have logged in before test.
34-
// Otherwise the main page will be changed to login page,
35-
// and all the test will fail.
36-
// You only need to login the first time you open the app after
37-
// installing it.
35+
onView(ViewMatchers.withId(R.id.sign_in_button))
36+
.perform(ViewActions.click())
37+
val googleSignInDialog = device.findObject(UiSelector().text("sjtuly1996@gmail.com"))
38+
googleSignInDialog.clickAndWaitForNewWindow()
39+
// Make sure that:
40+
// 1. Your google account must have signed out before test.
41+
// 2. You should have at lest one google account for your device,
42+
// which means, when you click sign in button, you have at least
43+
// one account to choose
3844
}
3945

4046

@@ -45,10 +51,19 @@ class MapsFragmentTest {
4551
Thread.sleep(3000)
4652
onView(ViewMatchers.withId(R.id.autocomplete_fragment))
4753
.perform(ViewActions.click())
48-
val new = device.findObject(UiSelector().text("Search"))
49-
new.text = "Central Park"
54+
val searchView = device.findObject(UiSelector().text("Search"))
55+
searchView.text = "Central Park"
5056
Thread.sleep(2000)
5157
device.click(300,800)
5258
Thread.sleep(2000)
5359
}
60+
61+
@After
62+
fun logoutUser() {
63+
onView(ViewMatchers.withId(R.id.bottom_navigation_item_profile))
64+
.perform(ViewActions.click())
65+
onView(ViewMatchers.withId(R.id.logout_button))
66+
.perform(ViewActions.click())
67+
Thread.sleep(1000)
68+
}
5469
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.google.samples.quickstart.canonical
2+
3+
import androidx.test.espresso.Espresso.onData
4+
import androidx.test.espresso.Espresso.onView
5+
import androidx.test.espresso.action.ViewActions.click
6+
import androidx.test.espresso.assertion.ViewAssertions.matches
7+
import androidx.test.espresso.matcher.ViewMatchers.*
8+
import androidx.test.ext.junit.runners.AndroidJUnit4
9+
import androidx.test.platform.app.InstrumentationRegistry
10+
import androidx.test.rule.ActivityTestRule
11+
import androidx.test.uiautomator.UiDevice
12+
import androidx.test.uiautomator.UiSelector
13+
import org.hamcrest.Matchers.anything
14+
import org.junit.After
15+
import org.junit.Before
16+
import org.junit.Rule
17+
import org.junit.Test
18+
import org.junit.runner.RunWith
19+
20+
@RunWith(AndroidJUnit4::class)
21+
class ProfileFragmentTest {
22+
@get:Rule
23+
var activityRule: ActivityTestRule<MainActivity>
24+
= ActivityTestRule(MainActivity::class.java)
25+
26+
lateinit var device : UiDevice
27+
28+
@Before
29+
fun setup() {
30+
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
31+
onView(withId(R.id.sign_in_button))
32+
.perform(click())
33+
val googleSignInDialog = device.findObject(UiSelector().text("sjtuly1996@gmail.com"))
34+
googleSignInDialog.clickAndWaitForNewWindow()
35+
Thread.sleep(1000)
36+
// Make sure that:
37+
// 1. Your google account must have signed out before test.
38+
// 2. You should have at lest one google account for your device,
39+
// which means, when you click sign in button, you have at least
40+
// one account to choose
41+
}
42+
43+
@Test
44+
fun checkUserInfoTest() {
45+
onView(withId(R.id.bottom_navigation_item_profile))
46+
.perform(click())
47+
onView(withId(R.id.usr_img))
48+
.check(matches(isDisplayed()))
49+
onView(withId(R.id.usr_name))
50+
.check(matches(withText("Yang Li")))
51+
onView(withId(R.id.usr_email))
52+
.check(matches(withText("sjtuly1996@gmail.com")))
53+
}
54+
55+
@Test
56+
fun checkUserStatisticTest() {
57+
onView(withId(R.id.bottom_navigation_item_profile))
58+
.perform(click())
59+
onView(withId(R.id.usr_run_time))
60+
.check(matches(withText("00:05:39")))
61+
onView(withId(R.id.usr_run_energy))
62+
.check(matches(withText("74")))
63+
}
64+
65+
@Test
66+
fun checkUserRunHistoryTest() {
67+
onView(withId(R.id.bottom_navigation_item_profile))
68+
.perform(click())
69+
onView(withId(R.id.run_history_list_view))
70+
.check(matches(isDisplayed()))
71+
onData(anything()).inAdapterView(withId(R.id.run_history_list_view))
72+
.atPosition(0)
73+
.onChildView(withId(R.id.single_run_datetime))
74+
.check(matches(withText("2020-08-12 20:11:04")))
75+
.perform(click())
76+
onData(anything()).inAdapterView(withId(R.id.run_history_list_view))
77+
.atPosition(0)
78+
.onChildView(withId(R.id.single_run_time))
79+
.check(matches(withText("00:00:02")))
80+
.perform(click())
81+
}
82+
83+
84+
@After
85+
fun logoutUser() {
86+
onView(withId(R.id.bottom_navigation_item_profile))
87+
.perform(click())
88+
onView(withId(R.id.logout_button))
89+
.perform(click())
90+
Thread.sleep(1000)
91+
}
92+
}

0 commit comments

Comments
 (0)