Skip to content

Commit

Permalink
Await for element in list.
Browse files Browse the repository at this point in the history
  • Loading branch information
boocmp committed Dec 16, 2024
1 parent dd60c78 commit 6eb81ae
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
41 changes: 28 additions & 13 deletions browser/brave_shields/ad_block_custom_resources_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,23 @@ namespace {

void AwaitElement(content::WebContents* web_contents,
const std::string& root,
const std::string& id) {
const std::string& id,
bool disappear = false) {
constexpr const char kScript[] = R"js(
(async () => {
while (!window.testing[$1].getElementById($2)) {
let waiter = () => { return !window.testing[$1].getElementById($2); };
if ($3) {
waiter = () => { return window.testing[$1].getElementById($2); };
}
while (waiter()) {
await new Promise(r => setTimeout(r, 10));
}
return true;
})();
)js";
EXPECT_TRUE(
content::EvalJs(web_contents, content::JsReplace(kScript, root, id))
.ExtractBool());
EXPECT_TRUE(content::EvalJs(web_contents,
content::JsReplace(kScript, root, id, disappear))
.ExtractBool());
}

bool ClickAddCustomScriptlet(content::WebContents* web_contents) {
Expand Down Expand Up @@ -90,12 +95,21 @@ std::string GetCustomScriptletContent(content::WebContents* web_contents) {
return GetCustomScriptletValue(web_contents, "scriptlet-content");
}

bool ClickSaveCustomScriptlet(content::WebContents* web_contents) {
bool ClickSaveCustomScriptlet(content::WebContents* web_contents,
const std ::string& name) {
AwaitElement(web_contents, "adblockScriptletEditor", "save");
return EvalJs(web_contents,
"window.testing.adblockScriptletEditor.getElementById('save')."
"click()")
.value.is_none();
if (!EvalJs(web_contents,
"window.testing.adblockScriptletEditor.getElementById('save')."
"click()")
.value.is_none()) {
return false;
}
std::string id = name.starts_with("user-") ? name : "user-" + name;
if (!id.ends_with(".js")) {
id += ".js";
}
AwaitElement(web_contents, "adblockScriptletList", id);
return true;
}

bool ClickCustomScriplet(content::WebContents* web_contents,
Expand Down Expand Up @@ -133,7 +147,7 @@ class AdblockCustomResourcesTest : public AdBlockServiceTest {

ASSERT_TRUE(SetCustomScriptletContent(web_contents(), value));
ASSERT_TRUE(SetCustomScriptletName(web_contents(), name));
ASSERT_TRUE(ClickSaveCustomScriptlet(web_contents()));
ASSERT_TRUE(ClickSaveCustomScriptlet(web_contents(), name));
}

void CheckCustomScriptlet(const base::Value& custom_scriptlet,
Expand Down Expand Up @@ -165,8 +179,7 @@ class AdblockCustomResourcesTest : public AdBlockServiceTest {
base::test::ScopedFeatureList feature_list_;
};

IN_PROC_BROWSER_TEST_F(AdblockCustomResourcesTest,
DISABLED_AddEditRemoveScriptlet) {
IN_PROC_BROWSER_TEST_F(AdblockCustomResourcesTest, AddEditRemoveScriptlet) {
EnableDeveloperMode(true);

NavigateToURL(GURL("brave://settings/shields/filters"));
Expand Down Expand Up @@ -202,6 +215,8 @@ IN_PROC_BROWSER_TEST_F(AdblockCustomResourcesTest,

ASSERT_TRUE(ClickCustomScriplet(web_contents(),
"user-custom-script-edited.js", "delete"));
AwaitElement(web_contents(), "adblockScriptletList",
"user-custom-script-edited.js", true);
{
const auto& custom_resources = GetCustomResources();
ASSERT_TRUE(custom_resources.is_list());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import { loadTimeData } from '../../i18n_setup.js'

import {
Scriptlet,
BraveAdblockBrowserProxyImpl
BraveAdblockBrowserProxyImpl,
ErrorCode
} from '../brave_adblock_browser_proxy.js'

import './brave_adblock_scriptlet_editor.js'
Expand Down Expand Up @@ -100,12 +101,13 @@ class AdblockScriptletList extends AdblockScriptletListBase {
}
}

this.browserProxy_.removeCustomScriptlet(
this.customScriptletsList_[e.model.index].name
)
this.browserProxy_.getCustomScriptlets().then((scriptlets) => {
this.customScriptletsList_ = scriptlets
})
this.browserProxy_
.removeCustomScriptlet(this.customScriptletsList_[e.model.index].name)
.then((_: ErrorCode) =>
this.browserProxy_.getCustomScriptlets().then((scriptlets) => {
this.customScriptletsList_ = scriptlets
})
)
}

scriptletEditorClosed_(_: any) {
Expand Down

0 comments on commit 6eb81ae

Please sign in to comment.