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