1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of 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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.renderer.impl;
17  
18  import org.eclipse.swt.SWT;
19  import org.eclipse.swt.widgets.Shell;
20  import org.seasar.eclipse.common.util.SWTUtil;
21  import org.seasar.framework.container.S2Container;
22  import org.seasar.framework.container.factory.S2ContainerFactory;
23  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
24  import org.seasar.framework.exception.ResourceNotFoundRuntimeException;
25  import org.seasar.framework.unit.S2FrameworkTestCase;
26  import org.seasar.framework.util.StringUtil;
27  import org.seasar.uruma.annotation.EventListener;
28  import org.seasar.uruma.annotation.PostOpenMethod;
29  import org.seasar.uruma.core.StandAloneUrumaStarter;
30  
31  /**
32   * レンダラのテストを行うための基底クラスです。</br>
33   * <p>
34   * 各レンダラのテストクラスは、本クラスを継承してください。</br>
35   * </p>
36   * 
37   * @author y-komori
38   */
39  public abstract class AbstractGUITest extends S2FrameworkTestCase {
40      protected StandAloneUrumaStarter uruma;
41  
42      public Shell shell;
43  
44      protected boolean result;
45  
46      /*
47       * @see junit.framework.TestCase#setUp()
48       */
49      @Override
50      protected void setUp() throws Exception {
51          uruma = StandAloneUrumaStarter.getInstance();
52          S2Container container = SingletonS2ContainerFactory.getContainer();
53          String componentName = StringUtil.decapitalize(getClass()
54                  .getSimpleName())
55                  + "Action";
56          container.register(this, componentName);
57  
58          // クラス名と同名の dicon ファイルが存在すればインクルードする
59          try {
60              S2Container child = S2ContainerFactory
61                      .create(convertPath(getClass().getSimpleName() + ".dicon"));
62              container.include(child);
63          } catch (ResourceNotFoundRuntimeException ex) {
64              // do nothing.
65          }
66  
67          result = false;
68      }
69  
70      /*
71       * @see junit.framework.TestCase#tearDown()
72       */
73      @Override
74      protected void tearDown() throws Exception {
75          StandAloneUrumaStarter.destroy();
76      }
77  
78      /**
79       * テスト画面を開きます。<br />
80       */
81      public void testRender() {
82          String path = convertPath(getClass().getSimpleName() + ".xml");
83          uruma.openWindow(path);
84          assertTrue(path, result);
85      }
86  
87      /**
88       * テスト画面オープン後に実行される処理です。<br />
89       */
90      @PostOpenMethod
91      public void postOpen() {
92          captureScreen();
93  
94          if (isAutoTestMode()) {
95              try {
96                  autoOK();
97              } catch (Exception ex) {
98                  ex.printStackTrace();
99                  fail(ex.getMessage());
100             }
101         }
102     }
103 
104     protected void captureScreen() {
105         String path = "log/" + getClass().getSimpleName() + ".png";
106         SWTUtil.saveWindowImage(shell, path, SWT.IMAGE_PNG);
107     }
108 
109     protected boolean isAutoTestMode() {
110         return Boolean.getBoolean("autoTest");
111     }
112 
113     protected void autoOK() throws Exception {
114         okButtonAction();
115     }
116 
117     /**
118      * 「OK」ボタンが押されたときに呼び出されるメソッドです。<br />
119      */
120     @EventListener(id = "okButton")
121     public void okButtonAction() {
122         shell.close();
123 
124         result = true;
125     }
126 
127     /**
128      * 「NG」ボタンが押されたときに呼び出されるメソッドです。<br />
129      */
130     @EventListener(id = "ngButton")
131     public void ngButtonAction() {
132         shell.close();
133         result = false;
134     }
135 }