Skip to content

Commit

Permalink
Serialize/deserialize ntp ad info wallpapers.
Browse files Browse the repository at this point in the history
  • Loading branch information
aseren committed Mar 10, 2022
1 parent 2b3eaf9 commit 269833e
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 9 deletions.
1 change: 1 addition & 0 deletions components/brave_ads/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ source_set("brave_ads_unit_tests") {
"//brave/vendor/bat-native-ads/src/bat/ads/internal/user_activity/user_activity_tabs_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/user_activity/user_activity_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/user_activity/user_activity_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/new_tab_page_ad_info_unittest.cc",
]

deps = [
Expand Down
2 changes: 2 additions & 0 deletions vendor/bat-native-ads/include/bat/ads/new_tab_page_ad_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ struct ADS_EXPORT NewTabPageAdInfo final : AdInfo {
NewTabPageAdInfo(const NewTabPageAdInfo& info);
~NewTabPageAdInfo();

bool operator==(const NewTabPageAdInfo& rhs) const;

bool IsValid() const;

std::string ToJson() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ struct ADS_EXPORT NewTabPageAdWallpaperFocalPointInfo final {
const NewTabPageAdWallpaperFocalPointInfo& info);
~NewTabPageAdWallpaperFocalPointInfo();

bool operator==(const NewTabPageAdWallpaperFocalPointInfo& rhs) const;

int x;
int y;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ struct ADS_EXPORT NewTabPageAdWallpaperInfo final {
NewTabPageAdWallpaperInfo(const NewTabPageAdWallpaperInfo& info);
~NewTabPageAdWallpaperInfo();

bool operator==(const NewTabPageAdWallpaperInfo& rhs) const;

std::string image_url;
NewTabPageAdWallpaperFocalPointInfo focal_point;
};
Expand Down
12 changes: 7 additions & 5 deletions vendor/bat-native-ads/src/bat/ads/ad_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ AdInfo::AdInfo(const AdInfo& info) = default;
AdInfo::~AdInfo() = default;

bool AdInfo::operator==(const AdInfo& rhs) const {
return type == rhs.type && uuid == rhs.uuid &&
creative_instance_id == rhs.creative_instance_id &&
creative_set_id == rhs.creative_set_id &&
campaign_id == rhs.campaign_id && advertiser_id == rhs.advertiser_id &&
segment == rhs.segment && target_url == rhs.target_url;
auto tie = [](const AdInfo& ad) {
return std::tie(ad.type, ad.uuid, ad.creative_instance_id,
ad.creative_set_id, ad.campaign_id, ad.advertiser_id,
ad.segment, ad.target_url);
};

return tie(*this) == tie(rhs);
}

bool AdInfo::operator!=(const AdInfo& rhs) const {
Expand Down
67 changes: 63 additions & 4 deletions vendor/bat-native-ads/src/bat/ads/new_tab_page_ad_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "bat/ads/new_tab_page_ad_info.h"

#include <tuple>

#include "bat/ads/internal/json_helper.h"
#include "bat/ads/internal/logging.h"

Expand All @@ -16,6 +18,18 @@ NewTabPageAdInfo::NewTabPageAdInfo(const NewTabPageAdInfo& info) = default;

NewTabPageAdInfo::~NewTabPageAdInfo() = default;

bool NewTabPageAdInfo::operator==(const NewTabPageAdInfo& rhs) const {
if (!AdInfo::operator==(rhs)) {
return false;
}

auto tie = [](const NewTabPageAdInfo& ad) {
return std::tie(ad.company_name, ad.image_url, ad.alt, ad.wallpapers);
};

return tie(*this) == tie(rhs);
}

bool NewTabPageAdInfo::IsValid() const {
if (!AdInfo::IsValid()) {
return false;
Expand Down Expand Up @@ -84,8 +98,37 @@ bool NewTabPageAdInfo::FromJson(const std::string& json) {
alt = document["alt"].GetString();
}

// TODO(https://github.com/brave/brave-browser/issues/14015): Read wallpapers
// JSON
wallpapers.clear();
if (document.HasMember("wallpapers")) {
for (const auto& wallpaper : document["wallpapers"].GetArray()) {
NewTabPageAdWallpaperInfo wallpaper_info;
auto iterator = wallpaper.FindMember("image_url");
if (iterator == wallpaper.MemberEnd() || !iterator->value.IsString()) {
continue;
}
wallpaper_info.image_url = iterator->value.GetString();

iterator = wallpaper.FindMember("focal_point");
if (iterator == wallpaper.MemberEnd() || !iterator->value.IsObject()) {
continue;
}

auto focal_point = iterator->value.GetObject();
iterator = focal_point.FindMember("x");
if (iterator == focal_point.MemberEnd() || !iterator->value.IsInt()) {
continue;
}
wallpaper_info.focal_point.x = iterator->value.GetInt();

iterator = focal_point.FindMember("y");
if (iterator == focal_point.MemberEnd() || !iterator->value.IsInt()) {
continue;
}
wallpaper_info.focal_point.y = iterator->value.GetInt();

wallpapers.push_back(wallpaper_info);
}
}

if (document.HasMember("target_url")) {
target_url = document["target_url"].GetString();
Expand Down Expand Up @@ -128,8 +171,24 @@ void SaveToJson(JsonWriter* writer, const NewTabPageAdInfo& info) {
writer->String("alt");
writer->String(info.alt.c_str());

// TODO(https://github.com/brave/brave-browser/issues/14015): Write wallpapers
// JSON
writer->String("wallpapers");
writer->StartArray();
for (const NewTabPageAdWallpaperInfo& wallpaper_info : info.wallpapers) {
writer->StartObject();
writer->String("image_url");
writer->String(wallpaper_info.image_url.c_str());
{
writer->String("focal_point");
writer->StartObject();
writer->String("x");
writer->Int(wallpaper_info.focal_point.x);
writer->String("y");
writer->Int(wallpaper_info.focal_point.y);
writer->EndObject();
}
writer->EndObject();
}
writer->EndArray();

writer->String("target_url");
writer->String(info.target_url.c_str());
Expand Down
107 changes: 107 additions & 0 deletions vendor/bat-native-ads/src/bat/ads/new_tab_page_ad_info_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "bat/ads/new_tab_page_ad_info.h"
#include "bat/ads/internal/json_helper.h"
#include "bat/ads/internal/unittest_base.h"
#include "bat/ads/internal/unittest_time_util.h"

// npm run test -- brave_unit_tests --filter=BatAdsNewTabPageAdInfoTest*

namespace ads {

namespace {

constexpr char kSampleNewTabPageAdInfoJson[] = R"(
{
"type": "new_tab_page_ad",
"uuid": "a337efac-34b8-4b85-8fb4-6bd97d33d16c",
"creative_instance_id": "9ae0a1df-e249-4cb1-93b9-7a2e727d9dfd",
"creative_set_id": "aea63f45-5295-498a-9b1b-dae1609b0cf7",
"campaign_id": "2809e72c-0162-4a63-9751-f63ca1aa4571",
"advertiser_id": "f8eb8aa5-e05b-462f-a249-57757765f00b",
"segment": "test-test",
"company_name": "test company",
"image_url": "testing_image_logo",
"alt": "testing alt",
"wallpapers": [
{
"image_url": "testing_wallpaper1",
"focal_point": {
"x": 1,
"y": 2
}
},
{
"image_url": "testing_wallpaper2",
"focal_point": {
"x": 3,
"y": 4
}
}
],
"target_url": "https://brave.com"
}
)";

NewTabPageAdInfo GetSampleNewTabPageAdInfo() {
NewTabPageAdInfo ad_info;
ad_info.type = AdType::kNewTabPageAd;
ad_info.uuid = "a337efac-34b8-4b85-8fb4-6bd97d33d16c";
ad_info.creative_instance_id = "9ae0a1df-e249-4cb1-93b9-7a2e727d9dfd";
ad_info.creative_set_id = "aea63f45-5295-498a-9b1b-dae1609b0cf7";
ad_info.campaign_id = "2809e72c-0162-4a63-9751-f63ca1aa4571";
ad_info.advertiser_id = "f8eb8aa5-e05b-462f-a249-57757765f00b";
ad_info.segment = "test-test";
ad_info.company_name = "test company";
ad_info.image_url = "testing_image_logo";
ad_info.alt = "testing alt";
ad_info.target_url = "https://brave.com";

NewTabPageAdWallpaperInfo wallpaper_info;
wallpaper_info.image_url = "testing_wallpaper1";
wallpaper_info.focal_point.x = 1;
wallpaper_info.focal_point.y = 2;
ad_info.wallpapers.push_back(wallpaper_info);

wallpaper_info.image_url = "testing_wallpaper2";
wallpaper_info.focal_point.x = 3;
wallpaper_info.focal_point.y = 4;
ad_info.wallpapers.push_back(wallpaper_info);

return ad_info;
}

} // namespace

class BatAdsNewTabPageAdInfoTest : public UnitTestBase {
protected:
BatAdsNewTabPageAdInfoTest() {}

~BatAdsNewTabPageAdInfoTest() override {}
};

TEST_F(BatAdsNewTabPageAdInfoTest, DeserializeNewTabPageAdInfo) {
NewTabPageAdInfo ad_info;
ASSERT_TRUE(ad_info.FromJson(kSampleNewTabPageAdInfoJson));

NewTabPageAdInfo expected_ad_info = GetSampleNewTabPageAdInfo();
EXPECT_EQ(expected_ad_info, ad_info);
}

TEST_F(BatAdsNewTabPageAdInfoTest, SerializeNewTabPageAdInfo) {
NewTabPageAdInfo ad_info = GetSampleNewTabPageAdInfo();
std::string json = ad_info.ToJson();

rapidjson::Document document;
document.Parse(kSampleNewTabPageAdInfoJson);
rapidjson::StringBuffer expected_buffer;
JsonWriter writer(expected_buffer);
document.Accept(writer);

EXPECT_EQ(expected_buffer.GetString(), json);
}

} // namespace ads
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "bat/ads/new_tab_page_ad_wallpaper_focal_point_info.h"

#include <tuple>

namespace ads {

NewTabPageAdWallpaperFocalPointInfo::NewTabPageAdWallpaperFocalPointInfo() =
Expand All @@ -16,4 +18,13 @@ NewTabPageAdWallpaperFocalPointInfo::NewTabPageAdWallpaperFocalPointInfo(
NewTabPageAdWallpaperFocalPointInfo::~NewTabPageAdWallpaperFocalPointInfo() =
default;

bool NewTabPageAdWallpaperFocalPointInfo::operator==(
const NewTabPageAdWallpaperFocalPointInfo& rhs) const {
auto tie = [](const NewTabPageAdWallpaperFocalPointInfo& info) {
return std::tie(info.x, info.y);
};

return tie(*this) == tie(rhs);
}

} // namespace ads
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "bat/ads/new_tab_page_ad_wallpaper_info.h"

#include <tuple>

namespace ads {

NewTabPageAdWallpaperInfo::NewTabPageAdWallpaperInfo() = default;
Expand All @@ -14,4 +16,13 @@ NewTabPageAdWallpaperInfo::NewTabPageAdWallpaperInfo(

NewTabPageAdWallpaperInfo::~NewTabPageAdWallpaperInfo() = default;

bool NewTabPageAdWallpaperInfo::operator==(
const NewTabPageAdWallpaperInfo& rhs) const {
auto tie = [](const NewTabPageAdWallpaperInfo& info) {
return std::tie(info.image_url, info.focal_point);
};

return tie(*this) == tie(rhs);
}

} // namespace ads

0 comments on commit 269833e

Please sign in to comment.