1
+ package com .zulip .android .helper ;
2
+
3
+ import android .content .res .Resources ;
4
+ import android .support .test .espresso .matcher .BoundedMatcher ;
5
+ import android .support .v7 .widget .RecyclerView ;
6
+ import android .view .View ;
7
+ import android .widget .TextView ;
8
+
9
+ import com .zulip .android .R ;
10
+ import com .zulip .android .models .Message ;
11
+ import com .zulip .android .models .MessageType ;
12
+ import com .zulip .android .viewholders .MessageHeaderParent ;
13
+ import com .zulip .android .viewholders .MessageHolder ;
14
+
15
+ import org .hamcrest .Description ;
16
+ import org .hamcrest .Matcher ;
17
+ import org .hamcrest .TypeSafeMatcher ;
18
+
19
+ public class Matchers {
20
+
21
+ public static Matcher <View > withFirstId (final int id ) {
22
+ return new TypeSafeMatcher <View >() {
23
+ Resources resources = null ;
24
+ boolean found = false ;
25
+
26
+ @ Override
27
+ public void describeTo (Description description ) {
28
+ String idDescription = Integer .toString (id );
29
+ if (resources != null ) {
30
+ try {
31
+ idDescription = resources .getResourceName (id );
32
+ } catch (Resources .NotFoundException e ) {
33
+ // No big deal, will just use the int value.
34
+ idDescription = String .format ("%s (resource name not found)" , id );
35
+ }
36
+ }
37
+ description .appendText ("with id: " + idDescription );
38
+ }
39
+
40
+ @ Override
41
+ public boolean matchesSafely (View view ) {
42
+ if (found ) return false ;
43
+ resources = view .getResources ();
44
+ if (id == view .getId ()) {
45
+ found = true ;
46
+ return true ;
47
+ }
48
+ return false ;
49
+ }
50
+ };
51
+ }
52
+
53
+ public static Matcher <RecyclerView .ViewHolder > withMessageHolderAndClick (final MessageType messageType , final int id ) {
54
+ return new BoundedMatcher <RecyclerView .ViewHolder , MessageHolder >(MessageHolder .class ) {
55
+ private boolean found = false ;
56
+
57
+ @ Override
58
+ public void describeTo (Description description ) {
59
+ description .appendText ("No ViewHolder found with text: " + messageType .toString ());
60
+ }
61
+
62
+ @ Override
63
+ protected boolean matchesSafely (MessageHolder item ) {
64
+ if (found ) return false ;
65
+ if (item .getLayoutPosition () < 0 ) return false ;
66
+ Message message = item .onItemClickListener .getMessageAtPosition (item .getLayoutPosition ());
67
+ if (message == null ) return false ;
68
+ if (messageType == message .getType ()) {
69
+ if (id == R .id .contentView ) {
70
+ item .onItemClickListener .onItemClick (R .id .contentView , item .getLayoutPosition ());
71
+ }
72
+ found = true ;
73
+ return true ;
74
+ } else {
75
+ return false ;
76
+ }
77
+ }
78
+ };
79
+ }
80
+
81
+ public static Matcher <RecyclerView .ViewHolder > withMessageHolder (final String text , final int textViewId ) {
82
+ return new BoundedMatcher <RecyclerView .ViewHolder , MessageHolder >(MessageHolder .class ) {
83
+ private boolean found = false ;
84
+
85
+ @ Override
86
+ public void describeTo (Description description ) {
87
+ description .appendText ("No ViewHolder found with text: " );
88
+ }
89
+
90
+ @ Override
91
+ protected boolean matchesSafely (MessageHolder item ) {
92
+ if (found ) return false ;
93
+ found = ((TextView ) item .itemView .findViewById (textViewId )).getText ().toString ().matches (text );
94
+ return found ;
95
+ }
96
+ };
97
+ }
98
+
99
+ public static Matcher <RecyclerView .ViewHolder > withMessageHeaderHolder (final MessageType messageType ) {
100
+ return new BoundedMatcher <RecyclerView .ViewHolder , MessageHeaderParent .MessageHeaderHolder >(MessageHeaderParent .MessageHeaderHolder .class ) {
101
+ private boolean found = false ;
102
+
103
+ @ Override
104
+ public void describeTo (Description description ) {
105
+ description .appendText ("No ViewHolder found with text: " + messageType .toString ());
106
+ }
107
+
108
+ @ Override
109
+ protected boolean matchesSafely (MessageHeaderParent .MessageHeaderHolder item ) {
110
+ if (found ) return false ;
111
+ MessageHeaderParent messageHeaderParent = item .onItemClickListener .getMessageHeaderParentAtPosition (item .getLayoutPosition ());
112
+ found = (messageType == messageHeaderParent .getMessageType ());
113
+ return found ;
114
+ }
115
+ };
116
+ }
117
+ }
0 commit comments