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