summaryrefslogtreecommitdiffstats
path: root/b2g/components/test/mochitest/test_filepicker_path.html
blob: 92c00dc682b9c93860897a035ba6557083917076 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=949944
-->
<head>
<meta charset="utf-8">
<title>Permission Prompt Test</title>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body onload="processTestCase()">
<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>
<script type="application/javascript">

'use strict';

var testCases = [
  // case 1: returns blob with name
  { pickedResult: { success: true,
                    result: {
                              type: 'text/plain',
                              blob: new Blob(['1234567890'],
                                             { type: 'text/plain' }),
                              name: 'test1.txt'
                            }
                },
    fileName: 'test1.txt' },
  // case 2: returns blob without name
  { pickedResult: { success: true,
                    result: {
                              type: 'text/plain',
                              blob: new Blob(['1234567890'],
                                             { type: 'text/plain' })
                            }
                },
    fileName: 'blob.txt' },
  // case 3: returns blob with full path name
  { pickedResult: { success: true,
                    result: {
                              type: 'text/plain',
                              blob: new Blob(['1234567890'],
                                             { type: 'text/plain' }),
                              name: '/full/path/test3.txt'
                            }
                },
    fileName: 'test3.txt' },
  // case 4: returns blob relative path name
  { pickedResult: { success: true,
                    result: {
                              type: 'text/plain',
                              blob: new Blob(['1234567890'],
                                             { type: 'text/plain' }),
                              name: 'relative/path/test4.txt'
                            }
                },
    fileName: 'test4.txt' },
  // case 5: returns file with name
  { pickedResult: { success: true,
                    result: {
                              type: 'text/plain',
                              blob: new File(['1234567890'],
                                             'useless-name.txt',
                                             { type: 'text/plain' }),
                              name: 'test5.txt'
                            }
                },
    fileName: 'test5.txt'},
  // case 6: returns file without name. This case may fail because we
  //         need to make sure the File can be sent through
  //         sendAsyncMessage API.
  { pickedResult: { success: true,
                    result: {
                              type: 'text/plain',
                              blob: new File(['1234567890'],
                                             'test6.txt',
                                             { type: 'text/plain' })
                            }
                },
    fileName: 'test6.txt'}
];

var chromeJS = SimpleTest.getTestFileURL('filepicker_path_handler_chrome.js');
var chromeScript = SpecialPowers.loadChromeScript(chromeJS);
var activeTestCase;

chromeScript.addMessageListener('pick-result-updated', handleMessage);
chromeScript.addMessageListener('file-picked-posted', handleMessage);

// handle messages returned from chromeScript
function handleMessage(data) {
  var fileInput = document.getElementById('fileInput');
  switch (data.type) {
    case 'pick-result-updated':
      fileInput.click();
      break;
    case 'file-picked-posted':
      is(fileInput.value, activeTestCase.fileName,
         'File should be able to send through message.');
      processTestCase();
      break;
  }
}

function processTestCase() {
  if (!testCases.length) {
    SimpleTest.finish();
    return;
  }
  activeTestCase = testCases.shift();
  var expectedResult = activeTestCase.pickedResult;
  if (navigator.userAgent.indexOf('Windows') > -1 &&
      expectedResult.result.name) {
    // If we run at a window box, we need to translate the path from '/' to '\\'
    var name = expectedResult.result.name;
    name = name.replace('/', '\\');
    // If the name is an absolute path, we need to prepend drive letter.
    if (name.startsWith('\\')) {
      name = 'C:' + name;
    }
    // update the expected name.
    expectedResult.result.name = name
  }
  chromeScript.sendAsyncMessage('update-pick-result', expectedResult);
}

</script>
<input type="file" id="fileInput">
</body>
</html>