b2g/components/test/mochitest/test_filepicker_path.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/b2g/components/test/mochitest/test_filepicker_path.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,135 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=949944
     1.8 +-->
     1.9 +<head>
    1.10 +<meta charset="utf-8">
    1.11 +<title>Permission Prompt Test</title>
    1.12 +<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    1.13 +<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.14 +</head>
    1.15 +<body onload="processTestCase()">
    1.16 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=949944"> [B2G][Helix][Browser][Wallpaper] use new File([blob], filename) to return a blob with filename when picking</a>
    1.17 +<script type="application/javascript">
    1.18 +
    1.19 +'use strict';
    1.20 +
    1.21 +var testCases = [
    1.22 +  // case 1: returns blob with name
    1.23 +  { pickedResult: { success: true,
    1.24 +                    result: {
    1.25 +                              type: 'text/plain',
    1.26 +                              blob: new Blob(['1234567890'],
    1.27 +                                             { type: 'text/plain' }),
    1.28 +                              name: 'test1.txt'
    1.29 +                            }
    1.30 +                },
    1.31 +    fileName: 'test1.txt' },
    1.32 +  // case 2: returns blob without name
    1.33 +  { pickedResult: { success: true,
    1.34 +                    result: {
    1.35 +                              type: 'text/plain',
    1.36 +                              blob: new Blob(['1234567890'],
    1.37 +                                             { type: 'text/plain' })
    1.38 +                            }
    1.39 +                },
    1.40 +    fileName: 'blob.txt' },
    1.41 +  // case 3: returns blob with full path name
    1.42 +  { pickedResult: { success: true,
    1.43 +                    result: {
    1.44 +                              type: 'text/plain',
    1.45 +                              blob: new Blob(['1234567890'],
    1.46 +                                             { type: 'text/plain' }),
    1.47 +                              name: '/full/path/test3.txt'
    1.48 +                            }
    1.49 +                },
    1.50 +    fileName: 'test3.txt' },
    1.51 +  // case 4: returns blob relative path name
    1.52 +  { pickedResult: { success: true,
    1.53 +                    result: {
    1.54 +                              type: 'text/plain',
    1.55 +                              blob: new Blob(['1234567890'],
    1.56 +                                             { type: 'text/plain' }),
    1.57 +                              name: 'relative/path/test4.txt'
    1.58 +                            }
    1.59 +                },
    1.60 +    fileName: 'test4.txt' },
    1.61 +  // case 5: returns file with name
    1.62 +  { pickedResult: { success: true,
    1.63 +                    result: {
    1.64 +                              type: 'text/plain',
    1.65 +                              blob: new File(['1234567890'],
    1.66 +                                             'useless-name.txt',
    1.67 +                                             { type: 'text/plain' }),
    1.68 +                              name: 'test5.txt'
    1.69 +                            }
    1.70 +                },
    1.71 +    fileName: 'test5.txt'},
    1.72 +  // case 6: returns file without name. This case may fail because we
    1.73 +  //         need to make sure the DOMFile can be sent through
    1.74 +  //         sendAsyncMessage API.
    1.75 +  { pickedResult: { success: true,
    1.76 +                    result: {
    1.77 +                              type: 'text/plain',
    1.78 +                              blob: new File(['1234567890'],
    1.79 +                                             'test6.txt',
    1.80 +                                             { type: 'text/plain' })
    1.81 +                            }
    1.82 +                },
    1.83 +    todo: true,
    1.84 +    fileName: 'test6.txt'}
    1.85 +];
    1.86 +
    1.87 +var chromeJS = SimpleTest.getTestFileURL('filepicker_path_handler_chrome.js');
    1.88 +var chromeScript = SpecialPowers.loadChromeScript(chromeJS);
    1.89 +var activeTestCase;
    1.90 +
    1.91 +chromeScript.addMessageListener('pick-result-updated', handleMessage);
    1.92 +chromeScript.addMessageListener('file-picked-posted', handleMessage);
    1.93 +
    1.94 +// handle messages returned from chromeScript
    1.95 +function handleMessage(data) {
    1.96 +  var fileInput = document.getElementById('fileInput');
    1.97 +  switch (data.type) {
    1.98 +    case 'pick-result-updated':
    1.99 +      fileInput.click();
   1.100 +      break;
   1.101 +    case 'file-picked-posted':
   1.102 +      if (activeTestCase.todo) {
   1.103 +        todo_is(fileInput.value, activeTestCase.fileName,
   1.104 +                'DOMFile should be able to send through message.');
   1.105 +      } else {
   1.106 +        is(fileInput.value, activeTestCase.fileName);
   1.107 +      }
   1.108 +      processTestCase();
   1.109 +      break;
   1.110 +  }
   1.111 +}
   1.112 +
   1.113 +function processTestCase() {
   1.114 +  if (!testCases.length) {
   1.115 +    SimpleTest.finish();
   1.116 +    return;
   1.117 +  }
   1.118 +  activeTestCase = testCases.shift();
   1.119 +  var expectedResult = activeTestCase.pickedResult;
   1.120 +  if (navigator.userAgent.indexOf('Windows') > -1 &&
   1.121 +      expectedResult.result.name) {
   1.122 +    // If we run at a window box, we need to translate the path from '/' to '\\'
   1.123 +    var name = expectedResult.result.name;
   1.124 +    name = name.replace('/', '\\');
   1.125 +    // If the name is an absolute path, we need to prepend drive letter.
   1.126 +    if (name.startsWith('\\')) {
   1.127 +      name = 'C:' + name;
   1.128 +    }
   1.129 +    // update the expected name.
   1.130 +    expectedResult.result.name = name
   1.131 +  }
   1.132 +  chromeScript.sendAsyncMessage('update-pick-result', expectedResult);
   1.133 +}
   1.134 +
   1.135 +</script>
   1.136 +<input type="file" id="fileInput">
   1.137 +</body>
   1.138 +</html>
   1.139 \ No newline at end of file

mercurial