|
1 | 1 | package org.fossasia.phimpme.leafpic.activities; |
2 | 2 |
|
| 3 | +import static android.content.Context.KEYGUARD_SERVICE; |
| 4 | +import static android.support.test.espresso.Espresso.onView; |
| 5 | +import static android.support.test.espresso.assertion.ViewAssertions.matches; |
| 6 | +import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; |
| 7 | +import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription; |
| 8 | +import static android.support.test.espresso.matcher.ViewMatchers.withId; |
| 9 | +import static org.hamcrest.Matchers.allOf; |
3 | 10 |
|
4 | 11 | import android.app.Activity; |
5 | 12 | import android.app.KeyguardManager; |
|
12 | 19 | import android.view.ViewGroup; |
13 | 20 | import android.view.ViewParent; |
14 | 21 | import android.view.WindowManager; |
15 | | - |
16 | 22 | import org.fossasia.phimpme.R; |
17 | 23 | import org.fossasia.phimpme.gallery.activities.LFMainActivity; |
18 | 24 | import org.hamcrest.Description; |
|
24 | 30 | import org.junit.Test; |
25 | 31 | import org.junit.runner.RunWith; |
26 | 32 |
|
27 | | -import static android.content.Context.KEYGUARD_SERVICE; |
28 | | -import static android.support.test.espresso.Espresso.onView; |
29 | | -import static android.support.test.espresso.assertion.ViewAssertions.matches; |
30 | | -import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; |
31 | | -import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription; |
32 | | -import static android.support.test.espresso.matcher.ViewMatchers.withId; |
33 | | -import static org.hamcrest.Matchers.allOf; |
34 | | - |
35 | 33 | @Ignore |
36 | 34 | @LargeTest |
37 | 35 | @RunWith(AndroidJUnit4.class) |
38 | 36 | public class HomeScreenTest { |
39 | 37 |
|
40 | | - @Rule |
41 | | - public ActivityTestRule<LFMainActivity> mActivityTestRule = new ActivityTestRule<>(LFMainActivity.class); |
42 | | - |
43 | | - @UiThreadTest |
44 | | - @Before |
45 | | - public void setUp() throws Exception { |
46 | | - final Activity activity = mActivityTestRule.getActivity(); |
47 | | - try { |
48 | | - mActivityTestRule.runOnUiThread(new Runnable() { |
49 | | - @Override |
50 | | - public void run() { |
51 | | - KeyguardManager mKG = (KeyguardManager) activity.getSystemService(KEYGUARD_SERVICE); |
52 | | - KeyguardManager.KeyguardLock mLock = mKG.newKeyguardLock(KEYGUARD_SERVICE); |
53 | | - mLock.disableKeyguard(); |
54 | | - |
55 | | - //turn the screen on |
56 | | - activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
57 | | - | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
58 | | - | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
59 | | - | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
60 | | - | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON); |
61 | | - } |
62 | | - }); |
63 | | - } catch (Throwable throwable) { |
64 | | - throwable.printStackTrace(); |
65 | | - } |
66 | | - } |
67 | | - |
68 | | - @Test |
69 | | - public void homeScreenTest() { |
70 | | - // Added a sleep statement to match the app's execution delay. |
71 | | - // The recommended way to handle such scenarios is to use Espresso idling resources: |
72 | | - // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html |
73 | | - try { |
74 | | - Thread.sleep(1000); |
75 | | - } catch (InterruptedException e) { |
76 | | - e.printStackTrace(); |
77 | | - } |
78 | | - |
79 | | - ViewInteraction frameLayout = onView( |
80 | | - allOf(withId(R.id.navigation_accounts), withContentDescription("Accounts"), |
81 | | - childAtPosition( |
82 | | - childAtPosition( |
83 | | - withId(R.id.bottombar), |
84 | | - 0), |
85 | | - 2), |
86 | | - isDisplayed())); |
87 | | - frameLayout.check(matches(isDisplayed())); |
88 | | - |
89 | | - ViewInteraction frameLayout3 = onView( |
90 | | - allOf(withId(R.id.navigation_camera), withContentDescription("Camera"), |
91 | | - childAtPosition( |
92 | | - childAtPosition( |
93 | | - withId(R.id.bottombar), |
94 | | - 0), |
95 | | - 0), |
96 | | - isDisplayed())); |
97 | | - frameLayout3.check(matches(isDisplayed())); |
98 | | - |
99 | | - ViewInteraction frameLayout4 = onView( |
100 | | - allOf(withId(R.id.navigation_home), |
101 | | - childAtPosition( |
102 | | - childAtPosition( |
103 | | - withId(R.id.bottombar), |
104 | | - 0), |
105 | | - 1), |
106 | | - isDisplayed())); |
107 | | - frameLayout4.check(matches(isDisplayed())); |
108 | | - |
109 | | - // Added a sleep statement to match the app's execution delay. |
110 | | - // The recommended way to handle such scenarios is to use Espresso idling resources: |
111 | | - // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html |
112 | | - try { |
113 | | - Thread.sleep(100); |
114 | | - } catch (InterruptedException e) { |
115 | | - e.printStackTrace(); |
116 | | - } |
117 | | - |
118 | | - } |
119 | | - |
120 | | - private static Matcher<View> childAtPosition( |
121 | | - final Matcher<View> parentMatcher, final int position) { |
122 | | - |
123 | | - return new TypeSafeMatcher<View>() { |
| 38 | + @Rule |
| 39 | + public ActivityTestRule<LFMainActivity> mActivityTestRule = |
| 40 | + new ActivityTestRule<>(LFMainActivity.class); |
| 41 | + |
| 42 | + @UiThreadTest |
| 43 | + @Before |
| 44 | + public void setUp() throws Exception { |
| 45 | + final Activity activity = mActivityTestRule.getActivity(); |
| 46 | + try { |
| 47 | + mActivityTestRule.runOnUiThread( |
| 48 | + new Runnable() { |
124 | 49 | @Override |
125 | | - public void describeTo(Description description) { |
126 | | - description.appendText("Child at position " + position + " in parent "); |
127 | | - parentMatcher.describeTo(description); |
| 50 | + public void run() { |
| 51 | + KeyguardManager mKG = (KeyguardManager) activity.getSystemService(KEYGUARD_SERVICE); |
| 52 | + KeyguardManager.KeyguardLock mLock = mKG.newKeyguardLock(KEYGUARD_SERVICE); |
| 53 | + mLock.disableKeyguard(); |
| 54 | + |
| 55 | + // turn the screen on |
| 56 | + activity |
| 57 | + .getWindow() |
| 58 | + .addFlags( |
| 59 | + WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
| 60 | + | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
| 61 | + | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
| 62 | + | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
| 63 | + | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON); |
128 | 64 | } |
| 65 | + }); |
| 66 | + } catch (Throwable throwable) { |
| 67 | + throwable.printStackTrace(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void homeScreenTest() { |
| 73 | + // Added a sleep statement to match the app's execution delay. |
| 74 | + // The recommended way to handle such scenarios is to use Espresso idling resources: |
| 75 | + // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html |
| 76 | + try { |
| 77 | + Thread.sleep(1000); |
| 78 | + } catch (InterruptedException e) { |
| 79 | + e.printStackTrace(); |
| 80 | + } |
129 | 81 |
|
130 | | - @Override |
131 | | - public boolean matchesSafely(View view) { |
132 | | - ViewParent parent = view.getParent(); |
133 | | - return parent instanceof ViewGroup && parentMatcher.matches(parent) |
134 | | - && view.equals(((ViewGroup) parent).getChildAt(position)); |
135 | | - } |
136 | | - }; |
| 82 | + ViewInteraction frameLayout = |
| 83 | + onView( |
| 84 | + allOf( |
| 85 | + withId(R.id.navigation_accounts), |
| 86 | + withContentDescription("Accounts"), |
| 87 | + childAtPosition(childAtPosition(withId(R.id.bottombar), 0), 2), |
| 88 | + isDisplayed())); |
| 89 | + frameLayout.check(matches(isDisplayed())); |
| 90 | + |
| 91 | + ViewInteraction frameLayout3 = |
| 92 | + onView( |
| 93 | + allOf( |
| 94 | + withId(R.id.navigation_camera), |
| 95 | + withContentDescription("Camera"), |
| 96 | + childAtPosition(childAtPosition(withId(R.id.bottombar), 0), 0), |
| 97 | + isDisplayed())); |
| 98 | + frameLayout3.check(matches(isDisplayed())); |
| 99 | + |
| 100 | + ViewInteraction frameLayout4 = |
| 101 | + onView( |
| 102 | + allOf( |
| 103 | + withId(R.id.navigation_home), |
| 104 | + childAtPosition(childAtPosition(withId(R.id.bottombar), 0), 1), |
| 105 | + isDisplayed())); |
| 106 | + frameLayout4.check(matches(isDisplayed())); |
| 107 | + |
| 108 | + // Added a sleep statement to match the app's execution delay. |
| 109 | + // The recommended way to handle such scenarios is to use Espresso idling resources: |
| 110 | + // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html |
| 111 | + try { |
| 112 | + Thread.sleep(100); |
| 113 | + } catch (InterruptedException e) { |
| 114 | + e.printStackTrace(); |
137 | 115 | } |
| 116 | + } |
| 117 | + |
| 118 | + private static Matcher<View> childAtPosition( |
| 119 | + final Matcher<View> parentMatcher, final int position) { |
| 120 | + |
| 121 | + return new TypeSafeMatcher<View>() { |
| 122 | + @Override |
| 123 | + public void describeTo(Description description) { |
| 124 | + description.appendText("Child at position " + position + " in parent "); |
| 125 | + parentMatcher.describeTo(description); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public boolean matchesSafely(View view) { |
| 130 | + ViewParent parent = view.getParent(); |
| 131 | + return parent instanceof ViewGroup |
| 132 | + && parentMatcher.matches(parent) |
| 133 | + && view.equals(((ViewGroup) parent).getChildAt(position)); |
| 134 | + } |
| 135 | + }; |
| 136 | + } |
138 | 137 | } |
0 commit comments