TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kurlcompletiontest.cpp 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C) 2004 David Faure <faure@kde.org>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License version 2 as published by the Free Software Foundation;
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public License
  14. * along with this library; see the file COPYING.LIB. If not, write to
  15. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301, USA.
  17. */
  18. #include <tdeapplication.h>
  19. #include <kurlcompletion.h>
  20. #include <kdebug.h>
  21. #include <tqdir.h>
  22. #include <assert.h>
  23. #include <tqfile.h>
  24. #include <ktempdir.h>
  25. #include <tdecmdlineargs.h>
  26. #include <unistd.h>
  27. class KURLCompletionTest
  28. {
  29. public:
  30. KURLCompletionTest() {}
  31. ~KURLCompletionTest() { teardown(); }
  32. void setup( bool setDirAsURL );
  33. void teardown();
  34. void testLocalRelativePath();
  35. void testLocalAbsolutePath();
  36. void testLocalURL();
  37. private:
  38. void waitForCompletion();
  39. KURLCompletion* m_completion;
  40. KTempDir* m_tempDir;
  41. KURL m_dirURL;
  42. TQString m_dir;
  43. };
  44. void KURLCompletionTest::setup( bool setDirAsURL )
  45. {
  46. kdDebug() << k_funcinfo << endl;
  47. m_completion = new KURLCompletion;
  48. m_tempDir = new KTempDir;
  49. m_tempDir->setAutoDelete( true );
  50. m_dir = m_tempDir->name();
  51. kdDebug() << "m_dir=" << m_dir << endl;
  52. Q_ASSERT( m_dir.endsWith( "/" ) );
  53. if ( setDirAsURL ) {
  54. KURL d; d.setPath( m_dir );
  55. m_completion->setDir( d.url() );
  56. } else {
  57. m_completion->setDir( m_dir );
  58. }
  59. m_dirURL.setPath( m_dir );
  60. TQFile f1( m_dir + "/file1" );
  61. bool ok = f1.open( IO_WriteOnly );
  62. assert( ok );
  63. f1.close();
  64. TQFile f2( m_dir + "/file#a" );
  65. ok = f2.open( IO_WriteOnly );
  66. assert( ok );
  67. f2.close();
  68. }
  69. void KURLCompletionTest::teardown()
  70. {
  71. delete m_completion;
  72. m_completion = 0;
  73. delete m_tempDir;
  74. m_tempDir = 0;
  75. }
  76. void KURLCompletionTest::waitForCompletion()
  77. {
  78. while ( m_completion->isRunning() ) {
  79. kdDebug() << "waiting for thread..." << endl;
  80. usleep( 10 );
  81. }
  82. }
  83. void KURLCompletionTest::testLocalRelativePath()
  84. {
  85. kdDebug() << k_funcinfo << endl;
  86. // Completion from relative path, with two matches
  87. m_completion->makeCompletion( "f" );
  88. waitForCompletion();
  89. TQStringList comp1all = m_completion->allMatches();
  90. assert( comp1all.count() == 2 );
  91. assert( comp1all.find( "file1" ) != comp1all.end() );
  92. assert( comp1all.find( "file#a" ) != comp1all.end() );
  93. TQString comp1 = m_completion->replacedPath( "file1" ); // like KURLRequester does
  94. assert( comp1 == "file1" );
  95. // Completion from relative path
  96. kdDebug() << endl << k_funcinfo << "now completing on 'file#'" << endl;
  97. m_completion->makeCompletion( "file#" );
  98. waitForCompletion();
  99. TQStringList compall = m_completion->allMatches();
  100. kdDebug() << compall << endl;
  101. assert( compall.count() == 1 );
  102. assert( compall.first() == "file#a" );
  103. TQString comp2 = m_completion->replacedPath( compall.first() ); // like KURLRequester does
  104. assert( comp2 == "file#a" );
  105. }
  106. void KURLCompletionTest::testLocalAbsolutePath()
  107. {
  108. // Completion from absolute path
  109. kdDebug() << k_funcinfo << m_dir+"file#" << endl;
  110. m_completion->makeCompletion( m_dir + "file#" );
  111. waitForCompletion();
  112. TQStringList compall = m_completion->allMatches();
  113. kdDebug() << compall << endl;
  114. assert( compall.count() == 1 );
  115. TQString comp = compall.first();
  116. assert( comp == m_dir + "file#a" );
  117. comp = m_completion->replacedPath( comp ); // like KURLRequester does
  118. assert( comp == m_dir + "file#a" );
  119. }
  120. void KURLCompletionTest::testLocalURL()
  121. {
  122. // Completion from URL
  123. kdDebug() << k_funcinfo << endl;
  124. KURL url = KURL::fromPathOrURL( m_dirURL.path() + "file" );
  125. m_completion->makeCompletion( url.prettyURL() );
  126. waitForCompletion();
  127. TQStringList comp1all = m_completion->allMatches();
  128. kdDebug() << comp1all << endl;
  129. assert( comp1all.count() == 2 );
  130. assert( comp1all.find( m_dirURL.url() + "file1" ) != comp1all.end() );
  131. TQString filehash = m_dirURL.url() + "file%23a";
  132. assert( comp1all.find( filehash ) != comp1all.end() );
  133. TQString filehashPath = m_completion->replacedPath( filehash ); // note that it returns a path!!
  134. kdDebug() << filehashPath << endl;
  135. assert( filehashPath == m_dirURL.path() + "file#a" );
  136. // Completion from URL with no match
  137. url = KURL::fromPathOrURL( m_dirURL.path() + "foobar" );
  138. kdDebug() << k_funcinfo << "makeCompletion(" << url << ")" << endl;
  139. TQString comp2 = m_completion->makeCompletion( url.prettyURL() );
  140. assert( comp2.isEmpty() );
  141. waitForCompletion();
  142. assert( m_completion->allMatches().isEmpty() );
  143. // Completion from URL with a ref -> no match
  144. url = KURL::fromPathOrURL( m_dirURL.path() + "f" );
  145. url.setRef( "ref" );
  146. kdDebug() << k_funcinfo << "makeCompletion(" << url << ")" << endl;
  147. m_completion->makeCompletion( url.prettyURL() );
  148. waitForCompletion();
  149. assert( m_completion->allMatches().isEmpty() );
  150. }
  151. int main( int argc, char **argv )
  152. {
  153. TDEApplication::disableAutoDcopRegistration();
  154. TDECmdLineArgs::init(argc,argv,"kurlcompletiontest", 0, 0, 0, 0);
  155. TDEApplication app(/*styles=*/false, /*gui=*/false);
  156. {
  157. KURLCompletionTest test;
  158. test.setup( false );
  159. test.testLocalRelativePath();
  160. test.testLocalAbsolutePath();
  161. test.testLocalURL();
  162. test.teardown();
  163. // Try again, with another KTempDir (to check that the caching doesn't give us wrong results)
  164. test.setup( true );
  165. test.testLocalRelativePath();
  166. test.testLocalAbsolutePath();
  167. test.testLocalURL();
  168. test.teardown();
  169. }
  170. tqDebug( "All tests OK." );
  171. return 0;
  172. }