Skip to content

Commit 8022a70

Browse files
committed
Twitch tests
1 parent ce0ba21 commit 8022a70

File tree

4 files changed

+266
-3
lines changed

4 files changed

+266
-3
lines changed

vendor/bat-native-ledger/src/bat/ledger/internal/bat_get_media.h

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <string>
1010
#include <map>
11+
#include <memory>
1112

1213
#include "bat/ledger/internal/bat_helper.h"
1314
#include "bat/ledger/internal/media/twitch.h"

vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.cc

+17-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <cmath>
88
#include <utility>
99
#include <vector>
10+
#include <iostream>
1011

1112
#include "bat/ledger/internal/bat_helper.h"
1213
#include "bat/ledger/internal/ledger_impl.h"
@@ -69,7 +70,10 @@ std::string MediaTwitch::GetMediaIdFromParts(
6970
std::string MediaTwitch::GetMediaURL(const std::string& media_id) {
7071
std::string res;
7172

72-
DCHECK(!media_id.empty());
73+
if (media_id.empty()) {
74+
return std::string();
75+
}
76+
7377
return "https://www.twitch.tv/" + media_id;
7478
}
7579

@@ -174,6 +178,8 @@ std::string MediaTwitch::GetLinkType(const std::string& url,
174178
bool is_valid_twitch_path =
175179
braveledger_bat_helper::HasSameDomainAndPath(
176180
url, "ttvnw.net", "/v1/segment/");
181+
182+
std::cout << is_valid_twitch_path << first_party_url;
177183
if (
178184
(
179185
(first_party_url.find("https://www.twitch.tv/") == 0 ||
@@ -192,6 +198,7 @@ std::string MediaTwitch::GetMediaIdFromUrl(
192198
const std::string& url,
193199
const std::string& publisher_blob) {
194200
std::string mediaId = braveledger_media::ExtractData(url, "twitch.tv/", "/");
201+
195202
if (url.find("twitch.tv/videos/") != std::string::npos) {
196203
mediaId = braveledger_media::ExtractData(publisher_blob,
197204
"<a class=\"tw-interactive channel-header__user tw-align-items-center "
@@ -207,7 +214,7 @@ std::string MediaTwitch::GetMediaIdFromUrl(
207214
std::string MediaTwitch::GetMediaKeyFromUrl(
208215
const std::string& id,
209216
const std::string& url) {
210-
if (id == "twitch") {
217+
if (id == "twitch" || id.empty()) {
211218
return std::string();
212219
}
213220

@@ -242,6 +249,10 @@ std::string MediaTwitch::GetPublisherName(
242249
std::string MediaTwitch::GetFaviconUrl(
243250
const std::string& publisher_blob,
244251
const std::string& handle) {
252+
if (handle.empty()) {
253+
return std::string();
254+
}
255+
245256
return braveledger_media::ExtractData(publisher_blob,
246257
"<figure class=\"tw-avatar tw-avatar--size-36\">"
247258
"<div class=\"tw-border-radius-medium tw-overflow-hidden\">"
@@ -252,6 +263,10 @@ std::string MediaTwitch::GetFaviconUrl(
252263

253264
// static
254265
std::string MediaTwitch::GetPublisherKey(const std::string& key) {
266+
if (key.empty()) {
267+
return std::string();
268+
}
269+
255270
return (std::string)TWITCH_MEDIA_TYPE + "#author:" + key;
256271
}
257272

vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.h

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <memory>
1111
#include <string>
1212

13+
#include "base/gtest_prod_util.h"
1314
#include "bat/ledger/ledger.h"
1415
#include "bat/ledger/internal/media/helper.h"
1516

@@ -122,6 +123,19 @@ class MediaTwitch : public ledger::LedgerCallbackHandler {
122123

123124
bat_ledger::LedgerImpl* ledger_; // NOT OWNED
124125
std::map<std::string, ledger::TwitchEventInfo> twitch_events;
126+
127+
// For testing purposes
128+
friend class MediaTwitchTest;
129+
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetMediaIdFromParts);
130+
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetMediaURL);
131+
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetTwitchStatus);
132+
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetTwitchDuration);
133+
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetMediaIdFromUrl);
134+
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetMediaKeyFromUrl);
135+
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetPublisherKey);
136+
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, UpdatePublisherData);
137+
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetPublisherName);
138+
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetFaviconUrl);
125139
};
126140

127141
} // namespace braveledger_media

vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch_unittest.cc

+234-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,239 @@ namespace braveledger_media {
1414
class MediaTwitchTest : public testing::Test {
1515
};
1616

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+
}
18251

19252
} // namespace braveledger_media

0 commit comments

Comments
 (0)