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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
|
/*******************************************************************************
* Copyright (c) 2005, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.e4.ui.keybinding.tests;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import org.eclipse.core.commands.CommandManager;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.core.commands.contexts.Context;
import org.eclipse.core.commands.contexts.ContextManager;
import org.eclipse.jface.bindings.Binding;
import org.eclipse.jface.bindings.BindingManager;
import org.eclipse.jface.bindings.Scheme;
import org.eclipse.jface.bindings.TriggerSequence;
import org.eclipse.jface.util.Util;
import org.eclipse.ui.tests.harness.util.UITestCase;
/**
* Test cases covering the various interaction between bindings. Bindings that
* have been removed. Bindings that have been added. Inheritance of various
* properties.
*
* @since 3.1
*/
public final class BindingInteractionsTest extends UITestCase {
/**
* The binding manager to use in each test case. A new binding manager is
* created for each test case, and it is disposed when the test is over.
*/
private BindingManager bindingManager = null;
/**
* The context manager to use in each test case. A new context manager is
* created for each test case, and it is disposed when the test is over.
*/
private ContextManager contextManager = null;
/**
* Constructor for <code>BindingInteractionsTest</code>.
*
* @param name
* The name of the test
*/
public BindingInteractionsTest(final String name) {
super(name);
}
/**
* Creates a new context manager and a binding manager for use in the test
* cases.
*/
protected void doSetUp() {
contextManager = new ContextManager();
bindingManager = new BindingManager(contextManager,
new CommandManager());
}
/**
* Releases the context manager and binding manager for garbage collection.
*/
protected void doTearDown() {
contextManager = null;
bindingManager = null;
}
/**
* <p>
* Tests whether two identical bindings lead to a conflict.
* </p>
*
* @throws NotDefinedException
* If the scheme we try to activate is not defined.
*/
public void testConflict() throws NotDefinedException {
final Context context = contextManager.getContext("na");
context.define("name", "description", null);
final Scheme scheme = bindingManager.getScheme("na");
scheme.define("name", "description", null);
bindingManager.setActiveScheme(scheme);
final Set activeContextIds = new HashSet();
activeContextIds.add("na");
contextManager.setActiveContextIds(activeContextIds);
final Binding binding1 = new TestBinding("conflict1", "na", "na", null,
null, Binding.SYSTEM, null);
bindingManager.addBinding(binding1);
final Binding binding2 = new TestBinding("conflict2", "na", "na", null,
null, Binding.SYSTEM, null);
bindingManager.addBinding(binding2);
TriggerSequence[] activeBindings = bindingManager
.getActiveBindingsFor(binding1.getParameterizedCommand());
assertFalse(binding1.equals(binding2));
assertTrue("Neither binding should be active",
activeBindings.length == 0);
activeBindings = bindingManager.getActiveBindingsFor(binding2
.getParameterizedCommand());
assertTrue("Neither binding should be active",
activeBindings.length == 0);
}
/**
* <p>
* Tests whether a plug-in developer can override a binding in a child
* context.
* </p>
*
* @throws NotDefinedException
* If the scheme we try to activate is not defined.
*/
public void testContextOverride() throws NotDefinedException {
final Context parentContext = contextManager.getContext("parent");
parentContext.define("parent", "parent context", null);
final Context childContext = contextManager.getContext("child");
childContext.define("child", "child context", "parent");
final Scheme scheme = bindingManager.getScheme("na");
scheme.define("name", "description", null);
bindingManager.setActiveScheme(scheme);
final Binding binding1 = new TestBinding("parent", "na", "parent",
null, null, Binding.SYSTEM, null);
bindingManager.addBinding(binding1);
final Binding binding2 = new TestBinding("child", "na", "child", null,
null, Binding.SYSTEM, null);
bindingManager.addBinding(binding2);
// Only "parent"
final Set activeContextIds = new HashSet();
activeContextIds.add("parent");
contextManager.setActiveContextIds(activeContextIds);
assertEquals(
"When only the parent context is active, only the parent binding is active.",
binding1, bindingManager
.getPerfectMatch(TestBinding.TRIGGER_SEQUENCE));
// Only "child"
activeContextIds.clear();
activeContextIds.add("child");
contextManager.setActiveContextIds(activeContextIds);
assertEquals(
"When only the child context is active, only the child binding is active.",
binding2, bindingManager
.getPerfectMatch(TestBinding.TRIGGER_SEQUENCE));
// Both "parent" and "child"
activeContextIds.add("parent");
contextManager.setActiveContextIds(activeContextIds);
assertEquals(
"When both contexts are active, only the child binding is active.",
binding2, bindingManager
.getPerfectMatch(TestBinding.TRIGGER_SEQUENCE));
}
/**
* <p>
* Tests whether a user-defined deletion actually works.
* </p>
*
* @throws NotDefinedException
* If the scheme we try to activate is not defined.
*/
public void testDeletedBinding() throws NotDefinedException {
final Context context = contextManager.getContext("na");
context.define("name", "description", null);
final Scheme scheme = bindingManager.getScheme("na");
scheme.define("name", "description", null);
bindingManager.setActiveScheme(scheme);
final Set activeContextIds = new HashSet();
activeContextIds.add("na");
contextManager.setActiveContextIds(activeContextIds);
final Binding binding1 = new TestBinding(null, "na", "na", null, null,
Binding.USER, null);
bindingManager.addBinding(binding1);
final Binding binding2 = new TestBinding("system", "na", "na", null,
null, Binding.SYSTEM, null);
bindingManager.addBinding(binding2);
assertEquals("The user should be able to remove bindings", null,
bindingManager.getPerfectMatch(TestBinding.TRIGGER_SEQUENCE));
}
/**
* <p>
* Tests whether a user-defined deletion in one context will allow a binding
* in a parent context to match. Bug 105655.
* </p>
*
* @throws NotDefinedException
* If the scheme we try to activate is not defined.
*/
public void testDeletedBindingAllowsParent() throws NotDefinedException {
final Context parentContext = contextManager.getContext("parent");
parentContext.define("name", "description", null);
final Context childContext = contextManager.getContext("child");
childContext.define("name", "description", "parent");
final Scheme scheme = bindingManager.getScheme("na");
scheme.define("name", "description", null);
bindingManager.setActiveScheme(scheme);
final Set activeContextIds = new HashSet();
activeContextIds.add("parent");
activeContextIds.add("child");
contextManager.setActiveContextIds(activeContextIds);
final Binding childBinding = new TestBinding("childCommand", "na",
"child", null, null, Binding.SYSTEM, null);
bindingManager.addBinding(childBinding);
final Binding deletion = new TestBinding(null, "na", "child", null,
null, Binding.USER, null);
bindingManager.addBinding(deletion);
final Binding parentBinding = new TestBinding("parentCommand", "na",
"parent", null, null, Binding.SYSTEM, null);
bindingManager.addBinding(parentBinding);
assertEquals(
"The user should be able to remove bindings to allow a parent binding",
"parentCommand", bindingManager.getPerfectMatch(
TestBinding.TRIGGER_SEQUENCE).getParameterizedCommand()
.getId());
}
/**
* <p>
* Tests a common case for binding deletion. The binding is defined on all
* platforms, then deleted on a specific platform, and defined again as
* something else.
* </p>
*
* @throws NotDefinedException
* If the scheme we try to activate is not defined.
*/
public void testDeletedBindingPlatform() throws NotDefinedException {
final String na = "na";
final Context context = contextManager.getContext(na);
context.define("name", "description", null);
final Scheme scheme = bindingManager.getScheme(na);
scheme.define("name", "description", null);
bindingManager.setActiveScheme(scheme);
final Set activeContextIds = new HashSet();
activeContextIds.add("na");
contextManager.setActiveContextIds(activeContextIds);
final Binding allPlatforms = new TestBinding("allPlatforms", na, na,
null, null, Binding.SYSTEM, null);
bindingManager.addBinding(allPlatforms);
final Binding deletion = new TestBinding(null, na, na, null, Util.getWS(),
Binding.SYSTEM, null);
bindingManager.addBinding(deletion);
final Binding platformSpecific = new TestBinding("platformSpecific",
na, na, null, Util.getWS(), Binding.SYSTEM, null);
bindingManager.addBinding(platformSpecific);
assertEquals(
"We should be able to change a binding on a particular platform",
platformSpecific, bindingManager
.getPerfectMatch(TestBinding.TRIGGER_SEQUENCE));
}
/**
* <p>
* Tests whether a system deletion for a different locale or platform
* actually works. It shouldn't. Deletions should only work if they specify
* a matching locale or platform.
* </p>
*
* @throws NotDefinedException
* If the scheme we try to activate is not defined.
*/
public void testDeletedBindingUnnecessarily() throws NotDefinedException {
final Context context = contextManager.getContext("na");
context.define("name", "description", null);
final Scheme scheme = bindingManager.getScheme("na");
scheme.define("name", "description", null);
bindingManager.setActiveScheme(scheme);
final Set activeContextIds = new HashSet();
activeContextIds.add("na");
contextManager.setActiveContextIds(activeContextIds);
final Binding binding1 = new TestBinding(null, "na", "na",
"not the current locale", null, Binding.SYSTEM, null);
final Binding binding2 = new TestBinding("system", "na", "na", null,
null, Binding.SYSTEM, null);
final Binding binding3 = new TestBinding(null, "na", "na", null,
"not the current platform", Binding.SYSTEM, null);
final Binding[] bindings = new Binding[3];
bindings[0] = binding1;
bindings[1] = binding2;
bindings[2] = binding3;
bindingManager.setBindings(bindings);
assertEquals(
"A binding should not cause a deletion if its locale or platform doesn't match",
binding2, bindingManager
.getPerfectMatch(TestBinding.TRIGGER_SEQUENCE));
}
/**
* <p>
* Tests whether a user can add a binding to the same conditions once
* they've deleted the system binding.
* </p>
*
* @throws NotDefinedException
* If the scheme we try to activate is not defined.
*/
public void testDeletedBindingWithAddition() throws NotDefinedException {
final Context context = contextManager.getContext("na");
context.define("name", "description", null);
final Scheme scheme = bindingManager.getScheme("na");
scheme.define("name", "description", null);
bindingManager.setActiveScheme(scheme);
final Set activeContextIds = new HashSet();
activeContextIds.add("na");
contextManager.setActiveContextIds(activeContextIds);
final Binding binding1 = new TestBinding(null, "na", "na", null, null,
Binding.USER, null);
bindingManager.addBinding(binding1);
final Binding binding2 = new TestBinding("system", "na", "na", null,
null, Binding.SYSTEM, null);
bindingManager.addBinding(binding2);
final Binding binding3 = new TestBinding("user", "na", "na", null,
null, Binding.USER, null);
bindingManager.addBinding(binding3);
assertEquals("The user redefine a particular binding", binding3,
bindingManager.getPerfectMatch(TestBinding.TRIGGER_SEQUENCE));
}
/**
* This tests the case where a plug-in developer unbinds a key, and then a
* user tries to bind to that key. The user should be allowed to bind.
*
* @throws NotDefinedException
* If the scheme we try to activate is not defined.
*/
public void testDeletedBindingWithUserOverride() throws NotDefinedException {
final Context context = contextManager.getContext("na");
context.define("name", "description", null);
final Scheme scheme = bindingManager.getScheme("na");
scheme.define("name", "description", null);
bindingManager.setActiveScheme(scheme);
final Set activeContextIds = new HashSet();
activeContextIds.add("na");
contextManager.setActiveContextIds(activeContextIds);
final Binding generalBinding = new TestBinding("general", scheme
.getId(), context.getId(), null, null, Binding.SYSTEM, null);
bindingManager.addBinding(generalBinding);
final Binding cancelOnPlatform = new TestBinding(null, scheme.getId(),
context.getId(), null, bindingManager.getPlatform(),
Binding.SYSTEM, null);
bindingManager.addBinding(cancelOnPlatform);
final Binding userOverride = new TestBinding("user", scheme.getId(),
context.getId(), null, bindingManager.getPlatform(),
Binding.USER, null);
bindingManager.addBinding(userOverride);
assertEquals("The user redefine a binding deleted in the system",
userOverride, bindingManager
.getPerfectMatch(TestBinding.TRIGGER_SEQUENCE));
}
/**
* Tests that if more than one deletion is defined for the same binding,
* that the deletion will still work. Bug 106574 points out a case where it
* is possible for a deletion to clobber another deletion.
*
* @since 3.2
*/
public void testDoubleDeletedBinding() {
final String parent = "parent";
final String child = "child";
// Set-up the contexts
final Context parentContext = contextManager.getContext(parent);
parentContext.define(parent, parent, null);
final Context childContext = contextManager.getContext(child);
childContext.define(child, child, parent);
final Set activeContextIds = new HashSet();
activeContextIds.add(parent);
activeContextIds.add(child);
contextManager.setActiveContextIds(activeContextIds);
// Set-up the schemes.
final Scheme scheme = bindingManager.getScheme("na");
scheme.define(parent, parent, null);
/*
* Set-up a binding, with two deletions. The first deletion matches the
* context, but the second does not.
*/
final Binding binding = new TestBinding("command", "na", parent, null,
null, Binding.SYSTEM, null);
bindingManager.addBinding(binding);
final Binding correctDeletion = new TestBinding(null, "na", parent,
null, null, Binding.USER, null);
bindingManager.addBinding(correctDeletion);
final Binding wrongDeletion = new TestBinding(null, "na", child, null,
null, Binding.USER, null);
bindingManager.addBinding(wrongDeletion);
// Test that the deletion worked.
assertEquals("The parent should not be active", null, bindingManager
.getPerfectMatch(binding.getTriggerSequence()));
}
/**
* This tests a complicated scenario that arises with the Emacs key binding
* set in the Eclipse workbench. The first binding belongs to a parent
* context, but a child scheme. The trigger sequences are not the same.
*
* @throws NotDefinedException
* If the scheme we try to activate is not defined.
*/
public void testDoubleParent() throws NotDefinedException {
final String parent = "parent";
final String child = "child";
// Set-up the contexts
final Context parentContext = contextManager.getContext(parent);
parentContext.define(parent, parent, null);
final Context childContext = contextManager.getContext(child);
childContext.define(child, child, parent);
final Set activeContextIds = new HashSet();
activeContextIds.add(parent);
activeContextIds.add(child);
contextManager.setActiveContextIds(activeContextIds);
// Set-up the schemes.
final Scheme parentScheme = bindingManager.getScheme(parent);
parentScheme.define(parent, parent, null);
final Scheme childScheme = bindingManager.getScheme(child);
childScheme.define(child, child, parent);
bindingManager.setActiveScheme(childScheme);
// Add two bindings.
final Binding parentBinding = new TestBinding(parent, parent, parent,
null, null, Binding.SYSTEM, null);
final Binding childBinding = new TestBinding(child, child, child, null,
null, Binding.SYSTEM, null);
bindingManager.addBinding(parentBinding);
bindingManager.addBinding(childBinding);
// Test to see that only the child is active.
assertTrue("The parent should not be active",
bindingManager.getActiveBindingsFor(parentBinding
.getParameterizedCommand()).length == 0);
assertTrue("The child should be active",
bindingManager.getActiveBindingsFor(childBinding
.getParameterizedCommand()).length != 0);
}
/**
* <p>
* Tests whether a plug-in developer can override a binding for a particular
* locale.
* </p>
*
* @throws NotDefinedException
* If the scheme we try to activate is not defined.
*/
public void testLocaleOverride() throws NotDefinedException {
final Context context = contextManager.getContext("na");
context.define("name", "description", null);
final Scheme scheme = bindingManager.getScheme("na");
scheme.define("name", "description", null);
bindingManager.setActiveScheme(scheme);
final Set activeContextIds = new HashSet();
activeContextIds.add("na");
contextManager.setActiveContextIds(activeContextIds);
final Binding binding1 = new TestBinding("base", "na", "na", null,
null, Binding.SYSTEM, null);
bindingManager.addBinding(binding1);
final Binding binding2 = new TestBinding(null, "na", "na", Locale
.getDefault().toString(), null, Binding.SYSTEM, null);
bindingManager.addBinding(binding2);
final Binding binding3 = new TestBinding("locale-specific", "na", "na",
Locale.getDefault().toString(), null, Binding.SYSTEM, null);
bindingManager.addBinding(binding3);
assertEquals(
"A plug-in developer should be able to change a binding for a locale",
binding3, bindingManager
.getPerfectMatch(TestBinding.TRIGGER_SEQUENCE));
}
/**
* <p>
* Tests whether a plug-in developer can override a binding for a particular
* platform.
* </p>
*
* @throws NotDefinedException
* If the scheme we try to activate is not defined.
*/
public void testPlatformOverride() throws NotDefinedException {
final Context context = contextManager.getContext("na");
context.define("name", "description", null);
final Scheme scheme = bindingManager.getScheme("na");
scheme.define("name", "description", null);
bindingManager.setActiveScheme(scheme);
final Set activeContextIds = new HashSet();
activeContextIds.add("na");
contextManager.setActiveContextIds(activeContextIds);
final Binding binding1 = new TestBinding("base", "na", "na", null,
null, Binding.SYSTEM, null);
bindingManager.addBinding(binding1);
final Binding binding2 = new TestBinding(null, "na", "na", null, Util.getWS(),
Binding.SYSTEM, null);
bindingManager.addBinding(binding2);
final Binding binding3 = new TestBinding("platform-specific", "na",
"na", null, Util.getWS(), Binding.SYSTEM, null);
bindingManager.addBinding(binding3);
assertEquals(
"A plug-in developer should be able to change a binding for a platform",
binding3, bindingManager
.getPerfectMatch(TestBinding.TRIGGER_SEQUENCE));
}
/**
* <p>
* Tests whether a binding in a child scheme will override a binding in a
* parent scheme. The test is set-up as follows:
* </p>
* <ul>
* <li>Binding1(commandId="child",schemeId="child",contextId="na",locale=null,platform=null,type=SYSTEM)</li>
* <li>Binding2(commandId="parent",schemeId="parent",contextId="na",locale=null,platform=null,type=SYSTEM)</li>
* </ul>
* <p>
* Binding1 should win.
* </p>
*
* @throws NotDefinedException
* If the scheme we try to activate is not defined.
*/
public void testSchemeOverride() throws NotDefinedException {
final Context context = contextManager.getContext("na");
context.define("name", "description", null);
final Scheme parentScheme = bindingManager.getScheme("parent");
parentScheme.define("parent", "parent scheme", null);
final Scheme childScheme = bindingManager.getScheme("child");
childScheme.define("child", "child scheme", "parent");
bindingManager.setActiveScheme(childScheme);
final Set activeContextIds = new HashSet();
activeContextIds.add("na");
contextManager.setActiveContextIds(activeContextIds);
final Binding binding1 = new TestBinding("child", "child", "na", null,
null, Binding.SYSTEM, null);
bindingManager.addBinding(binding1);
final Binding binding2 = new TestBinding("parent", "parent", "na",
null, null, Binding.SYSTEM, null);
bindingManager.addBinding(binding2);
assertEquals("The binding from the child scheme should be active",
binding1, bindingManager
.getPerfectMatch(TestBinding.TRIGGER_SEQUENCE));
}
/**
* This tests a Emacs-style scenario. In this scenario a child scheme
* defines a binding in a parent context. The parent scheme bindings the
* same trigger sequence in a child context. The child scheme definition
* should win.
*
* @throws NotDefinedException
* If the scheme we try to activate is not defined.
*/
public void testSchemeOverrideDifferentContexts()
throws NotDefinedException {
final String parent = "parent";
final String child = "child";
final Context parentContext = contextManager.getContext(parent);
parentContext.define("parent", "description", null);
final Context childContext = contextManager.getContext(child);
childContext.define("child", "description", parent);
final Scheme parentScheme = bindingManager.getScheme(parent);
parentScheme.define("parent", "parent scheme", null);
final Scheme childScheme = bindingManager.getScheme(child);
childScheme.define("child", "child scheme", parent);
bindingManager.setActiveScheme(childScheme);
final Set activeContextIds = new HashSet();
activeContextIds.add(parentContext.getId());
activeContextIds.add(childContext.getId());
contextManager.setActiveContextIds(activeContextIds);
final Binding binding1 = new TestBinding("parent",
parentScheme.getId(), childContext.getId(), null, null,
Binding.SYSTEM, null);
bindingManager.addBinding(binding1);
final Binding binding2 = new TestBinding("child", childScheme.getId(),
parentContext.getId(), null, null, Binding.SYSTEM, null);
bindingManager.addBinding(binding2);
assertEquals("The binding from the child scheme should be active",
binding2, bindingManager
.getPerfectMatch(TestBinding.TRIGGER_SEQUENCE));
}
/**
* <p>
* Tests whether a binding in a child scheme will override a binding in a
* parent scheme -- regardless of their type. The test is set-up as follows:
* </p>
* <ul>
* <li>Binding1(commandId="child",schemeId="child",contextId="na",locale=null,platform=null,type=SYSTEM)</li>
* <li>Binding2(commandId="parent",schemeId="parent",contextId="na",locale=null,platform=null,type=USER)</li>
* </ul>
* <p>
* Binding1 should win.
* </p>
*
* @throws NotDefinedException
* If the scheme we try to activate is not defined.
*/
public void testSchemeOverrideDifferentTypes() throws NotDefinedException {
final Context context = contextManager.getContext("na");
context.define("name", "description", null);
final Scheme parentScheme = bindingManager.getScheme("parent");
parentScheme.define("parent", "parent scheme", null);
final Scheme childScheme = bindingManager.getScheme("child");
childScheme.define("child", "child scheme", "parent");
bindingManager.setActiveScheme(childScheme);
final Set activeContextIds = new HashSet();
activeContextIds.add("na");
contextManager.setActiveContextIds(activeContextIds);
final Binding binding1 = new TestBinding("child", "child", "na", null,
null, Binding.SYSTEM, null);
bindingManager.addBinding(binding1);
final Binding binding2 = new TestBinding("parent", "parent", "na",
null, null, Binding.USER, null);
bindingManager.addBinding(binding2);
assertEquals("The binding from the child scheme should be active",
binding1, bindingManager
.getPerfectMatch(TestBinding.TRIGGER_SEQUENCE));
}
/**
* <p>
* Tests whether two bindings defined for sibling active contexts leads to a
* conflict.
* </p>
*
* @throws NotDefinedException
* If the scheme we try to activate is not defined.
*/
public void testSiblingContextConflict() throws NotDefinedException {
final Context context1 = contextManager.getContext("sibling1");
context1.define("sibling1", "first sibling context", null);
final Context context2 = contextManager.getContext("sibling2");
context2.define("sibling2", "second sibling context", null);
final Scheme scheme = bindingManager.getScheme("na");
scheme.define("name", "description", null);
bindingManager.setActiveScheme(scheme);
final Binding binding1 = new TestBinding("sibling1", "na", "sibling1",
null, null, Binding.SYSTEM, null);
bindingManager.addBinding(binding1);
final Binding binding2 = new TestBinding("sibling2", "na", "sibling2",
null, null, Binding.SYSTEM, null);
bindingManager.addBinding(binding2);
// One sibling active
final Set activeContextIds = new HashSet();
activeContextIds.add("sibling1");
contextManager.setActiveContextIds(activeContextIds);
assertEquals(
"When only the first sibling is active, only the first binding is active",
binding1, bindingManager
.getPerfectMatch(TestBinding.TRIGGER_SEQUENCE));
// Other sibling active
activeContextIds.clear();
activeContextIds.add("sibling2");
contextManager.setActiveContextIds(activeContextIds);
assertEquals(
"When only the second sibling is active, only the second binding is active",
binding2, bindingManager
.getPerfectMatch(TestBinding.TRIGGER_SEQUENCE));
// Both siblings are active
activeContextIds.add("sibling1");
contextManager.setActiveContextIds(activeContextIds);
assertEquals("When both contexts are active, a conflict should occur",
null, bindingManager
.getPerfectMatch(TestBinding.TRIGGER_SEQUENCE));
}
/**
* <p>
* Tests whether a user-defined binding will override the exact same binding
* defined in the system.
* </p>
* <ul>
* <li>Binding1(commandId="user",schemeId="na",contextId="na",locale=null,platform=null,type=USER)</li>
* <li>Binding2(commandId="system",schemeId="na",contextId="na",locale=null,platform=null,type=SYSTEM)</li>
* </ul>
* <p>
* Binding1 should win.
* </p>
*
* @throws NotDefinedException
* If the scheme we try to activate is not defined.
*/
public void testUserOverride() throws NotDefinedException {
final Context context = contextManager.getContext("na");
context.define("name", "description", null);
final Scheme scheme = bindingManager.getScheme("na");
scheme.define("name", "description", null);
bindingManager.setActiveScheme(scheme);
final Set activeContextIds = new HashSet();
activeContextIds.add("na");
contextManager.setActiveContextIds(activeContextIds);
final Binding binding1 = new TestBinding("user", "na", "na", null,
null, Binding.USER, null);
bindingManager.addBinding(binding1);
final Binding binding2 = new TestBinding("system", "na", "na", null,
null, Binding.SYSTEM, null);
bindingManager.addBinding(binding2);
assertEquals("The user-defined binding should be active", binding1,
bindingManager.getPerfectMatch(TestBinding.TRIGGER_SEQUENCE));
}
}
|