Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "media/base/test_data_util.h"
      6 
      7 #include "base/files/file_util.h"
      8 #include "base/logging.h"
      9 #include "base/numerics/safe_conversions.h"
     10 #include "base/path_service.h"
     11 #include "media/base/decoder_buffer.h"
     12 
     13 namespace media {
     14 
     15 const base::FilePath::CharType kTestDataPath[] =
     16     FILE_PATH_LITERAL("media/test/data");
     17 
     18 base::FilePath GetTestDataFilePath(const std::string& name) {
     19   base::FilePath file_path;
     20   CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &file_path));
     21   return file_path.Append(GetTestDataPath()).AppendASCII(name);
     22 }
     23 
     24 base::FilePath GetTestDataPath() {
     25   return base::FilePath(kTestDataPath);
     26 }
     27 
     28 std::string GetURLQueryString(const QueryParams& query_params) {
     29   std::string query = "";
     30   QueryParams::const_iterator itr = query_params.begin();
     31   for (; itr != query_params.end(); ++itr) {
     32     if (itr != query_params.begin())
     33       query.append("&");
     34     query.append(itr->first + "=" + itr->second);
     35   }
     36   return query;
     37 }
     38 
     39 scoped_refptr<DecoderBuffer> ReadTestDataFile(const std::string& name) {
     40   base::FilePath file_path = GetTestDataFilePath(name);
     41 
     42   int64 tmp = 0;
     43   CHECK(base::GetFileSize(file_path, &tmp))
     44       << "Failed to get file size for '" << name << "'";
     45 
     46   int file_size = base::checked_cast<int>(tmp);
     47 
     48   scoped_refptr<DecoderBuffer> buffer(new DecoderBuffer(file_size));
     49   CHECK_EQ(file_size,
     50            base::ReadFile(
     51                file_path, reinterpret_cast<char*>(buffer->writable_data()),
     52                file_size)) << "Failed to read '" << name << "'";
     53 
     54   return buffer;
     55 }
     56 
     57 }  // namespace media
     58