@@ -14,6 +14,239 @@ namespace braveledger_media {
14
14
class MediaTwitchTest : public testing ::Test {
15
15
};
16
16
17
- // TODO(nejczdovc): add tests
17
+ const char profile_html[] =
18
+ " <div class=\" tw-align-items-center tw-flex tw-flex-nowrap "
19
+ " tw-flex-shrink-0\" ><div class=\" channel-header__user-avatar "
20
+ " channel-header__user-avatar--active tw-align-items-stretch tw-flex "
21
+ " tw-flex-shrink-0 tw-mg-r-1\" ><div class=\" tw-relative\" ><figure "
22
+ " class=\" tw-avatar tw-avatar--size-36\" ><div "
23
+ " class=\" tw-border-radius-medium tw-overflow-hidden\" ><img "
24
+ " class=\" tw-avatar__img tw-image\" alt=\" dakotaz\" "
25
+ " src=\" https://static-cdn.jtvnw.net/jtv_user_pictures/473aea0f-a724-498"
26
+ " f-b7f1-e344f806ba8a-profile_image-70x70.png\" ></div></figure></div></d"
27
+ " iv><h5 class=\"\" >dakotaz</h5><div class=\" tw-inline-flex "
28
+ " tw-tooltip-wrapper\" "
29
+ " aria-describedby=\" ffe665f4fd8fe08606c1140d995d1548\" ><div "
30
+ " data-target=\" channel-header__verified-badge\" "
31
+ " class=\" channel-header__verified tw-align-items-center tw-flex "
32
+ " tw-mg-l-1\" ><figure class=\" tw-svg\" ><svg class=\" tw-svg__asset "
33
+ " tw-svg__asset--inherit tw-svg__asset--verified\" width=\" 20px\" "
34
+ " height=\" 20px\" version=\" 1.1\" viewBox=\" 0 0 20 20\" x=\" 0px\" y=\" 0x\" > "
35
+ " <path d=\" M13.683 8.731l-4.286 4a1.002 1.002 0 0 1-1.365 0l-1.714-1.602a1 "
36
+ " 1 0 0 1 1.365-1.461l1.03.963 3.605-3.363a1.001 1.001 0 0 1 1.365 "
37
+ " 1.463m4.279 1.077l-2.196-5.303a.5.5 0 0 "
38
+ " 0-.271-.27l-5.303-2.197a.499.499 0 0 0-.383 0L4.506 4.234a.5.5 0 0 "
39
+ " 0-.271.271L2.038 9.808a.508.508 0 0 0 0 .383l2.194 5.304a.501.501 0 0 "
40
+ " 0 .27.27l5.307 2.196a.487.487 0 0 0 .383 0l5.303-2.196a.501.501 0 0 0 "
41
+ " .27-.27l2.197-5.304a.499.499 0 0 0 0-.383\" "
42
+ " fill-rule=\" evenodd\" ></path></svg></figure></div><div "
43
+ " class=\" tw-tooltip tw-tooltip--align-center tw-tooltip--right\" "
44
+ " data-a-target=\" tw-tooltip-label\" role=\" tooltip\" "
45
+ " id=\" ffe665f4fd8fe08606c1140d995d1548\" >Verified User</div></div></div>" ;
46
+
47
+ TEST (MediaTwitchTest, GetMediaIdFromParts) {
48
+ // empty
49
+ std::string result = MediaTwitch::GetMediaIdFromParts ({});
50
+ ASSERT_EQ (result, " " );
51
+
52
+ // event is not on the list
53
+ result = MediaTwitch::GetMediaIdFromParts ({
54
+ {" event" , " test" },
55
+ {" properties" , " " }
56
+ });
57
+ ASSERT_EQ (result, " " );
58
+
59
+ // properties are missing
60
+ result = MediaTwitch::GetMediaIdFromParts ({
61
+ {" event" , " minute-watched" }
62
+ });
63
+ ASSERT_EQ (result, " " );
64
+
65
+ // channel is missing
66
+ result = MediaTwitch::GetMediaIdFromParts ({
67
+ {" event" , " minute-watched" },
68
+ {" properties" , " " }
69
+ });
70
+ ASSERT_EQ (result, " " );
71
+
72
+ // channel is provided
73
+ result = MediaTwitch::GetMediaIdFromParts ({
74
+ {" event" , " minute-watched" },
75
+ {" properties" , " " },
76
+ {" channel" , " dakotaz" }
77
+ });
78
+ ASSERT_EQ (result, " dakotaz" );
79
+
80
+ // vod is missing leading v
81
+ result = MediaTwitch::GetMediaIdFromParts ({
82
+ {" event" , " minute-watched" },
83
+ {" properties" , " " },
84
+ {" channel" , " dakotaz" },
85
+ {" vod" , " 123312312" }
86
+ });
87
+ ASSERT_EQ (result, " dakotaz" );
88
+
89
+ // vod is provided
90
+ result = MediaTwitch::GetMediaIdFromParts ({
91
+ {" event" , " minute-watched" },
92
+ {" properties" , " " },
93
+ {" channel" , " dakotaz" },
94
+ {" vod" , " v123312312" }
95
+ });
96
+ ASSERT_EQ (result, " dakotaz_vod_123312312" );
97
+ }
98
+
99
+ TEST (MediaTwitchTest, GetMediaURL) {
100
+ // empty
101
+ std::string result = MediaTwitch::GetMediaURL (" " );
102
+ ASSERT_EQ (result, " " );
103
+
104
+ // all ok
105
+ result = MediaTwitch::GetMediaURL (" dakotaz" );
106
+ ASSERT_EQ (result, " https://www.twitch.tv/dakotaz" );
107
+ }
108
+
109
+ TEST (MediaTwitchTest, GetTwitchStatus) {
110
+ // empty
111
+ ledger::TwitchEventInfo old_event;
112
+ ledger::TwitchEventInfo new_event;
113
+ std::string result = MediaTwitch::GetTwitchStatus (old_event, new_event);
114
+ ASSERT_EQ (result, " playing" );
115
+
116
+ // user paused the video
117
+ old_event.event_ = " video_pause" ;
118
+ old_event.status_ = " playing" ;
119
+ new_event.event_ = " video_pause" ;
120
+ result = MediaTwitch::GetTwitchStatus (old_event, new_event);
121
+ ASSERT_EQ (result, " paused" );
122
+
123
+ // user seeked while video was paused
124
+ old_event.status_ = " paused" ;
125
+ new_event.event_ = " player_click_vod_seek" ;
126
+ result = MediaTwitch::GetTwitchStatus (old_event, new_event);
127
+ ASSERT_EQ (result, " paused" );
128
+
129
+ // user skipped the video that was playing
130
+ old_event.status_ = " playing" ;
131
+ old_event.event_ = " video_pause" ;
132
+ new_event.event_ = " player_click_vod_seek" ;
133
+ result = MediaTwitch::GetTwitchStatus (old_event, new_event);
134
+ ASSERT_EQ (result, " playing" );
135
+
136
+ // user pauses a video, then seeks it and plays it again
137
+ old_event.status_ = " paused" ;
138
+ old_event.event_ = " player_click_vod_seek" ;
139
+ new_event.event_ = " video_pause" ;
140
+ result = MediaTwitch::GetTwitchStatus (old_event, new_event);
141
+ ASSERT_EQ (result, " playing" );
142
+ }
143
+
144
+ TEST (MediaTwitchTest, GetLinkType ) {
145
+ const std::string url (" https://k8923479-sub.cdn.ttvnw.net/v1/segment/" );
146
+
147
+ // url is not correct
148
+ std::string result = MediaTwitch::GetLinkType (" https://brave.com" ,
149
+ " https://www.twitch.tv" ,
150
+ " " );
151
+ ASSERT_EQ (result, " " );
152
+
153
+ // first party is off
154
+ result = MediaTwitch::GetLinkType (url, " https://www.brave.com" , " " );
155
+ ASSERT_EQ (result, " " );
156
+
157
+ // regular page
158
+ result = MediaTwitch::GetLinkType (url, " https://www.twitch.tv/" , " " );
159
+ ASSERT_EQ (result, " twitch" );
160
+
161
+ // mobile page
162
+ result = MediaTwitch::GetLinkType (url, " https://m.twitch.tv/" , " " );
163
+ ASSERT_EQ (result, " twitch" );
164
+
165
+ // player page
166
+ result = MediaTwitch::GetLinkType (url,
167
+ " https://brave.com/" ,
168
+ " https://player.twitch.tv/" );
169
+ ASSERT_EQ (result, " twitch" );
170
+ }
171
+
172
+ TEST (MediaTwitchTest, GetMediaKeyFromUrl) {
173
+ // id is empty
174
+ std::string result = MediaTwitch::GetMediaKeyFromUrl (" " , " " );
175
+ ASSERT_EQ (result, " " );
176
+
177
+ // id is twitch
178
+ result = MediaTwitch::GetMediaKeyFromUrl (" twitch" , " " );
179
+ ASSERT_EQ (result, " " );
180
+
181
+ // get vod id
182
+ result = MediaTwitch::GetMediaKeyFromUrl (
183
+ " dakotaz" ,
184
+ " https://www.twitch.tv/videos/411403500" );
185
+ ASSERT_EQ (result, " twitch_dakotaz_vod_411403500" );
186
+
187
+ // regular id
188
+ result = MediaTwitch::GetMediaKeyFromUrl (" dakotaz" , " " );
189
+ ASSERT_EQ (result, " twitch_dakotaz" );
190
+ }
191
+
192
+ TEST (MediaTwitchTest, GetPublisherKey) {
193
+ // empty
194
+ std::string result = MediaTwitch::GetPublisherKey (" " );
195
+ ASSERT_EQ (result, " " );
196
+
197
+ // all ok
198
+ result = MediaTwitch::GetPublisherKey (" key" );
199
+ ASSERT_EQ (result, " twitch#author:key" );
200
+ }
201
+
202
+ TEST (MediaTwitchTest, GetPublisherName) {
203
+ // blob is not correct
204
+ std::string result = MediaTwitch::GetPublisherName (" dfsfsdfsdfds" );
205
+ ASSERT_EQ (result, " " );
206
+
207
+ // all ok
208
+ result = MediaTwitch::GetPublisherName (profile_html);
209
+ ASSERT_EQ (result, " dakotaz" );
210
+ }
211
+
212
+ TEST (MediaTwitchTest, GetFaviconUrl) {
213
+ // handler is empty
214
+ std::string result = MediaTwitch::GetFaviconUrl (profile_html, " " );
215
+ ASSERT_EQ (result, " " );
216
+
217
+ // blob is not correct
218
+ result = MediaTwitch::GetFaviconUrl (" dfsfsdfsdfds" , " dakotaz" );
219
+ ASSERT_EQ (result, " " );
220
+
221
+ // all ok
222
+ result = MediaTwitch::GetFaviconUrl (profile_html, " dakotaz" );
223
+ ASSERT_EQ (result,
224
+ " https://static-cdn.jtvnw.net/jtv_user_pictures/473aea0f-a724-498"
225
+ " f-b7f1-e344f806ba8a-profile_image-70x70.png" );
226
+ }
227
+
228
+ TEST (MediaTwitchTest, UpdatePublisherData) {
229
+ // blob is not correct
230
+ std::string name;
231
+ std::string favicon_url;
232
+ MediaTwitch::UpdatePublisherData (
233
+ &name,
234
+ &favicon_url,
235
+ " dfsfsdfsdfds" );
236
+
237
+ ASSERT_EQ (name, " " );
238
+ ASSERT_EQ (favicon_url, " " );
239
+
240
+ // all ok
241
+ MediaTwitch::UpdatePublisherData (
242
+ &name,
243
+ &favicon_url,
244
+ profile_html);
245
+
246
+ ASSERT_EQ (name, " dakotaz" );
247
+ ASSERT_EQ (favicon_url,
248
+ " https://static-cdn.jtvnw.net/jtv_user_pictures/473aea0f-a724-498"
249
+ " f-b7f1-e344f806ba8a-profile_image-70x70.png" );
250
+ }
18
251
19
252
} // namespace braveledger_media
0 commit comments