Commit dc24a5d7 authored by deepak.m1's avatar deepak.m1 Committed by Commit bot

Testcases for nameddests for PDF.

Test cases added for nameddests and navigate.
common_test.js file have been added that have the common function for viewport_test and nameddestinations_test file.

This is followup for 447888 issue.

BUG=450105

Review URL: https://codereview.chromium.org/838723003

Cr-Commit-Position: refs/heads/master@{#314119}
parent cbcd16e9
...@@ -9,11 +9,21 @@ ...@@ -9,11 +9,21 @@
* @param {string} originalUrl The original page URL. * @param {string} originalUrl The original page URL.
* @param {Object} viewport The viewport info of the page. * @param {Object} viewport The viewport info of the page.
* @param {Object} paramsParser The object for URL parsing. * @param {Object} paramsParser The object for URL parsing.
* @param {Function} navigateInCurrentTabCallback The Callback function that
* gets called when navigation happens in the current tab.
* @param {Function} navigateInNewTabCallback The Callback function that gets
* called when navigation happens in the new tab.
*/ */
function Navigator(originalUrl, viewport, paramsParser) { function Navigator(originalUrl,
viewport,
paramsParser,
navigateInCurrentTabCallback,
navigateInNewTabCallback) {
this.originalUrl_ = originalUrl; this.originalUrl_ = originalUrl;
this.viewport_ = viewport; this.viewport_ = viewport;
this.paramsParser_ = paramsParser; this.paramsParser_ = paramsParser;
this.navigateInCurrentTabCallback_ = navigateInCurrentTabCallback;
this.navigateInNewTabCallback_ = navigateInNewTabCallback;
} }
Navigator.prototype = { Navigator.prototype = {
...@@ -62,19 +72,14 @@ Navigator.prototype = { ...@@ -62,19 +72,14 @@ Navigator.prototype = {
} }
if (newTab) { if (newTab) {
// Prefer the tabs API because it guarantees we can just open a new tab. this.navigateInNewTabCallback_(url);
// window.open doesn't have this guarantee.
if (chrome.tabs)
chrome.tabs.create({ url: url });
else
window.open(url);
} else { } else {
var pageNumber = var pageNumber =
this.paramsParser_.getViewportFromUrlParams(url).page; this.paramsParser_.getViewportFromUrlParams(url).page;
if (pageNumber != undefined) if (pageNumber != undefined)
this.viewport_.goToPage(pageNumber); this.viewport_.goToPage(pageNumber);
else else
window.location.href = url; this.navigateInCurrentTabCallback_(url);
} }
} }
}; };
...@@ -30,6 +30,27 @@ function getFilenameFromURL(url) { ...@@ -30,6 +30,27 @@ function getFilenameFromURL(url) {
return components[components.length - 1]; return components[components.length - 1];
} }
/**
* Called when navigation happens in the current tab.
* @param {string} url The url to be opened in the current tab.
*/
function onNavigateInCurrentTab(url) {
window.location.href = url;
}
/**
* Called when navigation happens in the new tab.
* @param {string} url The url to be opened in the new tab.
*/
function onNavigateInNewTab(url) {
// Prefer the tabs API because it guarantees we can just open a new tab.
// window.open doesn't have this guarantee.
if (chrome.tabs)
chrome.tabs.create({ url: url});
else
window.open(url);
}
/** /**
* The minimum number of pixels to offset the toolbar by from the bottom and * The minimum number of pixels to offset the toolbar by from the bottom and
* right side of the screen. * right side of the screen.
...@@ -171,7 +192,8 @@ function PDFViewer(streamDetails) { ...@@ -171,7 +192,8 @@ function PDFViewer(streamDetails) {
// Parse open pdf parameters. // Parse open pdf parameters.
this.paramsParser_ = new OpenPDFParamsParser(); this.paramsParser_ = new OpenPDFParamsParser();
this.navigator_ = new Navigator(this.streamDetails_.originalUrl, this.navigator_ = new Navigator(this.streamDetails_.originalUrl,
this.viewport_, this.paramsParser_); this.viewport_, this.paramsParser_, onNavigateInCurrentTab,
onNavigateInNewTab);
} }
PDFViewer.prototype = { PDFViewer.prototype = {
......
...@@ -73,11 +73,16 @@ class PDFExtensionTest : public ExtensionApiTest { ...@@ -73,11 +73,16 @@ class PDFExtensionTest : public ExtensionApiTest {
PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir); PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir);
test_data_dir = test_data_dir.Append( test_data_dir = test_data_dir.Append(
FILE_PATH_LITERAL("chrome/test/data/pdf")); FILE_PATH_LITERAL("chrome/test/data/pdf"));
test_data_dir = test_data_dir.AppendASCII(filename); base::FilePath test_util_path = test_data_dir.AppendASCII("test_util.js");
std::string test_util_js;
ASSERT_TRUE(base::ReadFileToString(test_util_path, &test_util_js));
base::FilePath test_file_path = test_data_dir.AppendASCII(filename);
std::string test_js; std::string test_js;
ASSERT_TRUE(base::ReadFileToString(test_data_dir, &test_js)); ASSERT_TRUE(base::ReadFileToString(test_file_path, &test_js));
ASSERT_TRUE(content::ExecuteScript(contents, test_js));
test_util_js.append(test_js);
ASSERT_TRUE(content::ExecuteScript(contents, test_util_js));
if (!catcher.GetNextResult()) if (!catcher.GetNextResult())
FAIL() << catcher.message(); FAIL() << catcher.message();
...@@ -99,3 +104,11 @@ IN_PROC_BROWSER_TEST_F(PDFExtensionTest, Viewport) { ...@@ -99,3 +104,11 @@ IN_PROC_BROWSER_TEST_F(PDFExtensionTest, Viewport) {
IN_PROC_BROWSER_TEST_F(PDFExtensionTest, Bookmark) { IN_PROC_BROWSER_TEST_F(PDFExtensionTest, Bookmark) {
RunTestsInFile("bookmarks_test.js", "test-bookmarks.pdf"); RunTestsInFile("bookmarks_test.js", "test-bookmarks.pdf");
} }
IN_PROC_BROWSER_TEST_F(PDFExtensionTest, Navigator) {
RunTestsInFile("navigator_test.js", "test.pdf");
}
IN_PROC_BROWSER_TEST_F(PDFExtensionTest, ParamsParser) {
RunTestsInFile("params_parser_test.js", "test.pdf");
}
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function NavigateInCurrentTabCallback() {
this.navigateInCurrentTabCalled = false;
this.callback = function() {
this.navigateInCurrentTabCalled = true;
}.bind(this);
this.reset = function() {
this.navigateInCurrentTabCalled = false;
};
}
function NavigateInNewTabCallback() {
this.navigateInNewTabCalled = false;
this.callback = function() {
this.navigateInNewTabCalled = true;
}.bind(this);
this.reset = function() {
this.navigateInNewTabCalled = false;
};
}
var tests = [
/**
* Test navigation within the page, opening a url in the same tab and
* opening a url in the new tab.
*/
function testNavigate() {
var mockWindow = new MockWindow(100, 100);
var mockSizer = new MockSizer();
var mockCallback = new MockViewportChangedCallback();
var viewport = new Viewport(mockWindow, mockSizer, mockCallback.callback,
function() {}, function() {}, 0, 0);
var paramsParser = new OpenPDFParamsParser();
paramsParser.namedDestinations['US'] = 0;
paramsParser.namedDestinations['UY'] = 2;
var url = "http://xyz.pdf";
var navigateInCurrentTabCallback = new NavigateInCurrentTabCallback();
var navigateInNewTabCallback = new NavigateInNewTabCallback();
var navigator = new Navigator(url, viewport, paramsParser,
navigateInCurrentTabCallback.callback,
navigateInNewTabCallback.callback);
var documentDimensions = new MockDocumentDimensions();
documentDimensions.addPage(100, 100);
documentDimensions.addPage(200, 200);
documentDimensions.addPage(100, 400);
viewport.setDocumentDimensions(documentDimensions);
viewport.setZoom(1);
mockCallback.reset();
// This should move viewport to page 0.
navigator.navigate(url + "#US", false);
chrome.test.assertTrue(mockCallback.wasCalled);
chrome.test.assertEq(0, viewport.position.x);
chrome.test.assertEq(0, viewport.position.y);
mockCallback.reset();
navigateInNewTabCallback.reset();
// This should open "http://xyz.pdf#US" in a new tab. So current tab
// viewport should not update and viewport position should remain same.
navigator.navigate(url + "#US", true);
chrome.test.assertFalse(mockCallback.wasCalled);
chrome.test.assertTrue(navigateInNewTabCallback.navigateInNewTabCalled);
chrome.test.assertEq(0, viewport.position.x);
chrome.test.assertEq(0, viewport.position.y);
mockCallback.reset();
// This should move viewport to page 2.
navigator.navigate(url + "#UY", false);
chrome.test.assertTrue(mockCallback.wasCalled);
chrome.test.assertEq(0, viewport.position.x);
chrome.test.assertEq(300, viewport.position.y);
mockCallback.reset();
navigateInCurrentTabCallback.reset();
// #ABC is not a named destination in the page so viewport should not
// update and viewport position should remain same. As this link will open
// in the same tab.
navigator.navigate(url + "#ABC", false);
chrome.test.assertFalse(mockCallback.wasCalled);
chrome.test.assertTrue(
navigateInCurrentTabCallback.navigateInCurrentTabCalled);
chrome.test.assertEq(0, viewport.position.x);
chrome.test.assertEq(300, viewport.position.y);
chrome.test.succeed();
}
];
var scriptingAPI = new PDFScriptingAPI(window, window);
scriptingAPI.setLoadCallback(function() {
chrome.test.runTests(tests);
});
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var tests = [
/**
* Test named destinations.
*/
function testParamsParser() {
var paramsParser = new OpenPDFParamsParser();
// Assigning page number for #nameddests.
paramsParser.namedDestinations['RU'] = 26;
paramsParser.namedDestinations['US'] = 0;
paramsParser.namedDestinations['UY'] = 22;
var url = "http://xyz.pdf";
// Checking #nameddest.
var urlParams = paramsParser.getViewportFromUrlParams(url + "#RU");
chrome.test.assertEq(urlParams.page, 26);
// Checking #nameddest=name.
urlParams = paramsParser.getViewportFromUrlParams(url + "#nameddest=US");
chrome.test.assertEq(urlParams.page, 0);
// Checking #page=pagenum nameddest.The document first page has a pagenum
// value of 1.
urlParams = paramsParser.getViewportFromUrlParams(url + "#page=6");
chrome.test.assertEq(urlParams.page, 5);
// Checking #zoom=scale.
urlParams = paramsParser.getViewportFromUrlParams(url + "#zoom=200");
chrome.test.assertEq(urlParams.zoom, 2);
// Checking #zoom=scale,left,top.
urlParams = paramsParser.getViewportFromUrlParams(url +
"#zoom=200,100,200");
chrome.test.assertEq(urlParams.zoom, 2);
chrome.test.assertEq(urlParams.position.x, 100);
chrome.test.assertEq(urlParams.position.y, 200);
// Checking #nameddest=name and zoom=scale.
urlParams = paramsParser.getViewportFromUrlParams(url +
"#nameddest=UY&zoom=150");
chrome.test.assertEq(urlParams.page, 22);
chrome.test.assertEq(urlParams.zoom, 1.5);
// Checking #page=pagenum and zoom=scale.
urlParams = paramsParser.getViewportFromUrlParams(url +
"#page=2&zoom=250");
chrome.test.assertEq(urlParams.page, 1);
chrome.test.assertEq(urlParams.zoom, 2.5);
// Checking #nameddest=name and zoom=scale,left,top.
urlParams = paramsParser.getViewportFromUrlParams(url +
"#nameddest=UY&zoom=150,100,200");
chrome.test.assertEq(urlParams.page, 22);
chrome.test.assertEq(urlParams.zoom, 1.5);
chrome.test.assertEq(urlParams.position.x, 100);
chrome.test.assertEq(urlParams.position.y, 200);
// Checking #page=pagenum and zoom=scale,left,top.
urlParams = paramsParser.getViewportFromUrlParams(url +
"#page=2&zoom=250,100,200");
chrome.test.assertEq(urlParams.page, 1);
chrome.test.assertEq(urlParams.zoom, 2.5);
chrome.test.assertEq(urlParams.position.x, 100);
chrome.test.assertEq(urlParams.position.y, 200);
chrome.test.succeed();
}
];
var scriptingAPI = new PDFScriptingAPI(window, window);
scriptingAPI.setLoadCallback(function() {
chrome.test.runTests(tests);
});
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Utilities that are used in multiple tests.
function MockWindow(width, height, sizer) {
this.innerWidth = width;
this.innerHeight = height;
this.addEventListener = function(e, f) {
if (e == 'scroll')
this.scrollCallback = f;
if (e == 'resize')
this.resizeCallback = f;
};
this.scrollTo = function(x, y) {
if (sizer) {
x = Math.min(x, parseInt(sizer.style.width) - width);
y = Math.min(y, parseInt(sizer.style.height) - height);
}
this.pageXOffset = Math.max(0, x);
this.pageYOffset = Math.max(0, y);
this.scrollCallback();
};
if (sizer) {
sizer.resizeCallback_ = function() {
this.scrollTo(this.pageXOffset, this.pageYOffset);
}.bind(this);
}
this.pageXOffset = 0;
this.pageYOffset = 0;
this.scrollCallback = null;
this.resizeCallback = null;
}
function MockSizer() {
var sizer = this;
this.style = {
width_: '0px',
height_: '0px',
get height() { return this.height_; },
set height(height) {
this.height_ = height;
if (sizer.resizeCallback_)
sizer.resizeCallback_();
},
get width() { return this.width_; },
set width(width) {
this.width_ = width;
if (sizer.resizeCallback_)
sizer.resizeCallback_();
},
};
}
function MockViewportChangedCallback() {
this.wasCalled = false;
this.callback = function() {
this.wasCalled = true;
}.bind(this);
this.reset = function() {
this.wasCalled = false;
};
}
function MockDocumentDimensions(width, height) {
this.width = width || 0;
this.height = height ? height : 0;
this.pageDimensions = [];
this.addPage = function(w, h) {
var y = 0;
if (this.pageDimensions.length != 0) {
y = this.pageDimensions[this.pageDimensions.length - 1].y +
this.pageDimensions[this.pageDimensions.length - 1].height;
}
this.width = Math.max(this.width, w);
this.height += h;
this.pageDimensions.push({
x: 0,
y: y,
width: w,
height: h
});
};
this.reset = function() {
this.width = 0;
this.height = 0;
this.pageDimensions = [];
};
}
...@@ -2,91 +2,6 @@ ...@@ -2,91 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
function MockWindow(width, height, sizer) {
this.innerWidth = width;
this.innerHeight = height;
this.addEventListener = function(e, f) {
if (e == 'scroll')
this.scrollCallback = f;
if (e == 'resize')
this.resizeCallback = f;
};
this.scrollTo = function(x, y) {
if (sizer) {
x = Math.min(x, parseInt(sizer.style.width) - width);
y = Math.min(y, parseInt(sizer.style.height) - height);
}
this.pageXOffset = Math.max(0, x);
this.pageYOffset = Math.max(0, y);
this.scrollCallback();
};
if (sizer) {
sizer.resizeCallback_ = function() {
this.scrollTo(this.pageXOffset, this.pageYOffset);
}.bind(this);
}
this.pageXOffset = 0;
this.pageYOffset = 0;
this.scrollCallback = null;
this.resizeCallback = null;
}
function MockSizer() {
var sizer = this;
this.style = {
width_: '0px',
height_: '0px',
get height() { return this.height_; },
set height(height) {
this.height_ = height;
if (sizer.resizeCallback_)
sizer.resizeCallback_();
},
get width() { return this.width_; },
set width(width) {
this.width_ = width;
if (sizer.resizeCallback_)
sizer.resizeCallback_();
},
};
}
function MockViewportChangedCallback() {
this.wasCalled = false;
this.callback = function() {
this.wasCalled = true;
}.bind(this);
this.reset = function() {
this.wasCalled = false;
};
}
function MockDocumentDimensions(width, height) {
this.width = width || 0;
this.height = height ? height : 0;
this.pageDimensions = [];
this.addPage = function(w, h) {
var y = 0;
if (this.pageDimensions.length != 0) {
y = this.pageDimensions[this.pageDimensions.length - 1].y +
this.pageDimensions[this.pageDimensions.length - 1].height;
}
this.width = Math.max(this.width, w);
this.height += h;
this.pageDimensions.push({
x: 0,
y: y,
width: w,
height: h
});
};
this.reset = function() {
this.width = 0;
this.height = 0;
this.pageDimensions = [];
};
}
var tests = [ var tests = [
function testDocumentNeedsScrollbars() { function testDocumentNeedsScrollbars() {
var viewport = var viewport =
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment