Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2011 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 <stddef.h>
      6 
      7 #include "base/file_version_info.h"
      8 #include "base/files/file_path.h"
      9 #include "base/macros.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "build/build_config.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 
     14 #if defined(OS_WIN)
     15 #include "base/path_service.h"
     16 #include "base/file_version_info_win.h"
     17 #endif
     18 
     19 using base::FilePath;
     20 
     21 namespace {
     22 
     23 #if defined(OS_WIN)
     24 FilePath GetTestDataPath() {
     25   FilePath path;
     26   PathService::Get(base::DIR_SOURCE_ROOT, &path);
     27   path = path.AppendASCII("base");
     28   path = path.AppendASCII("test");
     29   path = path.AppendASCII("data");
     30   path = path.AppendASCII("file_version_info_unittest");
     31   return path;
     32 }
     33 #endif
     34 
     35 }  // namespace
     36 
     37 #if defined(OS_WIN)
     38 TEST(FileVersionInfoTest, HardCodedProperties) {
     39   const wchar_t kDLLName[] = {L"FileVersionInfoTest1.dll"};
     40 
     41   const wchar_t* const kExpectedValues[15] = {
     42       // FileVersionInfoTest.dll
     43       L"Goooooogle",                                  // company_name
     44       L"Google",                                      // company_short_name
     45       L"This is the product name",                    // product_name
     46       L"This is the product short name",              // product_short_name
     47       L"The Internal Name",                           // internal_name
     48       L"4.3.2.1",                                     // product_version
     49       L"Private build property",                      // private_build
     50       L"Special build property",                      // special_build
     51       L"This is a particularly interesting comment",  // comments
     52       L"This is the original filename",               // original_filename
     53       L"This is my file description",                 // file_description
     54       L"1.2.3.4",                                     // file_version
     55       L"This is the legal copyright",                 // legal_copyright
     56       L"This is the legal trademarks",                // legal_trademarks
     57       L"This is the last change",                     // last_change
     58   };
     59 
     60   FilePath dll_path = GetTestDataPath();
     61   dll_path = dll_path.Append(kDLLName);
     62 
     63   scoped_ptr<FileVersionInfo> version_info(
     64       FileVersionInfo::CreateFileVersionInfo(dll_path));
     65 
     66   int j = 0;
     67   EXPECT_EQ(kExpectedValues[j++], version_info->company_name());
     68   EXPECT_EQ(kExpectedValues[j++], version_info->company_short_name());
     69   EXPECT_EQ(kExpectedValues[j++], version_info->product_name());
     70   EXPECT_EQ(kExpectedValues[j++], version_info->product_short_name());
     71   EXPECT_EQ(kExpectedValues[j++], version_info->internal_name());
     72   EXPECT_EQ(kExpectedValues[j++], version_info->product_version());
     73   EXPECT_EQ(kExpectedValues[j++], version_info->private_build());
     74   EXPECT_EQ(kExpectedValues[j++], version_info->special_build());
     75   EXPECT_EQ(kExpectedValues[j++], version_info->comments());
     76   EXPECT_EQ(kExpectedValues[j++], version_info->original_filename());
     77   EXPECT_EQ(kExpectedValues[j++], version_info->file_description());
     78   EXPECT_EQ(kExpectedValues[j++], version_info->file_version());
     79   EXPECT_EQ(kExpectedValues[j++], version_info->legal_copyright());
     80   EXPECT_EQ(kExpectedValues[j++], version_info->legal_trademarks());
     81   EXPECT_EQ(kExpectedValues[j++], version_info->last_change());
     82 }
     83 #endif
     84 
     85 #if defined(OS_WIN)
     86 TEST(FileVersionInfoTest, IsOfficialBuild) {
     87   const wchar_t* kDLLNames[] = {
     88     L"FileVersionInfoTest1.dll",
     89     L"FileVersionInfoTest2.dll"
     90   };
     91 
     92   const bool kExpected[] = {
     93     true,
     94     false,
     95   };
     96 
     97   // Test consistency check.
     98   ASSERT_EQ(arraysize(kDLLNames), arraysize(kExpected));
     99 
    100   for (size_t i = 0; i < arraysize(kDLLNames); ++i) {
    101     FilePath dll_path = GetTestDataPath();
    102     dll_path = dll_path.Append(kDLLNames[i]);
    103 
    104     scoped_ptr<FileVersionInfo> version_info(
    105         FileVersionInfo::CreateFileVersionInfo(dll_path));
    106 
    107     EXPECT_EQ(kExpected[i], version_info->is_official_build());
    108   }
    109 }
    110 #endif
    111 
    112 #if defined(OS_WIN)
    113 TEST(FileVersionInfoTest, CustomProperties) {
    114   FilePath dll_path = GetTestDataPath();
    115   dll_path = dll_path.AppendASCII("FileVersionInfoTest1.dll");
    116 
    117   scoped_ptr<FileVersionInfo> version_info(
    118       FileVersionInfo::CreateFileVersionInfo(dll_path));
    119 
    120   // Test few existing properties.
    121   std::wstring str;
    122   FileVersionInfoWin* version_info_win =
    123       static_cast<FileVersionInfoWin*>(version_info.get());
    124   EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 1",  &str));
    125   EXPECT_EQ(L"Un", str);
    126   EXPECT_EQ(L"Un", version_info_win->GetStringValue(L"Custom prop 1"));
    127 
    128   EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 2",  &str));
    129   EXPECT_EQ(L"Deux", str);
    130   EXPECT_EQ(L"Deux", version_info_win->GetStringValue(L"Custom prop 2"));
    131 
    132   EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 3",  &str));
    133   EXPECT_EQ(L"1600 Amphitheatre Parkway Mountain View, CA 94043", str);
    134   EXPECT_EQ(L"1600 Amphitheatre Parkway Mountain View, CA 94043",
    135             version_info_win->GetStringValue(L"Custom prop 3"));
    136 
    137   // Test an non-existing property.
    138   EXPECT_FALSE(version_info_win->GetValue(L"Unknown property",  &str));
    139   EXPECT_EQ(L"", version_info_win->GetStringValue(L"Unknown property"));
    140 }
    141 #endif
    142