/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.editor.impl;

import com.intellij.lang.LanguageAnnotators;
import com.intellij.lang.LanguageExtensionPoint;
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.HighlighterColors;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.impl.DefaultColorsScheme;
import com.intellij.openapi.editor.colors.impl.EditorColorsSchemeImpl;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.ex.MarkupModelEx;
import com.intellij.openapi.editor.ex.RangeHighlighterEx;
import com.intellij.openapi.editor.markup.MarkupModel;
import com.intellij.openapi.editor.markup.RangeHighlighter;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.fileTypes.PlainTextFileType;
import com.intellij.psi.PsiElement;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;

import java.awt.*;

/**
 * @author Dmitry Avdeev
 */
public class DocumentMarkupModelTest extends LightPlatformCodeInsightFixtureTestCase {

  public void testInfoTestAttributes() throws Exception {
    LanguageExtensionPoint<Annotator> extension = new LanguageExtensionPoint<>();
    extension.language="TEXT";
    extension.implementationClass = TestAnnotator.class.getName();
    PlatformTestUtil.registerExtension(ExtensionPointName.create(LanguageAnnotators.EP_NAME), extension, getTestRootDisposable());
    myFixture.configureByText(PlainTextFileType.INSTANCE, "foo");
    EditorColorsScheme scheme = new EditorColorsSchemeImpl(new DefaultColorsScheme()){{initFonts();}};
    scheme.setAttributes(HighlighterColors.TEXT, new TextAttributes(Color.black, Color.white, null, null, Font.PLAIN));
    ((EditorEx)myFixture.getEditor()).setColorsScheme(scheme);
    myFixture.doHighlighting();
    MarkupModel model = DocumentMarkupModel.forDocument(myFixture.getEditor().getDocument(), getProject(), false);
    RangeHighlighter[] highlighters = model.getAllHighlighters();
    assertEquals(1, highlighters.length);
    TextAttributes attributes = highlighters[0].getTextAttributes();
    assertNotNull(attributes);
    assertNull(attributes.getBackgroundColor());
    assertNull(attributes.getForegroundColor());
  }

  public void testPersistentHighlighterUpdateOnPartialDocumentUpdate() {
    Document document = new DocumentImpl("line0\nline1\nline2");
    MarkupModelEx model = (MarkupModelEx)DocumentMarkupModel.forDocument(document, getProject(), true);
    RangeHighlighterEx highlighter = model.addPersistentLineHighlighter(2, 0, null);
    new WriteCommandAction<Void>(getProject()){
      @Override
      protected void run(@NotNull Result<Void> result) throws Throwable {
        document.deleteString(document.getLineStartOffset(1), document.getTextLength());
      }
    }.execute();
    assertTrue(highlighter.isValid());
    assertEquals(6, highlighter.getStartOffset());
  }

    public static class TestAnnotator implements Annotator {
    @Override
    public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
      holder.createInfoAnnotation(element, null);
    }
  }
}

@@ -21,9 +21,9 @@
 -201
-5
+6
  Jet
@@ -1919,16 +1919,75 @@
 awt.*;%0A%0A
+import static org.assertj.core.api.Assertions.assertThat;%0A%0A
 /**%0A * @
@@ -2098,17 +2098,16 @@
 tCase %7B%0A
-%0A
   public
@@ -3046,18 +3046,13 @@
 sert
-Equals(1, 
+That(
 high
@@ -3063,15 +3063,19 @@
 ters
-.length
+).hasSize(1
 );%0A 
@@ -3152,23 +3152,20 @@
   assert
-NotNull
+That
 (attribu
@@ -3168,16 +3168,28 @@
 ributes)
+.isNotNull()
 ;%0A    as
@@ -3184,36 +3184,36 @@
 ll();%0A    assert
-Null
+That
 (attributes.getB
@@ -3225,24 +3225,33 @@
 oundColor())
+.isNull()
 ;%0A    assert
@@ -3250,20 +3250,20 @@
   assert
-Null
+That
 (attribu
@@ -3287,16 +3287,25 @@
 Color())
+.isNull()
 ;%0A  %7D%0A%0A 
@@ -3977,18 +3977,16 @@
 );%0A  %7D%0A%0A
-  
   public

use assertThat