michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: let mockTab = function(aName, aIsClosing) { michael@0: this.name = aName; michael@0: this.isClosing = aIsClosing; michael@0: }; michael@0: michael@0: mockTab.prototype = { michael@0: get chromeTab () { michael@0: let getAttribute = () => this.isClosing; michael@0: return { michael@0: hasAttribute: getAttribute michael@0: } michael@0: } michael@0: }; michael@0: michael@0: let getMockBrowserTabs = function(aTestTabs, aSelected) { michael@0: let result = { michael@0: _tabs: aTestTabs, michael@0: _selectedTab: aTestTabs[aSelected || 0], michael@0: getTabAtIndex: function(i) this._tabs[i] michael@0: }; michael@0: michael@0: return [result, Browser.getNextTab.bind(result)]; michael@0: }; michael@0: michael@0: gTests.push({ michael@0: desc: "Test Browser.getNextTab", michael@0: run: function() { michael@0: let [lonlyTab, fakeGetNextTab] = getMockBrowserTabs( michael@0: [new mockTab("a", false)]); michael@0: michael@0: ok(fakeGetNextTab(lonlyTab._selectedTab) == null, "null when only 1 tab left"); michael@0: ok(fakeGetNextTab(new mockTab("x", false)) == null, "null when tab doesnt exist"); michael@0: michael@0: let [twoTabs, fakeGetNextTab] = getMockBrowserTabs( michael@0: [new mockTab("a", false), new mockTab("b", false)]); michael@0: ok(fakeGetNextTab(twoTabs._selectedTab).name == "b", "get next tab"); michael@0: ok(fakeGetNextTab(twoTabs._tabs[1]).name == "a", "get selected tab"); michael@0: michael@0: let [twoTabsSecondSelected, fakeGetNextTab] = getMockBrowserTabs( michael@0: [new mockTab("a", false), new mockTab("b", false)], 1); michael@0: ok(fakeGetNextTab(twoTabsSecondSelected._selectedTab).name == "a", "get previous tab"); michael@0: michael@0: let [twoTabsOneClosing, fakeGetNextTab] = getMockBrowserTabs( michael@0: [new mockTab("a", false), new mockTab("b", true)]); michael@0: ok(fakeGetNextTab(twoTabsOneClosing._selectedTab) == null, "can't select closing tab"); michael@0: ok(fakeGetNextTab(twoTabsOneClosing._tabs[1]).name == "a", "get previous tab"); michael@0: michael@0: let [twoTabsBothClosing, fakeGetNextTab] = getMockBrowserTabs( michael@0: [new mockTab("a", true), new mockTab("b", true)]); michael@0: ok(fakeGetNextTab(twoTabsBothClosing._selectedTab) == null, "can't select closing tab"); michael@0: ok(fakeGetNextTab(twoTabsBothClosing._tabs[1]) == null, "can't select closing tab"); michael@0: michael@0: let [fiveTabsLastNotClosing, fakeGetNextTab] = getMockBrowserTabs( michael@0: [new mockTab("a", false), new mockTab("b", true), new mockTab("c", true), new mockTab("d", true), new mockTab("e", false)]); michael@0: ok(fakeGetNextTab(fiveTabsLastNotClosing._selectedTab).name == "e", "get next non-closing tab"); michael@0: michael@0: let [fiveTabsLastNotClosingLastSelected, fakeGetNextTab] = getMockBrowserTabs( michael@0: [new mockTab("a", false), new mockTab("b", true), new mockTab("c", true), new mockTab("d", true), new mockTab("e", false)], 4); michael@0: ok(fakeGetNextTab(fiveTabsLastNotClosingLastSelected._selectedTab).name == "a", "get previous non-closing tab"); michael@0: } michael@0: }); michael@0: michael@0: function test() { michael@0: runTests(); michael@0: }