1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/web_applications/chrome_pwa_launcher/launcher_update.h"
#include <string_view>
#include <vector>
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
#include "chrome/common/chrome_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace web_app {
namespace {
void CreateFileAndWriteData(const base::FilePath& file_path,
std::string_view data) {
ASSERT_TRUE(base::WriteFile(file_path, data));
}
} // namespace
class WebAppLauncherUpdateTest : public testing::Test {
protected:
WebAppLauncherUpdateTest() = default;
void SetUp() override {
// Create mock chrome_pwa_launcher.exe in <current dir>/<current version>/,
// where UpdatePwaLaunchers() expects it to be.
base::FilePath current_dir_path;
ASSERT_TRUE(base::PathService::Get(base::DIR_EXE, ¤t_dir_path));
const base::FilePath mock_version_dir_path =
current_dir_path.AppendASCII(chrome::kChromeVersion);
ASSERT_TRUE(mock_version_dir_.Set(mock_version_dir_path));
latest_version_path_ = mock_version_dir_path.Append(
FILE_PATH_LITERAL("chrome_pwa_launcher.exe"));
ASSERT_NO_FATAL_FAILURE(
CreateFileAndWriteData(latest_version_path_, "up-to-date content"));
// Create a separate temp directory to store mock launchers, to test
// launcher update across directories.
ASSERT_TRUE(mock_launcher_dir_.CreateUniqueTempDir());
mock_launcher_dir_path_ = mock_launcher_dir_.GetPath();
}
void TearDown() override {
// Fail the test if any created temporary directories can't be deleted.
EXPECT_TRUE(mock_version_dir_.Delete());
EXPECT_TRUE(mock_launcher_dir_.Delete());
}
// Create a file at path |mock_launcher_dir_path_|/|launcher_name| containing
// "obsolete content" and add it to |launcher_paths_|.
void CreateMockLauncher(const base::FilePath::StringType launcher_name) {
const base::FilePath launcher_path =
mock_launcher_dir_path_.Append(launcher_name);
ASSERT_NO_FATAL_FAILURE(
CreateFileAndWriteData(launcher_path, "obsolete content"));
launcher_paths_.push_back(launcher_path);
}
// Returns true if all launchers' contents match |latest_version_path_|'s
// contents, false if any launcher's contents do not.
bool AreLaunchersUpToDate() {
for (const auto& path : launcher_paths_) {
if (!base::ContentsEqual(path, latest_version_path_))
return false;
}
return true;
}
const std::vector<base::FilePath>& launcher_paths() const {
return launcher_paths_;
}
const base::FilePath& mock_launcher_dir_path() const {
return mock_launcher_dir_path_;
}
private:
base::FilePath latest_version_path_;
std::vector<base::FilePath> launcher_paths_;
base::FilePath mock_launcher_dir_path_;
base::ScopedTempDir mock_launcher_dir_;
base::ScopedTempDir mock_version_dir_;
};
TEST_F(WebAppLauncherUpdateTest, UpdatePwaLaunchers) {
CreateMockLauncher(FILE_PATH_LITERAL("launcher 1"));
CreateMockLauncher(FILE_PATH_LITERAL("launcher 2"));
ASSERT_FALSE(AreLaunchersUpToDate());
UpdatePwaLaunchers(launcher_paths());
ASSERT_TRUE(AreLaunchersUpToDate());
}
TEST_F(WebAppLauncherUpdateTest, DeleteOldLauncherVersions) {
CreateMockLauncher(FILE_PATH_LITERAL("launcher"));
const base::FilePath old_launcher_1 =
mock_launcher_dir_path().Append(FILE_PATH_LITERAL("launcher_old (1)"));
const base::FilePath old_launcher_3 =
mock_launcher_dir_path().Append(FILE_PATH_LITERAL("launcher_old (3)"));
CreateFileAndWriteData(old_launcher_1, "");
CreateFileAndWriteData(old_launcher_3, "");
ASSERT_TRUE(base::PathExists(old_launcher_1));
ASSERT_TRUE(base::PathExists(old_launcher_3));
UpdatePwaLaunchers(launcher_paths());
ASSERT_FALSE(base::PathExists(old_launcher_1));
ASSERT_FALSE(base::PathExists(old_launcher_3));
}
} // namespace web_app
|