Index: chrome/browser/conflicts/installed_programs_win_unittest.cc |
diff --git a/chrome/browser/conflicts/installed_programs_win_unittest.cc b/chrome/browser/conflicts/installed_programs_win_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..77f04644778d7a2555449c6a35ec030707182e1b |
--- /dev/null |
+++ b/chrome/browser/conflicts/installed_programs_win_unittest.cc |
@@ -0,0 +1,228 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/conflicts/installed_programs_win.h" |
+ |
+#include <map> |
+#include <utility> |
+ |
+#include "base/bind.h" |
+#include "base/macros.h" |
+#include "base/run_loop.h" |
+#include "base/strings/string_util.h" |
+#include "base/strings/stringprintf.h" |
+#include "base/test/scoped_task_scheduler.h" |
+#include "base/test/test_reg_util_win.h" |
+#include "base/win/registry.h" |
+#include "chrome/browser/conflicts/msi_util_win.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+struct FakeMSIProgram { |
+ base::string16 product_guid; |
+ bool is_system_component; |
+ bool is_microsoft_published; |
+ base::string16 display_name; |
+ base::string16 uninstall_string; |
+ std::vector<base::string16> components; |
+}; |
+ |
+class InstalledProgramsTest : public testing::Test { |
+ public: |
+ InstalledProgramsTest(); |
+ ~InstalledProgramsTest(); |
+ |
+ void SetUp() override; |
+ |
+ void SetUpFakeMSIProgram(const FakeMSIProgram& fake_msi_program); |
+ |
+ private: |
+ // Used to override the real GetMsiComponentPaths(). |
+ bool FakeGetMsiComponentPaths(const base::string16& product_guid, |
+ std::vector<base::string16>* component_paths); |
+ |
+ base::test::ScopedTaskScheduler scoped_task_scheduler_; |
+ registry_util::RegistryOverrideManager registry_override_manager_; |
+ |
+ // Override GetMsiComponentPaths(). The mapping from input to output is |
+ // managed by component_paths_map_. |
+ ScopedGetMsiComponentPathsOverride scoped_get_msi_component_paths_override_; |
+ std::map<base::string16, std::vector<base::string16>> component_paths_map_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(InstalledProgramsTest); |
+}; |
+ |
+InstalledProgramsTest::InstalledProgramsTest() |
+ : scoped_get_msi_component_paths_override_( |
+ base::Bind(&InstalledProgramsTest::FakeGetMsiComponentPaths, |
+ base::Unretained(this))) {} |
+ |
+InstalledProgramsTest::~InstalledProgramsTest() = default; |
+ |
+// ASSERT_NO_FATAL_FAILURE cannot be used in a constructor. |
+void InstalledProgramsTest::SetUp() { |
+ ASSERT_NO_FATAL_FAILURE( |
+ registry_override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE)); |
+ ASSERT_NO_FATAL_FAILURE( |
+ registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER)); |
+} |
+ |
+void InstalledProgramsTest::SetUpFakeMSIProgram( |
+ const FakeMSIProgram& fake_msi_program) { |
+ // Create the registry entry. |
+ const wchar_t kUninstallKeyPath[] = |
+ L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"; |
+ base::win::RegKey key( |
+ HKEY_CURRENT_USER, |
+ base::StringPrintf(L"%ls\\%ls", kUninstallKeyPath, |
+ fake_msi_program.product_guid.c_str()) |
+ .c_str(), |
+ KEY_WRITE); |
+ |
+ key.WriteValue(L"SystemComponent", |
+ fake_msi_program.is_system_component ? 1 : 0); |
+ key.WriteValue(L"UninstallString", fake_msi_program.uninstall_string.c_str()); |
+ if (fake_msi_program.is_microsoft_published) |
+ key.WriteValue(L"Publisher", L"Microsoft Corporation"); |
+ key.WriteValue(L"DisplayName", fake_msi_program.display_name.c_str()); |
+ |
+ component_paths_map_.insert(std::make_pair(fake_msi_program.product_guid, |
+ fake_msi_program.components)); |
+} |
+ |
+bool InstalledProgramsTest::FakeGetMsiComponentPaths( |
+ const base::string16& product_guid, |
+ std::vector<base::string16>* component_paths) { |
+ auto iter = component_paths_map_.find(product_guid); |
+ if (iter == component_paths_map_.end()) |
+ return false; |
+ |
+ *component_paths = iter->second; |
+ return true; |
+} |
+ |
+} // namespace |
+ |
+TEST_F(InstalledProgramsTest, InstalledPrograms) { |
+ // Some properties that are used multiple time are defined here to avoid |
+ // duplication. |
+ const wchar_t kValidDisplayName[] = L"ADisplayName"; |
+ const wchar_t kValidUninstallString[] = L"AnUninstallString"; |
+ const wchar_t kValidComponentPath[] = L"c:\\a\\valid\\file\\name.dll"; |
+ |
+ std::pair<FakeMSIProgram, bool> kTestCases[] = { |
+ { |
+ { |
+ L"", // Empty GUID. |
+ false, |
+ false, |
+ kValidDisplayName, |
+ kValidUninstallString, |
+ { |
+ kValidComponentPath, |
+ }, |
+ }, |
+ false, |
+ }, |
+ { |
+ { |
+ L"Empty DisplayName", |
+ false, |
+ false, |
+ L"", |
+ kValidUninstallString, |
+ { |
+ kValidComponentPath, |
+ }, |
+ }, |
+ false, |
+ }, |
+ { |
+ { |
+ L"Empty UninstallString", |
+ false, |
+ false, |
+ kValidDisplayName, |
+ L"", |
+ { |
+ kValidComponentPath, |
+ }, |
+ }, |
+ false, |
+ }, |
+ { |
+ { |
+ L"IsSystemComponent", |
+ true, |
+ false, |
+ kValidDisplayName, |
+ kValidUninstallString, |
+ { |
+ kValidComponentPath, |
+ }, |
+ }, |
+ false, |
+ }, |
+ { |
+ { |
+ L"MicrosoftPublished", |
+ false, |
+ true, |
+ kValidDisplayName, |
+ kValidUninstallString, |
+ { |
+ kValidComponentPath, |
+ }, |
+ }, |
+ false, |
+ }, |
+ { |
+ { |
+ L"NoValidComponents", |
+ false, |
+ false, |
+ kValidDisplayName, |
+ kValidUninstallString, |
+ { |
+ L"02:\\registry\\path", L"c:\\not\\dll\\path.txt", |
+ }, |
+ }, |
+ false, |
+ }, |
+ { |
+ { |
+ L"ValidEntry", |
+ false, |
+ false, |
+ L"Name5", |
+ kValidUninstallString, |
+ { |
+ L"c:\\valid\\path\\test.dll", L"c:\\valid\\path\\another.dll", |
+ }, |
+ }, |
+ true, |
+ }, |
+ }; |
+ |
+ for (const auto& test_case : kTestCases) |
+ SetUpFakeMSIProgram(test_case.first); |
+ |
+ InstalledPrograms installed_programs; |
+ |
+ installed_programs.Initialize(base::Closure()); |
+ |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ for (const auto& test_case : kTestCases) { |
+ for (const auto& path : test_case.first.components) { |
+ base::string16 display_name; |
+ EXPECT_EQ(test_case.second, installed_programs.GetInstalledProgramName( |
+ base::FilePath(path), &display_name)); |
+ if (test_case.second) { |
+ EXPECT_EQ(test_case.first.display_name, display_name); |
+ } |
+ } |
+ } |
+} |