browser/metro/base/tests/mochitest/browser_tabs.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/metro/base/tests/mochitest/browser_tabs.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,72 @@
     1.4 +// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +"use strict";
    1.10 +
    1.11 +let mockTab = function(aName, aIsClosing) {
    1.12 +  this.name = aName;
    1.13 +  this.isClosing = aIsClosing;
    1.14 +};
    1.15 +
    1.16 +mockTab.prototype = {
    1.17 +  get chromeTab () {
    1.18 +    let getAttribute = () => this.isClosing;
    1.19 +    return {
    1.20 +      hasAttribute: getAttribute
    1.21 +    }
    1.22 +  }
    1.23 +};
    1.24 +
    1.25 +let getMockBrowserTabs = function(aTestTabs, aSelected) {
    1.26 +  let result = {
    1.27 +    _tabs: aTestTabs,
    1.28 +    _selectedTab: aTestTabs[aSelected || 0],
    1.29 +    getTabAtIndex: function(i) this._tabs[i]
    1.30 +  };
    1.31 +
    1.32 +  return [result, Browser.getNextTab.bind(result)];
    1.33 +};
    1.34 +
    1.35 +gTests.push({
    1.36 +  desc: "Test Browser.getNextTab",
    1.37 +  run: function() {
    1.38 +    let [lonlyTab, fakeGetNextTab] = getMockBrowserTabs(
    1.39 +      [new mockTab("a", false)]);
    1.40 +
    1.41 +    ok(fakeGetNextTab(lonlyTab._selectedTab) == null, "null when only 1 tab left");
    1.42 +    ok(fakeGetNextTab(new mockTab("x", false)) == null, "null when tab doesnt exist");
    1.43 +
    1.44 +    let [twoTabs, fakeGetNextTab] = getMockBrowserTabs(
    1.45 +      [new mockTab("a", false), new mockTab("b", false)]);
    1.46 +    ok(fakeGetNextTab(twoTabs._selectedTab).name == "b", "get next tab");
    1.47 +    ok(fakeGetNextTab(twoTabs._tabs[1]).name == "a", "get selected tab");
    1.48 +
    1.49 +    let [twoTabsSecondSelected, fakeGetNextTab] = getMockBrowserTabs(
    1.50 +      [new mockTab("a", false), new mockTab("b", false)], 1);
    1.51 +    ok(fakeGetNextTab(twoTabsSecondSelected._selectedTab).name == "a", "get previous tab");
    1.52 +
    1.53 +    let [twoTabsOneClosing, fakeGetNextTab] = getMockBrowserTabs(
    1.54 +      [new mockTab("a", false), new mockTab("b", true)]);
    1.55 +    ok(fakeGetNextTab(twoTabsOneClosing._selectedTab) == null, "can't select closing tab");
    1.56 +    ok(fakeGetNextTab(twoTabsOneClosing._tabs[1]).name == "a", "get previous tab");
    1.57 +
    1.58 +    let [twoTabsBothClosing, fakeGetNextTab] = getMockBrowserTabs(
    1.59 +      [new mockTab("a", true), new mockTab("b", true)]);
    1.60 +    ok(fakeGetNextTab(twoTabsBothClosing._selectedTab) == null, "can't select closing tab");
    1.61 +    ok(fakeGetNextTab(twoTabsBothClosing._tabs[1]) == null, "can't select closing tab");
    1.62 +
    1.63 +    let [fiveTabsLastNotClosing, fakeGetNextTab] = getMockBrowserTabs(
    1.64 +      [new mockTab("a", false), new mockTab("b", true), new mockTab("c", true), new mockTab("d", true), new mockTab("e", false)]);
    1.65 +    ok(fakeGetNextTab(fiveTabsLastNotClosing._selectedTab).name == "e", "get next non-closing tab");
    1.66 +
    1.67 +    let [fiveTabsLastNotClosingLastSelected, fakeGetNextTab] = getMockBrowserTabs(
    1.68 +      [new mockTab("a", false), new mockTab("b", true), new mockTab("c", true), new mockTab("d", true), new mockTab("e", false)], 4);
    1.69 +    ok(fakeGetNextTab(fiveTabsLastNotClosingLastSelected._selectedTab).name == "a", "get previous non-closing tab");
    1.70 +  }
    1.71 +});
    1.72 +
    1.73 +function test() {
    1.74 +  runTests();
    1.75 +}

mercurial