Commit 70db5398 authored by Nancy Wang's avatar Nancy Wang Committed by Chromium LUCI CQ

Modify the full restore file reading function to get the restore data.

Add the callback function as the parameter of
FullRestoreReadHandler::ReadFromFile, to get the restore data read from
the restore data.

Add unit tests to test FullRestoreReadHandler and
FullRestoreSaveHandler to verify the full restore file read and write
process.

BUG=1146900

Change-Id: Id18405c1f68274726fe28008c196607bf759d2fc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2612656
Commit-Queue: Nancy Wang <nancylingwang@chromium.org>
Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#840900}
parent 4164496e
......@@ -44,11 +44,13 @@ source_set("unit_tests") {
sources = [
"full_restore_info_unittest.cc",
"full_restore_read_and_save_unittest.cc",
"restore_data_unittest.cc",
]
deps = [
":full_restore",
"//content/test:test_support",
"//testing/gtest",
]
}
......@@ -3,6 +3,7 @@ include_rules = [
"+chromeos/ui/base/window_state_type.h",
"+components/account_id/account_id.h",
"+components/services/app_service/public",
"+content/public/test",
"+ui",
]
......
// Copyright 2021 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 <memory>
#include <utility>
#include "ash/public/cpp/ash_features.h"
#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/timer/timer.h"
#include "components/full_restore/app_launch_info.h"
#include "components/full_restore/app_restore_data.h"
#include "components/full_restore/full_restore_read_handler.h"
#include "components/full_restore/full_restore_save_handler.h"
#include "components/full_restore/full_restore_utils.h"
#include "components/full_restore/restore_data.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace full_restore {
namespace {
const char kAppId[] = "aaa";
const int32_t kId1 = 100;
const int32_t kId2 = 200;
} // namespace
// Unit tests for restore data.
class FullRestoreReadAndSaveTest : public testing::Test {
public:
FullRestoreReadAndSaveTest() = default;
~FullRestoreReadAndSaveTest() override = default;
FullRestoreReadAndSaveTest(const FullRestoreReadAndSaveTest&) = delete;
FullRestoreReadAndSaveTest& operator=(const FullRestoreReadAndSaveTest&) =
delete;
void SetUp() override {
scoped_feature_list_.InitAndEnableFeature(ash::features::kFullRestore);
ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir());
}
const base::FilePath& GetPath() { return tmp_dir_.GetPath(); }
void ReadFromFile(const base::FilePath& file_path) {
FullRestoreReadHandler* read_handler =
FullRestoreReadHandler::GetInstance();
base::RunLoop run_loop;
read_handler->ReadFromFile(
file_path, base::BindLambdaForTesting(
[&](std::unique_ptr<RestoreData> restore_data) {
run_loop.Quit();
restore_data_ = std::move(restore_data);
}));
run_loop.Run();
}
const RestoreData* GetRestoreData(const base::FilePath& file_path) {
return restore_data_.get();
}
void AddAppLaunchInfo(const base::FilePath& file_path, int32_t id) {
full_restore::SaveAppLaunchInfo(
file_path, std::make_unique<full_restore::AppLaunchInfo>(kAppId, id));
}
void VerifyRestoreData(const base::FilePath& file_path, int32_t id) {
ReadFromFile(file_path);
const auto* restore_data = GetRestoreData(file_path);
ASSERT_TRUE(restore_data != nullptr);
const auto& launch_list = restore_data->app_id_to_launch_list();
EXPECT_EQ(1u, launch_list.size());
// Verify for |kAppId|
const auto launch_list_it = launch_list.find(kAppId);
EXPECT_TRUE(launch_list_it != launch_list.end());
EXPECT_EQ(1u, launch_list_it->second.size());
// Verify for |id|
const auto app_restore_data_it = launch_list_it->second.find(id);
EXPECT_TRUE(app_restore_data_it != launch_list_it->second.end());
}
content::BrowserTaskEnvironment& task_environment() {
return task_environment_;
}
private:
content::BrowserTaskEnvironment task_environment_;
base::ScopedTempDir tmp_dir_;
base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<RestoreData> restore_data_;
};
TEST_F(FullRestoreReadAndSaveTest, ReadEmptyRestoreData) {
ReadFromFile(GetPath());
EXPECT_FALSE(GetRestoreData(GetPath()));
}
TEST_F(FullRestoreReadAndSaveTest, SaveAndReadRestoreData) {
FullRestoreSaveHandler* save_handler = FullRestoreSaveHandler::GetInstance();
base::OneShotTimer* timer = save_handler->GetTimerForTesting();
// Add app launch info, and verity the timer starts.
AddAppLaunchInfo(GetPath(), kId1);
EXPECT_TRUE(timer->IsRunning());
// Add one more app launch info, and verity the timer is still running.
AddAppLaunchInfo(GetPath(), kId2);
EXPECT_TRUE(timer->IsRunning());
// Simulate timeout, and verity the timer stops.
timer->FireNow();
task_environment().RunUntilIdle();
ReadFromFile(GetPath());
// Verify the restore data can be read correctly.
const auto* restore_data = GetRestoreData(GetPath());
ASSERT_TRUE(restore_data);
const auto& launch_list = restore_data->app_id_to_launch_list();
EXPECT_EQ(1u, launch_list.size());
// Verify for |kAppId|
const auto launch_list_it = launch_list.find(kAppId);
EXPECT_TRUE(launch_list_it != launch_list.end());
EXPECT_EQ(2u, launch_list_it->second.size());
// Verify for |kId1|
const auto app_restore_data_it1 = launch_list_it->second.find(kId1);
EXPECT_TRUE(app_restore_data_it1 != launch_list_it->second.end());
// Verify for |kId2|
const auto app_restore_data_it2 = launch_list_it->second.find(kId2);
EXPECT_TRUE(app_restore_data_it2 != launch_list_it->second.end());
}
TEST_F(FullRestoreReadAndSaveTest, MultipleFilePaths) {
FullRestoreSaveHandler* save_handler = FullRestoreSaveHandler::GetInstance();
base::OneShotTimer* timer = save_handler->GetTimerForTesting();
base::ScopedTempDir tmp_dir1;
base::ScopedTempDir tmp_dir2;
ASSERT_TRUE(tmp_dir1.CreateUniqueTempDir());
ASSERT_TRUE(tmp_dir2.CreateUniqueTempDir());
// Add app launch info for |tmp_dir1|, and verity the timer starts.
AddAppLaunchInfo(tmp_dir1.GetPath(), kId1);
EXPECT_TRUE(timer->IsRunning());
// Add app launch info for |tmp_dir2|, and verity the timer is still running.
AddAppLaunchInfo(tmp_dir2.GetPath(), kId2);
EXPECT_TRUE(timer->IsRunning());
// Simulate timeout, and verity the timer stops.
timer->FireNow();
task_environment().RunUntilIdle();
VerifyRestoreData(tmp_dir1.GetPath(), kId1);
VerifyRestoreData(tmp_dir2.GetPath(), kId2);
}
} // namespace full_restore
......@@ -25,19 +25,23 @@ FullRestoreReadHandler::FullRestoreReadHandler() = default;
FullRestoreReadHandler::~FullRestoreReadHandler() = default;
void FullRestoreReadHandler::ReadFromFile(const base::FilePath& file_path) {
auto file_handler = base::MakeRefCounted<FullRestoreFileHandler>(file_path);
void FullRestoreReadHandler::ReadFromFile(const base::FilePath& profile_path,
Callback callback) {
auto file_handler =
base::MakeRefCounted<FullRestoreFileHandler>(profile_path);
file_handler->owning_task_runner()->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&FullRestoreFileHandler::ReadFromFile, file_handler.get()),
base::BindOnce(&FullRestoreReadHandler::OnGetRestoreData,
weak_factory_.GetWeakPtr(), file_path));
weak_factory_.GetWeakPtr(), profile_path,
std::move(callback)));
}
void FullRestoreReadHandler::OnGetRestoreData(
const base::FilePath& file_path,
const base::FilePath& profile_path,
Callback callback,
std::unique_ptr<RestoreData> restore_data) {
// TODO(crbug.com/1146900): Implement the restore_data saving.
std::move(callback).Run(std::move(restore_data));
}
} // namespace full_restore
......@@ -7,6 +7,7 @@
#include <memory>
#include "base/callback.h"
#include "base/component_export.h"
#include "base/memory/weak_ptr.h"
......@@ -25,6 +26,10 @@ class RestoreData;
// actual reading.
class COMPONENT_EXPORT(FULL_RESTORE) FullRestoreReadHandler {
public:
// The callback function to get the restore data when the reading operation is
// done.
using Callback = base::OnceCallback<void(std::unique_ptr<RestoreData>)>;
static FullRestoreReadHandler* GetInstance();
FullRestoreReadHandler();
......@@ -33,10 +38,15 @@ class COMPONENT_EXPORT(FULL_RESTORE) FullRestoreReadHandler {
FullRestoreReadHandler(const FullRestoreReadHandler&) = delete;
FullRestoreReadHandler& operator=(const FullRestoreReadHandler&) = delete;
void ReadFromFile(const base::FilePath& profile_dir);
// Reads the restore data from |profile_path| on a background task runner, and
// calls |callback| when the reading operation is done.
void ReadFromFile(const base::FilePath& profile_path, Callback callback);
private:
void OnGetRestoreData(const base::FilePath& file_path,
// Invoked when reading the restore data from |profile_path| is finished, and
// calls |callback| to notify that the reading operation is done.
void OnGetRestoreData(const base::FilePath& profile_path,
Callback callback,
std::unique_ptr<RestoreData>);
base::WeakPtrFactory<FullRestoreReadHandler> weak_factory_{this};
......
......@@ -49,6 +49,8 @@ class COMPONENT_EXPORT(FULL_RESTORE) FullRestoreSaveHandler {
// Save |window_info| to |profile_path_to_restore_data_|.
void SaveWindowInfo(const WindowInfo& window_info);
base::OneShotTimer* GetTimerForTesting() { return &save_timer_; }
private:
// Starts the timer that invokes Save (if timer isn't already running).
void MaybeStartSaveTimer();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment