a0dd480ae33ed6d0581b7f75352f1829ac5b7b54
[WebKit-https.git] / Source / WebKit2 / Scripts / webkit2 / messages_unittest.py
1 # Copyright (C) 2010 Apple Inc. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions
5 # are met:
6 # 1.  Redistributions of source code must retain the above copyright
7 #     notice, this list of conditions and the following disclaimer.
8 # 2.  Redistributions in binary form must reproduce the above copyright
9 #     notice, this list of conditions and the following disclaimer in the
10 #     documentation and/or other materials provided with the distribution.
11 #
12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
13 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
16 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
23 import unittest
24 from StringIO import StringIO
25
26 import messages
27 import parser
28
29 _messages_file_contents = """# Copyright (C) 2010 Apple Inc. All rights reserved.
30 #
31 # Redistribution and use in source and binary forms, with or without
32 # modification, are permitted provided that the following conditions
33 # are met:
34 # 1.  Redistributions of source code must retain the above copyright
35 #     notice, this list of conditions and the following disclaimer.
36 # 2.  Redistributions in binary form must reproduce the above copyright
37 #     notice, this list of conditions and the following disclaimer in the
38 #     documentation and/or other materials provided with the distribution.
39 #
40 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
41 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
42 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
44 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
46 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
47 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
49 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50
51 #if ENABLE(WEBKIT2)
52 #if NESTED_MASTER_CONDITION || MASTER_OR && MASTER_AND
53
54 messages -> WebPage LegacyReceiver {
55     LoadURL(WTF::String url)
56 #if ENABLE(TOUCH_EVENTS)
57     LoadSomething(WTF::String url)
58 #if NESTED_MESSAGE_CONDITION || SOME_OTHER_MESSAGE_CONDITION
59     TouchEvent(WebKit::WebTouchEvent event)
60 #endif
61 #if NESTED_MESSAGE_CONDITION && SOME_OTHER_MESSAGE_CONDITION
62     AddEvent(WebKit::WebTouchEvent event)
63 #endif
64     LoadSomethingElse(WTF::String url)
65 #endif
66     DidReceivePolicyDecision(uint64_t frameID, uint64_t listenerID, uint32_t policyAction)
67     Close()
68
69     PreferencesDidChange(WebKit::WebPreferencesStore store)
70     SendDoubleAndFloat(double d, float f)
71     SendInts(Vector<uint64_t> ints, Vector<Vector<uint64_t>> intVectors)
72
73     CreatePlugin(uint64_t pluginInstanceID, WebKit::Plugin::Parameters parameters) -> (bool result)
74     RunJavaScriptAlert(uint64_t frameID, WTF::String message) -> ()
75     GetPlugins(bool refresh) -> (Vector<WebCore::PluginInfo> plugins)
76     GetPluginProcessConnection(WTF::String pluginPath) -> (CoreIPC::Connection::Handle connectionHandle) Delayed
77
78     TestMultipleAttributes() -> () WantsConnection Delayed
79
80     TestParameterAttributes([AttributeOne AttributeTwo] uint64_t foo, double bar, [AttributeThree] double baz)
81
82     TemplateTest(HashMap<String, std::pair<String, uint64_t>> a)
83
84 #if PLATFORM(MAC)
85     DidCreateWebProcessConnection(CoreIPC::MachPort connectionIdentifier)
86 #endif
87
88 #if PLATFORM(MAC)
89     # Keyboard support
90     InterpretKeyEvent(uint32_t type) -> (Vector<WebCore::KeypressCommand> commandName)
91 #endif
92
93 #if ENABLE(DEPRECATED_FEATURE)
94     DeprecatedOperation(CoreIPC::DummyType dummy)
95 #endif
96
97 #if ENABLE(EXPERIMENTAL_FEATURE)
98     ExperimentalOperation(CoreIPC::DummyType dummy)
99 #endif
100 }
101
102 #endif
103 #endif
104 """
105
106 _expected_results = {
107     'name': 'WebPage',
108     'conditions': ('(ENABLE(WEBKIT2) && (NESTED_MASTER_CONDITION || MASTER_OR && MASTER_AND))'),
109     'messages': (
110         {
111             'name': 'LoadURL',
112             'parameters': (
113                 ('WTF::String', 'url'),
114             ),
115             'conditions': (None),
116         },
117         {
118             'name': 'LoadSomething',
119             'parameters': (
120                 ('WTF::String', 'url'),
121             ),
122             'conditions': ('ENABLE(TOUCH_EVENTS)'),
123         },
124         {
125             'name': 'TouchEvent',
126             'parameters': (
127                 ('WebKit::WebTouchEvent', 'event'),
128             ),
129             'conditions': ('(ENABLE(TOUCH_EVENTS) && (NESTED_MESSAGE_CONDITION || SOME_OTHER_MESSAGE_CONDITION))'),
130         },
131         {
132             'name': 'AddEvent',
133             'parameters': (
134                 ('WebKit::WebTouchEvent', 'event'),
135             ),
136             'conditions': ('(ENABLE(TOUCH_EVENTS) && (NESTED_MESSAGE_CONDITION && SOME_OTHER_MESSAGE_CONDITION))'),
137         },
138         {
139             'name': 'LoadSomethingElse',
140             'parameters': (
141                 ('WTF::String', 'url'),
142             ),
143             'conditions': ('ENABLE(TOUCH_EVENTS)'),
144         },
145         {
146             'name': 'DidReceivePolicyDecision',
147             'parameters': (
148                 ('uint64_t', 'frameID'),
149                 ('uint64_t', 'listenerID'),
150                 ('uint32_t', 'policyAction'),
151             ),
152             'conditions': (None),
153         },
154         {
155             'name': 'Close',
156             'parameters': (),
157             'conditions': (None),
158         },
159         {
160             'name': 'PreferencesDidChange',
161             'parameters': (
162                 ('WebKit::WebPreferencesStore', 'store'),
163             ),
164             'conditions': (None),
165         },
166         {
167             'name': 'SendDoubleAndFloat',
168             'parameters': (
169                 ('double', 'd'),
170                 ('float', 'f'),
171             ),
172             'conditions': (None),
173         },
174         {
175             'name': 'SendInts',
176             'parameters': (
177                 ('Vector<uint64_t>', 'ints'),
178                 ('Vector<Vector<uint64_t>>', 'intVectors')
179             ),
180             'conditions': (None),
181         },
182         {
183             'name': 'CreatePlugin',
184             'parameters': (
185                 ('uint64_t', 'pluginInstanceID'),
186                 ('WebKit::Plugin::Parameters', 'parameters')
187             ),
188             'reply_parameters': (
189                 ('bool', 'result'),
190             ),
191             'conditions': (None),
192         },
193         {
194             'name': 'RunJavaScriptAlert',
195             'parameters': (
196                 ('uint64_t', 'frameID'),
197                 ('WTF::String', 'message')
198             ),
199             'reply_parameters': (),
200             'conditions': (None),
201         },
202         {
203             'name': 'GetPlugins',
204             'parameters': (
205                 ('bool', 'refresh'),
206             ),
207             'reply_parameters': (
208                 ('Vector<WebCore::PluginInfo>', 'plugins'),
209             ),
210             'conditions': (None),
211         },
212         {
213             'name': 'GetPluginProcessConnection',
214             'parameters': (
215                 ('WTF::String', 'pluginPath'),
216             ),
217             'reply_parameters': (
218                 ('CoreIPC::Connection::Handle', 'connectionHandle'),
219             ),
220             'conditions': (None),
221         },
222         {
223             'name': 'TestMultipleAttributes',
224             'parameters': (
225             ),
226             'reply_parameters': (
227             ),
228             'conditions': (None),
229         },
230         {
231             'name': 'TestParameterAttributes',
232             'parameters': (
233                 ('uint64_t', 'foo', ('AttributeOne', 'AttributeTwo')),
234                 ('double', 'bar'),
235                 ('double', 'baz', ('AttributeThree',)),
236             ),
237             'conditions': (None),
238         },
239         {
240             'name': 'TemplateTest',
241             'parameters': (
242                 ('HashMap<String, std::pair<String, uint64_t>>', 'a'),
243             ),
244             'conditions': (None),
245         },
246         {
247             'name': 'DidCreateWebProcessConnection',
248             'parameters': (
249                 ('CoreIPC::MachPort', 'connectionIdentifier'),
250             ),
251             'conditions': ('PLATFORM(MAC)'),
252         },
253         {
254             'name': 'InterpretKeyEvent',
255             'parameters': (
256                 ('uint32_t', 'type'),
257             ),
258             'reply_parameters': (
259                 ('Vector<WebCore::KeypressCommand>', 'commandName'),
260             ),
261             'conditions': ('PLATFORM(MAC)'),
262         },
263         {
264             'name': 'DeprecatedOperation',
265             'parameters': (
266                 ('CoreIPC::DummyType', 'dummy'),
267             ),
268             'conditions': ('ENABLE(DEPRECATED_FEATURE)'),
269         },
270         {
271             'name': 'ExperimentalOperation',
272             'parameters': (
273                 ('CoreIPC::DummyType', 'dummy'),
274             ),
275             'conditions': ('ENABLE(EXPERIMENTAL_FEATURE)'),
276         }
277     ),
278 }
279
280
281 class MessagesTest(unittest.TestCase):
282     def setUp(self):
283         self.receiver = parser.parse(StringIO(_messages_file_contents))
284
285
286 class ParsingTest(MessagesTest):
287     def check_message(self, message, expected_message):
288         self.assertEquals(message.name, expected_message['name'])
289         self.assertEquals(len(message.parameters), len(expected_message['parameters']))
290         for index, parameter in enumerate(message.parameters):
291             expected_parameter = expected_message['parameters'][index]
292             self.assertEquals(parameter.type, expected_parameter[0])
293             self.assertEquals(parameter.name, expected_parameter[1])
294             if len(expected_parameter) > 2:
295                 self.assertEquals(parameter.attributes, frozenset(expected_parameter[2]))
296                 for attribute in expected_parameter[2]:
297                     self.assertTrue(parameter.has_attribute(attribute))
298             else:
299                 self.assertEquals(parameter.attributes, frozenset())
300         if message.reply_parameters != None:
301             for index, parameter in enumerate(message.reply_parameters):
302                 self.assertEquals(parameter.type, expected_message['reply_parameters'][index][0])
303                 self.assertEquals(parameter.name, expected_message['reply_parameters'][index][1])
304         else:
305             self.assertFalse('reply_parameters' in expected_message)
306         self.assertEquals(message.condition, expected_message['conditions'])
307
308     def test_receiver(self):
309         """Receiver should be parsed as expected"""
310         self.assertEquals(self.receiver.name, _expected_results['name'])
311         self.assertEquals(self.receiver.condition, _expected_results['conditions'])
312         self.assertEquals(len(self.receiver.messages), len(_expected_results['messages']))
313         for index, message in enumerate(self.receiver.messages):
314             self.check_message(message, _expected_results['messages'][index])
315
316 _expected_header = """/*
317  * Copyright (C) 2010 Apple Inc. All rights reserved.
318  *
319  * Redistribution and use in source and binary forms, with or without
320  * modification, are permitted provided that the following conditions
321  * are met:
322  * 1.  Redistributions of source code must retain the above copyright
323  *     notice, this list of conditions and the following disclaimer.
324  * 2.  Redistributions in binary form must reproduce the above copyright
325  *     notice, this list of conditions and the following disclaimer in the
326  *     documentation and/or other materials provided with the distribution.
327  *
328  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
329  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
330  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
331  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
332  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
333  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
334  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
335  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
336  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
337  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
338  */
339
340 #ifndef WebPageMessages_h
341 #define WebPageMessages_h
342
343 #if (ENABLE(WEBKIT2) && (NESTED_MASTER_CONDITION || MASTER_OR && MASTER_AND))
344
345 #include "Arguments.h"
346 #include "Connection.h"
347 #include "MessageEncoder.h"
348 #include "Plugin.h"
349 #include "StringReference.h"
350 #include <WebCore/KeyboardEvent.h>
351 #include <WebCore/PluginData.h>
352 #include <utility>
353 #include <wtf/HashMap.h>
354 #include <wtf/ThreadSafeRefCounted.h>
355 #include <wtf/Vector.h>
356
357 namespace CoreIPC {
358     class Connection;
359     class DummyType;
360     class MachPort;
361 }
362
363 namespace WTF {
364     class String;
365 }
366
367 namespace WebKit {
368     struct WebPreferencesStore;
369     class WebTouchEvent;
370 }
371
372 namespace Messages {
373 namespace WebPage {
374
375 static inline CoreIPC::StringReference messageReceiverName()
376 {
377     return CoreIPC::StringReference("WebPage");
378 }
379
380 class LoadURL {
381 public:
382     typedef std::tuple<WTF::String> DecodeType;
383
384     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
385     static CoreIPC::StringReference name() { return CoreIPC::StringReference("LoadURL"); }
386     static const bool isSync = false;
387
388     explicit LoadURL(const WTF::String& url)
389         : m_arguments(url)
390     {
391     }
392
393     const std::tuple<const WTF::String&> arguments() const
394     {
395         return m_arguments;
396     }
397
398 private:
399     std::tuple<const WTF::String&> m_arguments;
400 };
401
402 #if ENABLE(TOUCH_EVENTS)
403 class LoadSomething {
404 public:
405     typedef std::tuple<WTF::String> DecodeType;
406
407     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
408     static CoreIPC::StringReference name() { return CoreIPC::StringReference("LoadSomething"); }
409     static const bool isSync = false;
410
411     explicit LoadSomething(const WTF::String& url)
412         : m_arguments(url)
413     {
414     }
415
416     const std::tuple<const WTF::String&> arguments() const
417     {
418         return m_arguments;
419     }
420
421 private:
422     std::tuple<const WTF::String&> m_arguments;
423 };
424 #endif
425
426 #if (ENABLE(TOUCH_EVENTS) && (NESTED_MESSAGE_CONDITION || SOME_OTHER_MESSAGE_CONDITION))
427 class TouchEvent {
428 public:
429     typedef std::tuple<WebKit::WebTouchEvent> DecodeType;
430
431     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
432     static CoreIPC::StringReference name() { return CoreIPC::StringReference("TouchEvent"); }
433     static const bool isSync = false;
434
435     explicit TouchEvent(const WebKit::WebTouchEvent& event)
436         : m_arguments(event)
437     {
438     }
439
440     const std::tuple<const WebKit::WebTouchEvent&> arguments() const
441     {
442         return m_arguments;
443     }
444
445 private:
446     std::tuple<const WebKit::WebTouchEvent&> m_arguments;
447 };
448 #endif
449
450 #if (ENABLE(TOUCH_EVENTS) && (NESTED_MESSAGE_CONDITION && SOME_OTHER_MESSAGE_CONDITION))
451 class AddEvent {
452 public:
453     typedef std::tuple<WebKit::WebTouchEvent> DecodeType;
454
455     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
456     static CoreIPC::StringReference name() { return CoreIPC::StringReference("AddEvent"); }
457     static const bool isSync = false;
458
459     explicit AddEvent(const WebKit::WebTouchEvent& event)
460         : m_arguments(event)
461     {
462     }
463
464     const std::tuple<const WebKit::WebTouchEvent&> arguments() const
465     {
466         return m_arguments;
467     }
468
469 private:
470     std::tuple<const WebKit::WebTouchEvent&> m_arguments;
471 };
472 #endif
473
474 #if ENABLE(TOUCH_EVENTS)
475 class LoadSomethingElse {
476 public:
477     typedef std::tuple<WTF::String> DecodeType;
478
479     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
480     static CoreIPC::StringReference name() { return CoreIPC::StringReference("LoadSomethingElse"); }
481     static const bool isSync = false;
482
483     explicit LoadSomethingElse(const WTF::String& url)
484         : m_arguments(url)
485     {
486     }
487
488     const std::tuple<const WTF::String&> arguments() const
489     {
490         return m_arguments;
491     }
492
493 private:
494     std::tuple<const WTF::String&> m_arguments;
495 };
496 #endif
497
498 class DidReceivePolicyDecision {
499 public:
500     typedef std::tuple<uint64_t, uint64_t, uint32_t> DecodeType;
501
502     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
503     static CoreIPC::StringReference name() { return CoreIPC::StringReference("DidReceivePolicyDecision"); }
504     static const bool isSync = false;
505
506     DidReceivePolicyDecision(uint64_t frameID, uint64_t listenerID, uint32_t policyAction)
507         : m_arguments(frameID, listenerID, policyAction)
508     {
509     }
510
511     const std::tuple<uint64_t, uint64_t, uint32_t> arguments() const
512     {
513         return m_arguments;
514     }
515
516 private:
517     std::tuple<uint64_t, uint64_t, uint32_t> m_arguments;
518 };
519
520 class Close {
521 public:
522     typedef std::tuple<> DecodeType;
523
524     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
525     static CoreIPC::StringReference name() { return CoreIPC::StringReference("Close"); }
526     static const bool isSync = false;
527
528     const std::tuple<> arguments() const
529     {
530         return m_arguments;
531     }
532
533 private:
534     std::tuple<> m_arguments;
535 };
536
537 class PreferencesDidChange {
538 public:
539     typedef std::tuple<WebKit::WebPreferencesStore> DecodeType;
540
541     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
542     static CoreIPC::StringReference name() { return CoreIPC::StringReference("PreferencesDidChange"); }
543     static const bool isSync = false;
544
545     explicit PreferencesDidChange(const WebKit::WebPreferencesStore& store)
546         : m_arguments(store)
547     {
548     }
549
550     const std::tuple<const WebKit::WebPreferencesStore&> arguments() const
551     {
552         return m_arguments;
553     }
554
555 private:
556     std::tuple<const WebKit::WebPreferencesStore&> m_arguments;
557 };
558
559 class SendDoubleAndFloat {
560 public:
561     typedef std::tuple<double, float> DecodeType;
562
563     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
564     static CoreIPC::StringReference name() { return CoreIPC::StringReference("SendDoubleAndFloat"); }
565     static const bool isSync = false;
566
567     SendDoubleAndFloat(double d, float f)
568         : m_arguments(d, f)
569     {
570     }
571
572     const std::tuple<double, float> arguments() const
573     {
574         return m_arguments;
575     }
576
577 private:
578     std::tuple<double, float> m_arguments;
579 };
580
581 class SendInts {
582 public:
583     typedef std::tuple<Vector<uint64_t>, Vector<Vector<uint64_t>>> DecodeType;
584
585     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
586     static CoreIPC::StringReference name() { return CoreIPC::StringReference("SendInts"); }
587     static const bool isSync = false;
588
589     SendInts(const Vector<uint64_t>& ints, const Vector<Vector<uint64_t>>& intVectors)
590         : m_arguments(ints, intVectors)
591     {
592     }
593
594     const std::tuple<const Vector<uint64_t>&, const Vector<Vector<uint64_t>>&> arguments() const
595     {
596         return m_arguments;
597     }
598
599 private:
600     std::tuple<const Vector<uint64_t>&, const Vector<Vector<uint64_t>>&> m_arguments;
601 };
602
603 class CreatePlugin {
604 public:
605     typedef std::tuple<uint64_t, WebKit::Plugin::Parameters> DecodeType;
606
607     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
608     static CoreIPC::StringReference name() { return CoreIPC::StringReference("CreatePlugin"); }
609     static const bool isSync = true;
610
611     typedef CoreIPC::Arguments1<bool&> Reply;
612     CreatePlugin(uint64_t pluginInstanceID, const WebKit::Plugin::Parameters& parameters)
613         : m_arguments(pluginInstanceID, parameters)
614     {
615     }
616
617     const std::tuple<uint64_t, const WebKit::Plugin::Parameters&> arguments() const
618     {
619         return m_arguments;
620     }
621
622 private:
623     std::tuple<uint64_t, const WebKit::Plugin::Parameters&> m_arguments;
624 };
625
626 class RunJavaScriptAlert {
627 public:
628     typedef std::tuple<uint64_t, WTF::String> DecodeType;
629
630     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
631     static CoreIPC::StringReference name() { return CoreIPC::StringReference("RunJavaScriptAlert"); }
632     static const bool isSync = true;
633
634     typedef CoreIPC::Arguments0 Reply;
635     RunJavaScriptAlert(uint64_t frameID, const WTF::String& message)
636         : m_arguments(frameID, message)
637     {
638     }
639
640     const std::tuple<uint64_t, const WTF::String&> arguments() const
641     {
642         return m_arguments;
643     }
644
645 private:
646     std::tuple<uint64_t, const WTF::String&> m_arguments;
647 };
648
649 class GetPlugins {
650 public:
651     typedef std::tuple<bool> DecodeType;
652
653     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
654     static CoreIPC::StringReference name() { return CoreIPC::StringReference("GetPlugins"); }
655     static const bool isSync = true;
656
657     typedef CoreIPC::Arguments1<Vector<WebCore::PluginInfo>&> Reply;
658     explicit GetPlugins(bool refresh)
659         : m_arguments(refresh)
660     {
661     }
662
663     const std::tuple<bool> arguments() const
664     {
665         return m_arguments;
666     }
667
668 private:
669     std::tuple<bool> m_arguments;
670 };
671
672 class GetPluginProcessConnection {
673 public:
674     typedef std::tuple<WTF::String> DecodeType;
675
676     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
677     static CoreIPC::StringReference name() { return CoreIPC::StringReference("GetPluginProcessConnection"); }
678     static const bool isSync = true;
679
680     struct DelayedReply : public ThreadSafeRefCounted<DelayedReply> {
681         DelayedReply(PassRefPtr<CoreIPC::Connection>, std::unique_ptr<CoreIPC::MessageEncoder>);
682         ~DelayedReply();
683
684         bool send(const CoreIPC::Connection::Handle& connectionHandle);
685
686     private:
687         RefPtr<CoreIPC::Connection> m_connection;
688         std::unique_ptr<CoreIPC::MessageEncoder> m_encoder;
689     };
690
691     typedef CoreIPC::Arguments1<CoreIPC::Connection::Handle&> Reply;
692     explicit GetPluginProcessConnection(const WTF::String& pluginPath)
693         : m_arguments(pluginPath)
694     {
695     }
696
697     const std::tuple<const WTF::String&> arguments() const
698     {
699         return m_arguments;
700     }
701
702 private:
703     std::tuple<const WTF::String&> m_arguments;
704 };
705
706 class TestMultipleAttributes {
707 public:
708     typedef std::tuple<> DecodeType;
709
710     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
711     static CoreIPC::StringReference name() { return CoreIPC::StringReference("TestMultipleAttributes"); }
712     static const bool isSync = true;
713
714     struct DelayedReply : public ThreadSafeRefCounted<DelayedReply> {
715         DelayedReply(PassRefPtr<CoreIPC::Connection>, std::unique_ptr<CoreIPC::MessageEncoder>);
716         ~DelayedReply();
717
718         bool send();
719
720     private:
721         RefPtr<CoreIPC::Connection> m_connection;
722         std::unique_ptr<CoreIPC::MessageEncoder> m_encoder;
723     };
724
725     typedef CoreIPC::Arguments0 Reply;
726     const std::tuple<> arguments() const
727     {
728         return m_arguments;
729     }
730
731 private:
732     std::tuple<> m_arguments;
733 };
734
735 class TestParameterAttributes {
736 public:
737     typedef std::tuple<uint64_t, double, double> DecodeType;
738
739     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
740     static CoreIPC::StringReference name() { return CoreIPC::StringReference("TestParameterAttributes"); }
741     static const bool isSync = false;
742
743     TestParameterAttributes(uint64_t foo, double bar, double baz)
744         : m_arguments(foo, bar, baz)
745     {
746     }
747
748     const std::tuple<uint64_t, double, double> arguments() const
749     {
750         return m_arguments;
751     }
752
753 private:
754     std::tuple<uint64_t, double, double> m_arguments;
755 };
756
757 class TemplateTest {
758 public:
759     typedef std::tuple<HashMap<String, std::pair<String, uint64_t>>> DecodeType;
760
761     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
762     static CoreIPC::StringReference name() { return CoreIPC::StringReference("TemplateTest"); }
763     static const bool isSync = false;
764
765     explicit TemplateTest(const HashMap<String, std::pair<String, uint64_t>>& a)
766         : m_arguments(a)
767     {
768     }
769
770     const std::tuple<const HashMap<String, std::pair<String, uint64_t>>&> arguments() const
771     {
772         return m_arguments;
773     }
774
775 private:
776     std::tuple<const HashMap<String, std::pair<String, uint64_t>>&> m_arguments;
777 };
778
779 #if PLATFORM(MAC)
780 class DidCreateWebProcessConnection {
781 public:
782     typedef std::tuple<CoreIPC::MachPort> DecodeType;
783
784     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
785     static CoreIPC::StringReference name() { return CoreIPC::StringReference("DidCreateWebProcessConnection"); }
786     static const bool isSync = false;
787
788     explicit DidCreateWebProcessConnection(const CoreIPC::MachPort& connectionIdentifier)
789         : m_arguments(connectionIdentifier)
790     {
791     }
792
793     const std::tuple<const CoreIPC::MachPort&> arguments() const
794     {
795         return m_arguments;
796     }
797
798 private:
799     std::tuple<const CoreIPC::MachPort&> m_arguments;
800 };
801 #endif
802
803 #if PLATFORM(MAC)
804 class InterpretKeyEvent {
805 public:
806     typedef std::tuple<uint32_t> DecodeType;
807
808     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
809     static CoreIPC::StringReference name() { return CoreIPC::StringReference("InterpretKeyEvent"); }
810     static const bool isSync = true;
811
812     typedef CoreIPC::Arguments1<Vector<WebCore::KeypressCommand>&> Reply;
813     explicit InterpretKeyEvent(uint32_t type)
814         : m_arguments(type)
815     {
816     }
817
818     const std::tuple<uint32_t> arguments() const
819     {
820         return m_arguments;
821     }
822
823 private:
824     std::tuple<uint32_t> m_arguments;
825 };
826 #endif
827
828 #if ENABLE(DEPRECATED_FEATURE)
829 class DeprecatedOperation {
830 public:
831     typedef std::tuple<CoreIPC::DummyType> DecodeType;
832
833     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
834     static CoreIPC::StringReference name() { return CoreIPC::StringReference("DeprecatedOperation"); }
835     static const bool isSync = false;
836
837     explicit DeprecatedOperation(const CoreIPC::DummyType& dummy)
838         : m_arguments(dummy)
839     {
840     }
841
842     const std::tuple<const CoreIPC::DummyType&> arguments() const
843     {
844         return m_arguments;
845     }
846
847 private:
848     std::tuple<const CoreIPC::DummyType&> m_arguments;
849 };
850 #endif
851
852 #if ENABLE(EXPERIMENTAL_FEATURE)
853 class ExperimentalOperation {
854 public:
855     typedef std::tuple<CoreIPC::DummyType> DecodeType;
856
857     static CoreIPC::StringReference receiverName() { return messageReceiverName(); }
858     static CoreIPC::StringReference name() { return CoreIPC::StringReference("ExperimentalOperation"); }
859     static const bool isSync = false;
860
861     explicit ExperimentalOperation(const CoreIPC::DummyType& dummy)
862         : m_arguments(dummy)
863     {
864     }
865
866     const std::tuple<const CoreIPC::DummyType&> arguments() const
867     {
868         return m_arguments;
869     }
870
871 private:
872     std::tuple<const CoreIPC::DummyType&> m_arguments;
873 };
874 #endif
875
876 } // namespace WebPage
877 } // namespace Messages
878
879 #endif // (ENABLE(WEBKIT2) && (NESTED_MASTER_CONDITION || MASTER_OR && MASTER_AND))
880
881 #endif // WebPageMessages_h
882 """
883
884 _expected_receiver_implementation = """/*
885  * Copyright (C) 2010 Apple Inc. All rights reserved.
886  *
887  * Redistribution and use in source and binary forms, with or without
888  * modification, are permitted provided that the following conditions
889  * are met:
890  * 1.  Redistributions of source code must retain the above copyright
891  *     notice, this list of conditions and the following disclaimer.
892  * 2.  Redistributions in binary form must reproduce the above copyright
893  *     notice, this list of conditions and the following disclaimer in the
894  *     documentation and/or other materials provided with the distribution.
895  *
896  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
897  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
898  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
899  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
900  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
901  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
902  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
903  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
904  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
905  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
906  */
907
908 #include "config.h"
909
910 #if (ENABLE(WEBKIT2) && (NESTED_MASTER_CONDITION || MASTER_OR && MASTER_AND))
911
912 #include "WebPage.h"
913
914 #include "ArgumentCoders.h"
915 #include "Connection.h"
916 #if ENABLE(DEPRECATED_FEATURE) || ENABLE(EXPERIMENTAL_FEATURE)
917 #include "DummyType.h"
918 #endif
919 #include "HandleMessage.h"
920 #if PLATFORM(MAC)
921 #include "MachPort.h"
922 #endif
923 #include "MessageDecoder.h"
924 #include "Plugin.h"
925 #include "WebCoreArgumentCoders.h"
926 #if (ENABLE(TOUCH_EVENTS) && (NESTED_MESSAGE_CONDITION && SOME_OTHER_MESSAGE_CONDITION)) || (ENABLE(TOUCH_EVENTS) && (NESTED_MESSAGE_CONDITION || SOME_OTHER_MESSAGE_CONDITION))
927 #include "WebEvent.h"
928 #endif
929 #include "WebPageMessages.h"
930 #include "WebPreferencesStore.h"
931 #if PLATFORM(MAC)
932 #include <WebCore/KeyboardEvent.h>
933 #endif
934 #include <WebCore/PluginData.h>
935 #include <utility>
936 #include <wtf/HashMap.h>
937 #include <wtf/Vector.h>
938 #include <wtf/text/WTFString.h>
939
940 namespace Messages {
941
942 namespace WebPage {
943
944 GetPluginProcessConnection::DelayedReply::DelayedReply(PassRefPtr<CoreIPC::Connection> connection, std::unique_ptr<CoreIPC::MessageEncoder> encoder)
945     : m_connection(connection)
946     , m_encoder(std::move(encoder))
947 {
948 }
949
950 GetPluginProcessConnection::DelayedReply::~DelayedReply()
951 {
952     ASSERT(!m_connection);
953 }
954
955 bool GetPluginProcessConnection::DelayedReply::send(const CoreIPC::Connection::Handle& connectionHandle)
956 {
957     ASSERT(m_encoder);
958     *m_encoder << connectionHandle;
959     bool result = m_connection->sendSyncReply(std::move(m_encoder));
960     m_connection = nullptr;
961     return result;
962 }
963
964 TestMultipleAttributes::DelayedReply::DelayedReply(PassRefPtr<CoreIPC::Connection> connection, std::unique_ptr<CoreIPC::MessageEncoder> encoder)
965     : m_connection(connection)
966     , m_encoder(std::move(encoder))
967 {
968 }
969
970 TestMultipleAttributes::DelayedReply::~DelayedReply()
971 {
972     ASSERT(!m_connection);
973 }
974
975 bool TestMultipleAttributes::DelayedReply::send()
976 {
977     ASSERT(m_encoder);
978     bool result = m_connection->sendSyncReply(std::move(m_encoder));
979     m_connection = nullptr;
980     return result;
981 }
982
983 } // namespace WebPage
984
985 } // namespace Messages
986
987 namespace WebKit {
988
989 void WebPage::didReceiveWebPageMessage(CoreIPC::Connection*, CoreIPC::MessageDecoder& decoder)
990 {
991     if (decoder.messageName() == Messages::WebPage::LoadURL::name()) {
992         CoreIPC::handleMessage<Messages::WebPage::LoadURL>(decoder, this, &WebPage::loadURL);
993         return;
994     }
995 #if ENABLE(TOUCH_EVENTS)
996     if (decoder.messageName() == Messages::WebPage::LoadSomething::name()) {
997         CoreIPC::handleMessage<Messages::WebPage::LoadSomething>(decoder, this, &WebPage::loadSomething);
998         return;
999     }
1000 #endif
1001 #if (ENABLE(TOUCH_EVENTS) && (NESTED_MESSAGE_CONDITION || SOME_OTHER_MESSAGE_CONDITION))
1002     if (decoder.messageName() == Messages::WebPage::TouchEvent::name()) {
1003         CoreIPC::handleMessage<Messages::WebPage::TouchEvent>(decoder, this, &WebPage::touchEvent);
1004         return;
1005     }
1006 #endif
1007 #if (ENABLE(TOUCH_EVENTS) && (NESTED_MESSAGE_CONDITION && SOME_OTHER_MESSAGE_CONDITION))
1008     if (decoder.messageName() == Messages::WebPage::AddEvent::name()) {
1009         CoreIPC::handleMessage<Messages::WebPage::AddEvent>(decoder, this, &WebPage::addEvent);
1010         return;
1011     }
1012 #endif
1013 #if ENABLE(TOUCH_EVENTS)
1014     if (decoder.messageName() == Messages::WebPage::LoadSomethingElse::name()) {
1015         CoreIPC::handleMessage<Messages::WebPage::LoadSomethingElse>(decoder, this, &WebPage::loadSomethingElse);
1016         return;
1017     }
1018 #endif
1019     if (decoder.messageName() == Messages::WebPage::DidReceivePolicyDecision::name()) {
1020         CoreIPC::handleMessage<Messages::WebPage::DidReceivePolicyDecision>(decoder, this, &WebPage::didReceivePolicyDecision);
1021         return;
1022     }
1023     if (decoder.messageName() == Messages::WebPage::Close::name()) {
1024         CoreIPC::handleMessage<Messages::WebPage::Close>(decoder, this, &WebPage::close);
1025         return;
1026     }
1027     if (decoder.messageName() == Messages::WebPage::PreferencesDidChange::name()) {
1028         CoreIPC::handleMessage<Messages::WebPage::PreferencesDidChange>(decoder, this, &WebPage::preferencesDidChange);
1029         return;
1030     }
1031     if (decoder.messageName() == Messages::WebPage::SendDoubleAndFloat::name()) {
1032         CoreIPC::handleMessage<Messages::WebPage::SendDoubleAndFloat>(decoder, this, &WebPage::sendDoubleAndFloat);
1033         return;
1034     }
1035     if (decoder.messageName() == Messages::WebPage::SendInts::name()) {
1036         CoreIPC::handleMessage<Messages::WebPage::SendInts>(decoder, this, &WebPage::sendInts);
1037         return;
1038     }
1039     if (decoder.messageName() == Messages::WebPage::TestParameterAttributes::name()) {
1040         CoreIPC::handleMessage<Messages::WebPage::TestParameterAttributes>(decoder, this, &WebPage::testParameterAttributes);
1041         return;
1042     }
1043     if (decoder.messageName() == Messages::WebPage::TemplateTest::name()) {
1044         CoreIPC::handleMessage<Messages::WebPage::TemplateTest>(decoder, this, &WebPage::templateTest);
1045         return;
1046     }
1047 #if PLATFORM(MAC)
1048     if (decoder.messageName() == Messages::WebPage::DidCreateWebProcessConnection::name()) {
1049         CoreIPC::handleMessage<Messages::WebPage::DidCreateWebProcessConnection>(decoder, this, &WebPage::didCreateWebProcessConnection);
1050         return;
1051     }
1052 #endif
1053 #if ENABLE(DEPRECATED_FEATURE)
1054     if (decoder.messageName() == Messages::WebPage::DeprecatedOperation::name()) {
1055         CoreIPC::handleMessage<Messages::WebPage::DeprecatedOperation>(decoder, this, &WebPage::deprecatedOperation);
1056         return;
1057     }
1058 #endif
1059 #if ENABLE(EXPERIMENTAL_FEATURE)
1060     if (decoder.messageName() == Messages::WebPage::ExperimentalOperation::name()) {
1061         CoreIPC::handleMessage<Messages::WebPage::ExperimentalOperation>(decoder, this, &WebPage::experimentalOperation);
1062         return;
1063     }
1064 #endif
1065     ASSERT_NOT_REACHED();
1066 }
1067
1068 void WebPage::didReceiveSyncWebPageMessage(CoreIPC::Connection* connection, CoreIPC::MessageDecoder& decoder, std::unique_ptr<CoreIPC::MessageEncoder>& replyEncoder)
1069 {
1070     if (decoder.messageName() == Messages::WebPage::CreatePlugin::name()) {
1071         CoreIPC::handleMessage<Messages::WebPage::CreatePlugin>(decoder, *replyEncoder, this, &WebPage::createPlugin);
1072         return;
1073     }
1074     if (decoder.messageName() == Messages::WebPage::RunJavaScriptAlert::name()) {
1075         CoreIPC::handleMessage<Messages::WebPage::RunJavaScriptAlert>(decoder, *replyEncoder, this, &WebPage::runJavaScriptAlert);
1076         return;
1077     }
1078     if (decoder.messageName() == Messages::WebPage::GetPlugins::name()) {
1079         CoreIPC::handleMessage<Messages::WebPage::GetPlugins>(decoder, *replyEncoder, this, &WebPage::getPlugins);
1080         return;
1081     }
1082     if (decoder.messageName() == Messages::WebPage::GetPluginProcessConnection::name()) {
1083         CoreIPC::handleMessageDelayed<Messages::WebPage::GetPluginProcessConnection>(connection, decoder, replyEncoder, this, &WebPage::getPluginProcessConnection);
1084         return;
1085     }
1086     if (decoder.messageName() == Messages::WebPage::TestMultipleAttributes::name()) {
1087         CoreIPC::handleMessageDelayed<Messages::WebPage::TestMultipleAttributes>(connection, decoder, replyEncoder, this, &WebPage::testMultipleAttributes);
1088         return;
1089     }
1090 #if PLATFORM(MAC)
1091     if (decoder.messageName() == Messages::WebPage::InterpretKeyEvent::name()) {
1092         CoreIPC::handleMessage<Messages::WebPage::InterpretKeyEvent>(decoder, *replyEncoder, this, &WebPage::interpretKeyEvent);
1093         return;
1094     }
1095 #endif
1096     ASSERT_NOT_REACHED();
1097 }
1098
1099 } // namespace WebKit
1100
1101 #endif // (ENABLE(WEBKIT2) && (NESTED_MASTER_CONDITION || MASTER_OR && MASTER_AND))
1102 """
1103
1104
1105 class GeneratedFileContentsTest(unittest.TestCase):
1106     def assertGeneratedFileContentsEqual(self, first, second):
1107         first_list = first.split('\n')
1108         second_list = second.split('\n')
1109
1110         for index, first_line in enumerate(first_list):
1111             self.assertEquals(first_line, second_list[index])
1112
1113         self.assertEquals(len(first_list), len(second_list))
1114
1115
1116 class HeaderTest(GeneratedFileContentsTest):
1117     def test_header(self):
1118         file_contents = messages.generate_messages_header(StringIO(_messages_file_contents))
1119         self.assertGeneratedFileContentsEqual(file_contents, _expected_header)
1120
1121
1122 class ReceiverImplementationTest(GeneratedFileContentsTest):
1123     def test_receiver_implementation(self):
1124         file_contents = messages.generate_message_handler(StringIO(_messages_file_contents))
1125         self.assertGeneratedFileContentsEqual(file_contents, _expected_receiver_implementation)
1126
1127
1128 if __name__ == '__main__':
1129     unittest.main()