/**
   * Tests whether the preference store will be read automatically when a change to the preference
   * store is made.
   *
   * @throws ParseException If "ALT+SHIFT+Q A" cannot be parsed by KeySequence.
   */
  public final void testAutoLoad() throws ParseException {
    // Get the services.
    ICommandService commandService = (ICommandService) fWorkbench.getAdapter(ICommandService.class);
    IBindingService bindingService = (IBindingService) fWorkbench.getAdapter(IBindingService.class);
    bindingService.readRegistryAndPreferences(commandService);

    // Check the pre-conditions.
    final String emacsSchemeId = EMACS_SCHEME_ID;
    assertFalse(
        "The active scheme should be Emacs yet",
        emacsSchemeId.equals(bindingService.getActiveScheme().getId()));
    final KeySequence formalKeySequence = KeySequence.getInstance("ALT+SHIFT+Q A");
    final String commandId = "org.eclipse.ui.views.showView";
    Binding[] bindings = bindingService.getBindings();
    int i;
    for (i = 0; i < bindings.length; i++) {
      final Binding binding = bindings[i];
      if ((binding.getType() == Binding.USER)
          && (formalKeySequence.equals(binding.getTriggerSequence()))) {
        final ParameterizedCommand command = binding.getParameterizedCommand();
        final String actualCommandId = (command == null) ? null : command.getCommand().getId();
        assertFalse("The command should not yet be bound", commandId.equals(actualCommandId));
        break;
      }
    }
    assertEquals("There shouldn't be a matching command yet", bindings.length, i);

    // Modify the preference store.
    final IPreferenceStore store = WorkbenchPlugin.getDefault().getPreferenceStore();
    store.setValue(
        "org.eclipse.ui.commands",
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?><org.eclipse.ui.commands><activeKeyConfiguration keyConfigurationId=\""
            + emacsSchemeId
            + "\"/><keyBinding commandId=\""
            + commandId
            + "\" contextId=\"org.eclipse.ui.contexts.window\" keyConfigurationId=\"org.eclipse.ui.defaultAcceleratorConfiguration\" keySequence=\""
            + formalKeySequence
            + "\"/></org.eclipse.ui.commands>");

    // Check that the values have changed.
    assertEquals(
        "The active scheme should now be Emacs",
        emacsSchemeId,
        bindingService.getActiveScheme().getId());
    bindings = bindingService.getBindings();
    for (i = 0; i < bindings.length; i++) {
      final Binding binding = bindings[i];
      if ((binding.getType() == Binding.USER)
          && (formalKeySequence.equals(binding.getTriggerSequence()))) {
        final ParameterizedCommand command = binding.getParameterizedCommand();
        final String actualCommandId = (command == null) ? null : command.getCommand().getId();
        assertEquals("The command should be bound to 'ALT+SHIFT+Q A'", commandId, actualCommandId);
        break;
      }
    }
    assertFalse("There should be a matching command now", (bindings.length == i));
  }
  public void testAboutBindingIn3x() throws Exception {
    ICommandService commandService = (ICommandService) fWorkbench.getAdapter(ICommandService.class);
    IBindingService bindingService = (IBindingService) fWorkbench.getAdapter(IBindingService.class);

    final Scheme activeScheme = bindingService.getActiveScheme();

    ParameterizedCommand aboutCmd =
        new ParameterizedCommand(
            commandService.getCommand(IWorkbenchCommandConstants.HELP_ABOUT), null);
    ParameterizedCommand activateEditorCmd =
        new ParameterizedCommand(
            commandService.getCommand(IWorkbenchCommandConstants.WINDOW_ACTIVATE_EDITOR), null);

    final KeySequence keyF12 = KeySequence.getInstance("F12");
    final Binding editorBinding = bindingService.getPerfectMatch(keyF12);
    assertNotNull(editorBinding);
    assertEquals(activateEditorCmd, editorBinding.getParameterizedCommand());

    EBindingService ebs = (EBindingService) fWorkbench.getService(EBindingService.class);
    HashMap<String, String> attrs = new HashMap<String, String>();
    attrs.put(EBindingService.TYPE_ATTR_TAG, "user");
    final Binding localAboutBinding =
        ebs.createBinding(keyF12, aboutCmd, IContextService.CONTEXT_ID_WINDOW, attrs);
    assertEquals(Binding.USER, localAboutBinding.getType());

    final Binding[] bindings = bindingService.getBindings();
    Binding[] added = new Binding[bindings.length + 1];
    System.arraycopy(bindings, 0, added, 0, bindings.length);

    added[bindings.length] = localAboutBinding;
    bindingService.savePreferences(activeScheme, added);

    final Binding secondMatch = bindingService.getPerfectMatch(keyF12);
    // fails
    assertNotNull(secondMatch);
    assertEquals(aboutCmd, secondMatch.getParameterizedCommand());
  }
  public void testAboutBinding() throws Exception {
    ICommandService commandService = (ICommandService) fWorkbench.getAdapter(ICommandService.class);
    IBindingService bindingService = (IBindingService) fWorkbench.getAdapter(IBindingService.class);

    final Scheme activeScheme = bindingService.getActiveScheme();
    final Binding[] originalBindings = bindingService.getBindings();

    ParameterizedCommand aboutCmd =
        new ParameterizedCommand(
            commandService.getCommand(IWorkbenchCommandConstants.HELP_ABOUT), null);
    ParameterizedCommand activateEditorCmd =
        new ParameterizedCommand(
            commandService.getCommand(IWorkbenchCommandConstants.WINDOW_ACTIVATE_EDITOR), null);

    final KeySequence keyF12 = KeySequence.getInstance("F12");
    final KeySequence keyAltCtrlShiftI = KeySequence.getInstance("ALT+CTRL+SHIFT+I");
    final Binding editorBinding = bindingService.getPerfectMatch(keyF12);
    assertNotNull(editorBinding);
    assertEquals(activateEditorCmd, editorBinding.getParameterizedCommand());

    EBindingService ebs = (EBindingService) fWorkbench.getService(EBindingService.class);
    HashMap<String, String> attrs = new HashMap<String, String>();
    attrs.put(EBindingService.TYPE_ATTR_TAG, "user");
    final Binding localAboutBinding =
        ebs.createBinding(keyF12, aboutCmd, IContextService.CONTEXT_ID_WINDOW, attrs);
    assertEquals(Binding.USER, localAboutBinding.getType());

    // test unbinding a system binding and adding a user binding (same
    // triggers and context)
    final Binding[] bindings = originalBindings;
    Binding[] added = new Binding[bindings.length + 2];
    System.arraycopy(bindings, 0, added, 0, bindings.length);

    Binding del =
        new KeyBinding(
            keyF12,
            null,
            IBindingService.DEFAULT_DEFAULT_ACTIVE_SCHEME_ID,
            IContextService.CONTEXT_ID_WINDOW,
            null,
            null,
            null,
            Binding.USER);
    added[bindings.length] = del;
    added[bindings.length + 1] = localAboutBinding;
    bindingService.savePreferences(activeScheme, added);

    // the match should be the user binding that we just added
    final Binding secondMatch = bindingService.getPerfectMatch(keyF12);
    assertNotNull(secondMatch);
    assertEquals(aboutCmd, secondMatch.getParameterizedCommand());

    // go back to the defaults
    bindingService.savePreferences(activeScheme, originalBindings);
    final Binding thirdMatch = bindingService.getPerfectMatch(keyF12);
    assertNotNull(thirdMatch);
    assertEquals(activateEditorCmd, thirdMatch.getParameterizedCommand());

    // try assigning alt-ctrl-shift-i (no other binding uses this for this
    // context) to the 'about' command
    final Binding localAboutBinding1 =
        ebs.createBinding(keyAltCtrlShiftI, aboutCmd, IContextService.CONTEXT_ID_WINDOW, attrs);
    assertEquals(Binding.USER, localAboutBinding1.getType());
    Binding[] added1 = new Binding[bindings.length + 1];
    System.arraycopy(bindings, 0, added1, 0, bindings.length);
    added1[bindings.length] = localAboutBinding1;

    bindingService.savePreferences(activeScheme, added1);
    final Binding fourthMatch = bindingService.getPerfectMatch(keyAltCtrlShiftI);
    assertNotNull(fourthMatch);
    assertEquals(aboutCmd, fourthMatch.getParameterizedCommand());
  }