Skip to content

Commit

Permalink
Twitch tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Apr 17, 2019
1 parent ce0ba21 commit 8022a70
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <string>
#include <map>
#include <memory>

#include "bat/ledger/internal/bat_helper.h"
#include "bat/ledger/internal/media/twitch.h"
Expand Down
19 changes: 17 additions & 2 deletions vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cmath>
#include <utility>
#include <vector>
#include <iostream>

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

DCHECK(!media_id.empty());
if (media_id.empty()) {
return std::string();
}

return "https://www.twitch.tv/" + media_id;
}

Expand Down Expand Up @@ -174,6 +178,8 @@ std::string MediaTwitch::GetLinkType(const std::string& url,
bool is_valid_twitch_path =
braveledger_bat_helper::HasSameDomainAndPath(
url, "ttvnw.net", "/v1/segment/");

std::cout << is_valid_twitch_path << first_party_url;
if (
(
(first_party_url.find("https://www.twitch.tv/") == 0 ||
Expand All @@ -192,6 +198,7 @@ std::string MediaTwitch::GetMediaIdFromUrl(
const std::string& url,
const std::string& publisher_blob) {
std::string mediaId = braveledger_media::ExtractData(url, "twitch.tv/", "/");

if (url.find("twitch.tv/videos/") != std::string::npos) {
mediaId = braveledger_media::ExtractData(publisher_blob,
"<a class=\"tw-interactive channel-header__user tw-align-items-center "
Expand All @@ -207,7 +214,7 @@ std::string MediaTwitch::GetMediaIdFromUrl(
std::string MediaTwitch::GetMediaKeyFromUrl(
const std::string& id,
const std::string& url) {
if (id == "twitch") {
if (id == "twitch" || id.empty()) {
return std::string();
}

Expand Down Expand Up @@ -242,6 +249,10 @@ std::string MediaTwitch::GetPublisherName(
std::string MediaTwitch::GetFaviconUrl(
const std::string& publisher_blob,
const std::string& handle) {
if (handle.empty()) {
return std::string();
}

return braveledger_media::ExtractData(publisher_blob,
"<figure class=\"tw-avatar tw-avatar--size-36\">"
"<div class=\"tw-border-radius-medium tw-overflow-hidden\">"
Expand All @@ -252,6 +263,10 @@ std::string MediaTwitch::GetFaviconUrl(

// static
std::string MediaTwitch::GetPublisherKey(const std::string& key) {
if (key.empty()) {
return std::string();
}

return (std::string)TWITCH_MEDIA_TYPE + "#author:" + key;
}

Expand Down
14 changes: 14 additions & 0 deletions vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <memory>
#include <string>

#include "base/gtest_prod_util.h"
#include "bat/ledger/ledger.h"
#include "bat/ledger/internal/media/helper.h"

Expand Down Expand Up @@ -122,6 +123,19 @@ class MediaTwitch : public ledger::LedgerCallbackHandler {

bat_ledger::LedgerImpl* ledger_; // NOT OWNED
std::map<std::string, ledger::TwitchEventInfo> twitch_events;

// For testing purposes
friend class MediaTwitchTest;
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetMediaIdFromParts);
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetMediaURL);
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetTwitchStatus);
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetTwitchDuration);
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetMediaIdFromUrl);
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetMediaKeyFromUrl);
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetPublisherKey);
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, UpdatePublisherData);
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetPublisherName);
FRIEND_TEST_ALL_PREFIXES(MediaTwitchTest, GetFaviconUrl);
};

} // namespace braveledger_media
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,239 @@ namespace braveledger_media {
class MediaTwitchTest : public testing::Test {
};

// TODO(nejczdovc): add tests
const char profile_html[] =
"<div class=\"tw-align-items-center tw-flex tw-flex-nowrap "
"tw-flex-shrink-0\"><div class=\"channel-header__user-avatar "
"channel-header__user-avatar--active tw-align-items-stretch tw-flex "
"tw-flex-shrink-0 tw-mg-r-1\"><div class=\"tw-relative\"><figure "
"class=\"tw-avatar tw-avatar--size-36\"><div "
"class=\"tw-border-radius-medium tw-overflow-hidden\"><img "
"class=\"tw-avatar__img tw-image\" alt=\"dakotaz\" "
"src=\"https://static-cdn.jtvnw.net/jtv_user_pictures/473aea0f-a724-498"
"f-b7f1-e344f806ba8a-profile_image-70x70.png\"></div></figure></div></d"
"iv><h5 class=\"\">dakotaz</h5><div class=\"tw-inline-flex "
"tw-tooltip-wrapper\" "
"aria-describedby=\"ffe665f4fd8fe08606c1140d995d1548\"><div "
"data-target=\"channel-header__verified-badge\" "
"class=\"channel-header__verified tw-align-items-center tw-flex "
"tw-mg-l-1\"><figure class=\"tw-svg\"><svg class=\"tw-svg__asset "
"tw-svg__asset--inherit tw-svg__asset--verified\" width=\"20px\" "
"height=\"20px\" version=\"1.1\" viewBox=\"0 0 20 20\" x=\"0px\" y=\"0x\"> "
"<path d=\"M13.683 8.731l-4.286 4a1.002 1.002 0 0 1-1.365 0l-1.714-1.602a1 "
"1 0 0 1 1.365-1.461l1.03.963 3.605-3.363a1.001 1.001 0 0 1 1.365 "
"1.463m4.279 1.077l-2.196-5.303a.5.5 0 0 "
"0-.271-.27l-5.303-2.197a.499.499 0 0 0-.383 0L4.506 4.234a.5.5 0 0 "
"0-.271.271L2.038 9.808a.508.508 0 0 0 0 .383l2.194 5.304a.501.501 0 0 "
"0 .27.27l5.307 2.196a.487.487 0 0 0 .383 0l5.303-2.196a.501.501 0 0 0 "
".27-.27l2.197-5.304a.499.499 0 0 0 0-.383\" "
"fill-rule=\"evenodd\"></path></svg></figure></div><div "
"class=\"tw-tooltip tw-tooltip--align-center tw-tooltip--right\" "
"data-a-target=\"tw-tooltip-label\" role=\"tooltip\" "
"id=\"ffe665f4fd8fe08606c1140d995d1548\">Verified User</div></div></div>";

TEST(MediaTwitchTest, GetMediaIdFromParts) {
// empty
std::string result = MediaTwitch::GetMediaIdFromParts({});
ASSERT_EQ(result, "");

// event is not on the list
result = MediaTwitch::GetMediaIdFromParts({
{"event", "test"},
{"properties", ""}
});
ASSERT_EQ(result, "");

// properties are missing
result = MediaTwitch::GetMediaIdFromParts({
{"event", "minute-watched"}
});
ASSERT_EQ(result, "");

// channel is missing
result = MediaTwitch::GetMediaIdFromParts({
{"event", "minute-watched"},
{"properties", ""}
});
ASSERT_EQ(result, "");

// channel is provided
result = MediaTwitch::GetMediaIdFromParts({
{"event", "minute-watched"},
{"properties", ""},
{"channel", "dakotaz"}
});
ASSERT_EQ(result, "dakotaz");

// vod is missing leading v
result = MediaTwitch::GetMediaIdFromParts({
{"event", "minute-watched"},
{"properties", ""},
{"channel", "dakotaz"},
{"vod", "123312312"}
});
ASSERT_EQ(result, "dakotaz");

// vod is provided
result = MediaTwitch::GetMediaIdFromParts({
{"event", "minute-watched"},
{"properties", ""},
{"channel", "dakotaz"},
{"vod", "v123312312"}
});
ASSERT_EQ(result, "dakotaz_vod_123312312");
}

TEST(MediaTwitchTest, GetMediaURL) {
// empty
std::string result = MediaTwitch::GetMediaURL("");
ASSERT_EQ(result, "");

// all ok
result = MediaTwitch::GetMediaURL("dakotaz");
ASSERT_EQ(result, "https://www.twitch.tv/dakotaz");
}

TEST(MediaTwitchTest, GetTwitchStatus) {
// empty
ledger::TwitchEventInfo old_event;
ledger::TwitchEventInfo new_event;
std::string result = MediaTwitch::GetTwitchStatus(old_event, new_event);
ASSERT_EQ(result, "playing");

// user paused the video
old_event.event_ = "video_pause";
old_event.status_ = "playing";
new_event.event_ = "video_pause";
result = MediaTwitch::GetTwitchStatus(old_event, new_event);
ASSERT_EQ(result, "paused");

// user seeked while video was paused
old_event.status_ = "paused";
new_event.event_ = "player_click_vod_seek";
result = MediaTwitch::GetTwitchStatus(old_event, new_event);
ASSERT_EQ(result, "paused");

// user skipped the video that was playing
old_event.status_ = "playing";
old_event.event_ = "video_pause";
new_event.event_ = "player_click_vod_seek";
result = MediaTwitch::GetTwitchStatus(old_event, new_event);
ASSERT_EQ(result, "playing");

// user pauses a video, then seeks it and plays it again
old_event.status_ = "paused";
old_event.event_ = "player_click_vod_seek";
new_event.event_ = "video_pause";
result = MediaTwitch::GetTwitchStatus(old_event, new_event);
ASSERT_EQ(result, "playing");
}

TEST(MediaTwitchTest, GetLinkType ) {
const std::string url("https://k8923479-sub.cdn.ttvnw.net/v1/segment/");

// url is not correct
std::string result = MediaTwitch::GetLinkType("https://brave.com",
"https://www.twitch.tv",
"");
ASSERT_EQ(result, "");

// first party is off
result = MediaTwitch::GetLinkType(url, "https://www.brave.com", "");
ASSERT_EQ(result, "");

// regular page
result = MediaTwitch::GetLinkType(url, "https://www.twitch.tv/", "");
ASSERT_EQ(result, "twitch");

// mobile page
result = MediaTwitch::GetLinkType(url, "https://m.twitch.tv/", "");
ASSERT_EQ(result, "twitch");

// player page
result = MediaTwitch::GetLinkType(url,
"https://brave.com/",
"https://player.twitch.tv/");
ASSERT_EQ(result, "twitch");
}

TEST(MediaTwitchTest, GetMediaKeyFromUrl) {
// id is empty
std::string result = MediaTwitch::GetMediaKeyFromUrl("", "");
ASSERT_EQ(result, "");

// id is twitch
result = MediaTwitch::GetMediaKeyFromUrl("twitch", "");
ASSERT_EQ(result, "");

// get vod id
result = MediaTwitch::GetMediaKeyFromUrl(
"dakotaz",
"https://www.twitch.tv/videos/411403500");
ASSERT_EQ(result, "twitch_dakotaz_vod_411403500");

// regular id
result = MediaTwitch::GetMediaKeyFromUrl("dakotaz", "");
ASSERT_EQ(result, "twitch_dakotaz");
}

TEST(MediaTwitchTest, GetPublisherKey) {
// empty
std::string result = MediaTwitch::GetPublisherKey("");
ASSERT_EQ(result, "");

// all ok
result = MediaTwitch::GetPublisherKey("key");
ASSERT_EQ(result, "twitch#author:key");
}

TEST(MediaTwitchTest, GetPublisherName) {
// blob is not correct
std::string result = MediaTwitch::GetPublisherName("dfsfsdfsdfds");
ASSERT_EQ(result, "");

// all ok
result = MediaTwitch::GetPublisherName(profile_html);
ASSERT_EQ(result, "dakotaz");
}

TEST(MediaTwitchTest, GetFaviconUrl) {
// handler is empty
std::string result = MediaTwitch::GetFaviconUrl(profile_html, "");
ASSERT_EQ(result, "");

// blob is not correct
result = MediaTwitch::GetFaviconUrl("dfsfsdfsdfds", "dakotaz");
ASSERT_EQ(result, "");

// all ok
result = MediaTwitch::GetFaviconUrl(profile_html, "dakotaz");
ASSERT_EQ(result,
"https://static-cdn.jtvnw.net/jtv_user_pictures/473aea0f-a724-498"
"f-b7f1-e344f806ba8a-profile_image-70x70.png");
}

TEST(MediaTwitchTest, UpdatePublisherData) {
// blob is not correct
std::string name;
std::string favicon_url;
MediaTwitch::UpdatePublisherData(
&name,
&favicon_url,
"dfsfsdfsdfds");

ASSERT_EQ(name, "");
ASSERT_EQ(favicon_url, "");

// all ok
MediaTwitch::UpdatePublisherData(
&name,
&favicon_url,
profile_html);

ASSERT_EQ(name, "dakotaz");
ASSERT_EQ(favicon_url,
"https://static-cdn.jtvnw.net/jtv_user_pictures/473aea0f-a724-498"
"f-b7f1-e344f806ba8a-profile_image-70x70.png");
}

} // namespace braveledger_media

0 comments on commit 8022a70

Please sign in to comment.