Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / test / testsupport / fileutils_unittest.cc
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include "webrtc/test/testsupport/fileutils.h"
12
13 #include <stdio.h>
14
15 #include <list>
16 #include <string>
17
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "webrtc/test/testsupport/gtest_disable.h"
20
21 #ifdef WIN32
22 #define chdir _chdir
23 static const char* kPathDelimiter = "\\";
24 #else
25 static const char* kPathDelimiter = "/";
26 #endif
27
28 static const std::string kResourcesDir = "resources";
29 static const std::string kTestName = "fileutils_unittest";
30 static const std::string kExtension = "tmp";
31
32 namespace webrtc {
33
34 // Test fixture to restore the working directory between each test, since some
35 // of them change it with chdir during execution (not restored by the
36 // gtest framework).
37 class FileUtilsTest : public testing::Test {
38  protected:
39   FileUtilsTest() {
40   }
41   virtual ~FileUtilsTest() {}
42   // Runs before the first test
43   static void SetUpTestCase() {
44     original_working_dir_ = webrtc::test::WorkingDir();
45   }
46   void SetUp() {
47     ASSERT_EQ(chdir(original_working_dir_.c_str()), 0);
48   }
49   void TearDown() {
50     ASSERT_EQ(chdir(original_working_dir_.c_str()), 0);
51   }
52  private:
53   static std::string original_working_dir_;
54 };
55
56 std::string FileUtilsTest::original_working_dir_ = "";
57
58 // Tests that the project root path is returned for the default working
59 // directory that is automatically set when the test executable is launched.
60 // The test is not fully testing the implementation, since we cannot be sure
61 // of where the executable was launched from.
62 TEST_F(FileUtilsTest, ProjectRootPath) {
63   std::string project_root = webrtc::test::ProjectRootPath();
64   // Not very smart, but at least tests something.
65   ASSERT_GT(project_root.length(), 0u);
66 }
67
68 // Similar to the above test, but for the output dir
69 TEST_F(FileUtilsTest, DISABLED_ON_ANDROID(OutputPathFromUnchangedWorkingDir)) {
70   std::string path = webrtc::test::OutputPath();
71   std::string expected_end = "out";
72   expected_end = kPathDelimiter + expected_end + kPathDelimiter;
73   ASSERT_EQ(path.length() - expected_end.length(), path.find(expected_end));
74 }
75
76 // Tests with current working directory set to a directory higher up in the
77 // directory tree than the project root dir.
78 TEST_F(FileUtilsTest, DISABLED_ON_ANDROID(OutputPathFromRootWorkingDir)) {
79   ASSERT_EQ(0, chdir(kPathDelimiter));
80   ASSERT_EQ("./", webrtc::test::OutputPath());
81 }
82
83 TEST_F(FileUtilsTest, TempFilename) {
84   std::string temp_filename = webrtc::test::TempFilename(
85       webrtc::test::OutputPath(), "TempFilenameTest");
86   ASSERT_TRUE(webrtc::test::FileExists(temp_filename))
87       << "Couldn't find file: " << temp_filename;
88   remove(temp_filename.c_str());
89 }
90
91 // Only tests that the code executes
92 TEST_F(FileUtilsTest, CreateDir) {
93   std::string directory = "fileutils-unittest-empty-dir";
94   // Make sure it's removed if a previous test has failed:
95   remove(directory.c_str());
96   ASSERT_TRUE(webrtc::test::CreateDir(directory));
97   remove(directory.c_str());
98 }
99
100 TEST_F(FileUtilsTest, WorkingDirReturnsValue) {
101   // Hard to cover all platforms. Just test that it returns something without
102   // crashing:
103   std::string working_dir = webrtc::test::WorkingDir();
104   ASSERT_GT(working_dir.length(), 0u);
105 }
106
107 // Due to multiple platforms, it is hard to make a complete test for
108 // ResourcePath. Manual testing has been performed by removing files and
109 // verified the result confirms with the specified documentation for the
110 // function.
111 TEST_F(FileUtilsTest, ResourcePathReturnsValue) {
112   std::string resource = webrtc::test::ResourcePath(kTestName, kExtension);
113   ASSERT_GT(resource.find(kTestName), 0u);
114   ASSERT_GT(resource.find(kExtension), 0u);
115 }
116
117 TEST_F(FileUtilsTest, ResourcePathFromRootWorkingDir) {
118   ASSERT_EQ(0, chdir(kPathDelimiter));
119   std::string resource = webrtc::test::ResourcePath(kTestName, kExtension);
120   ASSERT_NE(resource.find("resources"), std::string::npos);
121   ASSERT_GT(resource.find(kTestName), 0u);
122   ASSERT_GT(resource.find(kExtension), 0u);
123 }
124
125 TEST_F(FileUtilsTest, GetFileSizeExistingFile) {
126   // Create a file with some dummy data in.
127   std::string temp_filename = webrtc::test::TempFilename(
128       webrtc::test::OutputPath(), "fileutils_unittest");
129   FILE* file = fopen(temp_filename.c_str(), "wb");
130   ASSERT_TRUE(file != NULL) << "Failed to open file: " << temp_filename;
131   ASSERT_GT(fprintf(file, "%s",  "Dummy data"), 0) <<
132       "Failed to write to file: " << temp_filename;
133   fclose(file);
134   ASSERT_GT(webrtc::test::GetFileSize(std::string(temp_filename.c_str())), 0u);
135   remove(temp_filename.c_str());
136 }
137
138 TEST_F(FileUtilsTest, GetFileSizeNonExistingFile) {
139   ASSERT_EQ(0u, webrtc::test::GetFileSize("non-existing-file.tmp"));
140 }
141
142 }  // namespace webrtc