Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: base/file_version_info_win_unittest.cc

Issue 2046583002: Don't use ::GetFileVersionInfo() in CreateFileVersionInfoForModule() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR grt #10 Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_version_info.h" 5 #include "base/file_version_info_win.h"
6
7 #include <windows.h>
6 8
7 #include <stddef.h> 9 #include <stddef.h>
8 10
9 #include <memory> 11 #include <memory>
10 12
13 #include "base/file_version_info.h"
11 #include "base/files/file_path.h" 14 #include "base/files/file_path.h"
12 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/memory/ptr_util.h"
13 #include "base/path_service.h" 17 #include "base/path_service.h"
14 #include "build/build_config.h" 18 #include "base/scoped_native_library.h"
15 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
16 20
17 #if defined(OS_WIN)
18 #include "base/file_version_info_win.h"
19 #endif
20
21 using base::FilePath; 21 using base::FilePath;
22 22
23 namespace { 23 namespace {
24 24
25 #if defined(OS_WIN)
26 FilePath GetTestDataPath() { 25 FilePath GetTestDataPath() {
27 FilePath path; 26 FilePath path;
28 PathService::Get(base::DIR_SOURCE_ROOT, &path); 27 PathService::Get(base::DIR_SOURCE_ROOT, &path);
29 path = path.AppendASCII("base"); 28 path = path.AppendASCII("base");
30 path = path.AppendASCII("test"); 29 path = path.AppendASCII("test");
31 path = path.AppendASCII("data"); 30 path = path.AppendASCII("data");
32 path = path.AppendASCII("file_version_info_unittest"); 31 path = path.AppendASCII("file_version_info_unittest");
33 return path; 32 return path;
34 } 33 }
35 #endif 34
35 class FileVersionInfoFactory {
36 public:
37 FileVersionInfoFactory(const FilePath& path) : path_(path) {}
Lei Zhang 2016/06/23 04:33:53 explicit, ditto below
fdoray 2016/06/27 20:51:29 Done.
38
39 std::unique_ptr<FileVersionInfo> Create() const {
40 return base::WrapUnique(FileVersionInfo::CreateFileVersionInfo(path_));
41 }
42
43 private:
44 const FilePath path_;
45
46 DISALLOW_COPY_AND_ASSIGN(FileVersionInfoFactory);
47 };
48
49 class FileVersionInfoForModuleFactory {
50 public:
51 FileVersionInfoForModuleFactory(const FilePath& path)
52 // Load the library with LOAD_LIBRARY_AS_IMAGE_RESOURCE since it shouldn't
53 // be executed.
54 : library_(::LoadLibraryEx(path.value().c_str(),
55 nullptr,
56 LOAD_LIBRARY_AS_IMAGE_RESOURCE)) {
57 EXPECT_TRUE(library_.is_valid());
58 }
59
60 std::unique_ptr<FileVersionInfo> Create() const {
61 return base::WrapUnique(
62 FileVersionInfo::CreateFileVersionInfoForModule(library_.get()));
63 }
64
65 private:
66 const base::ScopedNativeLibrary library_;
67
68 DISALLOW_COPY_AND_ASSIGN(FileVersionInfoForModuleFactory);
69 };
70
71 template <typename T>
72 class FileVersionInfoTest : public testing::Test {};
73
74 using FileVersionInfoFactories =
75 ::testing::Types<FileVersionInfoFactory, FileVersionInfoForModuleFactory>;
36 76
37 } // namespace 77 } // namespace
38 78
39 #if defined(OS_WIN) 79 TYPED_TEST_CASE(FileVersionInfoTest, FileVersionInfoFactories);
40 TEST(FileVersionInfoTest, HardCodedProperties) { 80
81 TYPED_TEST(FileVersionInfoTest, HardCodedProperties) {
41 const wchar_t kDLLName[] = {L"FileVersionInfoTest1.dll"}; 82 const wchar_t kDLLName[] = {L"FileVersionInfoTest1.dll"};
42 83
43 const wchar_t* const kExpectedValues[15] = { 84 const wchar_t* const kExpectedValues[15] = {
44 // FileVersionInfoTest.dll 85 // FileVersionInfoTest.dll
45 L"Goooooogle", // company_name 86 L"Goooooogle", // company_name
46 L"Google", // company_short_name 87 L"Google", // company_short_name
47 L"This is the product name", // product_name 88 L"This is the product name", // product_name
48 L"This is the product short name", // product_short_name 89 L"This is the product short name", // product_short_name
49 L"The Internal Name", // internal_name 90 L"The Internal Name", // internal_name
50 L"4.3.2.1", // product_version 91 L"4.3.2.1", // product_version
51 L"Private build property", // private_build 92 L"Private build property", // private_build
52 L"Special build property", // special_build 93 L"Special build property", // special_build
53 L"This is a particularly interesting comment", // comments 94 L"This is a particularly interesting comment", // comments
54 L"This is the original filename", // original_filename 95 L"This is the original filename", // original_filename
55 L"This is my file description", // file_description 96 L"This is my file description", // file_description
56 L"1.2.3.4", // file_version 97 L"1.2.3.4", // file_version
57 L"This is the legal copyright", // legal_copyright 98 L"This is the legal copyright", // legal_copyright
58 L"This is the legal trademarks", // legal_trademarks 99 L"This is the legal trademarks", // legal_trademarks
59 L"This is the last change", // last_change 100 L"This is the last change", // last_change
60 }; 101 };
61 102
62 FilePath dll_path = GetTestDataPath(); 103 FilePath dll_path = GetTestDataPath();
63 dll_path = dll_path.Append(kDLLName); 104 dll_path = dll_path.Append(kDLLName);
64 105
65 std::unique_ptr<FileVersionInfo> version_info( 106 TypeParam factory(dll_path);
66 FileVersionInfo::CreateFileVersionInfo(dll_path)); 107 std::unique_ptr<FileVersionInfo> version_info(factory.Create());
108 ASSERT_TRUE(version_info);
67 109
68 int j = 0; 110 int j = 0;
69 EXPECT_EQ(kExpectedValues[j++], version_info->company_name()); 111 EXPECT_EQ(kExpectedValues[j++], version_info->company_name());
70 EXPECT_EQ(kExpectedValues[j++], version_info->company_short_name()); 112 EXPECT_EQ(kExpectedValues[j++], version_info->company_short_name());
71 EXPECT_EQ(kExpectedValues[j++], version_info->product_name()); 113 EXPECT_EQ(kExpectedValues[j++], version_info->product_name());
72 EXPECT_EQ(kExpectedValues[j++], version_info->product_short_name()); 114 EXPECT_EQ(kExpectedValues[j++], version_info->product_short_name());
73 EXPECT_EQ(kExpectedValues[j++], version_info->internal_name()); 115 EXPECT_EQ(kExpectedValues[j++], version_info->internal_name());
74 EXPECT_EQ(kExpectedValues[j++], version_info->product_version()); 116 EXPECT_EQ(kExpectedValues[j++], version_info->product_version());
75 EXPECT_EQ(kExpectedValues[j++], version_info->private_build()); 117 EXPECT_EQ(kExpectedValues[j++], version_info->private_build());
76 EXPECT_EQ(kExpectedValues[j++], version_info->special_build()); 118 EXPECT_EQ(kExpectedValues[j++], version_info->special_build());
77 EXPECT_EQ(kExpectedValues[j++], version_info->comments()); 119 EXPECT_EQ(kExpectedValues[j++], version_info->comments());
78 EXPECT_EQ(kExpectedValues[j++], version_info->original_filename()); 120 EXPECT_EQ(kExpectedValues[j++], version_info->original_filename());
79 EXPECT_EQ(kExpectedValues[j++], version_info->file_description()); 121 EXPECT_EQ(kExpectedValues[j++], version_info->file_description());
80 EXPECT_EQ(kExpectedValues[j++], version_info->file_version()); 122 EXPECT_EQ(kExpectedValues[j++], version_info->file_version());
81 EXPECT_EQ(kExpectedValues[j++], version_info->legal_copyright()); 123 EXPECT_EQ(kExpectedValues[j++], version_info->legal_copyright());
82 EXPECT_EQ(kExpectedValues[j++], version_info->legal_trademarks()); 124 EXPECT_EQ(kExpectedValues[j++], version_info->legal_trademarks());
83 EXPECT_EQ(kExpectedValues[j++], version_info->last_change()); 125 EXPECT_EQ(kExpectedValues[j++], version_info->last_change());
84 } 126 }
85 #endif
86 127
87 #if defined(OS_WIN) 128 TYPED_TEST(FileVersionInfoTest, IsOfficialBuild) {
88 TEST(FileVersionInfoTest, IsOfficialBuild) { 129 const wchar_t* const kDLLNames[] = {L"FileVersionInfoTest1.dll",
89 const wchar_t* kDLLNames[] = { 130 L"FileVersionInfoTest2.dll"};
90 L"FileVersionInfoTest1.dll", 131
91 L"FileVersionInfoTest2.dll" 132 const bool kExpectedIsOfficialBuild[] = {
133 true, false,
92 }; 134 };
93 135
94 const bool kExpected[] = { 136 static_assert(arraysize(kDLLNames) == arraysize(kExpectedIsOfficialBuild),
Lei Zhang 2016/06/23 04:33:53 I realize this is pre-existing, but it's a bit won
fdoray 2016/06/27 20:51:29 Done.
95 true, 137 "kDLLNames should have the same number of elements as "
96 false, 138 "kExpectedIsOfficialBuild");
97 };
98
99 // Test consistency check.
100 ASSERT_EQ(arraysize(kDLLNames), arraysize(kExpected));
101 139
102 for (size_t i = 0; i < arraysize(kDLLNames); ++i) { 140 for (size_t i = 0; i < arraysize(kDLLNames); ++i) {
103 FilePath dll_path = GetTestDataPath(); 141 FilePath dll_path = GetTestDataPath();
104 dll_path = dll_path.Append(kDLLNames[i]); 142 dll_path = dll_path.Append(kDLLNames[i]);
105 143
106 std::unique_ptr<FileVersionInfo> version_info( 144 TypeParam factory(dll_path);
107 FileVersionInfo::CreateFileVersionInfo(dll_path)); 145 std::unique_ptr<FileVersionInfo> version_info(factory.Create());
146 ASSERT_TRUE(version_info);
108 147
109 EXPECT_EQ(kExpected[i], version_info->is_official_build()); 148 EXPECT_EQ(kExpectedIsOfficialBuild[i], version_info->is_official_build());
110 } 149 }
111 } 150 }
112 #endif
113 151
114 #if defined(OS_WIN) 152 TYPED_TEST(FileVersionInfoTest, CustomProperties) {
115 TEST(FileVersionInfoTest, CustomProperties) {
116 FilePath dll_path = GetTestDataPath(); 153 FilePath dll_path = GetTestDataPath();
117 dll_path = dll_path.AppendASCII("FileVersionInfoTest1.dll"); 154 dll_path = dll_path.AppendASCII("FileVersionInfoTest1.dll");
118 155
119 std::unique_ptr<FileVersionInfo> version_info( 156 TypeParam factory(dll_path);
120 FileVersionInfo::CreateFileVersionInfo(dll_path)); 157 std::unique_ptr<FileVersionInfo> version_info(factory.Create());
158 ASSERT_TRUE(version_info);
121 159
122 // Test few existing properties. 160 // Test few existing properties.
123 std::wstring str; 161 std::wstring str;
124 FileVersionInfoWin* version_info_win = 162 FileVersionInfoWin* version_info_win =
125 static_cast<FileVersionInfoWin*>(version_info.get()); 163 static_cast<FileVersionInfoWin*>(version_info.get());
126 EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 1", &str)); 164 EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 1", &str));
127 EXPECT_EQ(L"Un", str); 165 EXPECT_EQ(L"Un", str);
128 EXPECT_EQ(L"Un", version_info_win->GetStringValue(L"Custom prop 1")); 166 EXPECT_EQ(L"Un", version_info_win->GetStringValue(L"Custom prop 1"));
129 167
130 EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 2", &str)); 168 EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 2", &str));
131 EXPECT_EQ(L"Deux", str); 169 EXPECT_EQ(L"Deux", str);
132 EXPECT_EQ(L"Deux", version_info_win->GetStringValue(L"Custom prop 2")); 170 EXPECT_EQ(L"Deux", version_info_win->GetStringValue(L"Custom prop 2"));
133 171
134 EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 3", &str)); 172 EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 3", &str));
135 EXPECT_EQ(L"1600 Amphitheatre Parkway Mountain View, CA 94043", str); 173 EXPECT_EQ(L"1600 Amphitheatre Parkway Mountain View, CA 94043", str);
136 EXPECT_EQ(L"1600 Amphitheatre Parkway Mountain View, CA 94043", 174 EXPECT_EQ(L"1600 Amphitheatre Parkway Mountain View, CA 94043",
137 version_info_win->GetStringValue(L"Custom prop 3")); 175 version_info_win->GetStringValue(L"Custom prop 3"));
138 176
139 // Test an non-existing property. 177 // Test an non-existing property.
140 EXPECT_FALSE(version_info_win->GetValue(L"Unknown property", &str)); 178 EXPECT_FALSE(version_info_win->GetValue(L"Unknown property", &str));
141 EXPECT_EQ(L"", version_info_win->GetStringValue(L"Unknown property")); 179 EXPECT_EQ(L"", version_info_win->GetStringValue(L"Unknown property"));
142 } 180 }
143 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698