Skip to content

Commit

Permalink
[ads] Ads tab helper browser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey committed Oct 15, 2024
1 parent b4153a2 commit 72980bf
Show file tree
Hide file tree
Showing 9 changed files with 985 additions and 196 deletions.
33 changes: 16 additions & 17 deletions browser/brave_ads/tabs/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,21 @@ source_set("tabs") {
source_set("browser_tests") {
testonly = true

sources = [ "ads_tab_helper_browsertest.cc" ]

deps = [
":tabs",
"//brave/components/brave_ads/browser:test_support",
"//brave/components/brave_ads/core/public:headers",
"//brave/components/brave_rewards/common",
"//chrome/browser/ui",
"//net:test_support",
]

if (is_android) {
deps += [ "//chrome/test:test_support_ui_android" ]
} else {
deps += [ "//chrome/test:test_support_ui" ]
if (!is_android) {
sources = [ "ads_tab_helper_browsertest.cc" ]

deps = [
":tabs",
"//brave/browser/brave_ads",
"//brave/components/brave_ads/browser:test_support",
"//brave/components/brave_ads/core/public:headers",
"//brave/components/brave_rewards/common",
"//chrome/browser/ui",
"//chrome/test:test_support",
"//chrome/test:test_support_ui",
"//net:test_support",
]

defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ]
}

defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ]
}
978 changes: 830 additions & 148 deletions browser/brave_ads/tabs/ads_tab_helper_browsertest.cc

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ test("brave_browser_tests") {
"//brave/browser/autoplay:browser_tests",
"//brave/browser/brave_ads",
"//brave/browser/brave_ads:browser_tests",
"//brave/browser/brave_ads/tabs:browser_tests",
"//brave/browser/brave_news:browser_tests",
"//brave/browser/brave_rewards",
"//brave/browser/brave_rewards/test:browser_tests",
Expand Down
33 changes: 33 additions & 0 deletions test/data/brave_ads/autoplay_video.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">

<head>
<title>Brave stops ads from following you</title>

<style>
video {
width: 90%;
height: auto;
}
</style>
</head>

<body>
<h1>Yeah, Brave blocks that</h1>

<script>
document.addEventListener('DOMContentLoaded', function () {
const video = document.createElement('video');
video.src = '/brave_ads/video.mp4';
video.autoplay = true;
video.controls = true;
video.onerror = function (event) {
console.error('An error occurred while trying to load the video: ', event);
};

document.body.appendChild(video);
});
</script>
</body>

</html>
9 changes: 0 additions & 9 deletions test/data/brave_ads/basic_page.html

This file was deleted.

42 changes: 42 additions & 0 deletions test/data/brave_ads/multi_page_application.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">

<head>
<title>Adventure Awaits</title>
</head>

<body>
<h1>Welcome to Your Adventure</h1>
<p>
Embark on a journey of learning and discovery. Each step you take brings you closer to mastering new skills and
achieving your goals.
</p>
<ul>
<li><a href="rust.html" target="_self">Explore new programming languages</a></li>
<li><a href="open_source.html" target="_self">Contribute to open-source projects</a></li>
<li><a href="develop.html" target="_self">Develop innovative applications</a></li>
</ul>
<blockquote>
"The only limit to our realization of tomorrow is our doubts of today." - Franklin D. Roosevelt
</blockquote>
<table border="1">
<tr>
<th>Task</th>
<th>Status</th>
</tr>
<tr>
<td>Learn Rust</td>
<td>Completed</td>
</tr>
<tr>
<td>Contribute to a GitHub repository</td>
<td>In Progress</td>
</tr>
<tr>
<td>Build a mobile app</td>
<td>Pending</td>
</tr>
</table>
</body>

</html>
52 changes: 30 additions & 22 deletions test/data/brave_ads/single_page_application.html
Original file line number Diff line number Diff line change
@@ -1,48 +1,56 @@
<html>
<!DOCTYPE html>
<html lang="en">

<head>
<title>Single Page Application</title>
<script>
// Function to update the page header.
function displayContent(state) {
const pageHeader = document.querySelector("#pageHeader");
pageHeader.textContent = state.header;
}

// Event listener for clicks on the document.
document.addEventListener("click", async (event) => {
const creature = event.target.getAttribute("data-creature");
if (creature) {
event.preventDefault();
try {
displayContent(creature);
// Add a new entry to the history.
// This simulates loading a new page.
const newState = { header: creature };
history.pushState(json, "", newState);
} catch (err) {
console.error(err);
const navigationType = event.target.getAttribute("data-navigation-type");
if (navigationType) {
event.preventDefault(); // Stop the default link behavior.
if (navigationType === "same_document") {
try {
// Update the header.
displayContent({ header: navigationType });

// Change the URL without reloading.
const newState = { header: navigationType };
history.pushState(newState, "", navigationType);
} catch (err) {
// Log any errors.
console.error(err);
}
}
}
});

// Handle forward/back buttons
// Event listener for browser navigation (back/forward).
window.addEventListener("popstate", (event) => {
// If a state has been provided, we have a "simulated" page
// and we update the current page.
if (event.state) {
// Simulate the loading of the previous page
// Update the header based on the state.
displayContent(event.state);
}
});

// Create state on page load and replace the current history with it
// Set the initial state of the page.
const initialState = { header: "Home" };
history.replaceState(initialState, "", document.location.href);
</script>
</head>
<body>
<h1 id="pageHeader">Header</h1>

<li><a href="/brave_ads/history_api_spa.html">Home</a></li>
<li><a href="eagle" data-creature="eagle">Eagle</a></li>
<li><a href="vulture" data-creature="vulture">Vulture</a></li>
<body>
<h1 id="pageHeader">Home</h1>
<ul>
<li><a href="/" data-navigation-type="home">Home</a></li>
<li><a href="same_document" data-navigation-type="same_document">Same Document</a></li>
</ul>
</body>

</html>
33 changes: 33 additions & 0 deletions test/data/brave_ads/video.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">

<head>
<title>Brave stops ads from following you</title>

<style>
video {
width: 90%;
height: auto;
}
</style>
</head>

<body>
<h1>Yeah, Brave blocks that</h1>

<script>
document.addEventListener('DOMContentLoaded', function () {
const video = document.createElement('video');
video.src = '/brave_ads/video.mp4';
video.autoplay = false;
video.controls = true;
video.onerror = function (event) {
console.error('An error occurred while trying to load the video: ', event);
};

document.body.appendChild(video);
});
</script>
</body>

</html>
Binary file added test/data/brave_ads/video.mp4
Binary file not shown.

0 comments on commit 72980bf

Please sign in to comment.