1
+ package com .zulip .android .helper ;
2
+
3
+ import android .support .test .espresso .NoMatchingViewException ;
4
+ import android .support .test .espresso .ViewAssertion ;
5
+ import android .support .v7 .widget .RecyclerView ;
6
+ import android .text .format .DateUtils ;
7
+ import android .util .Log ;
8
+ import android .view .View ;
9
+
10
+ import com .zulip .android .ZulipApp ;
11
+ import com .zulip .android .activities .RecyclerMessageAdapter ;
12
+ import com .zulip .android .activities .ZulipActivity ;
13
+ import com .zulip .android .models .Message ;
14
+ import com .zulip .android .models .MessageType ;
15
+ import com .zulip .android .util .ZLog ;
16
+ import com .zulip .android .viewholders .MessageHeaderParent ;
17
+
18
+ import org .json .JSONException ;
19
+
20
+ import java .text .SimpleDateFormat ;
21
+ import java .util .Date ;
22
+ import java .util .Locale ;
23
+
24
+ import static android .support .test .espresso .matcher .ViewMatchers .assertThat ;
25
+ import static com .zulip .android .activities .ChatBoxTest .LOG_TAG ;
26
+ import static junit .framework .Assert .assertTrue ;
27
+
28
+ public class ViewAssertions {
29
+ public static ViewAssertion checkMessagesOnlyFromToday () {
30
+ return new ViewAssertion () {
31
+ @ Override
32
+ public void check (View view , NoMatchingViewException e ) {
33
+ if (!(view instanceof RecyclerView )) {
34
+ throw e ;
35
+ }
36
+ RecyclerView rv = (RecyclerView ) view ;
37
+ RecyclerMessageAdapter recyclerMessageAdapter = (RecyclerMessageAdapter ) rv .getAdapter ();
38
+ StringBuilder stringBuilder = new StringBuilder ();
39
+ stringBuilder .append ("OfDate-" + (new Date ()).toString () + " { " );
40
+ for (int index = 0 ; index < recyclerMessageAdapter .getItemCount (); index ++) {
41
+ if (recyclerMessageAdapter .getItem (index ) instanceof Message ) {
42
+ Message message = (Message ) recyclerMessageAdapter .getItem (index );
43
+ SimpleDateFormat sdf = new SimpleDateFormat ("HH:mm" , Locale .US );
44
+ stringBuilder .append (message .getID () + "-" + sdf .format (message .getTimestamp ()) + " , " );
45
+ assertTrue ("This message is not of today ID=" + message .getID () + ":" + message .getIdForHolder () + "\n content=" + message .getContent (), DateUtils .isToday (message .getTimestamp ().getTime ()));
46
+ }
47
+ }
48
+ stringBuilder .append (" }" );
49
+ printLogInPartsIfExceeded (stringBuilder , "checkMessagesOnlyFromToday" );
50
+ }
51
+ };
52
+ }
53
+
54
+ public static ViewAssertion checkOrderOfMessages (final ZulipActivity zulipActivity ) {
55
+ //ZulipActivity is needed for generating detailed info about position if doesn't matches
56
+ return new ViewAssertion () {
57
+ @ Override
58
+ public void check (View view , NoMatchingViewException noMatchingViewException ) {
59
+ if (!(view instanceof RecyclerView )) {
60
+ throw noMatchingViewException ;
61
+ }
62
+ StringBuilder stringBuilder = new StringBuilder ();
63
+ stringBuilder .append ('{' );
64
+ RecyclerMessageAdapter recyclerMessageAdapter = (RecyclerMessageAdapter ) ((RecyclerView ) view ).getAdapter ();
65
+ for (int i = 0 ; i < recyclerMessageAdapter .getItemCount () - 1 ; i ++) {
66
+ Object object = recyclerMessageAdapter .getItem (i );
67
+ if (object instanceof Message ) {
68
+ if (recyclerMessageAdapter .getItem (i + 1 ) instanceof Message ) {
69
+ boolean checkOrder = (((Message ) object ).getID () < ((Message ) recyclerMessageAdapter .getItem (i + 1 )).getID ());
70
+ assertTrue (generateDetailedInfoForOrder (((Message ) object ), ((Message ) recyclerMessageAdapter .getItem (i + 1 )), zulipActivity , i ), checkOrder );
71
+ }
72
+ stringBuilder .append (((Message ) object ).getID () + "," );
73
+ } else if (object instanceof MessageHeaderParent ) {
74
+ stringBuilder .append ("} | { " );
75
+ }
76
+ }
77
+ stringBuilder .append ('}' );
78
+ printLogInPartsIfExceeded (stringBuilder , "checkOrderOfMessages" );
79
+ }
80
+ };
81
+ }
82
+
83
+ private static String generateDetailedInfoForOrder (Message msg1 , Message msg2 , ZulipActivity zulipActivity , int index ) {
84
+ StringBuilder stringBuilder = new StringBuilder ();
85
+ stringBuilder .append ("atIndex: " + index );
86
+ stringBuilder .append ("Info for Message 1" );
87
+ stringBuilder .append ("Message ID:" + msg1 .getID () + "\n " );
88
+ stringBuilder .append ("Message idForHolder:" + msg1 .getIdForHolder () + "\n " );
89
+ stringBuilder .append ("Message content:" + msg1 .getContent () + "\n " );
90
+ stringBuilder .append ("Info for Message 2\n " );
91
+ stringBuilder .append ("Message ID:" + msg2 .getID () + "\n " );
92
+ stringBuilder .append ("Message idForHolder:" + msg2 .getIdForHolder () + "\n " );
93
+ stringBuilder .append ("Message content:" + msg2 .getContent () + "\n " );
94
+ if (zulipActivity .currentList .filter != null ) {
95
+ try {
96
+ stringBuilder .append ("JSONFilter json:" + zulipActivity .currentList .filter .getJsonFilter ());
97
+ } catch (JSONException e ) {
98
+ ZLog .logException (e );
99
+ }
100
+ stringBuilder .append ("currentTitle: " + zulipActivity .currentList .filter .getTitle ());
101
+ stringBuilder .append ("currentSubTitle: " + zulipActivity .currentList .filter .getSubtitle ());
102
+ } else {
103
+ stringBuilder .append ("currentZulipActivity: " + "homeList" );
104
+ }
105
+
106
+ return stringBuilder .toString ();
107
+ }
108
+
109
+ public static ViewAssertion checkIfMessagesMatchTheHeaderParent (final ZulipActivity zulipActivity ) {
110
+ return new ViewAssertion () {
111
+ @ Override
112
+ public void check (View view , NoMatchingViewException noMatchingViewException ) {
113
+ if (!(view instanceof RecyclerView )) {
114
+ throw noMatchingViewException ;
115
+ }
116
+
117
+ RecyclerMessageAdapter recyclerMessageAdapter = (RecyclerMessageAdapter ) ((RecyclerView ) view ).getAdapter ();
118
+ String lastHolderId = null ;
119
+ StringBuilder stringBuilder = new StringBuilder ();
120
+ for (int i = 0 ; i < recyclerMessageAdapter .getItemCount () - 1 ; i ++) {
121
+ Object object = recyclerMessageAdapter .getItem (i );
122
+ if (object instanceof MessageHeaderParent ) {
123
+ lastHolderId = ((MessageHeaderParent ) object ).getId ();
124
+ stringBuilder .append ("} \' MHP\' -\' " + lastHolderId + " {" );
125
+ } else if (object instanceof Message ) {
126
+ Message message = (Message ) object ;
127
+ boolean checkOrder = (lastHolderId .equals (message .getIdForHolder ()));
128
+ assertTrue (generateDetailDebugInfoForCorrectHeaderParent (message , zulipActivity , lastHolderId , i ), checkOrder );
129
+ stringBuilder .append (message .getIdForHolder () + "-" + message .getID () + " , " );
130
+ }
131
+ }
132
+ stringBuilder .append ('}' );
133
+ printLogInPartsIfExceeded (stringBuilder , "checkIfMessagesMatchTheHeaderParent" );
134
+ }
135
+ };
136
+ }
137
+
138
+ private static String generateDetailDebugInfoForCorrectHeaderParent (Message message , ZulipActivity zulipActivity , String lastHolderId , int index ) {
139
+ StringBuilder stringBuilder = new StringBuilder ();
140
+ stringBuilder .append ("AtIndex:" + index + "\n " );
141
+ stringBuilder .append ("LastHolderId MessageHeaderParent:" + lastHolderId + "\n " );
142
+ stringBuilder .append ("Info for Message 1\n " );
143
+ stringBuilder .append ("Message ID:" + message .getID () + "\n " );
144
+ stringBuilder .append ("Message idForHolder:" + message .getIdForHolder () + "\n " );
145
+ stringBuilder .append ("Message content:" + message .getContent () + "\n " );
146
+ if (zulipActivity .currentList .filter != null ) {
147
+
148
+ try {
149
+ stringBuilder .append ("JSONFilter json:" + zulipActivity .currentList .filter .getJsonFilter ());
150
+ } catch (JSONException e ) {
151
+ ZLog .logException (e );
152
+ }
153
+ stringBuilder .append ("currentTitle: " + zulipActivity .currentList .filter .getTitle ());
154
+ stringBuilder .append ("currentSubTitle: " + zulipActivity .currentList .filter .getSubtitle ());
155
+ } else {
156
+ stringBuilder .append ("currentZulipActivity: " + "homeList" );
157
+
158
+ }
159
+ return stringBuilder .toString ();
160
+ }
161
+
162
+ public static ViewAssertion checkIfBelongToSameNarrow (final ZulipActivity zulipActivity ) {
163
+ return new ViewAssertion () {
164
+ @ Override
165
+ public void check (View view , NoMatchingViewException noMatchingViewException ) {
166
+ if (!(view instanceof RecyclerView )) {
167
+ throw noMatchingViewException ;
168
+ }
169
+ RecyclerMessageAdapter recyclerMessageAdapter = (RecyclerMessageAdapter ) ((RecyclerView ) view ).getAdapter ();
170
+ String lastHolderId = null ;
171
+ int headerParentsCount = 0 ;
172
+ StringBuilder stringBuilder = new StringBuilder ();
173
+ for (int i = 0 ; i < recyclerMessageAdapter .getItemCount () - 1 ; i ++) {
174
+ Object object = recyclerMessageAdapter .getItem (i );
175
+ if (object instanceof MessageHeaderParent ) {
176
+ headerParentsCount ++;
177
+ assertTrue (generateDetailInfoSameNarrow ((MessageHeaderParent ) object , i , zulipActivity , lastHolderId ), headerParentsCount <= 1 );
178
+ lastHolderId = ((MessageHeaderParent ) object ).getId ();
179
+ stringBuilder .append (lastHolderId + " { " );
180
+ } else if (object instanceof Message ) {
181
+ boolean checkId = ((Message ) object ).getIdForHolder ().equals (lastHolderId );
182
+ assertTrue (generateDetailInfoSameNarrow (((Message ) object ), zulipActivity , lastHolderId , i ), checkId );
183
+ stringBuilder .append (((Message ) object ).getIdForHolder () + " , " );
184
+ }
185
+ }
186
+ stringBuilder .append (" }" );
187
+ printLogInPartsIfExceeded (stringBuilder , "checkIfBelongToSameNarrow" );
188
+ }
189
+ };
190
+ }
191
+
192
+ private static String generateDetailInfoSameNarrow (Message message , ZulipActivity zulipActivity , String lastHolderId , int index ) {
193
+ StringBuilder stringBuilder = new StringBuilder ();
194
+ stringBuilder .append ("AtIndex:" + index + "\n " );
195
+ stringBuilder .append ("lastHolderId:" + lastHolderId + "\n " );
196
+ stringBuilder .append ("message id:" + message .getIdForHolder () + "\n " );
197
+ stringBuilder .append ("vmessageType:" + message .getType ().toString () + "\n " );
198
+ stringBuilder .append ("message displayRecipent:" + message .getDisplayRecipient (ZulipApp .get ()) + "\n " );
199
+ if (message .getType () == MessageType .STREAM_MESSAGE ) {
200
+ stringBuilder .append ("message getStream:" + message .getStream ().getName () + "\n " );
201
+ stringBuilder .append ("message getSubject:" + message .getSubject () + "\n " );
202
+ } else {
203
+ stringBuilder .append ("message getRawRecipients:" + message .getRawRecipients () + "\n " );
204
+ }
205
+ stringBuilder .append ("Info for Message 1\n " );
206
+ stringBuilder .append ("\n CurrentListInfo:\n " );
207
+ if (zulipActivity .currentList .filter != null ) {
208
+ try {
209
+ stringBuilder .append ("JSONFilter json:" + zulipActivity .currentList .filter .getJsonFilter ());
210
+ } catch (JSONException e ) {
211
+ ZLog .logException (e );
212
+ }
213
+ stringBuilder .append ("currentTitle: " + zulipActivity .currentList .filter .getTitle ());
214
+ stringBuilder .append ("currentSubTitle: " + zulipActivity .currentList .filter .getSubtitle ());
215
+ } else {
216
+ stringBuilder .append ("currentZulipActivity: " + "homeList" );
217
+ }
218
+ return stringBuilder .toString ();
219
+
220
+ }
221
+
222
+ private static String generateDetailInfoSameNarrow (MessageHeaderParent object , int index , ZulipActivity zulipActivity , String lastHolderId ) {
223
+ StringBuilder stringBuilder = new StringBuilder ();
224
+ stringBuilder .append ("AtIndex:" + index + "\n " );
225
+ stringBuilder .append ("lastHolderId:" + lastHolderId + "\n " );
226
+ stringBuilder .append ("MessageHeaderParent id:" + object .getId () + "\n " );
227
+ stringBuilder .append ("MessageHeaderParent messageType:" + object .getMessageType ().toString () + "\n " );
228
+ stringBuilder .append ("MessageHeaderParent displayRecipent:" + object .getDisplayRecipent () + "\n " );
229
+ stringBuilder .append ("MessageHeaderParent getStream:" + object .getStream () + "\n " );
230
+ stringBuilder .append ("MessageHeaderParent getSubject:" + object .getSubject () + "\n " );
231
+ stringBuilder .append ("Info for Message 1\n " );
232
+ if (zulipActivity .currentList .filter != null ) {
233
+
234
+ try {
235
+ stringBuilder .append ("JSONFilter json:" + zulipActivity .currentList .filter .getJsonFilter ());
236
+ } catch (JSONException e ) {
237
+ ZLog .logException (e );
238
+ }
239
+ stringBuilder .append ("currentTitle: " + zulipActivity .currentList .filter .getTitle ());
240
+ stringBuilder .append ("currentSubTitle: " + zulipActivity .currentList .filter .getSubtitle ());
241
+ } else {
242
+ stringBuilder .append ("currentZulipActivity: " + "homeList" );
243
+
244
+ }
245
+ return stringBuilder .toString ();
246
+ }
247
+
248
+
249
+ private static void printLogInPartsIfExceeded (StringBuilder stringBuilder , String methodName ) {
250
+ if (stringBuilder .length () > 1800 ) {
251
+ int chunkCount = stringBuilder .length () / 4000 ;
252
+ for (int i = 0 ; i <= chunkCount ; i ++) {
253
+ int max = 4000 * (i + 1 );
254
+ if (max >= stringBuilder .length ()) {
255
+ Log .d (LOG_TAG , methodName + ": " + stringBuilder .substring (4000 * i ));
256
+ } else {
257
+ Log .d (LOG_TAG , methodName + ": " + stringBuilder .substring (4000 * i , max ));
258
+ }
259
+ }
260
+ } else {
261
+ Log .d (LOG_TAG , stringBuilder .toString ());
262
+ }
263
+ }
264
+
265
+
266
+ public static ViewAssertion hasItemsCount () {
267
+ return new ViewAssertion () {
268
+ @ Override
269
+ public void check (View view , NoMatchingViewException e ) {
270
+ if (!(view instanceof RecyclerView )) {
271
+ throw e ;
272
+ }
273
+ RecyclerView rv = (RecyclerView ) view ;
274
+ assertThat ("Items less than 2, which means no E-Mails!" , rv .getAdapter ().getItemCount (), org .hamcrest .Matchers .greaterThan (2 ));
275
+ }
276
+ };
277
+ }
278
+ }
0 commit comments