[IFT] Split Java Declaration lesson for general Declaration and Usages lesson and...
[idea/contrib.git] / ide-features-trainer / src / training / learn / lesson / java / navigation / JavaInheritanceHierarchyLesson.kt
1 // Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package training.learn.lesson.java.navigation
3
4 import com.intellij.codeInsight.CodeInsightBundle
5 import com.intellij.icons.AllIcons
6 import com.intellij.ide.IdeBundle
7 import com.intellij.java.analysis.JavaAnalysisBundle
8 import com.intellij.openapi.wm.ToolWindowId
9 import com.intellij.openapi.wm.ToolWindowManager
10 import com.intellij.openapi.wm.impl.content.BaseLabel
11 import com.intellij.testGuiFramework.framework.GuiTestUtil
12 import com.intellij.testGuiFramework.util.Key
13 import com.intellij.ui.InplaceButton
14 import com.intellij.ui.UIBundle
15 import training.commands.kotlin.TaskContext
16 import training.commands.kotlin.TaskRuntimeContext
17 import training.learn.LessonsBundle
18 import training.learn.interfaces.Module
19 import training.learn.lesson.kimpl.KLesson
20 import training.learn.lesson.kimpl.LessonContext
21 import training.learn.lesson.kimpl.LessonUtil
22 import training.learn.lesson.kimpl.closeAllFindTabs
23
24 class JavaInheritanceHierarchyLesson(module: Module)
25   : KLesson("java.inheritance.hierarchy.lesson", LessonsBundle.message("java.inheritance.hierarchy.lesson.name"), module, "JAVA") {
26   override val existedFile: String = "src/InheritanceHierarchySample.java"
27
28   override val lessonContent: LessonContext.() -> Unit = {
29     caret("foo(demo)")
30
31     actionTask("GotoImplementation") {
32       LessonsBundle.message("java.inheritance.hierarchy.goto.implementation", action(it), code("SomeInterface#foo"))
33     }
34
35     task {
36       text(LessonsBundle.message("java.inheritance.hierarchy.choose.any.implementation", LessonUtil.rawEnter()))
37
38       stateCheck {
39         (virtualFile.name == "DerivedClass1.java" || virtualFile.name == "DerivedClass2.java") && atDeclarationPosition()
40       }
41
42       test {
43         Thread.sleep(1000)
44         GuiTestUtil.shortcut(Key.ENTER)
45       }
46     }
47
48     task("GotoSuperMethod") {
49       text(LessonsBundle.message("java.inheritance.hierarchy.navigate.to.base", action(it), icon(AllIcons.Gutter.ImplementingMethod)))
50       stateCheck {
51         virtualFile.name == "SomeInterface.java" && atDeclarationPosition()
52       }
53
54       test { actions(it) }
55     }
56
57     task("GotoImplementation") {
58       text(LessonsBundle.message("java.inheritance.hierarchy.invoke.implementations.again", icon(AllIcons.Gutter.ImplementedMethod),
59                                  action(it)))
60       triggerByUiComponentAndHighlight { ui: InplaceButton ->
61         ui.toolTipText == IdeBundle.message("show.in.find.window.button.name")
62       }
63
64       test { actions(it) }
65     }
66
67     task {
68       before {
69         closeAllFindTabs()
70       }
71       text(LessonsBundle.message("java.inheritance.hierarchy.open.in.find.tool.window", findToolWindow(),
72                                  icon(ToolWindowManager.getInstance(project).getLocationIcon(ToolWindowId.FIND, AllIcons.General.Pin_tab))))
73       triggerByUiComponentAndHighlight(highlightBorder = false, highlightInside = false) { ui: BaseLabel ->
74         ui.text == (CodeInsightBundle.message("goto.implementation.findUsages.title", "foo")) ||
75         ui.text == (JavaAnalysisBundle.message("navigate.to.overridden.methods.title", "foo"))
76       }
77       test {
78         ideFrame {
79           val target = previous.ui!!
80           jComponent(target).click()
81           jComponent(target).click() // for some magic reason one click sometimes doesn't work :(
82         }
83       }
84     }
85
86     actionTask("HideActiveWindow") {
87       LessonsBundle.message("java.inheritance.hierarchy.hide.find.tool.window", action(it), findToolWindow())
88     }
89
90     actionTask("MethodHierarchy") {
91       LessonsBundle.message("java.inheritance.hierarchy.open.method.hierarchy", action(it))
92     }
93
94     actionTask("HideActiveWindow") {
95       LessonsBundle.message("java.inheritance.hierarchy.hide.method.hierarchy", hierarchyToolWindow(), action(it))
96     }
97
98     actionTask("TypeHierarchy") {
99       LessonsBundle.message("java.inheritance.hierarchy.open.class.hierarchy", action(it))
100     }
101
102     task {
103       text(LessonsBundle.message("java.inheritance.hierarchy.last.note",
104                                  action("GotoImplementation"),
105                                  action("GotoSuperMethod"),
106                                  action("MethodHierarchy"),
107                                  action("TypeHierarchy"),
108                                  action("GotoAction"),
109                                  strong("hierarchy")))
110     }
111   }
112
113   private fun TaskRuntimeContext.atDeclarationPosition(): Boolean {
114     return editor.document.charsSequence.let {
115       it.subSequence(editor.caretModel.currentCaret.offset, it.length).startsWith("foo(FileStructureDemo demo)")
116     }
117   }
118
119   private fun TaskContext.findToolWindow() = strong(UIBundle.message("tool.window.name.find"))
120   private fun TaskContext.hierarchyToolWindow() = strong(UIBundle.message("tool.window.name.hierarchy"))
121 }