summaryrefslogtreecommitdiffstats
path: root/browser/components/tests/browser/browser_contentpermissionprompt.js
blob: 3e2eb24f62f760f8feb1bd4a83c994a2a379798f (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
 * These tests test nsBrowserGlue's nsIContentPermissionPrompt
 * implementation behaviour with various types of
 * nsIContentPermissionRequests.
 */

"use strict";

XPCOMUtils.defineLazyServiceGetter(
  this,
  "ContentPermissionPrompt",
  "@mozilla.org/content-permission/prompt;1",
  "nsIContentPermissionPrompt"
);

/**
 * This is a partial implementation of nsIContentPermissionType.
 *
 * @param {string} type
 *        The string defining what type of permission is being requested.
 *        Example: "geo", "desktop-notification".
 * @return nsIContentPermissionType implementation.
 */
function MockContentPermissionType(type) {
  this.type = type;
}

MockContentPermissionType.prototype = {
  QueryInterface: ChromeUtils.generateQI(["nsIContentPermissionType"]),
  // We expose the wrappedJSObject so that we can be sure
  // in some of our tests that we're passing the right
  // nsIContentPermissionType around.
  wrappedJSObject: this,
};

/**
 * This is a partial implementation of nsIContentPermissionRequest.
 *
 * @param {Array<nsIContentPermissionType>} typesArray
 *        The types to assign to this nsIContentPermissionRequest,
 *        in order. You probably want to use MockContentPermissionType.
 * @return nsIContentPermissionRequest implementation.
 */
function MockContentPermissionRequest(typesArray) {
  this.types = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
  for (let type of typesArray) {
    this.types.appendElement(type);
  }
}

MockContentPermissionRequest.prototype = {
  QueryInterface: ChromeUtils.generateQI(["nsIContentPermissionRequest"]),
  // We expose the wrappedJSObject so that we can be sure
  // in some of our tests that we're passing the right
  // nsIContentPermissionRequest around.
  wrappedJSObject: this,
  // For some of our tests, we want to make sure that the
  // request is cancelled, so we add some instrumentation here
  // to check that cancel() is called.
  cancel() {
    this.cancelled = true;
  },
  cancelled: false,
  principal: Services.scriptSecurityManager.getSystemPrincipal(),
};

/**
 * Tests that if the nsIContentPermissionRequest has an empty
 * types array, that NS_ERROR_UNEXPECTED is thrown, and the
 * request is cancelled.
 */
add_task(async function test_empty_types() {
  let mockRequest = new MockContentPermissionRequest([]);
  Assert.throws(
    () => {
      ContentPermissionPrompt.prompt(mockRequest);
    },
    /NS_ERROR_UNEXPECTED/,
    "Should have thrown NS_ERROR_UNEXPECTED."
  );
  Assert.ok(mockRequest.cancelled, "Should have cancelled the request.");
});

/**
 * Tests that if the nsIContentPermissionRequest has more than
 * one type, that NS_ERROR_UNEXPECTED is thrown, and the request
 * is cancelled.
 */
add_task(async function test_multiple_types() {
  let mockRequest = new MockContentPermissionRequest([
    new MockContentPermissionType("test1"),
    new MockContentPermissionType("test2"),
  ]);

  Assert.throws(() => {
    ContentPermissionPrompt.prompt(mockRequest);
  }, /NS_ERROR_UNEXPECTED/);
  Assert.ok(mockRequest.cancelled, "Should have cancelled the request.");
});

/**
 * Tests that if the nsIContentPermissionRequest has a type that
 * does not implement nsIContentPermissionType that NS_NOINTERFACE
 * is thrown, and the request is cancelled.
 */
add_task(async function test_not_permission_type() {
  let mockRequest = new MockContentPermissionRequest([
    { QueryInterface: ChromeUtils.generateQI([]) },
  ]);

  Assert.throws(() => {
    ContentPermissionPrompt.prompt(mockRequest);
  }, /NS_NOINTERFACE/);
  Assert.ok(mockRequest.cancelled, "Should have cancelled the request.");
});

/**
 * Tests that if the nsIContentPermissionRequest is for a type
 * that is not recognized, that NS_ERROR_FAILURE is thrown and
 * the request is cancelled.
 */
add_task(async function test_unrecognized_type() {
  let mockRequest = new MockContentPermissionRequest([
    new MockContentPermissionType("test1"),
  ]);

  Assert.throws(() => {
    ContentPermissionPrompt.prompt(mockRequest);
  }, /NS_ERROR_FAILURE/);
  Assert.ok(mockRequest.cancelled, "Should have cancelled the request.");
});

/**
 * Tests that if we meet the minimal requirements for a
 * nsIContentPermissionRequest, that it will be passed to
 * ContentPermissionIntegration's createPermissionPrompt
 * method.
 */
add_task(async function test_working_request() {
  let mockType = new MockContentPermissionType("test-permission-type");
  let mockRequest = new MockContentPermissionRequest([mockType]);

  // mockPermissionPrompt is what createPermissionPrompt
  // will return. Returning some kind of object should be
  // enough to convince nsBrowserGlue that everything went
  // okay.
  let didPrompt = false;
  let mockPermissionPrompt = {
    prompt() {
      didPrompt = true;
    },
  };

  let integration = base => ({
    createPermissionPrompt(type, request) {
      Assert.equal(type, "test-permission-type");
      Assert.ok(
        Object.is(request.wrappedJSObject, mockRequest.wrappedJSObject)
      );
      return mockPermissionPrompt;
    },
  });

  // Register an integration so that we can capture the
  // calls into ContentPermissionIntegration.
  try {
    Integration.contentPermission.register(integration);

    ContentPermissionPrompt.prompt(mockRequest);
    Assert.ok(!mockRequest.cancelled, "Should not have cancelled the request.");
    Assert.ok(didPrompt, "Should have tried to show the prompt");
  } finally {
    Integration.contentPermission.unregister(integration);
  }
});