package permissions.dispatcher;

import org.intellij.lang.annotations.Language;
import org.junit.Test;

import static com.android.tools.lint.checks.infrastructure.TestFiles.java;
import static com.android.tools.lint.checks.infrastructure.TestLintTask.lint;
import static permissions.dispatcher.Utils.SOURCE_PATH;
import static permissions.dispatcher.Utils.getOnNeedsPermission;

public final class CallNeedsPermissionDetectorTest {

    @Test
    public void callNeedsPermissionMethod() throws Exception {

        @Language("JAVA") String onNeeds = getOnNeedsPermission();

        @Language("JAVA") String foo = ""
                + "package com.example;\n"
                + "public class Foo {\n"
                + "public void someMethod() {"
                + "Baz baz = new Baz();\n"
                + "baz.fooBar();  "
                + "}\n"
                + "}";

        @Language("JAVA") String baz = ""
                + "package com.example;\n"
                + "import permissions.dispatcher.NeedsPermission;\n"
                + "public class Baz {\n"
                + "@NeedsPermission(\"Test\")\n"
                + "public void fooBar() {\n"
                + "}\n"
                + "}";

        String expectedText = ""
                + "src/com/example/Foo.java:4: Error: Trying to access permission-protected method directly "
                + "["
                + CallNeedsPermissionDetector.ISSUE.getId()
                + "]\n"
                + "baz.fooBar();  }\n"
                + "~~~~~~~~~~~~\n"
                + "1 errors, 0 warnings\n";

        lint()
                .files(
                        java(SOURCE_PATH + "NeedsPermission.java", onNeeds),
                        java("src/com/example/Foo.java", foo),
                        java("src/com/example/Baz.java", baz))
                .issues(CallNeedsPermissionDetector.ISSUE)
                .run()
                .expect(expectedText)
                .expectErrorCount(1)
                .expectWarningCount(0);
    }

    @Test
    public void callNeedsPermissionMethodNoError() throws Exception {

        @Language("JAVA") String onNeeds = getOnNeedsPermission();

        @Language("JAVA") String foo = ""
                + "package com.example;\n"
                + "public class Foo {\n"
                + "public void someMethod() {"
                + "Baz baz = new Baz();\n"
                + "baz.noFooBar();\n"
                + "}\n"
                + "}";

        @Language("JAVA") String baz = ""
                + "package com.example;\n"
                + "public class Baz {\n"
                + "public void noFooBar() {\n"
                + "}\n"
                + "}";

        lint()
                .files(
                        java(SOURCE_PATH + "NeedsPermission.java", onNeeds),
                        java("src/com/example/Foo.java", foo),
                        java("src/com/example/Baz.java", baz))
                .issues(CallNeedsPermissionDetector.ISSUE)
                .run()
                .expectClean();
    }
}

@@ -2574,34 +2574,221 @@
      + %22
-public class Baz %7B
+import permissions.dispatcher.NeedsPermission;%5Cn%22%0A                + %22public class Baz %7B%5Cn%22%0A                + %22@NeedsPermission(%5C%22Test%5C%22)%5Cn%22%0A                + %22public void fooBar() %7B%5Cn%22%0A                + %22%7D
 %5Cn%22%0A    

Fix test case to more strict.