You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestBenchSection.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright 2000-2013 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.debug.internal;
  17. import com.google.gwt.event.dom.client.ClickEvent;
  18. import com.google.gwt.event.dom.client.ClickHandler;
  19. import com.google.gwt.event.dom.client.KeyCodes;
  20. import com.google.gwt.event.dom.client.MouseOutEvent;
  21. import com.google.gwt.event.dom.client.MouseOutHandler;
  22. import com.google.gwt.event.dom.client.MouseOverEvent;
  23. import com.google.gwt.event.dom.client.MouseOverHandler;
  24. import com.google.gwt.event.shared.HandlerRegistration;
  25. import com.google.gwt.user.client.Element;
  26. import com.google.gwt.user.client.Event;
  27. import com.google.gwt.user.client.Event.NativePreviewEvent;
  28. import com.google.gwt.user.client.Event.NativePreviewHandler;
  29. import com.google.gwt.user.client.ui.Button;
  30. import com.google.gwt.user.client.ui.FlowPanel;
  31. import com.google.gwt.user.client.ui.HTML;
  32. import com.google.gwt.user.client.ui.RootPanel;
  33. import com.google.gwt.user.client.ui.Widget;
  34. import com.vaadin.client.ApplicationConfiguration;
  35. import com.vaadin.client.ApplicationConnection;
  36. import com.vaadin.client.ComponentConnector;
  37. import com.vaadin.client.ServerConnector;
  38. import com.vaadin.client.Util;
  39. import com.vaadin.client.ValueMap;
  40. import com.vaadin.client.componentlocator.ComponentLocator;
  41. /**
  42. * Provides functionality for picking selectors for Vaadin TestBench.
  43. *
  44. * @since 7.1.4
  45. * @author Vaadin Ltd
  46. */
  47. public class TestBenchSection implements Section {
  48. /**
  49. * Selector widget showing a selector in a program-usable form.
  50. */
  51. private static class SelectorWidget extends HTML {
  52. private static int selectorIndex = 1;
  53. final private String path;
  54. public SelectorWidget(final String path) {
  55. this.path = path;
  56. String html = "<div class=\""
  57. + VDebugWindow.STYLENAME
  58. + "-selector\"><span class=\"tb-selector\">"
  59. + Util.escapeHTML("WebElement element" + (selectorIndex++)
  60. + " = getDriver().findElement(By.vaadin(\"" + path
  61. + "\"));") + "</span></div>";
  62. setHTML(html);
  63. addMouseOverHandler(new MouseOverHandler() {
  64. @Override
  65. public void onMouseOver(MouseOverEvent event) {
  66. for (ApplicationConnection a : ApplicationConfiguration
  67. .getRunningApplications()) {
  68. Element element = new ComponentLocator(a)
  69. .getElementByPath(SelectorWidget.this.path);
  70. ComponentConnector connector = Util
  71. .getConnectorForElement(a, a.getUIConnector()
  72. .getWidget(), element);
  73. if (connector == null) {
  74. connector = Util.getConnectorForElement(a,
  75. RootPanel.get(), element);
  76. }
  77. if (connector != null) {
  78. Highlight.showOnly(connector);
  79. break;
  80. }
  81. }
  82. }
  83. });
  84. addMouseOutHandler(new MouseOutHandler() {
  85. @Override
  86. public void onMouseOut(MouseOutEvent event) {
  87. Highlight.hideAll();
  88. }
  89. });
  90. }
  91. }
  92. private final DebugButton tabButton = new DebugButton(Icon.SELECTOR,
  93. "Pick Vaadin TestBench selectors");
  94. private final FlowPanel content = new FlowPanel();
  95. private final HierarchyPanel hierarchyPanel = new HierarchyPanel();
  96. private final FlowPanel selectorPanel = new FlowPanel();
  97. private final FlowPanel controls = new FlowPanel();
  98. private final Button find = new DebugButton(Icon.HIGHLIGHT,
  99. "Select a component on the page to inspect it");
  100. private final Button refreshHierarchy = new DebugButton(Icon.HIERARCHY,
  101. "Refresh the connector hierarchy tree");
  102. private HandlerRegistration highlightModeRegistration = null;
  103. public TestBenchSection() {
  104. controls.add(refreshHierarchy);
  105. refreshHierarchy.setStylePrimaryName(VDebugWindow.STYLENAME_BUTTON);
  106. refreshHierarchy.addClickHandler(new ClickHandler() {
  107. @Override
  108. public void onClick(ClickEvent event) {
  109. hierarchyPanel.update();
  110. }
  111. });
  112. controls.add(find);
  113. find.setStylePrimaryName(VDebugWindow.STYLENAME_BUTTON);
  114. find.addClickHandler(new ClickHandler() {
  115. @Override
  116. public void onClick(ClickEvent event) {
  117. toggleFind();
  118. }
  119. });
  120. hierarchyPanel.addListener(new SelectConnectorListener() {
  121. @Override
  122. public void select(ServerConnector connector, Element element) {
  123. pickSelector(connector, element);
  124. }
  125. });
  126. content.setStylePrimaryName(VDebugWindow.STYLENAME + "-testbench");
  127. content.add(hierarchyPanel);
  128. content.add(selectorPanel);
  129. }
  130. @Override
  131. public DebugButton getTabButton() {
  132. return tabButton;
  133. }
  134. @Override
  135. public Widget getControls() {
  136. return controls;
  137. }
  138. @Override
  139. public Widget getContent() {
  140. return content;
  141. }
  142. @Override
  143. public void show() {
  144. }
  145. @Override
  146. public void hide() {
  147. stopFind();
  148. }
  149. @Override
  150. public void meta(ApplicationConnection ac, ValueMap meta) {
  151. // NOP
  152. }
  153. @Override
  154. public void uidl(ApplicationConnection ac, ValueMap uidl) {
  155. // NOP
  156. }
  157. private boolean isFindMode() {
  158. return (highlightModeRegistration != null);
  159. }
  160. private void toggleFind() {
  161. if (isFindMode()) {
  162. stopFind();
  163. } else {
  164. startFind();
  165. }
  166. }
  167. private void startFind() {
  168. Highlight.hideAll();
  169. if (!isFindMode()) {
  170. highlightModeRegistration = Event
  171. .addNativePreviewHandler(highlightModeHandler);
  172. find.addStyleDependentName(VDebugWindow.STYLENAME_ACTIVE);
  173. }
  174. }
  175. private void stopFind() {
  176. if (isFindMode()) {
  177. highlightModeRegistration.removeHandler();
  178. highlightModeRegistration = null;
  179. find.removeStyleDependentName(VDebugWindow.STYLENAME_ACTIVE);
  180. }
  181. }
  182. private void pickSelector(ServerConnector connector, Element element) {
  183. String path = findTestBenchSelector(connector, element);
  184. if (null != path && !path.isEmpty()) {
  185. selectorPanel.add(new SelectorWidget(path));
  186. }
  187. }
  188. private String findTestBenchSelector(ServerConnector connector,
  189. Element element) {
  190. String path = null;
  191. ApplicationConnection connection = connector.getConnection();
  192. if (connection != null) {
  193. if (null == element) {
  194. // try to find the root element of the connector
  195. if (connector instanceof ComponentConnector) {
  196. Widget widget = ((ComponentConnector) connector)
  197. .getWidget();
  198. if (widget != null) {
  199. element = widget.getElement();
  200. }
  201. }
  202. }
  203. if (null != element) {
  204. path = new ComponentLocator(connection)
  205. .getPathForElement(element);
  206. }
  207. }
  208. return path;
  209. }
  210. private final NativePreviewHandler highlightModeHandler = new NativePreviewHandler() {
  211. @Override
  212. public void onPreviewNativeEvent(NativePreviewEvent event) {
  213. if (event.getTypeInt() == Event.ONKEYDOWN
  214. && event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ESCAPE) {
  215. stopFind();
  216. Highlight.hideAll();
  217. return;
  218. }
  219. if (event.getTypeInt() == Event.ONMOUSEMOVE
  220. || event.getTypeInt() == Event.ONCLICK) {
  221. Element eventTarget = Util.getElementFromPoint(event
  222. .getNativeEvent().getClientX(), event.getNativeEvent()
  223. .getClientY());
  224. if (VDebugWindow.get().getElement().isOrHasChild(eventTarget)) {
  225. return;
  226. }
  227. // make sure that not finding the highlight element only
  228. Highlight.hideAll();
  229. eventTarget = Util.getElementFromPoint(event.getNativeEvent()
  230. .getClientX(), event.getNativeEvent().getClientY());
  231. ComponentConnector connector = findConnector(eventTarget);
  232. if (event.getTypeInt() == Event.ONMOUSEMOVE) {
  233. if (connector != null) {
  234. Highlight.showOnly(connector);
  235. event.cancel();
  236. event.consume();
  237. event.getNativeEvent().stopPropagation();
  238. return;
  239. }
  240. } else if (event.getTypeInt() == Event.ONCLICK) {
  241. event.cancel();
  242. event.consume();
  243. event.getNativeEvent().stopPropagation();
  244. if (connector != null) {
  245. Highlight.showOnly(connector);
  246. pickSelector(connector, eventTarget);
  247. return;
  248. }
  249. }
  250. }
  251. event.cancel();
  252. }
  253. };
  254. private ComponentConnector findConnector(Element element) {
  255. for (ApplicationConnection a : ApplicationConfiguration
  256. .getRunningApplications()) {
  257. ComponentConnector connector = Util.getConnectorForElement(a, a
  258. .getUIConnector().getWidget(), element);
  259. if (connector == null) {
  260. connector = Util.getConnectorForElement(a, RootPanel.get(),
  261. element);
  262. }
  263. if (connector != null) {
  264. return connector;
  265. }
  266. }
  267. return null;
  268. }
  269. }