/**
   * Called to create an instance of <code>TestEnvironment</code> with an object to test and related
   * objects. Subclasses should implement this method to provide the implementation and related
   * objects. The method is called from <code>getTestEnvironment()</code>.
   *
   * @param tParam test parameters
   * @param log writer to log information while testing
   * @see TestEnvironment
   * @see #getTestEnvironment
   */
  @Override
  protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log)
      throws Exception {

    log.println("creating a test environment");

    if (xTextDoc != null) {
      xTextDoc.dispose(); // get a soffice factory object
    }
    SOfficeFactory SOF = SOfficeFactory.getFactory(tParam.getMSF());

    log.println("creating a text document");
    xTextDoc = SOF.createTextDoc(null);

    util.utils.waitForEventIdle(tParam.getMSF());

    XModel aModel1 = UnoRuntime.queryInterface(XModel.class, xTextDoc);

    XController secondController = aModel1.getCurrentController();

    XDispatchProvider aProv = UnoRuntime.queryInterface(XDispatchProvider.class, secondController);

    XDispatch getting = null;

    log.println("opening DatasourceBrowser");
    URL the_url = new URL();
    the_url.Complete = ".component:DB/DataSourceBrowser";
    getting = aProv.queryDispatch(the_url, "_beamer", 12);
    PropertyValue[] noArgs = new PropertyValue[0];
    getting.dispatch(the_url, noArgs);

    util.utils.waitForEventIdle(tParam.getMSF());

    XFrame the_frame1 = the_Desk.getCurrentFrame();

    if (the_frame1 == null) {
      log.println("Current frame was not found !!!");
    }

    XFrame the_frame2 = the_frame1.findFrame("_beamer", 4);

    the_frame2.setName("DatasourceBrowser");

    XInterface oObj = null;

    final XSelectionSupplier xSelect =
        UnoRuntime.queryInterface(XSelectionSupplier.class, the_frame2.getController());

    PropertyValue[] params =
        new PropertyValue[] {new PropertyValue(), new PropertyValue(), new PropertyValue()};
    params[0].Name = "DataSourceName";
    params[0].Value = "Bibliography";
    params[1].Name = "CommandType";
    params[1].Value = Integer.valueOf(com.sun.star.sdb.CommandType.TABLE);
    params[2].Name = "Command";
    params[2].Value = "biblio";

    final PropertyValue[] fParams = params;

    util.utils.waitForEventIdle(tParam.getMSF());

    XWindow xWindow = secondController.getFrame().getContainerWindow();

    XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow);

    AccessibilityTools.printAccessibleTree(
        log, xRoot, tParam.getBool(util.PropertyName.DEBUG_IS_ACTIVE));

    oObj =
        AccessibilityTools.getAccessibleObjectForRole(
            xRoot, AccessibleRole.PANEL, "", "AccessibleBrowseBox");

    log.println("ImplementationName: " + util.utils.getImplName(oObj));

    TestEnvironment tEnv = new TestEnvironment(oObj);

    tEnv.addObjRelation(
        "EventProducer",
        new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {

          public void fireEvent() {
            try {
              xSelect.select(fParams);
            } catch (com.sun.star.lang.IllegalArgumentException e) {
              e.printStackTrace();
            }
          }
        });

    return tEnv;
  }
  /**
   * Creating a Testenvironment for the interfaces to be tested.
   * For object creation first a
   * <code>com.sun.star.form.component.GridControl<code> instance
   * is added to the <code>ControlShape</code>. Then this model's
   * control is retrieved.
   *
   *     Object relations created :
   * <ul>
   *  <li> <code>'GRAPHICS'</code> for
   *      {@link ifc.awt._XView} test : <code>XGraphics</code>
   *   object different that belong to the object tested.</li>
   *  <li> <code>'CONTEXT'</code> for
   *      {@link ifc.awt._XControl} </li>
   *  <li> <code>'WINPEER'</code> for
   *      {@link ifc.awt._XControl} </li>
   *  <li> <code>'TOOLKIT'</code> for
   *      {@link ifc.awt._XControl} </li>
   *  <li> <code>'MODEL'</code> for
   *      {@link ifc.awt._XControl} </li>
   *  <li> <code>'XWindow.AnotherWindow'</code> for
   *      {@link ifc.awt._XWindow} for switching focus.</li>
   *  <li> <code>'XDispatch.URL'</code> for
   *      {@link ifc.frame._XDispatch} the url which moves
   *      DB cursor to the next row (".uno:FormSlots/moveToNext").</li>
   *  <li> <code>'XContainer.Container'</code> for
   *      {@link ifc.container._XContainer} as the component created
   *      doesn't support <code>XContainer</code> itself, but
   *      it is supported by its model. So this model is passed.</li>
   *  <li> <code>'INSTANCE'</code> for
   *      {@link ifc.container._XContainer} the instance to be
   *      inserted into collection. Is a column instance.</li>
   * </ul>
   */
  protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
    XInterface oObj = null;
    XWindowPeer the_win = null;
    XToolkit the_kit = null;
    XDevice aDevice = null;
    XGraphics aGraphic = null;
    XPropertySet aControl = null;
    XPropertySet aControl2 = null;
    XPropertySet aControl3 = null;
    XPropertySet aControl4 = null;
    XGridColumnFactory columns = null;

    // Insert a ControlShape and get the ControlModel
    XControlShape aShape = createGrid(xTextDoc, 3000, 4500, 15000, 10000);

    XControlModel the_Model = aShape.getControl();

    WriterTools.getDrawPage(xTextDoc).add(aShape);

    XLoadable formLoader = FormTools.bindForm(xTextDoc);

    // Try to query XControlAccess
    XControlAccess the_access =
        UnoRuntime.queryInterface(XControlAccess.class, xTextDoc.getCurrentController());

    try {
      columns = UnoRuntime.queryInterface(XGridColumnFactory.class, the_Model);
      aControl = columns.createColumn("TextField");
      aControl.setPropertyValue("DataField", "Identifier");
      aControl.setPropertyValue("Label", "Identifier");
      aControl2 = columns.createColumn("TextField");
      aControl2.setPropertyValue("DataField", "Publisher");
      aControl2.setPropertyValue("Label", "Publisher");
      aControl3 = columns.createColumn("TextField");
      aControl3.setPropertyValue("DataField", "Author");
      aControl3.setPropertyValue("Label", "Author");
      aControl4 = columns.createColumn("TextField");
      aControl4.setPropertyValue("DataField", "Title");
      aControl4.setPropertyValue("Label", "Title");
    } catch (com.sun.star.lang.IllegalArgumentException e) {
      // Some exception occurs.FAILED
      log.println("!!! Couldn't create instance : " + e);
      throw new StatusException("Can't create column instances.", e);
    } catch (com.sun.star.lang.WrappedTargetException e) {
      // Some exception occurs.FAILED
      log.println("!!! Couldn't create instance : " + e);
      throw new StatusException("Can't create column instances.", e);
    } catch (com.sun.star.beans.PropertyVetoException e) {
      // Some exception occurs.FAILED
      log.println("!!! Couldn't create instance : " + e);
      throw new StatusException("Can't create column instances.", e);
    } catch (com.sun.star.beans.UnknownPropertyException e) {
      // Some exception occurs.FAILED
      log.println("!!! Couldn't create instance : " + e);
      throw new StatusException("Can't create column instances.", e);
    }

    XNameContainer aContainer = UnoRuntime.queryInterface(XNameContainer.class, the_Model);

    try {
      aContainer.insertByName("First", aControl);
      aContainer.insertByName("Second", aControl2);
    } catch (com.sun.star.uno.Exception e) {
      log.println("!!! Could't insert column Instance");
      e.printStackTrace(log);
      throw new StatusException("Can't insert columns", e);
    }

    // now get the OGridControl
    try {
      oObj = the_access.getControl(the_Model);
      the_win = the_access.getControl(the_Model).getPeer();
      the_kit = the_win.getToolkit();
      aDevice = the_kit.createScreenCompatibleDevice(200, 200);
      aGraphic = aDevice.createGraphics();
    } catch (com.sun.star.uno.Exception e) {
      log.println("Couldn't get GridControl");
      e.printStackTrace(log);
      throw new StatusException("Couldn't get GridControl", e);
    }

    // creating another window
    aShape = FormTools.createControlShape(xTextDoc, 3000, 4500, 15000, 10000, "TextField");

    WriterTools.getDrawPage(xTextDoc).add(aShape);

    the_Model = aShape.getControl();

    // Try to query XControlAccess
    the_access = UnoRuntime.queryInterface(XControlAccess.class, xTextDoc.getCurrentController());

    // now get the TextControl
    XWindow win = null;
    Object cntrl = null;

    try {
      cntrl = the_access.getControl(the_Model);
      win = UnoRuntime.queryInterface(XWindow.class, cntrl);
    } catch (com.sun.star.uno.Exception e) {
      log.println("Couldn't get Control");
      e.printStackTrace(log);
      throw new StatusException("Couldn't get Control", e);
    }

    log.println("creating a new environment for object");

    TestEnvironment tEnv = new TestEnvironment(oObj);

    // Relations for XSelectionSupplier
    tEnv.addObjRelation(
        "Selections", new Object[] {new Object[] {new Integer(0)}, new Object[] {new Integer(1)}});
    tEnv.addObjRelation(
        "Comparer",
        new Comparator<Integer>() {
          public int compare(Integer o1, Integer o2) {
            return o1.compareTo(o2);
          }
        });

    // Realtion for XContainer
    tEnv.addObjRelation("XContainer.Container", aContainer);
    tEnv.addObjRelation("INSTANCE", aControl3);
    tEnv.addObjRelation("INSTANCE2", aControl4);

    // Adding ObjRelation for XView
    tEnv.addObjRelation("GRAPHICS", aGraphic);

    // Adding ObjRelation for XControl
    tEnv.addObjRelation("CONTEXT", xTextDoc);
    tEnv.addObjRelation("WINPEER", the_win);
    tEnv.addObjRelation("TOOLKIT", the_kit);
    tEnv.addObjRelation("MODEL", the_Model);

    // Adding relation for XWindow
    tEnv.addObjRelation("XWindow.AnotherWindow", win);

    // Adding relation for XDispatch
    URL url = new URL();
    url.Complete = ".uno:FormSlots/moveToNext";

    // url.Complete = ".uno:GridSlots/RowHeight";
    // url.Complete = ".uno:GridSlots/RowHeight" ;
    tEnv.addObjRelation("XDispatch.URL", url);

    log.println("ImplName: " + utils.getImplName(oObj));

    FormTools.switchDesignOf((XMultiServiceFactory) Param.getMSF(), xTextDoc);

    // adding relation for XUpdateBroadcaster
    final XInterface ctrl = oObj;
    final XLoadable formLoaderF = formLoader;
    final XPropertySet ps = UnoRuntime.queryInterface(XPropertySet.class, aControl2);
    tEnv.addObjRelation(
        "XUpdateBroadcaster.Checker",
        new ifc.form._XUpdateBroadcaster.UpdateChecker() {
          private String lastText = "";

          public void update() throws com.sun.star.uno.Exception {
            if (!formLoaderF.isLoaded()) {
              formLoaderF.load();
            }

            lastText = "_" + ps.getPropertyValue("Text");
            ps.setPropertyValue("Text", lastText);
          }

          public void commit() throws com.sun.star.sdbc.SQLException {
            XBoundComponent bound = UnoRuntime.queryInterface(XBoundComponent.class, ctrl);
            XResultSetUpdate update =
                UnoRuntime.queryInterface(XResultSetUpdate.class, formLoaderF);

            bound.commit();
            update.updateRow();
          }

          public boolean wasCommited() throws com.sun.star.uno.Exception {
            String getS = (String) ps.getPropertyValue("Text");

            return lastText.equals(getS);
          }
        });

    return tEnv;
  } // finish method getTestEnvironment