Revision: 664
Author: allain.lalonde
Date: Thu Aug  6 16:42:53 2009
Log: Broke apart tests of PHtmlView so that 1 test = 1 behaviour.
http://code.google.com/p/piccolo2d/source/detail?r=664

Modified:
   
/piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/nodes/PHtmlView.java
   
/piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/nodes/PHtmlViewTest.java

=======================================
---  
/piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/nodes/PHtmlView.java
         
Thu Aug  6 16:04:58 2009
+++  
/piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/nodes/PHtmlView.java
         
Thu Aug  6 16:42:53 2009
@@ -148,7 +148,7 @@
       */
      public PHtmlView(final String text, final Font font, final Color  
textColor) {
          label = new JLabel(text);
-        label.setFont(font);
+        label.setFont(font);
          label.setForeground(textColor);
          super.setBounds(0, 0, label.getPreferredSize().getWidth(),  
label.getPreferredSize().getHeight());
          update();
@@ -175,10 +175,16 @@
      public void setText(final String text) {
          final String oldText = label.getText();

-        label.setText(text);
-
-        update();
-        firePropertyChange(PROPERTY_CODE_TEXT, PROPERTY_TEXT, oldText,  
label.getText());
+        if (oldText == null && text == null) {
+            return;
+        }
+
+        if (oldText == null || !oldText.equals(text)) {
+            label.setText(text);
+
+            update();
+            firePropertyChange(PROPERTY_CODE_TEXT, PROPERTY_TEXT, oldText,  
label.getText());
+        }
      }

      /**
@@ -263,13 +269,15 @@
       */
      private void fitHeightToHtmlContent() {
          htmlView.setSize((float) getWidth(), 0f);
-        // float preferredWrapWidth =  
htmlView.getPreferredSpan(View.X_AXIS);
+
          float preferredWrapHeight = htmlView.getPreferredSpan(View.Y_AXIS);
          label.setPreferredSize(new Dimension((int) getWidth(), (int)  
preferredWrapHeight));

+        Font oldFont = label.getFont();
          JFrame frame = new JFrame();
          frame.getContentPane().add(label);
          frame.pack();
+        label.setFont(oldFont);

          if (getHeight() != label.getHeight()) {
              super.setBounds(getX(), getY(), getWidth(), label.getHeight());
@@ -302,9 +310,10 @@
          super.paint(paintContext);

          if (label.getWidth() != 0 && label.getHeight() != 0) {
-            final Graphics2D g2 = paintContext.getGraphics();
-
+            paintContext.pushClip(getBounds());
+            final Graphics2D g2 = paintContext.getGraphics();
              htmlView.paint(g2, getBounds().getBounds());
+            paintContext.popClip(getBounds());
          }
      }

=======================================
---  
/piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/nodes/PHtmlViewTest.java
     
Thu Aug  6 16:04:58 2009
+++  
/piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/nodes/PHtmlViewTest.java
     
Thu Aug  6 16:42:53 2009
@@ -53,68 +53,66 @@
          mockListener = new MockPropertyChangeListener();
      }

-    public void testConstructor() {
-        PHtmlView html0 = new PHtmlView();
-        assertNotNull(html0);
-        assertEquals(null, html0.getText());
-        assertEquals(PHtmlView.DEFAULT_FONT, html0.getFont());
-        assertEquals(PHtmlView.DEFAULT_TEXT_COLOR, html0.getTextColor());
-
-        PHtmlView html1 = new PHtmlView(null);
-        assertNotNull(html1);
-        assertEquals(null, html1.getText());
-        assertEquals(PHtmlView.DEFAULT_FONT, html1.getFont());
-        assertEquals(PHtmlView.DEFAULT_TEXT_COLOR, html1.getTextColor());
-
-        PHtmlView html2 = new PHtmlView("not html");
-        assertNotNull(html2);
-        assertEquals("not html", html2.getText());
-        assertEquals(PHtmlView.DEFAULT_FONT, html2.getFont());
-        assertEquals(PHtmlView.DEFAULT_TEXT_COLOR, html2.getTextColor());
-
-        PHtmlView html3 = new PHtmlView("<html><body><p>html  
text</p></body></html>");
-        assertNotNull(html3);
-        assertEquals("<html><body><p>html text</p></body></html>",  
html3.getText());
-        assertEquals(PHtmlView.DEFAULT_FONT, html3.getFont());
-        assertEquals(PHtmlView.DEFAULT_TEXT_COLOR, html3.getTextColor());
-
+    public void testConstructorRetainsHtmlWithCSSStyling() {
+        PHtmlView html = new PHtmlView("<html><body><p  
style=\"font-family: sans-serif; font-color: blue\">html  
text</p></body></html>");
+        assertEquals("<html><body><p style=\"font-family: sans-serif;  
font-color: blue\">html text</p></body></html>", html.getText());
+    }
+
+    public void testConstructorRetainsAllParametersWhenRealHtml() {
+        PHtmlView html = new PHtmlView("<html><body><p>html  
text</p></body></html>");
+        assertEquals("<html><body><p>html text</p></body></html>",  
html.getText());
+    }
+
+    public void testConstructorRetainsAllParameters() {
          Font font = new Font("Serif", Font.PLAIN, 12);
-        PHtmlView html4 = new PHtmlView("not html", font, Color.RED);
-        assertNotNull(html4);
-        assertEquals("not html", html4.getText());
-        assertEquals(font, html4.getFont());
-        assertEquals(Color.RED, html4.getTextColor());
-
-        PHtmlView html5 = new PHtmlView("<html><body><p>html  
text</p></body></html>", font, Color.RED);
-        assertNotNull(html5);
-        assertEquals("<html><body><p>html text</p></body></html>",  
html5.getText());
-        assertEquals(font, html5.getFont());
-        assertEquals(Color.RED, html5.getTextColor());
-
-        PHtmlView html6 = new PHtmlView("<html><body><p  
style=\"font-family: sans-serif; font-color: blue\">html  
text</p></body></html>", font, Color.RED);
-        assertNotNull(html6);
-        assertEquals("<html><body><p style=\"font-family: sans-serif;  
font-color: blue\">html text</p></body></html>", html6.getText());
-        assertEquals(font, html6.getFont());
-        assertEquals(Color.RED, html6.getTextColor());
-
-        PHtmlView html7 = new PHtmlView("not html", null, Color.RED);
-        assertNotNull(html7);
-        assertEquals("not html", html7.getText());
-        assertEquals(null, html7.getFont());
-        assertEquals(Color.RED, html7.getTextColor());
-
-        PHtmlView html8 = new PHtmlView("not html", font, null);
-        assertNotNull(html8);
-        assertEquals("not html", html8.getText());
-        assertEquals(font, html8.getFont());
-        assertEquals(null, html8.getTextColor());
-
+        PHtmlView html = new PHtmlView("not html", font, Color.RED);
+        assertEquals("not html", html.getText());
+        assertEquals(font, html.getFont());
+        assertEquals(Color.RED, html.getTextColor());
+    }
+
+    public void testConstructorAcceptsRealisticHtml() {
+        PHtmlView html = new PHtmlView("<html><body><p>html  
text</p></body></html>");
+        assertNotNull(html);
+        assertEquals("<html><body><p>html text</p></body></html>",  
html.getText());
+    }
+
+    public void testConstructorAcceptsNonHtmlText() {
+        PHtmlView html = new PHtmlView("not html");
+        assertNotNull(html);
+        assertEquals("not html", html.getText());
+        assertEquals(PHtmlView.DEFAULT_FONT, html.getFont());
+        assertEquals(PHtmlView.DEFAULT_TEXT_COLOR, html.getTextColor());
+    }
+
+    public void testConstructorAcceptsNullHtml() {
+        PHtmlView html = new PHtmlView(null);
+        assertEquals(null, html.getText());
+    }
+
+    public void testDefaultConstructorHasExpectedDefaults() {
+        PHtmlView html = new PHtmlView();
+        assertEquals(null, html.getText());
+        assertEquals(PHtmlView.DEFAULT_FONT, html.getFont());
+        assertEquals(PHtmlView.DEFAULT_TEXT_COLOR, html.getTextColor());
+    }
+
+    public void testConstructorAcceptsNullFontAndColor() {
          PHtmlView html9 = new PHtmlView("not html", null, null);
-        assertNotNull(html9);
-        assertEquals("not html", html9.getText());
          assertEquals(null, html9.getFont());
          assertEquals(null, html9.getTextColor());
      }
+
+    public void testConstructorAcceptsNullColor() {
+        Font font = new Font("Serif", Font.PLAIN, 12);
+        PHtmlView html = new PHtmlView("not html", font, null);
+        assertEquals(null, html.getTextColor());
+    }
+
+    public void testConstructorAcceptsNullFont() {
+        PHtmlView html = new PHtmlView("not html", null, Color.RED);
+        assertEquals(null, html.getFont());
+    }

      public void testGetClickedAddressReturnsSingleQuotedAddress() {
          PHtmlView html = new PHtmlView("<a  
href='http://www.testing.com'>testing</a>");
@@ -206,7 +204,7 @@

      public void testSetHtmlFiresEventOnChangeOnly() {
          PHtmlView html = new PHtmlView();
-        html.addPropertyChangeListener(mockListener);
+        html.addPropertyChangeListener(PHtmlView.PROPERTY_TEXT,  
mockListener);
          html.setText("testing");
          assertEquals(1, mockListener.getPropertyChangeCount());
          assertEquals(PHtmlView.PROPERTY_TEXT,  
mockListener.getPropertyChange(0).getPropertyName());

--~--~---------~--~----~------------~-------~--~----~
Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to