"Fossies" - the Fresh Open Source Software Archive

Member "google-gadgets-for-linux-0.11.2/extensions/linux_system_framework/tests/filesystem_file_test.cc" (15 Mar 2009, 5406 Bytes) of package /linux/misc/old/google-gadgets-for-linux-0.11.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2   Copyright 2008 Google Inc.
    3 
    4   Licensed under the Apache License, Version 2.0 (the "License");
    5   you may not use this file except in compliance with the License.
    6   You may obtain a copy of the License at
    7 
    8        http://www.apache.org/licenses/LICENSE-2.0
    9 
   10   Unless required by applicable law or agreed to in writing, software
   11   distributed under the License is distributed on an "AS IS" BASIS,
   12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13   See the License for the specific language governing permissions and
   14   limitations under the License.
   15 */
   16 
   17 #include <cstdio>
   18 #include <cstdlib>
   19 #include <dirent.h>
   20 #include <sys/stat.h>
   21 #include <sys/types.h>
   22 #include "ggadget/common.h"
   23 #include "ggadget/logger.h"
   24 #include "unittest/gtest.h"
   25 #include "../file_system.h"
   26 
   27 using namespace ggadget;
   28 using namespace ggadget::framework;
   29 using namespace ggadget::framework::linux_system;
   30 
   31 #define TEST_DIR_NAME "GGL_FileSystem_Test"
   32 #define TEST_DIR "/tmp/" TEST_DIR_NAME
   33 
   34 TEST(FileSystem, GetInformation) {
   35   FileSystem filesystem;
   36   mkdir(TEST_DIR, 0700);
   37   FILE *file = fopen(TEST_DIR "/file.cc", "wb");
   38   fwrite("test", 1, 4, file);
   39   fclose(file);
   40 
   41   FileInterface *fi = filesystem.GetFile(TEST_DIR "/file.cc");
   42   ASSERT_TRUE(fi != NULL);
   43 
   44   EXPECT_EQ(TEST_DIR "/file.cc", fi->GetPath());
   45   EXPECT_EQ("file.cc", fi->GetName());
   46   EXPECT_EQ(4, fi->GetSize());
   47   EXPECT_GT(fi->GetDateLastModified().value, 0U);
   48   EXPECT_GT(fi->GetDateLastAccessed().value, 0U);
   49 
   50   fi->Destroy();
   51   filesystem.DeleteFolder(TEST_DIR, true);
   52 }
   53 
   54 TEST(FileSystem, SetName) {
   55   FileSystem filesystem;
   56   mkdir(TEST_DIR, 0700);
   57   FILE *file = fopen(TEST_DIR "/file.cc", "wb");
   58   fwrite("test", 1, 4, file);
   59   fclose(file);
   60 
   61   FileInterface *fi = filesystem.GetFile(TEST_DIR "/file.cc");
   62   ASSERT_TRUE(fi != NULL);
   63 
   64   EXPECT_TRUE(fi->SetName("file2.cc"));
   65   EXPECT_EQ(TEST_DIR "/file2.cc", fi->GetPath());
   66   EXPECT_TRUE(filesystem.FileExists(TEST_DIR "/file2.cc"));
   67   EXPECT_FALSE(filesystem.FileExists(TEST_DIR "/file.cc"));
   68 
   69   // SetName() doesn't support moving file.
   70   EXPECT_FALSE(fi->SetName("/tmp/file3.cc"));
   71   EXPECT_EQ(TEST_DIR "/file2.cc", fi->GetPath());
   72   EXPECT_TRUE(filesystem.FileExists(TEST_DIR "/file2.cc"));
   73   EXPECT_FALSE(filesystem.FileExists(TEST_DIR "/file.cc"));
   74 
   75   fi->Destroy();
   76   filesystem.DeleteFolder(TEST_DIR, true);
   77 }
   78 
   79 TEST(FileSystem, GetParentFolder) {
   80   FileSystem filesystem;
   81   mkdir(TEST_DIR, 0700);
   82   FILE *file = fopen(TEST_DIR "/file.cc", "wb");
   83   fwrite("test", 1, 4, file);
   84   fclose(file);
   85 
   86   FileInterface *fi = filesystem.GetFile(TEST_DIR "/file.cc");
   87   ASSERT_TRUE(fi != NULL);
   88 
   89   FolderInterface *folder = fi->GetParentFolder();
   90   EXPECT_EQ(TEST_DIR, folder->GetPath());
   91   folder->Destroy();
   92 
   93   fi->Destroy();
   94   filesystem.DeleteFolder(TEST_DIR, true);
   95 }
   96 
   97 TEST(FileSystem, Delete) {
   98   FileSystem filesystem;
   99   mkdir(TEST_DIR, 0700);
  100   FILE *file = fopen(TEST_DIR "/file.cc", "wb");
  101   fwrite("test", 1, 4, file);
  102   fclose(file);
  103 
  104   FileInterface *fi = filesystem.GetFile(TEST_DIR "/file.cc");
  105   ASSERT_TRUE(fi != NULL);
  106 
  107   fi->Delete(true);
  108   EXPECT_FALSE(filesystem.FileExists(TEST_DIR "/file.cc"));
  109 
  110   fi->Destroy();
  111   filesystem.DeleteFolder(TEST_DIR, true);
  112 }
  113 
  114 TEST(FileSystem, Copy) {
  115   FileSystem filesystem;
  116   mkdir(TEST_DIR, 0700);
  117   mkdir(TEST_DIR "2", 0700);
  118   FILE *file = fopen(TEST_DIR "/file.cc", "wb");
  119   fwrite("test", 1, 4, file);
  120   fclose(file);
  121 
  122   FileInterface *fi = filesystem.GetFile(TEST_DIR "/file.cc");
  123   ASSERT_TRUE(fi != NULL);
  124 
  125   // Copies a file to another file.
  126   EXPECT_TRUE(fi->Copy(TEST_DIR "/file2.cc", false));
  127   EXPECT_FALSE(fi->Copy(TEST_DIR "/file2.cc", false));
  128   EXPECT_TRUE(fi->Copy(TEST_DIR "/file2.cc", true));
  129   EXPECT_TRUE(filesystem.FileExists(TEST_DIR "/file.cc"));
  130   EXPECT_TRUE(filesystem.FileExists(TEST_DIR "/file2.cc"));
  131 
  132   // Copies a file to another folder.
  133   EXPECT_FALSE(fi->Copy(TEST_DIR "2", false));
  134   EXPECT_TRUE(fi->Copy(TEST_DIR "2/", false));
  135   EXPECT_FALSE(fi->Copy(TEST_DIR "2/", false));
  136   EXPECT_TRUE(fi->Copy(TEST_DIR "2/", true));
  137   EXPECT_TRUE(filesystem.FileExists(TEST_DIR "/file.cc"));
  138   EXPECT_TRUE(filesystem.FileExists(TEST_DIR "2/file.cc"));
  139 
  140   // Copies a file to itself.
  141   EXPECT_FALSE(fi->Copy(TEST_DIR "/file.cc", false));
  142   EXPECT_TRUE(fi->Copy(TEST_DIR "/file.cc", true));
  143 
  144   fi->Destroy();
  145   filesystem.DeleteFolder(TEST_DIR, true);
  146   filesystem.DeleteFolder(TEST_DIR "2", true);
  147 }
  148 
  149 TEST(FileSystem, Move) {
  150   FileSystem filesystem;
  151   mkdir(TEST_DIR, 0700);
  152   mkdir(TEST_DIR "2", 0700);
  153   FILE *file = fopen(TEST_DIR "/file.cc", "wb");
  154   fwrite("test", 1, 4, file);
  155   fclose(file);
  156 
  157   FileInterface *fi = filesystem.GetFile(TEST_DIR "/file.cc");
  158   ASSERT_TRUE(fi != NULL);
  159 
  160   // Moves a file to another file.
  161   EXPECT_TRUE(fi->Move(TEST_DIR "/file2.cc"));
  162   EXPECT_FALSE(filesystem.FileExists(TEST_DIR "/file.cc"));
  163   EXPECT_TRUE(filesystem.FileExists(TEST_DIR "/file2.cc"));
  164   EXPECT_EQ(TEST_DIR "/file2.cc", fi->GetPath());
  165 
  166   // Moves a file to another folder.
  167   EXPECT_TRUE(fi->Move(TEST_DIR "2/"));
  168   EXPECT_FALSE(fi->Move(TEST_DIR "2/"));
  169   EXPECT_FALSE(filesystem.FileExists(TEST_DIR "/file2.cc"));
  170   EXPECT_TRUE(filesystem.FileExists(TEST_DIR "2/file2.cc"));
  171 
  172   fi->Destroy();
  173   filesystem.DeleteFolder(TEST_DIR, true);
  174   filesystem.DeleteFolder(TEST_DIR "2", true);
  175 }
  176 
  177 int main(int argc, char **argv) {
  178   testing::ParseGTestFlags(&argc, argv);
  179 
  180   return RUN_ALL_TESTS();
  181 }