Test the zip asset loader API. @author Wolfgang Christian
| 26 | * |
| 27 | */ |
| 28 | @SuppressWarnings("serial") |
| 29 | public class AssetsTest extends Test_ { |
| 30 | |
| 31 | static { |
| 32 | |
| 33 | // This declaration ensures OSPRuntime has been run, as |
| 34 | // it is where the assets are defined. |
| 35 | if (OSPRuntime.isJS) { |
| 36 | OSPLog.debug("assets=" + Assets.getInstance().toString()); |
| 37 | } |
| 38 | |
| 39 | } |
| 40 | |
| 41 | AssetsTest() { |
| 42 | if (!OSPRuntime.isJS) { |
| 43 | System.out.println("Assets are JavaScript only now."); |
| 44 | System.exit(0); |
| 45 | } |
| 46 | // test that an image that is NOT in the zip file can be loaded directly. |
| 47 | String imageName = "org/opensourcephysics/resources/cover.gif"; |
| 48 | URL url = Assets.getURLFromPath(imageName, true); |
| 49 | if (url == null) { |
| 50 | OSPLog.debug(imageName + " was not found in an asset ZIP file"); |
| 51 | url = Assets.getURLFromPath(imageName, false); |
| 52 | } |
| 53 | System.out.println("url=" + url); |
| 54 | if (url != null) { |
| 55 | ImageIcon icon = new ImageIcon(url); |
| 56 | JFrame frame = new JFrame("Asset Loader Example"); |
| 57 | IconPanel imagePanel = new IconPanel(icon); |
| 58 | imagePanel.setLayout(new BorderLayout()); |
| 59 | frame.add(imagePanel); |
| 60 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 61 | frame.setSize(400, 400); |
| 62 | frame.setVisible(true); |
| 63 | } |
| 64 | |
| 65 | // Test that we can get the ZipEntry list. |
| 66 | getFileList("osp-assets.zip"); |
| 67 | |
| 68 | // Test that we can get the bytes for cover.gif, which is not in the zip file |
| 69 | byte[] bytes = Assets.getURLContents(url); |
| 70 | |
| 71 | String magic = new String(new byte[] { bytes[0], bytes[1], bytes[2], bytes[3] }); |
| 72 | OSPLog.debug("\n\n" + magic + " " + bytes.length); |
| 73 | |
| 74 | // Test that we can get the bytes for a file that is NOT in the zip file |
| 75 | bytes = Assets.getAssetBytes(imageName); |
| 76 | assert(bytes.length == 30189); |
| 77 | |
| 78 | // Test that we can get the bytes for a file that is in the zip file |
| 79 | bytes = Assets.getAssetBytes("org/opensourcephysics/resources/display/drawing_tools.xml"); |
| 80 | assert(bytes.length == 964); |
| 81 | |
| 82 | // test with file name space |
| 83 | url = Assets.getURLFromPath("test/spacetest.zip!/Car in a loop with friction.trk"); |
| 84 | bytes = Assets.getURLContents(url); |
| 85 | assert(bytes.length == 50356); |
nothing calls this directly
no test coverage detected