-
Notifications
You must be signed in to change notification settings - Fork 862
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support merging regional and custom hostname cosmetic adblock resources
- Loading branch information
1 parent
613cff1
commit 611881d
Showing
7 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
components/brave_shields/browser/cosmetic_merge_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* 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 "base/json/json_reader.h" | ||
#include "brave/components/brave_shields/browser/ad_block_service_helper.h" | ||
#include "testing/gmock/include/gmock/gmock.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace brave_shields { | ||
|
||
using ::testing::_; | ||
|
||
class CosmeticResourceMergeTest : public testing::Test { | ||
public: | ||
CosmeticResourceMergeTest() {} | ||
~CosmeticResourceMergeTest() override {} | ||
|
||
void CompareMergeFromStrings( | ||
const std::string& a, | ||
const std::string& b, | ||
const std::string& expected) { | ||
base::Optional<base::Value> a_val = base::JSONReader::Read(a); | ||
ASSERT_TRUE(a_val); | ||
|
||
base::Optional<base::Value> b_val = base::JSONReader::Read(b); | ||
ASSERT_TRUE(b_val); | ||
|
||
const base::Optional<base::Value> expected_val = | ||
base::JSONReader::Read(expected); | ||
ASSERT_TRUE(expected_val); | ||
|
||
MergeResourcesInto(&*a_val, &*b_val); | ||
|
||
ASSERT_EQ(*a_val, *expected_val); | ||
} | ||
|
||
protected: | ||
void SetUp() override {} | ||
|
||
void TearDown() override {} | ||
}; | ||
|
||
const char EMPTY_RESOURCES[] = "{" | ||
"\"hide_selectors\": [], " | ||
"\"style_selectors\": {}, " | ||
"\"exceptions\": [], " | ||
"\"injected_script\": \"\"" | ||
"}"; | ||
|
||
const char NONEMPTY_RESOURCES[] = "{" | ||
"\"hide_selectors\": [\"a\", \"b\"], " | ||
"\"style_selectors\": {\"c\": \"color: #fff\", \"d\": \"color: #000\"}, " | ||
"\"exceptions\": [\"e\", \"f\"], " | ||
"\"injected_script\": \"console.log('g')\"" | ||
"}"; | ||
|
||
TEST_F(CosmeticResourceMergeTest, MergeTwoEmptyResources) { | ||
const std::string a = EMPTY_RESOURCES; | ||
const std::string b = EMPTY_RESOURCES; | ||
|
||
// Same as EMPTY_RESOURCES, but with an additional newline in the | ||
// injected_script | ||
const std::string expected = "{" | ||
"\"hide_selectors\": [], " | ||
"\"style_selectors\": {}, " | ||
"\"exceptions\": [], " | ||
"\"injected_script\": \"\n\"" | ||
"}"; | ||
|
||
CompareMergeFromStrings(a, b, expected); | ||
} | ||
|
||
TEST_F(CosmeticResourceMergeTest, MergeEmptyIntoNonEmpty) { | ||
const std::string a = NONEMPTY_RESOURCES; | ||
const std::string b = EMPTY_RESOURCES; | ||
|
||
// Same as a, but with an additional newline at the end of the | ||
// injected_script | ||
const std::string expected = "{" | ||
"\"hide_selectors\": [\"a\", \"b\"], " | ||
"\"style_selectors\": {\"c\": \"color: #fff\", \"d\": \"color: #000\"}, " | ||
"\"exceptions\": [\"e\", \"f\"], " | ||
"\"injected_script\": \"console.log('g')\n\"" | ||
"}"; | ||
|
||
CompareMergeFromStrings(a, b, expected); | ||
} | ||
|
||
TEST_F(CosmeticResourceMergeTest, MergeNonEmptyIntoEmpty) { | ||
const std::string a = EMPTY_RESOURCES; | ||
const std::string b = NONEMPTY_RESOURCES; | ||
|
||
// Same as b, but with an additional newline at the beginning of the | ||
// injected_script | ||
const std::string expected = "{" | ||
"\"hide_selectors\": [\"a\", \"b\"]," | ||
"\"style_selectors\": {\"c\": \"color: #fff\", \"d\": \"color: #000\"}, " | ||
"\"exceptions\": [\"e\", \"f\"], " | ||
"\"injected_script\": \"\nconsole.log('g')\"" | ||
"}"; | ||
|
||
CompareMergeFromStrings(a, b, expected); | ||
} | ||
|
||
TEST_F(CosmeticResourceMergeTest, MergeNonEmptyIntoNonEmpty) { | ||
const std::string a = NONEMPTY_RESOURCES; | ||
const std::string b = "{" | ||
"\"hide_selectors\": [\"h\", \"i\"], " | ||
"\"style_selectors\": {\"j\": \"color: #eee\", \"k\": \"color: #111\"}, " | ||
"\"exceptions\": [\"l\", \"m\"], " | ||
"\"injected_script\": \"console.log('n')\"" | ||
"}"; | ||
|
||
const std::string expected = "{" | ||
"\"hide_selectors\": [\"a\", \"b\", \"h\", \"i\"], " | ||
"\"style_selectors\": {" | ||
"\"c\": \"color: #fff\", " | ||
"\"d\": \"color: #000\", " | ||
"\"j\": \"color: #eee\", " | ||
"\"k\": \"color: #111\"" | ||
"}, " | ||
"\"exceptions\": [\"e\", \"f\", \"l\", \"m\"], " | ||
"\"injected_script\": \"console.log('g')\nconsole.log('n')\"" | ||
"}"; | ||
|
||
CompareMergeFromStrings(a, b, expected); | ||
} | ||
|
||
|
||
} // namespace brave_shields |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters