| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
package org.eclipse.swtbot.generator; |
| 12 | |
|
| 13 | |
import java.io.FileWriter; |
| 14 | |
import java.io.PrintWriter; |
| 15 | |
import java.util.Set; |
| 16 | |
import java.util.TreeSet; |
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
public class HamcrestFactoryWriter implements HamcrestWriter { |
| 23 | |
|
| 24 | |
private final String packageName; |
| 25 | |
private final String shortClassName; |
| 26 | |
private final PrintWriter output; |
| 27 | |
|
| 28 | 0 | private final String newLine = "\n"; |
| 29 | |
|
| 30 | 0 | public HamcrestFactoryWriter(String packageName, String shortClassName, FileWriter fileWriter) { |
| 31 | 0 | this.packageName = packageName; |
| 32 | 0 | this.shortClassName = shortClassName; |
| 33 | 0 | output = new PrintWriter(fileWriter); |
| 34 | |
|
| 35 | 0 | } |
| 36 | |
|
| 37 | |
public void writeHeader(Set<String> imports) { |
| 38 | 0 | output.append("// Generated source. DO NOT MODIFY.").append(newLine); |
| 39 | 0 | output.append("// To add new widgets, please see README file in the generator plugin.").append(newLine); |
| 40 | 0 | output.append("package ").append(packageName).append(';').append(newLine); |
| 41 | 0 | output.append(newLine); |
| 42 | 0 | output.append(newLine); |
| 43 | 0 | writeImports(imports); |
| 44 | 0 | } |
| 45 | |
|
| 46 | |
public void beginClassDefinition() { |
| 47 | 0 | output.append(newLine); |
| 48 | 0 | output.append(newLine); |
| 49 | 0 | output.append("/**\n" + |
| 50 | |
" * This class contains convenience API to find widgets in SWTBot.\n" + |
| 51 | |
" * Most users would start off as follows: \n" + |
| 52 | |
" * \n" + |
| 53 | |
" * <pre>\n" + |
| 54 | |
" * SWTBot bot = new SWTBot();\n" + |
| 55 | |
" * \n" + |
| 56 | |
" * bot.button("hello world").click();\n" + |
| 57 | |
" * \n" + |
| 58 | |
" * // in case you have two edit buttons in two different groups\n" + |
| 59 | |
" * // say an edit button in the "Address" section,\n" + |
| 60 | |
" * // and another in "Bank Account" section, you can do the following\n" + |
| 61 | |
" * // to click on the "Edit" button on the "Bank Account" section.\n" + |
| 62 | |
" * // This is the recommended way to use SWTBot, instead of finding widgets based on its index.\n" + |
| 63 | |
" * bot.buttonInGroup("Edit", "Bank Account").click();\n" + |
| 64 | |
" * </pre>\n" + |
| 65 | |
" * \n" + |
| 66 | |
" * For finding widgets using custom matchers:\n" + |
| 67 | |
" * \n" + |
| 68 | |
" * <pre>\n" + |
| 69 | |
" * SWTBot bot = new SWTBot();\n" + |
| 70 | |
" * //\n" + |
| 71 | |
" * // find a button within the currently active shell:\n" + |
| 72 | |
" * //\n" + |
| 73 | |
" * SWTBotButton button = new SWTBotButton((Button) bot.widget(aMatcher)); // or\n" + |
| 74 | |
" * SWTBotButton button = new SWTBotButton((Button)bot.widget(aMatcher, 3)); // for the 4th widget\n" + |
| 75 | |
" * //\n" + |
| 76 | |
" * // to find a button within a particular parent composite:\n" + |
| 77 | |
" * //\n" + |
| 78 | |
" * SWTBotButton button = new SWTBotButton((Button) bot.widget(aMatcher, parentComposite)); //or\n" + |
| 79 | |
" * SWTBotButton button = new SWTBotButton((Button) bot.widget(aMatcher, parentComposite, 3)); //for the 4th widget\n" + |
| 80 | |
" * </pre>\n" + |
| 81 | |
" *\n" + |
| 82 | |
" * @version $Id$\n" + |
| 83 | |
" */\n"); |
| 84 | 0 | output.append("public class ").append(shortClassName).append(" extends SWTBotFactory {").append(newLine).append(newLine); |
| 85 | 0 | } |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
public void writeFooter() { |
| 90 | 0 | output.append(" private Matcher<? extends List> withLabel(String label) {\n" |
| 91 | |
+ " return WidgetMatcherFactory.withLabel(label, finder);\n" |
| 92 | |
+ " }\n\n"); |
| 93 | 0 | output.append('}').append(newLine); |
| 94 | 0 | } |
| 95 | |
|
| 96 | |
public void close() { |
| 97 | 0 | output.close(); |
| 98 | 0 | } |
| 99 | |
|
| 100 | |
public void flush() { |
| 101 | 0 | output.flush(); |
| 102 | 0 | } |
| 103 | |
|
| 104 | |
public void writeMethod(String method) { |
| 105 | 0 | output.append(method.toString()).append(newLine); |
| 106 | 0 | } |
| 107 | |
|
| 108 | |
private void writeImports(Set<String> imports) { |
| 109 | |
|
| 110 | 0 | imports = new TreeSet<String>(imports); |
| 111 | |
|
| 112 | 0 | imports.add("import org.eclipse.swt.SWT"); |
| 113 | |
|
| 114 | 0 | imports.add("import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException"); |
| 115 | 0 | imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic"); |
| 116 | 0 | imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withText"); |
| 117 | 0 | imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withId"); |
| 118 | 0 | imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withText"); |
| 119 | 0 | imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMessage"); |
| 120 | 0 | imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.inGroup"); |
| 121 | 0 | imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withStyle"); |
| 122 | 0 | imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withTooltip"); |
| 123 | 0 | imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.widgetOfType"); |
| 124 | 0 | imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.allOf"); |
| 125 | 0 | imports.add("import org.eclipse.swtbot.swt.finder.finders.ControlFinder"); |
| 126 | 0 | imports.add("import org.eclipse.swtbot.swt.finder.finders.ChildrenControlFinder"); |
| 127 | 0 | imports.add("import org.eclipse.swtbot.swt.finder.finders.Finder"); |
| 128 | 0 | imports.add("import org.eclipse.swtbot.swt.finder.finders.MenuFinder"); |
| 129 | 0 | imports.add("import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarButton"); |
| 130 | 0 | imports.add("import org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory"); |
| 131 | 0 | imports.add("import org.eclipse.swt.widgets.Widget"); |
| 132 | |
|
| 133 | 0 | imports.add("import org.hamcrest.Matcher"); |
| 134 | |
|
| 135 | 0 | for (String importz : imports) { |
| 136 | 0 | output.append(importz).append(";").append(newLine); |
| 137 | |
} |
| 138 | |
|
| 139 | 0 | } |
| 140 | |
|
| 141 | |
public void beginConstructors() { |
| 142 | 0 | output.append(" /**\n" + |
| 143 | |
" * Constructs a bot.\n" + |
| 144 | |
" */\n" + |
| 145 | |
" public SWTBot() {\n" + |
| 146 | |
" this(new ControlFinder(), new MenuFinder());\n" + |
| 147 | |
" }\n" + |
| 148 | |
"\n" + |
| 149 | |
" /**\n" + |
| 150 | |
" * Constructs a bot that will match the contents of the given parentWidget.\n" + |
| 151 | |
" * \n" + |
| 152 | |
" * @param parent the parent\n" + |
| 153 | |
" */\n" + |
| 154 | |
" public SWTBot(Widget parent) {\n" + |
| 155 | |
" this(new ChildrenControlFinder(parent), new MenuFinder());\n" + |
| 156 | |
" }\n" + |
| 157 | |
" /**\n" + |
| 158 | |
" * Constructs an instance of the bot using the given control finder and menu finder.\n" + |
| 159 | |
" * \n" + |
| 160 | |
" * @param controlFinder the {@link ControlFinder} used to identify and find controls.\n" + |
| 161 | |
" * @param menuFinder the {@link MenuFinder} used to find menu items.\n" + |
| 162 | |
" */\n" + |
| 163 | |
" public SWTBot(ControlFinder controlFinder, MenuFinder menuFinder) {\n" + |
| 164 | |
" this(new Finder(controlFinder, menuFinder));\n" + |
| 165 | |
" }\n" + |
| 166 | |
"\n" + |
| 167 | |
" /**\n" + |
| 168 | |
" * Constructs a bot with the given finder.\n" + |
| 169 | |
" * \n" + |
| 170 | |
" * @param finder the finder.\n" + |
| 171 | |
" */\n" + |
| 172 | |
" public SWTBot(Finder finder) {\n" + |
| 173 | |
" super(finder);\n" + |
| 174 | |
" }\n\n"); |
| 175 | 0 | } |
| 176 | |
} |