Bug 865478 - Add getItemsByUrl and removeItem methods to richgrid + tests. r=mbrubeck

This commit is contained in:
Sam Foster 2013-04-26 10:29:59 +01:00
parent 3681f9d8ae
commit 95e612bedb
3 changed files with 96 additions and 6 deletions

View File

@ -225,15 +225,29 @@
<parameter name="aSkipArrange"/>
<body>
<![CDATA[
let removal = this.getItemAtIndex(anIndex);
this.removeChild(removal);
if (!aSkipArrange)
this.arrangeItems();
let item = this.getItemAtIndex(anIndex);
return item ? this.removeItem(item, aSkipArrange) : null;
]]>
</body>
</method>
<method name="removeItem">
<parameter name="aItem"/>
<parameter name="aSkipArrange"/>
<body>
<![CDATA[
let removal = aItem.parentNode == this && this.removeChild(aItem);
if (removal && !aSkipArrange)
this.arrangeItems();
// note that after removal the node is unbound
// so none of the richgriditem binding methods & properties are available
return removal;
]]>
</body>
</method>
<method name="getIndexOfItem">
<parameter name="anItem"/>
<body>
@ -241,7 +255,7 @@
if (!anItem)
return -1;
return Array.prototype.indexOf.call(this.children, anItem);
return Array.indexOf(this.children, anItem);
]]>
</body>
</method>
@ -257,6 +271,15 @@
</body>
</method>
<method name="getItemsByUrl">
<parameter name="aUrl"/>
<body>
<![CDATA[
return this.querySelectorAll('richgriditem[value="'+aUrl+'"]');
]]>
</body>
</method>
<!-- Interface for offsetting selection and checking bounds -->
<property name="isSelectionAtStart" readonly="true"

View File

@ -39,6 +39,19 @@
<richgriditem value="about:blank" id="grid4_item2" label="2nd item"/>
</richgrid>
</hbox>
<hbox>
<richgrid id="grid5" seltype="single" flex="1">
<richgriditem value="about:blank" id="grid5_item1" label="First item"/>
<richgriditem value="http://bugzilla.mozilla.org/" id="grid5_item2" label="2nd item"/>
<richgriditem value="about:blank" id="grid5_item3" label="3rd item"/>
<richgriditem value="http://bugzilla.mozilla.org/" id="grid5_item4" label="4th item"/>
</richgrid>
</hbox>
<hbox>
<richgrid id="grid6" seltype="single" flex="1">
<richgriditem value="about:blank" id="grid6_item1" label="First item"/>
</richgrid>
</hbox>
<hbox>
<richgrid id="grid-select1" seltype="single" flex="1">
<richgriditem value="about:blank" id="grid-select1_item1" label="First item"/>

View File

@ -1,4 +1,4 @@
let doc, win;
let doc;
function test() {
waitForExplicitFinish();
@ -170,6 +170,60 @@ gTests.push({
}
});
gTests.push({
desc: "getItemsByUrl",
run: function() {
let grid = doc.querySelector("#grid5");
is(grid.itemCount, 4, "4 items total");
is(typeof grid.getItemsByUrl, "function", "getItemsByUrl is a function on the grid");
['about:blank', 'http://bugzilla.mozilla.org/'].forEach(function(testUrl) {
let items = grid.getItemsByUrl(testUrl);
is(items.length, 2, "2 matching items in the test grid");
is(items.item(0).url, testUrl, "Matched item has correct url property");
is(items.item(1).url, testUrl, "Matched item has correct url property");
});
let badUrl = 'http://gopher.well.com:70/';
let items = grid.getItemsByUrl(badUrl);
is(items.length, 0, "0 items matched url: "+badUrl);
}
});
gTests.push({
desc: "removeItem",
run: function() {
let grid = doc.querySelector("#grid5");
is(grid.itemCount, 4, "4 items total");
is(typeof grid.removeItem, "function", "removeItem is a function on the grid");
let arrangeStub = stubMethod(grid, "arrangeItems");
let removedFirst = grid.removeItem( grid.children[0] );
is(arrangeStub.callCount, 1, "arrangeItems is called when we removeItem");
let removed2nd = grid.removeItem( grid.children[0], true);
is(removed2nd.getAttribute("label"), "2nd item", "the next item was returned");
is(grid.itemCount, 2, "2 items remain");
// callCount should still be at 1
is(arrangeStub.callCount, 1, "arrangeItems is not called when we pass the truthy skipArrange param");
let otherItem = grid.ownerDocument.querySelector("#grid6_item1");
let removedFail = grid.removeItem(otherItem);
ok(!removedFail, "Falsy value returned when non-child item passed");
is(grid.itemCount, 2, "2 items remain");
// callCount should still be at 1
is(arrangeStub.callCount, 1, "arrangeItems is not called when nothing is matched");
arrangeStub.restore();
}
});
gTests.push({
desc: "selections (single)",
run: function() {