-
Notifications
You must be signed in to change notification settings - Fork 867
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 9c05217
Showing
7 changed files
with
261 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
149 changes: 149 additions & 0 deletions
149
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,149 @@ | ||
/* Copyright (c) 2019 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_base_service.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); | ||
if (!a_val) { | ||
LOG(ERROR) << "Could not parse first resources"; | ||
FAIL(); | ||
} | ||
|
||
base::Optional<base::Value> b_val = base::JSONReader::Read(b); | ||
if (!b_val) { | ||
LOG(ERROR) << "Could not parse second resources"; | ||
FAIL(); | ||
} | ||
|
||
const base::Optional<base::Value> expected_val = | ||
base::JSONReader::Read(expected); | ||
if (!expected_val) { | ||
LOG(ERROR) << "Could not parse expected resources"; | ||
FAIL(); | ||
} | ||
|
||
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\": \"\"" | ||
"}"; | ||
|
||
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 = "{" | ||
"\"hide_selectors\": [\"a\", \"b\"], " | ||
"\"style_selectors\": {\"c\": \"color: #fff\", \"d\": \"color: #000\"}, " | ||
"\"exceptions\": [\"e\", \"f\"], " | ||
"\"injected_script\": \"console.log('g')\"" | ||
"}"; | ||
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 = "{" | ||
"\"hide_selectors\": [\"a\", \"b\"], " | ||
"\"style_selectors\": {\"c\": \"color: #fff\", \"d\": \"color: #000\"}, " | ||
"\"exceptions\": [\"e\", \"f\"], " | ||
"\"injected_script\": \"console.log('g')\"" | ||
"}"; | ||
|
||
// 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 = "{" | ||
"\"hide_selectors\": [\"a\", \"b\"], " | ||
"\"style_selectors\": {\"c\": \"color: #fff\", \"d\": \"color: #000\"}, " | ||
"\"exceptions\": [\"e\", \"f\"], " | ||
"\"injected_script\": \"console.log('g')\"" | ||
"}"; | ||
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