Changeset 39038


Ignore:
Timestamp:
2024-05-21T23:19:11+12:00 (3 weeks ago)
Author:
anupama
Message:

Working with GLI's metadata table is proving very hard as the GLI code has automated its behaviour: whenever the user clicks in a metavalue cell with a value and presses enter, a dummy next cell is created. This appears to be causing all kinds of side effects to my testing code when I try to work with JTableFixture and when I try to work with GLI's behaviour as it is (I thought of pasting strings in there using the clipboard, but that doesn't work in this case).

Location:
gs3-extensions/testing/trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • gs3-extensions/testing/trunk/src/README

    r39033 r39038  
    3838- VERY USEFUL BASICS: https://docs.oracle.com/javase/6/docs/api/javax/swing/package-summary.html#threading
    3939- EDT and AssertJ Swing: https://joel-costigliola.github.io/assertj/assertj-swing-edt.html
    40 
     40- Possibl useful: https://assertj-swing.readthedocs.io/en/latest/assertj-swing-basics/#base-test-class
    4141
    4242NOTES on moving any code into Runnable class inside Gatherer.invokeOnDispatchThread (which does SwingUtilities.invokeAndWait/invokeLater):
  • gs3-extensions/testing/trunk/src/src/gstests/tutorials/RunGLITest.java

    r39036 r39038  
    327327             "Simple image collection");
    328328   
    329     loadCollection("backdrop");
     329    //loadCollection("backdrop");
    330330    PAUSE(3);
    331331    */ 
     
    359359       
    360360    PAUSE(3);
    361     */
     361   
    362362   
    363363    switchToPane(CREATE_PANE);
     
    366366    //previewCollection();
    367367    buildAndPreviewCollection(MAX_BUILD_TIMEOUT);
    368 
    369     switchToPane(ENRICH_PANE);
    370     PAUSE(1);
     368    */
     369    switchToPane(ENRICH_PANE); 
     370    PAUSE(1);   
    371371    //openCollectionPath("Bear.jpg", CLICK);
    372     exitGLI(); // save popup changes
     372    //changeToMetadataSet("Dublin Core Metadata Element Set");
     373
     374    setMeta("Bear.jpg", "dc.Title", "Bear");
     375    //addMeta("Bear.jpg", "dc.Title", "Toto");
     376    //addMeta("Bear.jpg", "dc.Title", "Lala");
     377   
     378      String[] metavalues = {"lala", "pinky", "dodo"};
     379      addMetaValsToCurrentDoc("dc.Title", metavalues);
     380    //setMeta("Cat.jpg", "dc.Title", "Cat");
     381    //setMeta("Cheetah.jpg", "dc.Title", "Cheetah");
     382    PAUSE(2);
     383    //exitGLI(); // needed, to save warning popup changes
    373384    }
    374385   
  • gs3-extensions/testing/trunk/src/src/org/greenstone/gsdl3/testing/GSGUITestingUtil.java

    r39036 r39038  
    22
    33import java.io.File;
     4import java.util.Collection;
     5import java.util.Iterator;
    46import java.util.Map;
     7import java.util.AbstractMap.SimpleEntry;
     8import java.util.concurrent.TimeUnit;
    59import java.util.regex.*;
    610import java.awt.Component;
     11import java.awt.Toolkit;
    712import java.awt.event.InputEvent;
    813import javax.swing.*;
     14import java.awt.datatransfer.*; // Clipboard, Transferable, DataFlavor;
     15
    916
    1017// only JUnit and JAssert-Swing, no Selenium in this class
     
    1219import org.assertj.swing.fixture.*;
    1320import org.assertj.swing.timing.Timeout;
    14 
     21import org.assertj.swing.data.TableCell;
     22   
    1523import org.assertj.swing.timing.Pause;
    1624import org.assertj.swing.timing.Condition;
    17 import java.util.concurrent.TimeUnit;
     25import org.assertj.swing.util.Platform;
    1826   
    1927// GLI imports
     
    3543//import org.assertj.swing.core.matcher.DialogMatcher; // withTitle
    3644//import org.assertj.swing.core.matcher.FrameMatcher; // withTitle
    37 
     45import org.assertj.swing.core.KeyPressInfo;
    3846
    3947// static imports (allows shorthand global calls directly to the static functions imported)
     
    4654import static org.assertj.swing.finder.WindowFinder.findFrame;
    4755import static org.assertj.swing.finder.WindowFinder.findDialog;
    48 
     56import static org.assertj.swing.core.KeyPressInfo.*; // declares keycodes
    4957
    5058/*
     
    989997    /********************* ENRICH PANE ******************/
    990998    // docPath can be just docName or collectionSubfolder/Subfolder2/docName
    991     public static void addMeta(String docPath, Map metanamesToValues) {}
     999    //public static void addMeta(String docPath, Map metanamesToValues) {}
    9921000    public static void addFolderLevelMeta(String folderPath, Map metanamesToValues) {}
    9931001
     1002    public static void changeToMetadataSet(String toSet) {
     1003    window.button("EnrichPane.external_split.GLIButton.Manage Metadata Sets...").click();
     1004    //DialogFixture mdsetDialog = findDialog("MetadataSetDialog").using(robot);
     1005    DialogFixture mdsetDialog = window.dialog("MetadataSetDialog");
     1006    // Add, select toSet, Add, Close
     1007    mdsetDialog.button("MetadataSetDialog.add_button").click();
     1008    // Name of next dialog that pops up ends up being AddMetadataSetPrompt.add_set_dialog
     1009    DialogFixture addMetaDialog = mdsetDialog.dialog("AddMetadataSetPrompt.add_set_dialog");//window.dialog("AddMetadataSetPrompt.add_set_dialog");//findDialog(DialogMatcher.withTitle(Dictionary.get("MetadataSetDialog.Add_Title"))).using(robot);
     1010    JListFixture mds_list = addMetaDialog.list("AddMetadataSetPrompt.available_sets_list");
     1011    PAUSE(3);
     1012    mds_list.selectItem(Pattern.compile("^"+toSet+".*$"));
     1013    PAUSE(2);
     1014    addMetaDialog.button("AddMetadataSetPrompt.add_button").click();
     1015    PAUSE(1);
     1016    mdsetDialog.button("MetadataSetDialog.close_button").click();
     1017    }
     1018
     1019    public static void setMeta(String documentPath, String metaname, String metavalue) {
     1020    openCollectionPath(documentPath, CLICK);
     1021    JTableFixture metatable = window.table("MetadataValueTable");
     1022    JTableCellFixture cell = metatable.cell(metaname);
     1023    int row = cell.row();
     1024    int col = cell.column();
     1025    TableCell valueCell = TableCell.row(row).column(col+1);
     1026    metatable.enterValue(valueCell, metavalue);
     1027    System.err.println("#### Metaname column: " + col);
     1028   
     1029    }
     1030    /*
     1031    public static void addMeta(String documentPath, String metaname, String metavalue) {
     1032    openCollectionPath(documentPath, CLICK);
     1033    JTableFixture metatable = window.table("MetadataValueTable");
     1034    JTableCellFixture cell = metatable.cell(metaname); // First matching metaname
     1035    //GLI has programmed behaviour that makes this code funny:
     1036    //It pre positions the cursor for you to enter the Next metaname
     1037    // But in assertJ swing testing code we need to access cells manually.
     1038    // But always clicking on value cell for metaname and hitting RETURN allows
     1039    // adding meta in correct cell. But we can only call enterValue() on row and column
     1040    // of a cell we know. So we need to find the table(row,col) of the cell GLI positioned
     1041    // us at after we press click and RETURN and then enter the value there.
     1042    cell.click();
     1043    //PAUSE(0.5);
     1044    metatable.pressKey(java.awt.event.KeyEvent.VK_ENTER);
     1045    PAUSE(0.5);
     1046
     1047
     1048    enterStringInFocusedElement(metatable, metavalue);
     1049    //openCollectionPath(documentPath, CLICK);
     1050    }
     1051    */
     1052    public static void enterStringInFocusedElement(AbstractComponentFixture widget,
     1053                           String theString)
     1054    {
     1055    // BAD IDEA: looping through characters and mapping them to keycodes won't work if unicode
     1056    // https://stackoverflow.com/questions/196830/what-is-the-easiest-best-most-correct-way-to-iterate-through-the-characters-of-a
     1057    // pseudocode was:
     1058    //for each character in metavalue
     1059    //   widget.pressKey(c);
     1060   
     1061
     1062    // Copy paste should work if I can copy to clipboard
     1063    // https://stackoverflow.com/questions/3591945/copying-to-the-clipboard-in-java
     1064    System.err.println("### Copying to clipboard: " + theString);
     1065    putOnClipboard(theString);
     1066    System.err.println("### Pasting from clipboard.");
     1067    pasteClipboard(widget);
     1068   
     1069    }
     1070
     1071    public static void putOnClipboard(String theString) {
     1072    // https://stackoverflow.com/questions/3591945/copying-to-the-clipboard-in-java
     1073    StringSelection selection = new StringSelection(theString);
     1074    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
     1075    clipboard.setContents(selection, selection);
     1076
     1077    }
     1078    //http://www.avajava.com/tutorials/lessons/how-do-i-get-a-string-from-the-clipboard.html
     1079    public static String getStringFromClipboard() {
     1080    String result = "Clipboard retrieval failed";
     1081    try {
     1082        Toolkit toolkit = Toolkit.getDefaultToolkit();
     1083        Clipboard clipboard = toolkit.getSystemClipboard();
     1084        result = (String) clipboard.getData(DataFlavor.stringFlavor);
     1085    } catch(Exception e) {
     1086        System.err.println("Failed to retrieve from clipboard. Exception: " + e.getMessage());
     1087    }
     1088    return result;
     1089    }
     1090    public static void pasteClipboard(AbstractComponentFixture widget) {
     1091    //https://joel-costigliola.github.io/assertj/swing/api/org/assertj/swing/core/KeyPressInfo.html
     1092    //  Specify that 'SHIFT' + 'R' should be pressed:
     1093    // // import static org.assertj.swing.fixture.KeyPressInfo.*;
     1094    // KeyPressInfo i = key(VK_R).modifiers(SHIFT_MASK);
     1095    // For platform-safe mask pressing (e.g. 'Control' in Windows or 'Command' in MacOS) use Platform.controlOrCommandMask().
     1096
     1097    // press Ctrl/Command+V on widget
     1098    // TODO:
     1099    //KeyPressInfo kpi = keyCode(java.awt.event.KeyEvent.VK_V).modifiers(Platform.controlOrCommandMask());
     1100    KeyPressInfo kpi = keyCode(java.awt.event.KeyEvent.VK_V).modifiers(InputEvent.CTRL_MASK);
     1101    widget.pressAndReleaseKey(kpi);
     1102    }
     1103   
     1104    /*
     1105    public static void addMeta(String documentPath, String metaname, String metavalue) {
     1106    openCollectionPath(documentPath, CLICK);
     1107    JTableFixture metatable = window.table("MetadataValueTable");
     1108    JTableCellFixture cell = metatable.cell(metaname); // First matching metaname
     1109    //GLI has programmed behaviour that makes this code funny:
     1110    //It pre positions the cursor for you to enter the Next metaname
     1111    // But in assertJ swing testing code we need to access cells manually.
     1112    // But always clicking on value cell for metaname and hitting RETURN allows
     1113    // adding meta in correct cell. But we can only call enterValue() on row and column
     1114    // of a cell we know. So we need to find the table(row,col) of the cell GLI positioned
     1115    // us at after we press click and RETURN and then enter the value there.
     1116    cell.click();
     1117    PAUSE(0.5);
     1118    metatable.pressKey(java.awt.event.KeyEvent.VK_ENTER);
     1119    PAUSE(0.5);
     1120    // table was modified by GLI, get it again?
     1121    metatable = window.table("MetadataValueTable");
     1122    // GLI should now automatically have positioned us in the cell we'd like to enter data
     1123    // GLI would have created a new row with the same metaname and empty metavalue field.
     1124    // Work out what the correct row of it is by calculating the row of the last matching
     1125    // metaname cell.
     1126    int row = cell.row();
     1127    int col = cell.column();
     1128    // Look for Last matching metaname
     1129    TableCell metanameCell;
     1130    do {       
     1131        metanameCell = TableCell.row(row).column(col);
     1132        row++; // check next row's metaname
     1133    } while(metatable.valueAt(TableCell.row(row).column(col)).equals(metaname));
     1134   
     1135    row = metatable.cell(metanameCell).row(); // found row with the last matching metaname
     1136
     1137   
     1138   
     1139    // metavalue is one column past from metaname
     1140    TableCell valueCell = TableCell.row(row).column(col+1);
     1141    System.err.println("#### row " + row + " should have no metaval: " + metatable.valueAt(valueCell));
     1142
     1143    //System.err.println("#### Metaname column: " + col + " value: " + metatable.valueAt(valueCell));
     1144    metatable.enterValue(valueCell, metavalue);
     1145    }
     1146    */
     1147   
     1148    // WORKS!!!
     1149    public static void addMeta(String documentPath, String metaname, String metavalue) {
     1150    openCollectionPath(documentPath, CLICK);
     1151    JTableFixture metatable = window.table("MetadataValueTable");
     1152    JTableCellFixture cell = metatable.cell(metaname); // First matching metaname
     1153
     1154    int row = cell.row();
     1155    int col = cell.column();
     1156    // Look for Last matching metaname
     1157    TableCell metanameCell;
     1158    do {       
     1159        metanameCell = TableCell.row(row).column(col);
     1160        row++; // check next row's metaname
     1161
     1162        //row++; // check next row's metaname
     1163        //metanameCell = TableCell.row(row).column(col);
     1164    } while(metatable.valueAt(TableCell.row(row).column(col)).equals(metaname));
     1165    row = metatable.cell(metanameCell).row(); // found row with the last matching metaname
     1166
     1167    // On Enter press below, GLI will create a new empty row for the metaname for
     1168    // the new metadata to be added, so overcounting by 1 in the loop is what is needed
     1169    // and we *don't* want to go one row back. It just ends up working out.
     1170    //do {     
     1171    //    row++; // check next row's metaname
     1172    //    metanameCell = TableCell.row(row).column(col);
     1173    //} while(metatable.valueAt(metanameCell).equals(metaname));   
     1174
     1175    //metatable.cell(metanameCell).click();
     1176    // metavalue is one column past metaname
     1177    TableCell valueCell = TableCell.row(row).column(col+1);
     1178    //metatable.enterValue(valueCell, metavalue);
     1179    System.err.println("#### Metaname column: " + col + " value: " + metatable.valueAt(valueCell));
     1180    //click on cell and hit enter to add meta       
     1181    metatable.cell(valueCell).click();
     1182    //PAUSE(0.5);
     1183    metatable.pressKey(java.awt.event.KeyEvent.VK_ENTER);
     1184    //PAUSE(0.5);
     1185    valueCell = TableCell.row(row+1).column(col+1);
     1186    metatable.enterValue(valueCell, metavalue);
     1187    //metatable.pressKey(java.awt.event.KeyEvent.VK_ENTER); // To make value stick?
     1188    }
     1189    // NOT WORKING
     1190    public static void addMetaValsToCurrentDoc(String metaname, String[] metavalues) {
     1191    JTableFixture metatable = window.table("MetadataValueTable");
     1192    JTableCellFixture cell = metatable.cell(metaname); // First matching metaname
     1193    int row = cell.row();
     1194    int col = cell.column();
     1195    TableCell valueCell = TableCell.row(row).column(col+1);
     1196
     1197    metatable.cell(valueCell).click();
     1198    metatable.pressKey(java.awt.event.KeyEvent.VK_ENTER);
     1199    for(String metavalue : metavalues) {
     1200        System.err.println("### Copying to clipboard: " + metavalue);
     1201        putOnClipboard(metavalue);
     1202       
     1203        System.err.println("### Pasting from clipboard: " + getStringFromClipboard());
     1204        metatable.pressKey(java.awt.event.KeyEvent.VK_A);
     1205        metatable.pressKey(java.awt.event.KeyEvent.VK_N);
     1206        metatable.pressKey(java.awt.event.KeyEvent.VK_U);
     1207        //pasteClipboard(metatable);
     1208        //KeyPressInfo kpi = keyCode(java.awt.event.KeyEvent.VK_V).modifiers(InputEvent.SHIFT_MASK);
     1209        KeyPressInfo kpi = keyCode(java.awt.event.KeyEvent.VK_V).modifiers(InputEvent.CTRL_MASK);
     1210        metatable.pressAndReleaseKey(kpi);
     1211        metatable.pressKey(java.awt.event.KeyEvent.VK_ENTER);
     1212        //PAUSE(0.5);
     1213    }
     1214
     1215    /*
     1216    int row = cell.row();
     1217    int col = cell.column();
     1218    // Look for Last matching metaname
     1219    TableCell metanameCell;
     1220    do {       
     1221        metanameCell = TableCell.row(row).column(col);
     1222        row++; // check next row's metaname
     1223
     1224        //row++; // check next row's metaname
     1225        //metanameCell = TableCell.row(row).column(col);
     1226    } while(metatable.valueAt(TableCell.row(row).column(col)).equals(metaname));
     1227    row = metatable.cell(metanameCell).row(); // found row with the last matching metaname
     1228
     1229    // On Enter press below, GLI will create a new empty row for the metaname for
     1230    // the new metadata to be added, so overcounting by 1 in the loop is what is needed
     1231    // and we *don't* want to go one row back. It just ends up working out.
     1232    //do {     
     1233    //    row++; // check next row's metaname
     1234    //    metanameCell = TableCell.row(row).column(col);
     1235    //} while(metatable.valueAt(metanameCell).equals(metaname));   
     1236
     1237    //metatable.cell(metanameCell).click();
     1238    // metavalue is one column past metaname
     1239    TableCell valueCell = TableCell.row(row).column(col+1);
     1240    //metatable.enterValue(valueCell, metavalue);
     1241    System.err.println("#### Metaname column: " + col + " value: " + metatable.valueAt(valueCell));
     1242    //click on cell and hit enter to add meta       
     1243    metatable.cell(valueCell).click();
     1244    metatable.pressKey(java.awt.event.KeyEvent.VK_ENTER);
     1245    metatable.enterValue(valueCell, metavalues[0]);
     1246   
     1247    for(String metavalue : metavalues) {
     1248       
     1249        // MIXED THIS UP IN TRYING TO GET IT TO WORK
     1250          metatable.enterValue(valueCell, metavalue);
     1251        //need to refresh table model, as a row was added
     1252        metatable = window.table("MetadataValueTable");
     1253        row++;     
     1254        valueCell = TableCell.row(row).column(col+1);
     1255        metatable.cell(valueCell).click();
     1256        metatable.pressKey(java.awt.event.KeyEvent.VK_ENTER);
     1257       
     1258    }
     1259    */
     1260    /*
     1261    //PAUSE(0.5);
     1262    for(String metavalue : metavalues) {
     1263        row = metatable.cell(metanameCell).row();
     1264        metatable.cell(metanameCell).click();
     1265       
     1266        // do something with new row created automatically by GLI
     1267        // metatable.valueAt(TableCell.row(row+1).column(col));
     1268           
     1269        //PAUSE(0.5);
     1270        //row++;
     1271        // get current row again
     1272        metanameCell = TableCell.row(row).column(col);
     1273        if(metatable.valueAt(metanameCell).equals(metaname)) {
     1274        valueCell = TableCell.row(row).column(col+1);
     1275        metatable.enterValue(valueCell, metavalue);
     1276        }
     1277        metatable.pressKey(java.awt.event.KeyEvent.VK_ENTER);
     1278    }*/
     1279    }
     1280   
     1281    public static void addMeta(String documentPath, Collection<SimpleEntry<String, Object>> metadata) {
     1282    openCollectionPath(documentPath, CLICK);
     1283    JTableFixture metatable = window.table("MetadataValueTable");
     1284
     1285    Iterator<SimpleEntry<String, Object>> it = metadata.iterator();
     1286    while(it.hasNext()) {
     1287        SimpleEntry<String, Object> entry = it.next();
     1288        String metaname = entry.getKey();       
     1289        Object value = entry.getValue();
     1290
     1291        if(value instanceof String) {
     1292        String[] metavals = new String[1];
     1293        metavals[0] = (String)value;
     1294        addMetaValsToCurrentDoc(metaname, metavals);
     1295        } else {
     1296        Collection<String> valueList = (Collection<String>)value;
     1297        String[] metavals = new String[valueList.size()];
     1298        metavals = valueList.toArray(metavals);
     1299        addMetaValsToCurrentDoc(metaname, metavals);
     1300        }
     1301    }   
     1302    }
     1303   
     1304    /*
     1305    public static void addMeta(String documentPath, String metaname, String metavalue) {
     1306    openCollectionPath(documentPath, CLICK);
     1307    JTableFixture metatable = window.table("MetadataValueTable");
     1308    JTableCellFixture cell = metatable.cell(metaname);
     1309    int row = cell.row();
     1310    int col = cell.column();
     1311   
     1312    TableCell valueCell = TableCell.row(row).column(col+1);
     1313    while(!metatable.valueAt(valueCell).equals("")) {
     1314        //click on cell and hit enter to add meta       
     1315        metatable.cell(valueCell).click();
     1316        //PAUSE(2);
     1317        metatable.pressKey(java.awt.event.KeyEvent.VK_ENTER);
     1318        //PAUSE(2);
     1319        row = row+1;
     1320        valueCell = TableCell.row(row).column(col+1);
     1321        System.err.println("*** |" + metatable.valueAt(valueCell) + "|");
     1322    }
     1323   
     1324    metatable.enterValue(valueCell, metavalue);
     1325    System.err.println("#### Metaname column: " + col);
     1326   
     1327    }
     1328    */
     1329    /*
     1330    public static void setMeta(String document, Map<String, Object> metadataMap) {
     1331    //Set<Map.Entry<String, Object>> metadataSet = metadataMap.entrySet();
     1332    //Iterator<Map.Entry<String, Object>> i = metadataSet.iterator();
     1333    //i.next();
     1334    Set<String> metanames = metadataMap.keySet();
     1335    for(String metaname : metanames) {
     1336        Object value = metadataMap.get(metaname);
     1337        if(value instanceof String) {
     1338        setMeta(document, metaname, (String)value);
     1339        } else {
     1340        List<String> valueList = (List<String>)value;
     1341        for(String metavalue : valueList) {
     1342            setMeta(document, metaname, metavalue);
     1343        }
     1344        }
     1345    }   
     1346    }
     1347    */
     1348
     1349    // https://stackoverflow.com/questions/6121246/list-of-entries-how-to-add-a-new-entry
     1350    // Google: Java List of SimpeEntry   
     1351    public static void setMeta(String document, Collection<SimpleEntry<String, Object>> metadata) {
     1352    //Set<Map.Entry<String, Object>> metadataSet = metadataMap.entrySet();
     1353    //Iterator<Map.Entry<String, Object>> i = metadataSet.iterator();
     1354    //i.next();
     1355    Iterator<SimpleEntry<String, Object>> metaEntries = metadata.iterator();
     1356    while(metaEntries.hasNext()) {
     1357        SimpleEntry<String, Object> meta = metaEntries.next();
     1358        String metaname = meta.getKey();
     1359        Object value = meta.getValue();
     1360        if(value instanceof String) {
     1361        setMeta(document, metaname, (String)value);
     1362        } else {
     1363        Collection<String> valueList = (Collection<String>)value;
     1364        for(String metavalue : valueList) {
     1365            setMeta(document, metaname, metavalue);
     1366        }
     1367        }
     1368    }   
     1369    }
    9941370    /********************* DOWNLOAD PANE ******************/
    9951371    // TODO: more functions needed here: e.g. serverInfo?
Note: See TracChangeset for help on using the changeset viewer.