Android Chromoting: Remove exit-fullscreen button.
[chromium-blink-merge.git] / tools / json_schema_compiler / js_externs_generator_test.py
blob004b79c1f8d9c457dbe4aabc8672dd29577a3458
1 #!/usr/bin/env python
2 # Copyright 2015 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 import idl_schema
7 import json_parse
8 from js_externs_generator import JsExternsGenerator
9 from datetime import datetime
10 import model
11 import unittest
14 # The contents of a fake idl file.
15 fake_idl = """
16 // Copyright 2014 The Chromium Authors. All rights reserved.
17 // Use of this source code is governed by a BSD-style license that can be
18 // found in the LICENSE file.
20 // A totally fake API.
21 namespace fakeApi {
22 enum Greek {
23 ALPHA,
24 BETA,
25 GAMMA,
26 DELTA
29 dictionary Bar {
30 long num;
33 dictionary Baz {
34 DOMString str;
35 long num;
36 boolean b;
37 Greek letter;
38 Greek? optionalLetter;
39 long[] arr;
40 Bar[]? optionalObjArr;
41 Greek[] enumArr;
42 any[] anythingGoes;
43 Bar obj;
44 long? maybe;
45 (DOMString or Greek or long[]) choice;
48 callback VoidCallback = void();
50 callback BazGreekCallback = void(Baz baz, Greek greek);
52 interface Functions {
53 // Does something exciting! And what's more, this is a multiline function
54 // comment! It goes onto multiple lines!
55 // |baz| : The baz to use.
56 static void doSomething(Baz baz, VoidCallback callback);
58 // |callback| : The callback which will most assuredly in all cases be
59 // called; that is, of course, iff such a callback was provided and is
60 // not at all null.
61 static void bazGreek(optional BazGreekCallback callback);
63 [deprecated="Use a new method."] static DOMString returnString();
66 interface Events {
67 // Fired when we realize it's a trap!
68 static void onTrapDetected(Baz baz);
71 """
73 # The output we expect from our fake idl file.
74 expected_output = """// Copyright %s The Chromium Authors. All rights reserved.
75 // Use of this source code is governed by a BSD-style license that can be
76 // found in the LICENSE file.
78 /** @fileoverview Externs generated from namespace: fakeApi */
80 /**
81 * @const
83 chrome.fakeApi = {};
85 /**
86 * @enum {string}
87 * @see https://developer.chrome.com/extensions/fakeApi#type-Greek
89 chrome.fakeApi.Greek = {
90 ALPHA: 'ALPHA',
91 BETA: 'BETA',
92 GAMMA: 'GAMMA',
93 DELTA: 'DELTA',
96 /**
97 * @typedef {{
98 * num: number
99 * }}
100 * @see https://developer.chrome.com/extensions/fakeApi#type-Bar
102 var Bar;
105 * @typedef {{
106 * str: string,
107 * num: number,
108 * b: boolean,
109 * letter: !chrome.fakeApi.Greek,
110 * optionalLetter: (!chrome.fakeApi.Greek|undefined),
111 * arr: !Array<number>,
112 * optionalObjArr: (!Array<Bar>|undefined),
113 * enumArr: !Array<!chrome.fakeApi.Greek>,
114 * anythingGoes: !Array<*>,
115 * obj: Bar,
116 * maybe: (number|undefined),
117 * choice: (string|!chrome.fakeApi.Greek|!Array<number>)
118 * }}
119 * @see https://developer.chrome.com/extensions/fakeApi#type-Baz
121 var Baz;
124 * Does something exciting! And what's more, this is a multiline function
125 * comment! It goes onto multiple lines!
126 * @param {Baz} baz The baz to use.
127 * @param {function():void} callback
128 * @see https://developer.chrome.com/extensions/fakeApi#method-doSomething
130 chrome.fakeApi.doSomething = function(baz, callback) {};
133 * @param {function(Baz, !chrome.fakeApi.Greek):void=} callback The callback
134 * which will most assuredly in all cases be called; that is, of course, iff
135 * such a callback was provided and is not at all null.
136 * @see https://developer.chrome.com/extensions/fakeApi#method-bazGreek
138 chrome.fakeApi.bazGreek = function(callback) {};
141 * @return {string}
142 * @deprecated Use a new method.
143 * @see https://developer.chrome.com/extensions/fakeApi#method-returnString
145 chrome.fakeApi.returnString = function() {};
148 * Fired when we realize it's a trap!
149 * @type {!ChromeEvent}
150 * @see https://developer.chrome.com/extensions/fakeApi#event-onTrapDetected
152 chrome.fakeApi.onTrapDetected;
153 """ % datetime.now().year
156 fake_json = """// Copyright 2014 The Chromium Authors. All rights reserved.
157 // Use of this source code is governed by a BSD-style license that can be
158 // found in the LICENSE file.
162 "namespace": "fakeJson",
163 "description": "Fake JSON API Stuff",
164 "functions": [ {
165 "name": "funcWithInlineObj",
166 "type": "function",
167 "parameters": [
169 "type": "object",
170 "name": "inlineObj",
171 "description": "Evil inline object! With a super duper duper long\
172 string description that causes problems!",
173 "properties": {
174 "foo": {
175 "type": "boolean",
176 "optional": "true",
177 "description": "The foo."
179 "bar": {
180 "type": "integer",
181 "description": "The bar."
183 "baz": {
184 "type": "object",
185 "description": "Inception object.",
186 "properties": {
187 "depth": {
188 "type": "integer"
195 "name": "callback",
196 "type": "function",
197 "parameters": [
199 "type": "object",
200 "name": "returnObj",
201 "properties": {
202 "str": { "type": "string"}
206 "description": "The callback to this heinous method"
209 "returns": {
210 "type": "object",
211 "properties": {
212 "str": { "type": "string" },
213 "int": { "type": "number" }
218 ]"""
220 json_expected = """// Copyright %s The Chromium Authors. All rights reserved.
221 // Use of this source code is governed by a BSD-style license that can be
222 // found in the LICENSE file.
224 /** @fileoverview Externs generated from namespace: fakeJson */
227 * @const
229 chrome.fakeJson = {};
232 * @param {{
233 * foo: (boolean|undefined),
234 * bar: number,
235 * baz: {
236 * depth: number
238 * }} inlineObj Evil inline object! With a super duper duper long string
239 * description that causes problems!
240 * @param {function({
241 * str: string
242 * }):void} callback The callback to this heinous method
243 * @return {{
244 * str: string,
245 * int: number
246 * }}
247 * @see https://developer.chrome.com/extensions/fakeJson#method-funcWithInlineObj
249 chrome.fakeJson.funcWithInlineObj = function(inlineObj, callback) {};
250 """ % datetime.now().year
253 class JsExternGeneratorTest(unittest.TestCase):
254 def _GetNamespace(self, fake_content, filename, is_idl):
255 """Returns a namespace object for the given content"""
256 api_def = (idl_schema.Process(fake_content, filename) if is_idl
257 else json_parse.Parse(fake_content))
258 m = model.Model()
259 return m.AddNamespace(api_def[0], filename)
261 def setUp(self):
262 self.maxDiff = None # Lets us see the full diff when inequal.
264 def testBasic(self):
265 namespace = self._GetNamespace(fake_idl, 'fake_api.idl', True)
266 self.assertMultiLineEqual(expected_output,
267 JsExternsGenerator().Generate(namespace).Render())
269 def testJsonWithInlineObjects(self):
270 namespace = self._GetNamespace(fake_json, 'fake_api.json', False)
271 self.assertMultiLineEqual(json_expected,
272 JsExternsGenerator().Generate(namespace).Render())
275 if __name__ == '__main__':
276 unittest.main()