package com.intellij.json;

import com.intellij.codeInsight.template.Template;
import com.intellij.codeInsight.template.TemplateContextType;
import com.intellij.codeInsight.template.TemplateManager;
import com.intellij.codeInsight.template.impl.TemplateImpl;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
import com.intellij.json.liveTemplates.JsonContextType;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;

/**
 * @author Mikhail Golubev
 */
public class JsonLiveTemplateTest extends JsonTestCase {

  private void doExpandabilityTest(@NotNull String originalText, boolean canBeExpanded) {
    myFixture.configureByText(JsonFileType.INSTANCE, originalText);
    final Template template = createJsonTemplate("foo", "foo", "[42]");
    assertEquals(canBeExpanded, TemplateManagerImpl.isApplicable(myFixture.getFile(), myFixture.getCaretOffset(), (TemplateImpl)template));
  }

  @NotNull
  private Template createJsonTemplate(@NotNull String name, @NotNull String group, @NotNull String text) {
    final TemplateManager templateManager = TemplateManager.getInstance(getProject());
    final Template template = templateManager.createTemplate(name, group, text);

    final TemplateContextType context = ContainerUtil.findInstance(TemplateContextType.EP_NAME.getExtensions(), JsonContextType.class);
    assertNotNull(context);
    ((TemplateImpl)template).getTemplateContext().setEnabled(context, true);
    return template;
  }

  public void testNotExpandableInsideStringLiteral() {
    doExpandabilityTest("{\"bar\": \"fo<caret>o\"}", false);
  }

  public void testNotExpandableInsidePropertyKey() {
    doExpandabilityTest("{fo<caret>o: \"bar\"}", false);
  }

  public void testExpandableAtTopLevel() {
    doExpandabilityTest("fo<caret>o", true);
  }
}

@@ -21,16 +21,173 @@
 .json;%0A%0A
+import com.intellij.codeInsight.lookup.Lookup;%0Aimport com.intellij.codeInsight.lookup.LookupManager;%0Aimport com.intellij.codeInsight.lookup.impl.LookupImpl;%0A
 import c
@@ -500,42 +500,280 @@
 lij.
-json.liveTemplates.JsonContextType
+codeInsight.template.impl.TemplateSettings;%0Aimport com.intellij.codeInsight.template.impl.actions.ListTemplatesAction;%0Aimport com.intellij.json.liveTemplates.JsonContextType;%0Aimport com.intellij.openapi.command.WriteCommandAction;%0Aimport com.intellij.openapi.editor.Editor
 ;%0Aim
@@ -971,84 +971,66 @@
 ate 
-void doExpandabilityTest(@NotNull String originalText, boolean canBeExpanded
+boolean isApplicableContextUnderCaret(@NotNull String text
 ) %7B%0A
@@ -1086,17 +1086,9 @@
 CE, 
-originalT
+t
 ext)
@@ -1169,35 +1169,14 @@
     
-assertEquals(canBeExpanded,
+return
  Tem
@@ -1277,17 +1277,16 @@
 emplate)
-)
 ;%0A  %7D%0A%0A 
@@ -1811,16 +1811,75 @@
 , true);
+%0A%0A    TemplateSettings.getInstance().addTemplate(template);
 %0A    ret
@@ -1956,34 +1956,56 @@
 ) %7B%0A    
-doExpandabilityTes
+assertFalse(isApplicableContextUnderCare
 t(%22%7B%5C%22ba
@@ -2021,31 +2021,25 @@
 %3Ccaret%3Eo%5C%22%7D%22
-, false
+)
 );%0A  %7D%0A%0A  pu
@@ -2091,34 +2091,56 @@
 ) %7B%0A    
-doExpandabilityTes
+assertFalse(isApplicableContextUnderCare
 t(%22%7Bfo%3Cc
@@ -2160,15 +2160,9 @@
 %5C%22%7D%22
-, false
+)
 );%0A 
@@ -2216,46 +2216,875 @@
     
-doExpandabilityTes
+assertTrue(isApplicableContextUnderCaret(%22fo%3Ccaret%3Eo%22));%0A  %7D%0A%0A  public void testCustomTemplateExpansion() %7B%0A    final String templateContent = %22%7B%5Cn%22 +%0A                                   %22  %5C%22foo%5C%22: %5C%22$1$%5C%22%5Cn%22 +%0A                                   %22%7D%22;%0A    createJsonTemplate(%22foo%22, %22foo%22, templateContent);%0A    myFixture.configureByTex
 t(
+JsonFileType.INSTANCE, 
 %22fo
+o
 %3Ccaret%3E
-o%22, true
+%22);%0A    final Editor editor = myFixture.getEditor();%0A    WriteCommandAction.runWriteCommandAction(null, new Runnable() %7B%0A      @Override%0A      public void run() %7B%0A        new ListTemplatesAction().actionPerformedImpl(getProject(), editor);%0A        final LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(editor);%0A        assertNotNull(lookup);%0A        lookup.finishLookup(Lookup.NORMAL_SELECT_CHAR);%0A      %7D%0A    %7D);%0A    myFixture.checkResult(templateContent.replaceAll(%22%5C%5C$.*?%5C%5C$%22, %22%22)
 );%0A 

Add test for live template expansion in JSON (just for reference)