Merge pull request #7222 from brave/issues/12828

Add unit tests for Brave Ads country/subdivision codes
This commit is contained in:
Terry Mancey
2020-11-25 14:35:40 +00:00
committed by GitHub
10 changed files with 121 additions and 23 deletions
+2
View File
@@ -66,6 +66,8 @@ source_set("brave_ads_unit_tests") {
"//brave/vendor/bat-native-ads/src/bat/ads/internal/frequency_capping/permission_rules/unblinded_tokens_frequency_cap_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/frequency_capping/permission_rules/user_activity_frequency_cap_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/legacy_migration/legacy_migration_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/locale/country_code_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/locale/subdivision_code_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/p2a/p2a_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/platform/platform_helper_mock.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/platform/platform_helper_mock.h",
+2 -2
View File
@@ -400,10 +400,10 @@ source_set("ads") {
"src/bat/ads/internal/json_helper.h",
"src/bat/ads/internal/legacy_migration/legacy_migration_util.cc",
"src/bat/ads/internal/legacy_migration/legacy_migration_util.h",
"src/bat/ads/internal/locale/anonymous_country_codes.h",
"src/bat/ads/internal/locale/country_code_anonymity_set.h",
"src/bat/ads/internal/locale/country_code_util.cc",
"src/bat/ads/internal/locale/country_code_util.h",
"src/bat/ads/internal/locale/large_anonymity_country_codes.h",
"src/bat/ads/internal/locale/other_country_codes.h",
"src/bat/ads/internal/locale/subdivision_code_util.cc",
"src/bat/ads/internal/locale/subdivision_code_util.h",
"src/bat/ads/internal/locale/supported_country_codes.h",
@@ -3,15 +3,15 @@
* 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/. */
#ifndef BAT_ADS_INTERNAL_LOCALE_LARGE_ANONYMITY_COUNTRY_CODES_H_
#define BAT_ADS_INTERNAL_LOCALE_LARGE_ANONYMITY_COUNTRY_CODES_H_
#ifndef BAT_ADS_INTERNAL_LOCALE_COUNTRY_CODE_ANONYMITY_SET_H_
#define BAT_ADS_INTERNAL_LOCALE_COUNTRY_CODE_ANONYMITY_SET_H_
#include <set>
#include <string>
namespace ads {
const std::set<std::string> kLargeAnonymityCountryCodes = {
const std::set<std::string> kCountryCodeAnonymitySet = {
"US", // United States of America
"CA", // Canada
"GB", // United Kingdom (Great Britain and Northern Ireland)
@@ -146,4 +146,4 @@ const std::set<std::string> kLargeAnonymityCountryCodes = {
} // namespace ads
#endif // BAT_ADS_INTERNAL_LOCALE_LARGE_ANONYMITY_COUNTRY_CODES_H_
#endif // BAT_ADS_INTERNAL_LOCALE_COUNTRY_CODE_ANONYMITY_SET_H_
@@ -6,30 +6,30 @@
#include "bat/ads/internal/locale/country_code_util.h"
#include "brave/components/l10n/common/locale_util.h"
#include "bat/ads/internal/locale/anonymous_country_codes.h"
#include "bat/ads/internal/locale/large_anonymity_country_codes.h"
#include "bat/ads/internal/locale/country_code_anonymity_set.h"
#include "bat/ads/internal/locale/other_country_codes.h"
namespace ads {
namespace locale {
bool HasLargeAnonymity(
bool IsMemberOfAnonymitySet(
const std::string& locale) {
const std::string country_code = brave_l10n::GetCountryCode(locale);
const auto iter = kLargeAnonymityCountryCodes.find(country_code);
if (iter == kLargeAnonymityCountryCodes.end()) {
const auto iter = kCountryCodeAnonymitySet.find(country_code);
if (iter == kCountryCodeAnonymitySet.end()) {
return false;
}
return true;
}
bool IsAnonymous(
bool ShouldClassifyAsOther(
const std::string& locale) {
const std::string country_code = brave_l10n::GetCountryCode(locale);
const auto iter = kAnonymousCountryCodes.find(country_code);
if (iter == kAnonymousCountryCodes.end()) {
const auto iter = kOtherCountryCodes.find(country_code);
if (iter == kOtherCountryCodes.end()) {
return false;
}
@@ -11,10 +11,10 @@
namespace ads {
namespace locale {
bool HasLargeAnonymity(
bool IsMemberOfAnonymitySet(
const std::string& locale);
bool IsAnonymous(
bool ShouldClassifyAsOther(
const std::string& locale);
} // namespace locale
@@ -0,0 +1,60 @@
/* Copyright (c) 2020 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/internal/locale/country_code_util.h"
#include "testing/gtest/include/gtest/gtest.h"
// npm run test -- brave_unit_tests --filter=BatAds*
namespace ads {
TEST(BatAdsCountryCodeUtilTest,
IsMemberOfAnonymitySet) {
// Arrange
// Act
const bool is_member_of_anonymity_set =
locale::IsMemberOfAnonymitySet("en-US");
// Assert
EXPECT_TRUE(is_member_of_anonymity_set);
}
TEST(BatAdsCountryCodeUtilTest,
IsNotMemberOfAnonymitySet) {
// Arrange
// Act
const bool is_member_of_anonymity_set =
locale::IsMemberOfAnonymitySet("en-XX");
// Assert
EXPECT_FALSE(is_member_of_anonymity_set);
}
TEST(BatAdsCountryCodeUtilTest,
ShouldClassifyAsOther) {
// Arrange
// Act
const bool is_anonymous = locale::ShouldClassifyAsOther("en-CX");
// Assert
EXPECT_TRUE(is_anonymous);
}
TEST(BatAdsCountryCodeUtilTest,
ShouldNotClassifyAsOther) {
// Arrange
// Act
const bool is_anonymous = locale::ShouldClassifyAsOther("en-XX");
// Assert
EXPECT_FALSE(is_anonymous);
}
} // namespace ads
@@ -3,15 +3,15 @@
* 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/. */
#ifndef BAT_ADS_INTERNAL_LOCALE_ANONYMOUS_COUNTRY_CODES_H_
#define BAT_ADS_INTERNAL_LOCALE_ANONYMOUS_COUNTRY_CODES_H_
#ifndef BAT_ADS_INTERNAL_LOCALE_OTHER_COUNTRY_CODES_H_
#define BAT_ADS_INTERNAL_LOCALE_OTHER_COUNTRY_CODES_H_
#include <set>
#include <string>
namespace ads {
const std::set<std::string> kAnonymousCountryCodes = {
const std::set<std::string> kOtherCountryCodes = {
"AS", // American Samoa
"AI", // Anguilla
"AQ", // Antarctica
@@ -72,4 +72,4 @@ const std::set<std::string> kAnonymousCountryCodes = {
} // namespace ads
#endif // BAT_ADS_INTERNAL_LOCALE_ANONYMOUS_COUNTRY_CODES_H_
#endif // BAT_ADS_INTERNAL_LOCALE_OTHER_COUNTRY_CODES_H_
@@ -0,0 +1,36 @@
/* Copyright (c) 2020 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/internal/locale/subdivision_code_util.h"
#include "testing/gtest/include/gtest/gtest.h"
// npm run test -- brave_unit_tests --filter=BatAds*
namespace ads {
TEST(BatAdsSubdivisionCodeUtilTest,
GetCountryCode) {
// Arrange
// Act
const std::string country_code = locale::GetCountryCode("US-CA");
// Assert
EXPECT_EQ("US", country_code);
}
TEST(BatAdsSubdivisionCodeUtilTest,
GetSubdivisionCode) {
// Arrange
// Act
const std::string subdivision_code = locale::GetSubdivisionCode("US-CA");
// Assert
EXPECT_EQ("CA", subdivision_code);
}
} // namespace ads
@@ -19,7 +19,7 @@ const SupportedCountryCodesMap kSupportedCountryCodes = {
// Append newly supported country codes with a new schema version and update
// |kSupportedCountryCodesSchemaVersionNumber| to match the new version
// |kLargeAnonymityCountryCodes| and |kAnonymousCountryCodes| in
// |kCountryCodeAnonymitySet| and |kOtherCountryCodes| in
// |bat-native-ads/src/bat/ads/internal/locale/country_codes.h| must be
// updated to reflect newly supported regions
@@ -84,11 +84,11 @@ std::string CreateConfirmationRequestDTO(
const std::string locale =
brave_l10n::LocaleHelper::GetInstance()->GetLocale();
if (locale::HasLargeAnonymity(locale)) {
if (locale::IsMemberOfAnonymitySet(locale)) {
const std::string country_code = brave_l10n::GetCountryCode(locale);
dto.SetKey("countryCode", base::Value(country_code));
} else {
if (locale::IsAnonymous(locale)) {
if (locale::ShouldClassifyAsOther(locale)) {
dto.SetKey("countryCode", base::Value("??"));
}
}