Skip to content

Commit

Permalink
Replaced videoDetected variable with hasListener
Browse files Browse the repository at this point in the history
  • Loading branch information
PyvesB committed Nov 27, 2017
1 parent d401d16 commit eef787f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 32 deletions.
11 changes: 4 additions & 7 deletions content/content.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
var videoDetected = false;
var videoChecker;

function checkForVideos() {
const videos = document.getElementsByTagName("video");
if (videos.length > 0) {
chrome.storage.local.get("state", function(value) {
const enabled = (value["state"] !== "disabled");
if (!videoDetected) {
// A video was detected, previously none were.
videoDetected = true;
// Check whether video previously detected (i.e. already listening to
// storage changes).
if (!chrome.storage.onChanged.hasListener(handleStorageChanges)) {
// Listen to changes to the storage made by the popup.
chrome.storage.onChanged.addListener(handleStorageChanges);
if (enabled) {
Expand All @@ -20,9 +19,7 @@ function checkForVideos() {
}
updateUnprocessedVideos(videos, enabled);
});
} else if (videoDetected) {
// A video was previously detected, but there aren't any now.
videoDetected = false;
} else if (chrome.storage.onChanged.hasListener(handleStorageChanges)) {
// No more video: do not react to subsequent storage changes.
chrome.storage.onChanged.removeListener(handleStorageChanges);
// Notify background so that tab icon is changed to pause.
Expand Down
42 changes: 17 additions & 25 deletions tests/spec/ContentSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,68 +32,63 @@ describe("Content script", function() {
});

it("should do nothing if no videos are detected and none previously were", function() {
videoDetected = false;

checkForVideos();

expect(chrome.storage.local.get.notCalled).toBeTruthy();
expect(videoDetected).toBeFalsy();
expect(chrome.storage.onChanged.addListener.notCalled).toBeTruthy();
expect(chrome.storage.onChanged.removeListener.notCalled).toBeTruthy();
expect(chrome.runtime.sendMessage.notCalled).toBeTruthy();
});

it("should remove listener and notify background if all previsouly detected videos are gone", function() {
videoDetected = true;
chrome.storage.onChanged.addListener(handleStorageChanges);

checkForVideos();

expect(chrome.storage.local.get.notCalled).toBeTruthy();
expect(chrome.storage.onChanged.removeListener.calledOnce).toBeTruthy();
expect(videoDetected).toBeFalsy();
expect(chrome.runtime.sendMessage.withArgs({
active : false
}).calledOnce).toBeTruthy();
});

it("should update unprocessed videos if some videos were previsouly detected", function() {
it("should add listener and update unprocessed videos if videos detected for the first time, but not notify background if extension disabled", function() {
document.body.appendChild(video);
videoDetected = true;
chrome.storage.local.get.withArgs("state").yields({
state : "enabled"
state : "disabled"
});
spyOn(window, "updateUnprocessedVideos");

checkForVideos();

expect(chrome.storage.local.get.calledOnce).toBeTruthy();
expect(videoDetected).toBeTruthy();
expect(chrome.storage.onChanged.addListener.notCalled).toBeTruthy();
expect(chrome.storage.onChanged.addListener.calledOnce).toBeTruthy();
expect(chrome.runtime.sendMessage.notCalled).toBeTruthy();
expect(window.updateUnprocessedVideos.calls.mostRecent().args[0][0]).toEqual(video);
expect(window.updateUnprocessedVideos.calls.mostRecent().args[1]).toBeTruthy();
expect(window.updateUnprocessedVideos.calls.mostRecent().args[1]).toBeFalsy();
});

it("should add listener and update unprocessed videos if videos detected for the first time, but not notify background if extension disabled", function() {
it("should add listener, update unprocessed videos and notify background if videos detected for the first time and extension enabled", function() {
document.body.appendChild(video);
videoDetected = false;
chrome.storage.local.get.withArgs("state").yields({
state : "disabled"
state : "enabled"
});
spyOn(window, "updateUnprocessedVideos");

checkForVideos();

expect(chrome.storage.local.get.calledOnce).toBeTruthy();
expect(videoDetected).toBeTruthy();
expect(chrome.storage.onChanged.addListener.calledOnce).toBeTruthy();
expect(chrome.runtime.sendMessage.notCalled).toBeTruthy();
expect(chrome.runtime.sendMessage.withArgs({
active : true
}).calledOnce).toBeTruthy();
expect(window.updateUnprocessedVideos.calls.mostRecent().args[0][0]).toEqual(video);
expect(window.updateUnprocessedVideos.calls.mostRecent().args[1]).toBeFalsy();
expect(window.updateUnprocessedVideos.calls.mostRecent().args[1]).toBeTruthy();
});

it("should add listener, update unprocessed videos and notify background if videos detected for the first time and extension enabled", function() {
it("should update unprocessed videos but not notify background if some videos were previsouly detected", function() {
document.body.appendChild(video);
videoDetected = false;
chrome.storage.onChanged.addListener(handleStorageChanges);
chrome.storage.local.get.withArgs("state").yields({
state : "enabled"
});
Expand All @@ -102,14 +97,11 @@ describe("Content script", function() {
checkForVideos();

expect(chrome.storage.local.get.calledOnce).toBeTruthy();
expect(videoDetected).toBeTruthy();
expect(chrome.storage.onChanged.addListener.calledOnce).toBeTruthy();
expect(chrome.runtime.sendMessage.withArgs({
active : true
}).calledOnce).toBeTruthy();
expect(chrome.runtime.sendMessage.notCalled).toBeTruthy();
expect(window.updateUnprocessedVideos.calls.mostRecent().args[0][0]).toEqual(video);
expect(window.updateUnprocessedVideos.calls.mostRecent().args[1]).toBeTruthy();
});

});

describe("Update unprocessed videos", function() {
Expand Down

0 comments on commit eef787f

Please sign in to comment.