diff --git a/content/treestyletab/treestyletab.js b/content/treestyletab/treestyletab.js index f2993ed0..1b249953 100644 --- a/content/treestyletab/treestyletab.js +++ b/content/treestyletab/treestyletab.js @@ -1965,7 +1965,7 @@ catch(e) { if (startCount < allLinksCount) { var firstNode = this.evaluateXPath( 'descendant-or-self::node()[not(child::node()) and not(preceding-sibling::node())]', - linksResult.snapshotItem(startCount), + linksResult.snapshotItem(allLinksCount-1), XPathResult.FIRST_ORDERED_NODE_TYPE ).singleNodeValue; contentRange.selectNodeContents(firstNode); diff --git a/tests/fixtures/frameTest.html b/tests/fixtures/frameTest.html index 8ae74298..10941b4e 100644 --- a/tests/fixtures/frameTest.html +++ b/tests/fixtures/frameTest.html @@ -4,8 +4,9 @@ テスト - + + diff --git a/tests/fixtures/frameTestInline.html b/tests/fixtures/frameTestInline.html index ca6fc942..f4b8a807 100644 --- a/tests/fixtures/frameTestInline.html +++ b/tests/fixtures/frameTestInline.html @@ -6,6 +6,7 @@

テスト

- + diff --git a/tests/fixtures/links.html b/tests/fixtures/links.html new file mode 100644 index 00000000..c32d1fd8 --- /dev/null +++ b/tests/fixtures/links.html @@ -0,0 +1,26 @@ + + + + + テスト + + + + + diff --git a/tests/unit/treestyletab_linkUtils.test.js b/tests/unit/treestyletab_linkUtils.test.js new file mode 100644 index 00000000..78dff776 --- /dev/null +++ b/tests/unit/treestyletab_linkUtils.test.js @@ -0,0 +1,202 @@ +var win; +var sv; + +function setUp() +{ + utils.setPref('browser.tabs.warnOnClose', false); + + yield utils.setUpTestWindow(); + win = utils.getTestWindow(); + sv = win.TreeStyleTabService; + + yield Do(utils.addTab('../fixtures/links.html')); + var tabs = gBrowser.mTabs; + gBrowser.removeAllTabsBut(tabs[tabs.length-1]); + assert.equals(1, tabs.length); +} + +function tearDown() +{ + win = null; + utils.tearDownTestWindow(); +} + +function $(aId) +{ + return content.document.getElementById(aId); +} + +function test_getCurrentFrame() +{ + yield Do(utils.addTab('../fixtures/frameTest.html')); + yield Do(utils.addTab('../fixtures/frameTestInline.html')); + var tabs = gBrowser.mTabs; + assert.equals(3, tabs.length); + + var frame; + + frame = gBrowser.contentWindow; + assert.equals(frame, sv.getCurrentFrame()); + assert.equals(frame, sv.getCurrentFrame(frame)); + + frame = tabs[1].linkedBrowser.contentWindow.frames[1]; + assert.equals(frame, sv.getCurrentFrame(frame)); + + frame = tabs[2].linkedBrowser.contentWindow.frames[0]; + assert.equals(frame, sv.getCurrentFrame(frame)); +} + +function test_getLinksInRange() +{ + var range = content.document.createRange(); + + range.setStartBefore($('item2')); + range.setEndAfter($('item4')); + assert.equals( + [$('link2'), $('link3'), $('link4')], + sv.getLinksInRange(range) + ); + + range.setStartBefore($('link2')); + range.setEndAfter($('link4')); + assert.equals( + [$('link2'), $('link3'), $('link4')], + sv.getLinksInRange(range) + ); + + range.setStartBefore($('em2')); + range.setEndAfter($('em4')); + assert.equals( + [$('link2'), $('link3'), $('link4')], + sv.getLinksInRange(range) + ); + + range.setStart($('em2').firstChild, 2); + range.setEnd($('em4').firstChild, 2); + assert.equals( + [$('link2'), $('link3'), $('link4')], + sv.getLinksInRange(range) + ); + + range.setStartAfter($('em1')); + range.setEndBefore($('em5')); + assert.equals( + [$('link2'), $('link3'), $('link4')], + sv.getLinksInRange(range) + ); + + range.setStartAfter($('em1').firstChild); + range.setEndBefore($('em5').firstChild); + assert.equals( + [$('link2'), $('link3'), $('link4')], + sv.getLinksInRange(range) + ); +} + +function test_getSelectionLinks() +{ + var selection = content.getSelection(); + assert.equals(0, selection.rangeCount); + + var range1 = content.document.createRange(); + range1.setStartBefore($('link2')); + range1.setEndAfter($('link4')); + selection.addRange(range1); + + assert.equals(1, selection.rangeCount); + assert.equals( + [$('link2'), $('link3'), $('link4')], + sv.getSelectionLinks(content) + ); + + var range2 = content.document.createRange(); + range2.setStartBefore($('link6')); + range2.setEndAfter($('link8')); + selection.addRange(range2); + + var range3 = content.document.createRange(); + range3.setStartBefore($('link12')); + range3.setEndAfter($('link14')); + selection.addRange(range3); + + assert.equals(3, selection.rangeCount); + assert.equals( + [$('link2'), $('link3'), $('link4'), + $('link6'), $('link7'), $('link8'), + $('link12'), $('link13'), $('link14')], + sv.getSelectionLinks(content) + ); + + selection.removeAllRanges(); + range1.detach(); + range2.detach(); + range3.detach(); +} + +function test_openSelectionLinks() +{ + function assert_openSelectionLinksInFrame(aFrame) + { + function $(aId) + { + return aFrame.document.getElementById(aId); + } + + gBrowser.removeAllTabsBut(gBrowser.selectedTab); + assert.equals(1, gBrowser.mTabs.length); + + var selection = aFrame.getSelection(); + assert.equals(0, selection.rangeCount); + + var range1 = aFrame.document.createRange(); + range1.setStartBefore($('link2')); + range1.setEndAfter($('link4')); + selection.addRange(range1); + assert.equals(1, selection.rangeCount); + + var tabs = gBrowser.mTabs; + assert.equals(1, tabs.length); + + sv.openSelectionLinks(aFrame); + assert.equals(4, tabs.length); + assert.equals( + [tabs[1], tabs[2], tabs[3]], + sv.getChildTabs(tabs[0]) + ); + + gBrowser.removeAllTabsBut(tabs[0]); + assert.equals(1, tabs.length); + + var range2 = aFrame.document.createRange(); + range2.setStartBefore($('link6')); + range2.setEndAfter($('link8')); + selection.addRange(range2); + + var range3 = aFrame.document.createRange(); + range3.setStartBefore($('link12')); + range3.setEndAfter($('link14')); + selection.addRange(range3); + + sv.openSelectionLinks(aFrame); + assert.equals(10, tabs.length); + assert.equals( + [tabs[1], tabs[2], tabs[3], + tabs[4], tabs[5], tabs[6], + tabs[7], tabs[8], tabs[9]], + sv.getChildTabs(tabs[0]) + ); + + selection.removeAllRanges(); + range1.detach(); + range2.detach(); + range3.detach(); + } + + assert_openSelectionLinksInFrame(content); + + yield Do(utils.addTab('../fixtures/frameTest.html', { selected : true })); + assert_openSelectionLinksInFrame(content.frames[2]); + + yield Do(utils.addTab('../fixtures/frameTestInline.html', { selected : true })); + assert_openSelectionLinksInFrame(content.frames[1]); +} diff --git a/tests/unit/treestyletab_treeUtils.test.js b/tests/unit/treestyletab_treeUtils.test.js new file mode 100644 index 00000000..95135a4e --- /dev/null +++ b/tests/unit/treestyletab_treeUtils.test.js @@ -0,0 +1,21 @@ +/* + +sv.rootTabs +sv.getParentTab(tab) +sv.getRootTab(tab) +sv.getNextSiblingTab(tab) +sv.getPreviousSiblingTab(tab) +sv.getChildTabs(tab, array) +sv.hasChildTabs(tab) +sv.getDescendantTabs(tab) +sv.getFirstChildTab(tab) +sv.getLastChildTab(tab) +sv.getChildIndex(tab, parent) + +sv.removeTabSubTree(tabs, onlychildren) +sv.collapseExpandAllSubtree(collapse) + +sv.registerCollapseExpandPostProcess(func) + + +*/