-
Notifications
You must be signed in to change notification settings - Fork 909
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
52 changed files
with
2,632 additions
and
445 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
81 changes: 81 additions & 0 deletions
81
browser/ntp_background/ntp_sponsored_rich_media_browsertest.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,81 @@ | ||
/* Copyright (c) 2025 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 https://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "base/command_line.h" | ||
#include "base/files/file_path.h" | ||
#include "base/functional/bind.h" | ||
#include "base/path_service.h" | ||
#include "brave/browser/brave_browser_process.h" | ||
#include "brave/components/constants/brave_paths.h" | ||
#include "brave/components/ntp_background_images/browser/ntp_background_images_service.h" | ||
#include "brave/components/ntp_background_images/browser/ntp_background_images_service_waiter.h" | ||
#include "chrome/browser/ui/browser.h" | ||
#include "chrome/test/base/chrome_test_utils.h" | ||
#include "chrome/test/base/platform_browser_test.h" | ||
#include "chrome/test/base/ui_test_utils.h" | ||
#include "content/public/browser/web_contents_observer.h" | ||
#include "content/public/test/browser_test.h" | ||
#include "content/public/test/browser_test_utils.h" | ||
#include "content/public/test/test_navigation_observer.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
#include "url/gurl.h" | ||
|
||
namespace ntp_background_images { | ||
|
||
namespace { | ||
|
||
constexpr char kRichMediaUrl[] = | ||
R"(chrome-untrusted://rich-media/aa0b561e-9eed-4aaa-8999-5627bc6b14fd/index.html)"; | ||
|
||
} // namespace | ||
|
||
class NTPSponsoredRichMediaBrowserTest : public PlatformBrowserTest { | ||
protected: | ||
void SetUpOnMainThread() override { | ||
PlatformBrowserTest::SetUpOnMainThread(); | ||
|
||
const base::FilePath test_data_file_path = | ||
base::PathService::CheckedGet(brave::DIR_TEST_DATA); | ||
|
||
const base::FilePath component_file_path = | ||
test_data_file_path.AppendASCII("ntp_background_images") | ||
.AppendASCII("components") | ||
.AppendASCII("rich_media"); | ||
|
||
NTPBackgroundImagesService* const ntp_background_images_service = | ||
g_brave_browser_process->ntp_background_images_service(); | ||
ASSERT_TRUE(ntp_background_images_service); | ||
|
||
NTPBackgroundImagesServiceWaiter waiter(*ntp_background_images_service); | ||
ntp_background_images_service->OnSponsoredComponentReady( | ||
/*is_super_referral=*/false, component_file_path); | ||
waiter.WaitForOnSponsoredImagesUpdated(); | ||
} | ||
|
||
content::WebContents* GetActiveWebContents() { | ||
return chrome_test_utils::GetActiveWebContents(this); | ||
} | ||
}; | ||
|
||
IN_PROC_BROWSER_TEST_F(NTPSponsoredRichMediaBrowserTest, | ||
LoadResourceAndClickButton) { | ||
content::WebContentsConsoleObserver console_observer(GetActiveWebContents()); | ||
console_observer.SetFilter(base::BindRepeating( | ||
[](const content::WebContentsConsoleObserver::Message& message) { | ||
return message.log_level == blink::mojom::ConsoleMessageLevel::kError; | ||
})); | ||
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GURL(kRichMediaUrl))); | ||
EXPECT_TRUE(console_observer.messages().empty()); | ||
|
||
ASSERT_TRUE( | ||
content::ExecJs(GetActiveWebContents(), | ||
"document.querySelector('.button-box').click();")); | ||
content::EvalJsResult result = | ||
content::EvalJs(GetActiveWebContents(), | ||
"document.querySelector('.button-box').textContent;"); | ||
EXPECT_EQ(result.ExtractString(), "Clicked"); | ||
} | ||
|
||
} // namespace ntp_background_images |
93 changes: 93 additions & 0 deletions
93
browser/ntp_background/ntp_sponsored_rich_media_with_csp_violation_browsertest.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,93 @@ | ||
/* Copyright (c) 2025 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 https://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "base/command_line.h" | ||
#include "base/files/file_path.h" | ||
#include "base/path_service.h" | ||
#include "brave/browser/brave_browser_process.h" | ||
#include "brave/components/constants/brave_paths.h" | ||
#include "brave/components/ntp_background_images/browser/ntp_background_images_service.h" | ||
#include "brave/components/ntp_background_images/browser/ntp_background_images_service_waiter.h" | ||
#include "chrome/browser/ui/browser.h" | ||
#include "chrome/test/base/chrome_test_utils.h" | ||
#include "chrome/test/base/platform_browser_test.h" | ||
#include "chrome/test/base/ui_test_utils.h" | ||
#include "content/public/browser/web_contents_observer.h" | ||
#include "content/public/test/browser_test.h" | ||
#include "content/public/test/browser_test_utils.h" | ||
#include "content/public/test/test_navigation_observer.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
#include "url/gurl.h" | ||
|
||
namespace ntp_background_images { | ||
|
||
namespace { | ||
|
||
constexpr char kRichMediaUrl[] = | ||
R"(chrome-untrusted://rich-media/aa0b561e-9eed-4aaa-8999-5627bc6b14fd/index.html)"; | ||
|
||
} // namespace | ||
|
||
class NTPSponsoredRichMediaWithCSPViolationBrowserTest | ||
: public PlatformBrowserTest { | ||
protected: | ||
void SetUpOnMainThread() override { | ||
PlatformBrowserTest::SetUpOnMainThread(); | ||
|
||
const base::FilePath test_data_file_path = | ||
base::PathService::CheckedGet(brave::DIR_TEST_DATA); | ||
|
||
const base::FilePath component_file_path = | ||
test_data_file_path.AppendASCII("ntp_background_images") | ||
.AppendASCII("components") | ||
.AppendASCII("rich_media_with_csp_violation"); | ||
|
||
NTPBackgroundImagesService* const ntp_background_images_service = | ||
g_brave_browser_process->ntp_background_images_service(); | ||
ASSERT_TRUE(ntp_background_images_service); | ||
|
||
NTPBackgroundImagesServiceWaiter waiter(*ntp_background_images_service); | ||
ntp_background_images_service->OnSponsoredComponentReady( | ||
/*is_super_referral=*/false, component_file_path); | ||
waiter.WaitForOnSponsoredImagesUpdated(); | ||
} | ||
|
||
content::WebContents* GetActiveWebContents() { | ||
return chrome_test_utils::GetActiveWebContents(this); | ||
} | ||
|
||
void NavigateToUrlAndVerifyExpectation( | ||
const std::string& console_observer_pattern) { | ||
content::WebContentsConsoleObserver console_observer( | ||
GetActiveWebContents()); | ||
console_observer.SetPattern(console_observer_pattern); | ||
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GURL(kRichMediaUrl))); | ||
EXPECT_TRUE(console_observer.Wait()); | ||
} | ||
}; | ||
|
||
IN_PROC_BROWSER_TEST_F(NTPSponsoredRichMediaWithCSPViolationBrowserTest, | ||
DoNotLoadLocalResource) { | ||
NavigateToUrlAndVerifyExpectation( | ||
"Not allowed to load local resource: chrome://csp-violation/script.js"); | ||
} | ||
|
||
IN_PROC_BROWSER_TEST_F(NTPSponsoredRichMediaWithCSPViolationBrowserTest, | ||
DoNotLoadLocalResourceWithDifferentOrigin) { | ||
NavigateToUrlAndVerifyExpectation( | ||
R"(Refused to load the stylesheet )" | ||
R"('chrome-untrusted://csp-violation/styles.css' because it violates the )" | ||
R"(following Content Security Policy directive: "style-src 'self'".*)"); | ||
} | ||
|
||
IN_PROC_BROWSER_TEST_F(NTPSponsoredRichMediaWithCSPViolationBrowserTest, | ||
DoNotLoadRemoteResource) { | ||
NavigateToUrlAndVerifyExpectation( | ||
R"(Refused to load the image 'https://csp-violation.com/background.jpg' )" | ||
R"(because it violates the following Content Security Policy directive: )" | ||
R"("img-src 'self'".*)"); | ||
} | ||
|
||
} // namespace ntp_background_images |
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
Oops, something went wrong.