Coverage Report - org.eclipse.swtbot.generator.HamcrestFactoryWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
HamcrestFactoryWriter
0%
0/53
0%
0/2
1.111
 
 1  
 /*******************************************************************************
 2  
  * Copyright (c) 2008 Ketan Padegaonkar and others.
 3  
  * All rights reserved. This program and the accompanying materials
 4  
  * are made available under the terms of the Eclipse Public License v1.0
 5  
  * which accompanies this distribution, and is available at
 6  
  * http://www.eclipse.org/legal/epl-v10.html
 7  
  * 
 8  
  * Contributors:
 9  
  *     Ketan Padegaonkar - initial API and implementation
 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  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 20  
  * @version $Id$
 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"; //$NON-NLS-1$
 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); //$NON-NLS-1$
 39  0
                 output.append("// To add new widgets, please see README file in the generator plugin.").append(newLine); //$NON-NLS-1$
 40  0
                 output.append("package ").append(packageName).append(';').append(newLine); //$NON-NLS-1$
 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" + //$NON-NLS-1$
 50  
                                 " * This class contains convenience API to find widgets in SWTBot.\n" + //$NON-NLS-1$
 51  
                                 " * Most users would start off as follows: \n" + //$NON-NLS-1$
 52  
                                 " * \n" + //$NON-NLS-1$
 53  
                                 " * <pre>\n" + //$NON-NLS-1$
 54  
                                 " *    SWTBot bot = new SWTBot();\n" + //$NON-NLS-1$
 55  
                                 " *    \n" + //$NON-NLS-1$
 56  
                                 " *    bot.button(&quot;hello world&quot;).click();\n" + //$NON-NLS-1$
 57  
                                 " *    \n" + //$NON-NLS-1$
 58  
                                 " *    // in case you have two edit buttons in two different groups\n" + //$NON-NLS-1$
 59  
                                 " *    // say an edit button in the &quot;Address&quot; section,\n" + //$NON-NLS-1$
 60  
                                 " *    // and another in &quot;Bank Account&quot; section, you can do the following\n" + //$NON-NLS-1$
 61  
                                 " *    // to click on the &quot;Edit&quot; button on the &quot;Bank Account&quot; section.\n" + //$NON-NLS-1$
 62  
                                 " *    // This is the recommended way to use SWTBot, instead of finding widgets based on its index.\n" + //$NON-NLS-1$
 63  
                                 " *    bot.buttonInGroup(&quot;Edit&quot;, &quot;Bank Account&quot;).click();\n" + //$NON-NLS-1$
 64  
                                 " * </pre>\n" + //$NON-NLS-1$
 65  
                                 " * \n" + //$NON-NLS-1$
 66  
                                 " * For finding widgets using custom matchers:\n" + //$NON-NLS-1$
 67  
                                 " * \n" + //$NON-NLS-1$
 68  
                                 " * <pre>\n" + //$NON-NLS-1$
 69  
                                 " *    SWTBot bot = new SWTBot();\n" + //$NON-NLS-1$
 70  
                                 " *    //\n" + //$NON-NLS-1$
 71  
                                 " *    // find a button within the currently active shell:\n" + //$NON-NLS-1$
 72  
                                 " *    //\n" + //$NON-NLS-1$
 73  
                                 " *    SWTBotButton button = new SWTBotButton((Button) bot.widget(aMatcher)); // or\n" + //$NON-NLS-1$
 74  
                                 " *    SWTBotButton button = new SWTBotButton((Button)bot.widget(aMatcher, 3)); // for the 4th widget\n" + //$NON-NLS-1$
 75  
                                 " *    //\n" + //$NON-NLS-1$
 76  
                                 " *    // to find a button within a particular parent composite:\n" + //$NON-NLS-1$
 77  
                                 " *    //\n" + //$NON-NLS-1$
 78  
                                 " *    SWTBotButton button = new SWTBotButton((Button) bot.widget(aMatcher, parentComposite)); //or\n" + //$NON-NLS-1$
 79  
                                 " *    SWTBotButton button = new SWTBotButton((Button) bot.widget(aMatcher, parentComposite, 3)); //for the 4th widget\n" + //$NON-NLS-1$
 80  
                                 " * </pre>\n" + //$NON-NLS-1$
 81  
                                 " *\n" + //$NON-NLS-1$
 82  
                                 " * @version $Id$\n" + //$NON-NLS-1$
 83  
                                 " */\n"); //$NON-NLS-1$
 84  0
                 output.append("public class ").append(shortClassName).append(" extends SWTBotFactory {").append(newLine).append(newLine); //$NON-NLS-1$ //$NON-NLS-2$
 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"); //$NON-NLS-1$
 113  
 
 114  0
                 imports.add("import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException"); //$NON-NLS-1$
 115  0
                 imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic"); //$NON-NLS-1$
 116  0
                 imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withText"); //$NON-NLS-1$
 117  0
                 imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withId"); //$NON-NLS-1$
 118  0
                 imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withText"); //$NON-NLS-1$
 119  0
                 imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMessage"); //$NON-NLS-1$
 120  0
                 imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.inGroup"); //$NON-NLS-1$
 121  0
                 imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withStyle"); //$NON-NLS-1$
 122  0
                 imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withTooltip"); //$NON-NLS-1$
 123  0
                 imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.widgetOfType"); //$NON-NLS-1$
 124  0
                 imports.add("import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.allOf"); //$NON-NLS-1$
 125  0
                 imports.add("import org.eclipse.swtbot.swt.finder.finders.ControlFinder"); //$NON-NLS-1$
 126  0
                 imports.add("import org.eclipse.swtbot.swt.finder.finders.ChildrenControlFinder"); //$NON-NLS-1$
 127  0
                 imports.add("import org.eclipse.swtbot.swt.finder.finders.Finder"); //$NON-NLS-1$
 128  0
                 imports.add("import org.eclipse.swtbot.swt.finder.finders.MenuFinder"); //$NON-NLS-1$
 129  0
                 imports.add("import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarButton"); //$NON-NLS-1$
 130  0
                 imports.add("import org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory"); //$NON-NLS-1$
 131  0
                 imports.add("import org.eclipse.swt.widgets.Widget"); //$NON-NLS-1$
 132  
 
 133  0
                 imports.add("import org.hamcrest.Matcher"); //$NON-NLS-1$
 134  
 
 135  0
                 for (String importz : imports) {
 136  0
                         output.append(importz).append(";").append(newLine); //$NON-NLS-1$
 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  
 }