OLD | NEW |
---|---|
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" |
13 #include "base/path_service.h" | 16 #include "base/path_service.h" |
14 #include "build/build_config.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 | 18 |
17 #if defined(OS_WIN) | |
18 #include "base/file_version_info_win.h" | |
19 #endif | |
20 | |
21 using base::FilePath; | 19 using base::FilePath; |
22 | 20 |
23 namespace { | 21 namespace { |
24 | 22 |
25 #if defined(OS_WIN) | |
26 FilePath GetTestDataPath() { | 23 FilePath GetTestDataPath() { |
27 FilePath path; | 24 FilePath path; |
28 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 25 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
29 path = path.AppendASCII("base"); | 26 path = path.AppendASCII("base"); |
30 path = path.AppendASCII("test"); | 27 path = path.AppendASCII("test"); |
31 path = path.AppendASCII("data"); | 28 path = path.AppendASCII("data"); |
32 path = path.AppendASCII("file_version_info_unittest"); | 29 path = path.AppendASCII("file_version_info_unittest"); |
33 return path; | 30 return path; |
34 } | 31 } |
35 #endif | 32 |
33 class FileVersionInfoFactory { | |
34 public: | |
35 FileVersionInfoFactory(const FilePath& path) : path_(path) {} | |
36 | |
37 FileVersionInfo* Create() const { | |
38 return FileVersionInfo::CreateFileVersionInfo(path_); | |
39 } | |
40 | |
41 private: | |
42 const FilePath path_; | |
43 | |
44 DISALLOW_COPY_AND_ASSIGN(FileVersionInfoFactory); | |
45 }; | |
46 | |
47 class FileVersionInfoForModuleFactory { | |
48 public: | |
49 FileVersionInfoForModuleFactory(const FilePath& path) | |
50 : library_(::LoadLibraryEx(path.value().c_str(), | |
grt (UTC plus 2)
2016/06/17 20:35:00
please add a brief comment explaining why base::Sc
fdoray
2016/06/20 14:06:41
ScopedNativeLibrary can be used for this. We just
| |
51 NULL, | |
52 LOAD_LIBRARY_AS_IMAGE_RESOURCE)) { | |
53 EXPECT_TRUE(library_); | |
54 } | |
55 ~FileVersionInfoForModuleFactory() { ::FreeLibrary(library_); } | |
56 | |
57 FileVersionInfo* Create() const { | |
58 return FileVersionInfo::CreateFileVersionInfoForModule(library_); | |
59 } | |
60 | |
61 private: | |
62 const HMODULE library_; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(FileVersionInfoForModuleFactory); | |
65 }; | |
66 | |
67 template <typename T> | |
68 class FileVersionInfoTest : public testing::Test {}; | |
69 | |
70 using FileVersionInfoFactories = | |
71 ::testing::Types<FileVersionInfoFactory, FileVersionInfoForModuleFactory>; | |
36 | 72 |
37 } // namespace | 73 } // namespace |
38 | 74 |
39 #if defined(OS_WIN) | 75 TYPED_TEST_CASE(FileVersionInfoTest, FileVersionInfoFactories); |
40 TEST(FileVersionInfoTest, HardCodedProperties) { | 76 |
77 TYPED_TEST(FileVersionInfoTest, HardCodedProperties) { | |
41 const wchar_t kDLLName[] = {L"FileVersionInfoTest1.dll"}; | 78 const wchar_t kDLLName[] = {L"FileVersionInfoTest1.dll"}; |
42 | 79 |
43 const wchar_t* const kExpectedValues[15] = { | 80 const wchar_t* const kExpectedValues[15] = { |
44 // FileVersionInfoTest.dll | 81 // FileVersionInfoTest.dll |
45 L"Goooooogle", // company_name | 82 L"Goooooogle", // company_name |
46 L"Google", // company_short_name | 83 L"Google", // company_short_name |
47 L"This is the product name", // product_name | 84 L"This is the product name", // product_name |
48 L"This is the product short name", // product_short_name | 85 L"This is the product short name", // product_short_name |
49 L"The Internal Name", // internal_name | 86 L"The Internal Name", // internal_name |
50 L"4.3.2.1", // product_version | 87 L"4.3.2.1", // product_version |
51 L"Private build property", // private_build | 88 L"Private build property", // private_build |
52 L"Special build property", // special_build | 89 L"Special build property", // special_build |
53 L"This is a particularly interesting comment", // comments | 90 L"This is a particularly interesting comment", // comments |
54 L"This is the original filename", // original_filename | 91 L"This is the original filename", // original_filename |
55 L"This is my file description", // file_description | 92 L"This is my file description", // file_description |
56 L"1.2.3.4", // file_version | 93 L"1.2.3.4", // file_version |
57 L"This is the legal copyright", // legal_copyright | 94 L"This is the legal copyright", // legal_copyright |
58 L"This is the legal trademarks", // legal_trademarks | 95 L"This is the legal trademarks", // legal_trademarks |
59 L"This is the last change", // last_change | 96 L"This is the last change", // last_change |
60 }; | 97 }; |
61 | 98 |
62 FilePath dll_path = GetTestDataPath(); | 99 FilePath dll_path = GetTestDataPath(); |
63 dll_path = dll_path.Append(kDLLName); | 100 dll_path = dll_path.Append(kDLLName); |
64 | 101 |
65 std::unique_ptr<FileVersionInfo> version_info( | 102 typename TypeParam factory(dll_path); |
66 FileVersionInfo::CreateFileVersionInfo(dll_path)); | 103 std::unique_ptr<FileVersionInfo> version_info(factory.Create()); |
104 ASSERT_TRUE(version_info); | |
67 | 105 |
68 int j = 0; | 106 int j = 0; |
69 EXPECT_EQ(kExpectedValues[j++], version_info->company_name()); | 107 EXPECT_EQ(kExpectedValues[j++], version_info->company_name()); |
70 EXPECT_EQ(kExpectedValues[j++], version_info->company_short_name()); | 108 EXPECT_EQ(kExpectedValues[j++], version_info->company_short_name()); |
71 EXPECT_EQ(kExpectedValues[j++], version_info->product_name()); | 109 EXPECT_EQ(kExpectedValues[j++], version_info->product_name()); |
72 EXPECT_EQ(kExpectedValues[j++], version_info->product_short_name()); | 110 EXPECT_EQ(kExpectedValues[j++], version_info->product_short_name()); |
73 EXPECT_EQ(kExpectedValues[j++], version_info->internal_name()); | 111 EXPECT_EQ(kExpectedValues[j++], version_info->internal_name()); |
74 EXPECT_EQ(kExpectedValues[j++], version_info->product_version()); | 112 EXPECT_EQ(kExpectedValues[j++], version_info->product_version()); |
75 EXPECT_EQ(kExpectedValues[j++], version_info->private_build()); | 113 EXPECT_EQ(kExpectedValues[j++], version_info->private_build()); |
76 EXPECT_EQ(kExpectedValues[j++], version_info->special_build()); | 114 EXPECT_EQ(kExpectedValues[j++], version_info->special_build()); |
77 EXPECT_EQ(kExpectedValues[j++], version_info->comments()); | 115 EXPECT_EQ(kExpectedValues[j++], version_info->comments()); |
78 EXPECT_EQ(kExpectedValues[j++], version_info->original_filename()); | 116 EXPECT_EQ(kExpectedValues[j++], version_info->original_filename()); |
79 EXPECT_EQ(kExpectedValues[j++], version_info->file_description()); | 117 EXPECT_EQ(kExpectedValues[j++], version_info->file_description()); |
80 EXPECT_EQ(kExpectedValues[j++], version_info->file_version()); | 118 EXPECT_EQ(kExpectedValues[j++], version_info->file_version()); |
81 EXPECT_EQ(kExpectedValues[j++], version_info->legal_copyright()); | 119 EXPECT_EQ(kExpectedValues[j++], version_info->legal_copyright()); |
82 EXPECT_EQ(kExpectedValues[j++], version_info->legal_trademarks()); | 120 EXPECT_EQ(kExpectedValues[j++], version_info->legal_trademarks()); |
83 EXPECT_EQ(kExpectedValues[j++], version_info->last_change()); | 121 EXPECT_EQ(kExpectedValues[j++], version_info->last_change()); |
84 } | 122 } |
85 #endif | |
86 | 123 |
87 #if defined(OS_WIN) | 124 TYPED_TEST(FileVersionInfoTest, IsOfficialBuild) { |
88 TEST(FileVersionInfoTest, IsOfficialBuild) { | 125 const wchar_t* kDLLNames[] = {L"FileVersionInfoTest1.dll", |
grt (UTC plus 2)
2016/06/17 20:34:59
nit: const wchar_t* const
fdoray
2016/06/20 14:06:41
Done.
| |
89 const wchar_t* kDLLNames[] = { | 126 L"FileVersionInfoTest2.dll"}; |
90 L"FileVersionInfoTest1.dll", | |
91 L"FileVersionInfoTest2.dll" | |
92 }; | |
93 | 127 |
94 const bool kExpected[] = { | 128 const bool kExpected[] = { |
95 true, | 129 true, false, |
96 false, | |
97 }; | 130 }; |
98 | 131 |
99 // Test consistency check. | 132 // Test consistency check. |
100 ASSERT_EQ(arraysize(kDLLNames), arraysize(kExpected)); | 133 ASSERT_EQ(arraysize(kDLLNames), arraysize(kExpected)); |
101 | 134 |
102 for (size_t i = 0; i < arraysize(kDLLNames); ++i) { | 135 for (size_t i = 0; i < arraysize(kDLLNames); ++i) { |
103 FilePath dll_path = GetTestDataPath(); | 136 FilePath dll_path = GetTestDataPath(); |
104 dll_path = dll_path.Append(kDLLNames[i]); | 137 dll_path = dll_path.Append(kDLLNames[i]); |
105 | 138 |
106 std::unique_ptr<FileVersionInfo> version_info( | 139 typename TypeParam factory(dll_path); |
107 FileVersionInfo::CreateFileVersionInfo(dll_path)); | 140 std::unique_ptr<FileVersionInfo> version_info(factory.Create()); |
141 ASSERT_TRUE(version_info); | |
108 | 142 |
109 EXPECT_EQ(kExpected[i], version_info->is_official_build()); | 143 EXPECT_EQ(kExpected[i], version_info->is_official_build()); |
110 } | 144 } |
111 } | 145 } |
112 #endif | |
113 | 146 |
114 #if defined(OS_WIN) | 147 TYPED_TEST(FileVersionInfoTest, CustomProperties) { |
115 TEST(FileVersionInfoTest, CustomProperties) { | |
116 FilePath dll_path = GetTestDataPath(); | 148 FilePath dll_path = GetTestDataPath(); |
117 dll_path = dll_path.AppendASCII("FileVersionInfoTest1.dll"); | 149 dll_path = dll_path.AppendASCII("FileVersionInfoTest1.dll"); |
118 | 150 |
119 std::unique_ptr<FileVersionInfo> version_info( | 151 typename TypeParam factory(dll_path); |
120 FileVersionInfo::CreateFileVersionInfo(dll_path)); | 152 std::unique_ptr<FileVersionInfo> version_info(factory.Create()); |
153 ASSERT_TRUE(version_info); | |
121 | 154 |
122 // Test few existing properties. | 155 // Test few existing properties. |
123 std::wstring str; | 156 std::wstring str; |
124 FileVersionInfoWin* version_info_win = | 157 FileVersionInfoWin* version_info_win = |
125 static_cast<FileVersionInfoWin*>(version_info.get()); | 158 static_cast<FileVersionInfoWin*>(version_info.get()); |
126 EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 1", &str)); | 159 EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 1", &str)); |
127 EXPECT_EQ(L"Un", str); | 160 EXPECT_EQ(L"Un", str); |
128 EXPECT_EQ(L"Un", version_info_win->GetStringValue(L"Custom prop 1")); | 161 EXPECT_EQ(L"Un", version_info_win->GetStringValue(L"Custom prop 1")); |
129 | 162 |
130 EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 2", &str)); | 163 EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 2", &str)); |
131 EXPECT_EQ(L"Deux", str); | 164 EXPECT_EQ(L"Deux", str); |
132 EXPECT_EQ(L"Deux", version_info_win->GetStringValue(L"Custom prop 2")); | 165 EXPECT_EQ(L"Deux", version_info_win->GetStringValue(L"Custom prop 2")); |
133 | 166 |
134 EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 3", &str)); | 167 EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 3", &str)); |
135 EXPECT_EQ(L"1600 Amphitheatre Parkway Mountain View, CA 94043", str); | 168 EXPECT_EQ(L"1600 Amphitheatre Parkway Mountain View, CA 94043", str); |
136 EXPECT_EQ(L"1600 Amphitheatre Parkway Mountain View, CA 94043", | 169 EXPECT_EQ(L"1600 Amphitheatre Parkway Mountain View, CA 94043", |
137 version_info_win->GetStringValue(L"Custom prop 3")); | 170 version_info_win->GetStringValue(L"Custom prop 3")); |
138 | 171 |
139 // Test an non-existing property. | 172 // Test an non-existing property. |
140 EXPECT_FALSE(version_info_win->GetValue(L"Unknown property", &str)); | 173 EXPECT_FALSE(version_info_win->GetValue(L"Unknown property", &str)); |
141 EXPECT_EQ(L"", version_info_win->GetStringValue(L"Unknown property")); | 174 EXPECT_EQ(L"", version_info_win->GetStringValue(L"Unknown property")); |
142 } | 175 } |
143 #endif | |
OLD | NEW |