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.
5 #include "base/file_version_info_win.h"
13 #include "base/file_version_info.h"
14 #include "base/files/file_path.h"
15 #include "base/macros.h"
16 #include "base/memory/ptr_util.h"
17 #include "base/path_service.h"
18 #include "base/scoped_native_library.h"
19 #include "testing/gtest/include/gtest/gtest.h"
25 FilePath GetTestDataPath() {
27 base::PathService::Get(base::DIR_SOURCE_ROOT, &path);
28 path = path.AppendASCII("base");
29 path = path.AppendASCII("test");
30 path = path.AppendASCII("data");
31 path = path.AppendASCII("file_version_info_unittest");
35 class FileVersionInfoFactory {
37 explicit FileVersionInfoFactory(const FilePath& path) : path_(path) {}
39 std::unique_ptr<FileVersionInfo> Create() const {
40 return base::WrapUnique(FileVersionInfo::CreateFileVersionInfo(path_));
46 DISALLOW_COPY_AND_ASSIGN(FileVersionInfoFactory);
49 class FileVersionInfoForModuleFactory {
51 explicit FileVersionInfoForModuleFactory(const FilePath& path)
52 // Load the library with LOAD_LIBRARY_AS_IMAGE_RESOURCE since it shouldn't
54 : library_(::LoadLibraryEx(path.value().c_str(),
56 LOAD_LIBRARY_AS_IMAGE_RESOURCE)) {
57 EXPECT_TRUE(library_.is_valid());
60 std::unique_ptr<FileVersionInfo> Create() const {
61 return base::WrapUnique(
62 FileVersionInfo::CreateFileVersionInfoForModule(library_.get()));
66 const base::ScopedNativeLibrary library_;
68 DISALLOW_COPY_AND_ASSIGN(FileVersionInfoForModuleFactory);
72 class FileVersionInfoTest : public testing::Test {};
74 using FileVersionInfoFactories =
75 ::testing::Types<FileVersionInfoFactory, FileVersionInfoForModuleFactory>;
79 TYPED_TEST_CASE(FileVersionInfoTest, FileVersionInfoFactories);
81 TYPED_TEST(FileVersionInfoTest, HardCodedProperties) {
82 const wchar_t kDLLName[] = {L"FileVersionInfoTest1.dll"};
84 const wchar_t* const kExpectedValues[15] = {
85 // FileVersionInfoTest.dll
86 L"Goooooogle", // company_name
87 L"Google", // company_short_name
88 L"This is the product name", // product_name
89 L"This is the product short name", // product_short_name
90 L"The Internal Name", // internal_name
91 L"4.3.2.1", // product_version
92 L"Private build property", // private_build
93 L"Special build property", // special_build
94 L"This is a particularly interesting comment", // comments
95 L"This is the original filename", // original_filename
96 L"This is my file description", // file_description
97 L"1.2.3.4", // file_version
98 L"This is the legal copyright", // legal_copyright
99 L"This is the legal trademarks", // legal_trademarks
100 L"This is the last change", // last_change
103 FilePath dll_path = GetTestDataPath();
104 dll_path = dll_path.Append(kDLLName);
106 TypeParam factory(dll_path);
107 std::unique_ptr<FileVersionInfo> version_info(factory.Create());
108 ASSERT_TRUE(version_info);
111 EXPECT_EQ(kExpectedValues[j++], version_info->company_name());
112 EXPECT_EQ(kExpectedValues[j++], version_info->company_short_name());
113 EXPECT_EQ(kExpectedValues[j++], version_info->product_name());
114 EXPECT_EQ(kExpectedValues[j++], version_info->product_short_name());
115 EXPECT_EQ(kExpectedValues[j++], version_info->internal_name());
116 EXPECT_EQ(kExpectedValues[j++], version_info->product_version());
117 EXPECT_EQ(kExpectedValues[j++], version_info->private_build());
118 EXPECT_EQ(kExpectedValues[j++], version_info->special_build());
119 EXPECT_EQ(kExpectedValues[j++], version_info->comments());
120 EXPECT_EQ(kExpectedValues[j++], version_info->original_filename());
121 EXPECT_EQ(kExpectedValues[j++], version_info->file_description());
122 EXPECT_EQ(kExpectedValues[j++], version_info->file_version());
123 EXPECT_EQ(kExpectedValues[j++], version_info->legal_copyright());
124 EXPECT_EQ(kExpectedValues[j++], version_info->legal_trademarks());
125 EXPECT_EQ(kExpectedValues[j++], version_info->last_change());
128 TYPED_TEST(FileVersionInfoTest, IsOfficialBuild) {
130 const wchar_t* const dll_name;
131 const bool is_official_build;
133 {L"FileVersionInfoTest1.dll", true}, {L"FileVersionInfoTest2.dll", false},
136 for (const auto& test_item : kTestItems) {
137 const FilePath dll_path = GetTestDataPath().Append(test_item.dll_name);
139 TypeParam factory(dll_path);
140 std::unique_ptr<FileVersionInfo> version_info(factory.Create());
141 ASSERT_TRUE(version_info);
143 EXPECT_EQ(test_item.is_official_build, version_info->is_official_build());
147 TYPED_TEST(FileVersionInfoTest, CustomProperties) {
148 FilePath dll_path = GetTestDataPath();
149 dll_path = dll_path.AppendASCII("FileVersionInfoTest1.dll");
151 TypeParam factory(dll_path);
152 std::unique_ptr<FileVersionInfo> version_info(factory.Create());
153 ASSERT_TRUE(version_info);
155 // Test few existing properties.
157 FileVersionInfoWin* version_info_win =
158 static_cast<FileVersionInfoWin*>(version_info.get());
159 EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 1", &str));
160 EXPECT_EQ(L"Un", str);
161 EXPECT_EQ(L"Un", version_info_win->GetStringValue(L"Custom prop 1"));
163 EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 2", &str));
164 EXPECT_EQ(L"Deux", str);
165 EXPECT_EQ(L"Deux", version_info_win->GetStringValue(L"Custom prop 2"));
167 EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 3", &str));
168 EXPECT_EQ(L"1600 Amphitheatre Parkway Mountain View, CA 94043", str);
169 EXPECT_EQ(L"1600 Amphitheatre Parkway Mountain View, CA 94043",
170 version_info_win->GetStringValue(L"Custom prop 3"));
172 // Test an non-existing property.
173 EXPECT_FALSE(version_info_win->GetValue(L"Unknown property", &str));
174 EXPECT_EQ(L"", version_info_win->GetStringValue(L"Unknown property"));