2016-05-13 18:20:26 +03:00
function createPath ( parentDir , dirOrFile ) {
return parentDir . path + ( parentDir . path == '/' ? '' : '/' ) + dirOrFile . name ;
}
2017-02-06 13:07:54 +03:00
function createRelativePath ( parentDir , dirOrFile ) {
let path = createPath ( parentDir , dirOrFile ) ;
is ( path [ 0 ] , "/" , "The full path should start with '/'" ) ;
return path . substring ( 1 ) ;
}
2016-04-20 12:39:33 +03:00
function setup _tests ( aNext ) {
2016-05-30 17:01:47 +03:00
SimpleTest . requestLongerTimeout ( 2 ) ;
2016-05-13 14:11:38 +03:00
SpecialPowers . pushPrefEnv ( { "set" : [ [ "dom.input.dirpicker" , true ] ,
2017-03-16 10:53:49 +03:00
[ "dom.filesystem.pathcheck.disabled" , true ] ,
2016-05-13 14:11:38 +03:00
[ "dom.webkitBlink.dirPicker.enabled" , true ] ] } , aNext ) ;
2016-04-20 12:39:33 +03:00
}
2016-04-13 14:15:56 +03:00
function test _basic ( aDirectory , aNext ) {
ok ( aDirectory , "Directory exists." ) ;
ok ( aDirectory instanceof Directory , "We have a directory." ) ;
2016-04-18 10:32:30 +03:00
is ( aDirectory . path , '/' + aDirectory . name , "directory.path must be '/'+name" ) ;
2016-04-13 14:15:56 +03:00
aNext ( ) ;
}
function test _getFilesAndDirectories ( aDirectory , aRecursive , aNext ) {
function checkSubDir ( dir ) {
return dir . getFilesAndDirectories ( ) . then (
function ( data ) {
for ( var i = 0 ; i < data . length ; ++ i ) {
ok ( data [ i ] instanceof File || data [ i ] instanceof Directory , "Just Files or Directories" ) ;
if ( data [ i ] instanceof Directory ) {
isnot ( data [ i ] . name , '/' , "Subdirectory should be called with the leafname" ) ;
isnot ( data [ i ] . path , '/' , "Subdirectory path should be called with the leafname" ) ;
isnot ( data [ i ] . path , dir . path , "Subdirectory path should contain the parent path." ) ;
2016-05-13 18:20:26 +03:00
is ( data [ i ] . path , createPath ( dir , data [ i ] ) , "Subdirectory path should be called parentdir.path + '/' + leafname: " + data [ i ] . path ) ;
2016-04-13 14:15:56 +03:00
}
2016-05-13 14:11:38 +03:00
if ( data [ i ] instanceof File ) {
2017-02-06 13:07:54 +03:00
is ( data [ i ] . webkitRelativePath , createRelativePath ( dir , data [ i ] ) , "File.webkitRelativePath should be called: parentdir.path + '/' + file.name: " + data [ i ] . webkitRelativePath ) ;
2016-05-13 14:11:38 +03:00
}
2016-04-13 14:15:56 +03:00
}
}
) ;
}
aDirectory . getFilesAndDirectories ( ) . then (
function ( data ) {
ok ( data . length , "We should have some data." ) ;
var promises = [ ] ;
for ( var i = 0 ; i < data . length ; ++ i ) {
ok ( data [ i ] instanceof File || data [ i ] instanceof Directory , "Just Files or Directories: " + data [ i ] . name ) ;
if ( data [ i ] instanceof Directory ) {
isnot ( data [ i ] . name , '/' , "Subdirectory should be called with the leafname" ) ;
2016-05-13 18:20:26 +03:00
is ( data [ i ] . path , createPath ( aDirectory , data [ i ] ) , "Subdirectory path should be called parentdir.path + '/' + leafname: " + data [ i ] . path ) ;
2016-04-13 14:15:56 +03:00
if ( aRecursive ) {
promises . push ( checkSubDir ( data [ i ] ) ) ;
}
}
2016-05-13 14:11:38 +03:00
if ( data [ i ] instanceof File ) {
2017-02-06 13:07:54 +03:00
is ( data [ i ] . webkitRelativePath , createRelativePath ( aDirectory , data [ i ] ) , "File.webkitRelativePath should be called file.name: " + data [ i ] . webkitRelativePath ) ;
2016-05-13 14:11:38 +03:00
}
2016-04-13 14:15:56 +03:00
}
return Promise . all ( promises ) ;
} ,
function ( ) {
ok ( false , "Something when wrong" ) ;
}
) . then ( aNext ) ;
}
function test _getFiles ( aDirectory , aRecursive , aNext ) {
aDirectory . getFiles ( aRecursive ) . then (
function ( data ) {
for ( var i = 0 ; i < data . length ; ++ i ) {
2016-05-13 14:11:38 +03:00
ok ( data [ i ] instanceof File , "File: " + data [ i ] . name ) ;
2017-02-06 13:07:54 +03:00
is ( aDirectory . path [ 0 ] , '/' , "Directory path must start with '/'" ) ;
ok ( data [ i ] . webkitRelativePath . indexOf ( aDirectory . path . substring ( 1 ) ) == 0 &&
2016-05-13 14:11:38 +03:00
data [ i ] . webkitRelativePath . indexOf ( '/' + data [ i ] . name ) + ( '/' + data [ i ] . name ) . length == data [ i ] . webkitRelativePath . length ,
"File.webkitRelativePath should be called dir.path + '/' + file.name: " + data [ i ] . webkitRelativePath ) ;
2016-04-13 14:15:56 +03:00
}
} ,
function ( ) {
ok ( false , "Something when wrong" ) ;
}
) . then ( aNext ) ;
}
function test _getFiles _recursiveComparison ( aDirectory , aNext ) {
aDirectory . getFiles ( true ) . then ( function ( data ) {
is ( data . length , 2 , "Only 2 files for this test." ) ;
ok ( data [ 0 ] . name == 'foo.txt' || data [ 0 ] . name == 'bar.txt' , "First filename matches" ) ;
ok ( data [ 1 ] . name == 'foo.txt' || data [ 1 ] . name == 'bar.txt' , "Second filename matches" ) ;
} ) . then ( function ( ) {
return aDirectory . getFiles ( false ) ;
} ) . then ( function ( data ) {
is ( data . length , 1 , "Only 1 file for this test." ) ;
ok ( data [ 0 ] . name == 'foo.txt' || data [ 0 ] . name == 'bar.txt' , "First filename matches" ) ;
} ) . catch ( function ( ) {
ok ( false , "Something when wrong" ) ;
} ) . then ( aNext ) ;
}