/*******************************************************************************
 * Copyright (C) 2011, Google Inc.
 *
 * 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
 *******************************************************************************/

package com.google.eclipse.mechanic.core.keybinding;

import com.google.common.collect.ImmutableMap;
import com.google.eclipse.mechanic.core.keybinding.KbaChangeSet.Action;
import com.google.eclipse.mechanic.core.keybinding.KbaChangeSet.KbaBindingList;
import com.google.eclipse.mechanic.core.keybinding.KeyBindingsManualFormatter.BindingType;
import com.google.eclipse.mechanic.internal.TaskType;

import org.junit.Test;

import java.io.StringReader;
import java.util.Map;

/**
 * @author zorzella
 */
public class KeyBindingsManualFormatterTest {

  private static final KbaChangeSetQualifier QUALIFIER = new KbaChangeSetQualifier(
      "org.eclipse.ui.defaultAcceleratorConfiguration",
      null, // platform
      "org.eclipse.ui.contexts.window",
      Action.ADD.toString());
  
  @Test
  public void testARoundTripCommandWithParams() {
    
    Map<KbaChangeSetQualifier, KbaChangeSet> map = kbaMap(new KbaBindingList(
        kbaBindingCommandWithParams()));
    
    String json = KeyBindingsManualFormatter.getBindingsPrintout(BindingType.USER, map, "", TaskType.LASTMOD);
    KeyBindingsModel kbaFromJson = KeyBindingsParser.deSerialize(new StringReader(json));
  }

  @Test
  public void testARoundTripCommandWithNoParams() {
    
    Map<KbaChangeSetQualifier, KbaChangeSet> map = kbaMap(new KbaBindingList(
        kbaBindingCommandWithNoParams()));
    
    String json = KeyBindingsManualFormatter.getBindingsPrintout(BindingType.USER, map, "", TaskType.LASTMOD);
    KeyBindingsModel kbaFromJson = KeyBindingsParser.deSerialize(new StringReader(json));
  }
  
  private static ImmutableMap<KbaChangeSetQualifier, KbaChangeSet> kbaMap(
      KbaBindingList kbaBindingList) {
    return ImmutableMap.<KbaChangeSetQualifier, KbaChangeSet>builder()
        .put(QUALIFIER, kbaChangeSetFor(kbaBindingList))
        .build();
  }

  private static KbaChangeSet kbaChangeSetFor(KbaBindingList kbaBindingList) {
    return new KbaChangeSet(QUALIFIER.scheme, QUALIFIER.platform, QUALIFIER.context, QUALIFIER.action, kbaBindingList);
  }

  private static KbaBinding kbaBindingCommandWithNoParams() {
    KbaBinding kbaBinding = new KbaBinding(
      "Ctrl+/",
      "org.eclipse.jdt.ui.edit.text.java.toggle.comment");
    return kbaBinding;
  }

  private static KbaBinding kbaBindingCommandWithParams() {
    Map<String,String> params = ImmutableMap.<String,String>builder()
        .put("org.eclipse.ui.views.showView.viewId", "org.eclipse.jdt.debug.ui.DisplayView")
        .build();
    
    KbaBinding kbaBinding = new KbaBinding(
      "Shift+Alt+Q I",
      "org.eclipse.ui.views.showView",
      params);
    return kbaBinding;
  }
}

@@ -920,31 +920,91 @@
 p;%0A%0A
-/**%0A * @author zorzella
+import junit.framework.Assert;%0A%0A/**%0A * Tests for %7B@link KeyBindingsManualFormatter%7D
 %0A */
@@ -1864,32 +1864,463 @@
 arams()));%0A    %0A
+    String json = KeyBindingsManualFormatter.getBindingsPrintout(%0A        BindingType.USER,%0A        map,%0A        %22Joe's Apartment%22,%0A        TaskType.LASTMOD);%0A%0A    Assert.assertTrue(json, json.contains(%22%5C'description%5C' : %5C'Joe''s Apartment%5C',%22));%0A  %7D%0A%0A  @Test%0A  public void testEscapingDescription() %7B%0A    %0A    Map%3CKbaChangeSetQualifier, KbaChangeSet%3E map = kbaMap(new KbaBindingList(%0A        kbaBindingCommandWithParams()));%0A    %0A
     String json 
@@ -2406,32 +2406,32 @@
 kType.LASTMOD);%0A
-
     KeyBindingsM
@@ -2508,18 +2508,16 @@
 ));%0A  %7D%0A
-  
 %0A  priva

Add test for Issue 62.