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.

MissingHierarchyDetection.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2000-2016 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.tests.application;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.List;
  20. import java.util.logging.Handler;
  21. import java.util.logging.LogRecord;
  22. import java.util.logging.Logger;
  23. import com.vaadin.server.VaadinRequest;
  24. import com.vaadin.server.VaadinSession;
  25. import com.vaadin.tests.components.AbstractReindeerTestUIWithLog;
  26. import com.vaadin.ui.Button;
  27. import com.vaadin.ui.Button.ClickEvent;
  28. import com.vaadin.ui.Component;
  29. import com.vaadin.ui.CssLayout;
  30. import com.vaadin.ui.Label;
  31. import com.vaadin.ui.SelectiveRenderer;
  32. public class MissingHierarchyDetection extends AbstractReindeerTestUIWithLog {
  33. private boolean isChildRendered = true;
  34. private BrokenCssLayout brokenLayout = new BrokenCssLayout();
  35. private CssLayout normalLayout = new CssLayout(
  36. new Label("Normal layout child"));
  37. private List<LogRecord> pendingErrors = Collections
  38. .synchronizedList(new ArrayList<>());
  39. public class BrokenCssLayout extends CssLayout
  40. implements SelectiveRenderer {
  41. public BrokenCssLayout() {
  42. setCaption("Broken layout");
  43. Label label = new Label("Child component");
  44. label.setId("label");
  45. addComponent(label);
  46. }
  47. @Override
  48. public boolean isRendered(Component childComponent) {
  49. return isChildRendered;
  50. }
  51. }
  52. @Override
  53. protected void setup(VaadinRequest request) {
  54. // Catch log messages so we can see if there is an error
  55. Logger vaadinSessionLogger = Logger
  56. .getLogger(VaadinSession.class.getName());
  57. vaadinSessionLogger.addHandler(new Handler() {
  58. @Override
  59. public void publish(LogRecord record) {
  60. if (record.getThrown() instanceof AssertionError) {
  61. pendingErrors.add(record);
  62. vaadinSessionLogger.removeHandler(this);
  63. }
  64. }
  65. @Override
  66. public void flush() {
  67. }
  68. @Override
  69. public void close() throws SecurityException {
  70. }
  71. });
  72. addComponent(brokenLayout);
  73. addComponent(normalLayout);
  74. addComponent(new Button("Toggle properly", new Button.ClickListener() {
  75. @Override
  76. public void buttonClick(ClickEvent event) {
  77. toggle(true);
  78. }
  79. }));
  80. addComponent(
  81. new Button("Toggle improperly", new Button.ClickListener() {
  82. @Override
  83. public void buttonClick(ClickEvent event) {
  84. toggle(false);
  85. }
  86. }));
  87. addComponent(new Button("Check for errors", new Button.ClickListener() {
  88. @Override
  89. public void buttonClick(ClickEvent event) {
  90. if (!pendingErrors.isEmpty()) {
  91. log(pendingErrors.remove(0).getThrown().getMessage());
  92. } else {
  93. log("No errors");
  94. }
  95. }
  96. }));
  97. }
  98. private void toggle(boolean properly) {
  99. isChildRendered = !isChildRendered;
  100. if (properly) {
  101. brokenLayout.markAsDirtyRecursive();
  102. }
  103. normalLayout.getComponent(0).setVisible(isChildRendered);
  104. // Must also have a state change of the layout to trigger special case
  105. // related to optimizations
  106. normalLayout.setCaption("With child: " + isChildRendered);
  107. }
  108. }