Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/apps/system/test/unit/identity_test.js
blob: 150944c4e782227a5d0f9f88fdee9fe2209e305c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';

requireApp('system/js/identity.js');
requireApp('system/test/unit/mock_chrome_event.js');
requireApp('system/test/unit/mock_trusted_ui_manager.js');
requireApp('system/test/unit/mock_l10n.js');

// ensure its defined as a global so mocha will not complain about us
// leaking new global variables during the test
if (!window.TrustedUIManager) {
  window.TrustedUIManager = true;
}

suite('identity', function() {
  var subject;
  var realL10n;
  var realTrustedUIManager;
  var realDispatchEvent;

  var lastDispatchedEvent = null;

  suiteSetup(function() {
    subject = Identity;
    realTrustedUIManager = window.TrustedUIManager;
    window.TrustedUIManager = MockTrustedUIManager;

    realL10n = navigator.mozL10n;
    navigator.mozL10n = MockL10n;

    realDispatchEvent = subject._dispatchEvent;
    subject._dispatchEvent = function (obj) {
      lastDispatchedEvent = obj;
    };
  });

  suiteTeardown(function() {
    window.TrustedUIManager = realTrustedUIManager;
    subject._dispatchEvent = realDispatchEvent;

    navigator.mozL10n = realL10n;
  });

  setup(function() {});

  teardown(function() {
    MockTrustedUIManager.mTeardown();
  });

  suite('open popup', function() {
    setup(function() {
      var event = new MockChromeEvent({
        type: 'open-id-dialog',
        id: 'test-open-event-id',
        showUI: true
      });
      subject.handleEvent(event);
    });

    test('popup parameters', function() {
      assert.equal(MockTrustedUIManager.mOpened, true);
      assert.equal(MockTrustedUIManager.mName, 'persona-signin');
      assert.equal(MockTrustedUIManager.mChromeEventId, 'test-open-event-id');
    });

    test('frame event listener', function() {
      var frame = MockTrustedUIManager.mFrame;
      var event = document.createEvent('CustomEvent');
      event.initCustomEvent('mozbrowserloadstart', true, true, {target: frame});
      frame.dispatchEvent(event);

      assert.equal(frame, lastDispatchedEvent.frame);
      assert.equal('test-open-event-id', lastDispatchedEvent.id);
    });
  });

  suite('close popup', function() {
    setup(function() {
      var event = new MockChromeEvent({
        type: 'received-id-assertion',
        id: 'test-close-event-id',
        showUI: true
      });
      subject.handleEvent(event);
    });

    test('close', function() {
      assert.equal(false, MockTrustedUIManager.mOpened);
      assert.equal('test-close-event-id', lastDispatchedEvent.id);
    });
  });
});