Home | History | Annotate | Download | only in file_handlers
      1 // Copyright 2013 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 "chrome/browser/extensions/api/file_handlers/mime_util.h"
      6 
      7 #include <string>
      8 #include <vector>
      9 
     10 #include "base/files/file_path.h"
     11 #include "base/files/file_util.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/run_loop.h"
     14 #include "chrome/test/base/testing_profile.h"
     15 #include "content/public/browser/browser_thread.h"
     16 #include "content/public/test/test_browser_thread_bundle.h"
     17 #include "content/public/test/test_file_system_context.h"
     18 #include "content/public/test/test_utils.h"
     19 #include "testing/gtest/include/gtest/gtest.h"
     20 
     21 namespace extensions {
     22 namespace app_file_handler_util {
     23 namespace {
     24 
     25 const char kOrigin[] = "chrome-extension://cmalghjoncmjoeakimpfhojhpgemgaje";
     26 const char kJPEGExtensionFilePath[] = "/fake/path/foo.jpg";
     27 const char kJPEGExtensionUpperCaseFilePath[] = "/fake/path/FOO.JPG";
     28 
     29 // Saves the returned mime type to a variable.
     30 void OnMimeTypeResult(std::string* output, const std::string& mime_type) {
     31   *output = mime_type;
     32 }
     33 
     34 // Saves returned mime types to a vector.
     35 void OnMimeTypesCollected(std::vector<std::string>* output,
     36                           scoped_ptr<std::vector<std::string> > mime_types) {
     37   *output = *mime_types;
     38 }
     39 
     40 // Creates a native local file system URL for a local path.
     41 storage::FileSystemURL CreateNativeLocalFileSystemURL(
     42     storage::FileSystemContext* context,
     43     const base::FilePath local_path) {
     44   return context->CreateCrackedFileSystemURL(
     45       GURL(kOrigin), storage::kFileSystemTypeNativeLocal, local_path);
     46 }
     47 
     48 }  // namespace
     49 
     50 class FileHandlersMimeUtilTest : public testing::Test {
     51  protected:
     52   FileHandlersMimeUtilTest() {}
     53   virtual ~FileHandlersMimeUtilTest() {}
     54 
     55   virtual void SetUp() OVERRIDE {
     56     ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
     57     file_system_context_ =
     58         content::CreateFileSystemContextForTesting(NULL, data_dir_.path());
     59 
     60     EXPECT_TRUE(base::CreateTemporaryFile(&html_mime_file_path_));
     61     const std::string kSampleContent = "<html><body></body></html>";
     62     EXPECT_TRUE(base::WriteFile(
     63         html_mime_file_path_, kSampleContent.c_str(), kSampleContent.size()));
     64   }
     65 
     66   content::TestBrowserThreadBundle thread_bundle_;
     67   TestingProfile profile_;
     68   scoped_refptr<storage::FileSystemContext> file_system_context_;
     69   base::ScopedTempDir data_dir_;
     70   base::FilePath html_mime_file_path_;
     71 };
     72 
     73 TEST_F(FileHandlersMimeUtilTest, GetMimeTypeForLocalPath) {
     74   {
     75     std::string result;
     76     GetMimeTypeForLocalPath(
     77         &profile_,
     78         base::FilePath::FromUTF8Unsafe(kJPEGExtensionFilePath),
     79         base::Bind(&OnMimeTypeResult, &result));
     80     content::RunAllBlockingPoolTasksUntilIdle();
     81     EXPECT_EQ("image/jpeg", result);
     82   }
     83 
     84   {
     85     std::string result;
     86     GetMimeTypeForLocalPath(
     87         &profile_,
     88         base::FilePath::FromUTF8Unsafe(kJPEGExtensionUpperCaseFilePath),
     89         base::Bind(&OnMimeTypeResult, &result));
     90     content::RunAllBlockingPoolTasksUntilIdle();
     91     EXPECT_EQ("image/jpeg", result);
     92   }
     93 
     94   {
     95     std::string result;
     96     GetMimeTypeForLocalPath(&profile_,
     97                             html_mime_file_path_,
     98                             base::Bind(&OnMimeTypeResult, &result));
     99     content::RunAllBlockingPoolTasksUntilIdle();
    100     EXPECT_EQ("text/html", result);
    101   }
    102 }
    103 
    104 TEST_F(FileHandlersMimeUtilTest, MimeTypeCollector_ForURLs) {
    105   MimeTypeCollector collector(&profile_);
    106 
    107   std::vector<storage::FileSystemURL> urls;
    108   urls.push_back(CreateNativeLocalFileSystemURL(
    109       file_system_context_.get(),
    110       base::FilePath::FromUTF8Unsafe(kJPEGExtensionFilePath)));
    111   urls.push_back(CreateNativeLocalFileSystemURL(
    112       file_system_context_.get(),
    113       base::FilePath::FromUTF8Unsafe(kJPEGExtensionUpperCaseFilePath)));
    114   urls.push_back(CreateNativeLocalFileSystemURL(file_system_context_.get(),
    115                                                 html_mime_file_path_));
    116 
    117   std::vector<std::string> result;
    118   collector.CollectForURLs(urls, base::Bind(&OnMimeTypesCollected, &result));
    119   content::RunAllBlockingPoolTasksUntilIdle();
    120 
    121   ASSERT_EQ(3u, result.size());
    122   EXPECT_EQ("image/jpeg", result[0]);
    123   EXPECT_EQ("image/jpeg", result[1]);
    124   EXPECT_EQ("text/html", result[2]);
    125 }
    126 
    127 TEST_F(FileHandlersMimeUtilTest, MimeTypeCollector_ForLocalPaths) {
    128   MimeTypeCollector collector(&profile_);
    129 
    130   std::vector<base::FilePath> local_paths;
    131   local_paths.push_back(base::FilePath::FromUTF8Unsafe(kJPEGExtensionFilePath));
    132   local_paths.push_back(
    133       base::FilePath::FromUTF8Unsafe(kJPEGExtensionUpperCaseFilePath));
    134   local_paths.push_back(html_mime_file_path_);
    135 
    136   std::vector<std::string> result;
    137   collector.CollectForLocalPaths(local_paths,
    138                                  base::Bind(&OnMimeTypesCollected, &result));
    139   content::RunAllBlockingPoolTasksUntilIdle();
    140 
    141   ASSERT_EQ(3u, result.size());
    142   EXPECT_EQ("image/jpeg", result[0]);
    143   EXPECT_EQ("image/jpeg", result[1]);
    144   EXPECT_EQ("text/html", result[2]);
    145 }
    146 
    147 }  // namespace app_file_handler_util
    148 }  // namespace extensions
    149