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

Modify FullRestoreReadAndSaveTest unit tests to add windows tests.

Modify the full restore component parameter to use the consistent
parameter name 'profile_path'.

BUG=1146900

Change-Id: I9dadccd5811eb1ad56c9e01149941a65ff8912cc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2620119
Commit-Queue: Nancy Wang <nancylingwang@chromium.org>
Reviewed-by: default avatarSammie Quon <sammiequon@chromium.org>
Cr-Commit-Position: refs/heads/master@{#842388}
parent 3a86ff3b
...@@ -52,5 +52,6 @@ source_set("unit_tests") { ...@@ -52,5 +52,6 @@ source_set("unit_tests") {
":full_restore", ":full_restore",
"//content/test:test_support", "//content/test:test_support",
"//testing/gtest", "//testing/gtest",
"//ui/aura:test_support",
] ]
} }
...@@ -17,17 +17,23 @@ ...@@ -17,17 +17,23 @@
#include "components/full_restore/full_restore_save_handler.h" #include "components/full_restore/full_restore_save_handler.h"
#include "components/full_restore/full_restore_utils.h" #include "components/full_restore/full_restore_utils.h"
#include "components/full_restore/restore_data.h" #include "components/full_restore/restore_data.h"
#include "components/full_restore/window_info.h"
#include "content/public/test/browser_task_environment.h" #include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/test/test_windows.h"
#include "ui/aura/window.h"
namespace full_restore { namespace full_restore {
namespace { namespace {
const char kAppId[] = "aaa"; constexpr char kAppId[] = "aaa";
const int32_t kId1 = 100; constexpr int32_t kId1 = 100;
const int32_t kId2 = 200; constexpr int32_t kId2 = 200;
constexpr int32_t kActivationIndex1 = 100;
constexpr int32_t kActivationIndex2 = 101;
} // namespace } // namespace
...@@ -71,7 +77,19 @@ class FullRestoreReadAndSaveTest : public testing::Test { ...@@ -71,7 +77,19 @@ class FullRestoreReadAndSaveTest : public testing::Test {
file_path, std::make_unique<full_restore::AppLaunchInfo>(kAppId, id)); file_path, std::make_unique<full_restore::AppLaunchInfo>(kAppId, id));
} }
void VerifyRestoreData(const base::FilePath& file_path, int32_t id) { void CreateWindowInfo(int32_t id, int32_t index) {
std::unique_ptr<aura::Window> window(
aura::test::CreateTestWindowWithId(id, nullptr));
WindowInfo window_info;
window_info.window = window.get();
window->SetProperty(full_restore::kWindowIdKey, id);
window_info.activation_index = index;
full_restore::SaveWindowInfo(window_info);
}
void VerifyRestoreData(const base::FilePath& file_path,
int32_t id,
int32_t index) {
ReadFromFile(file_path); ReadFromFile(file_path);
const auto* restore_data = GetRestoreData(file_path); const auto* restore_data = GetRestoreData(file_path);
...@@ -88,6 +106,10 @@ class FullRestoreReadAndSaveTest : public testing::Test { ...@@ -88,6 +106,10 @@ class FullRestoreReadAndSaveTest : public testing::Test {
// Verify for |id| // Verify for |id|
const auto app_restore_data_it = launch_list_it->second.find(id); const auto app_restore_data_it = launch_list_it->second.find(id);
EXPECT_TRUE(app_restore_data_it != launch_list_it->second.end()); EXPECT_TRUE(app_restore_data_it != launch_list_it->second.end());
const auto& data = app_restore_data_it->second;
EXPECT_TRUE(data->activation_index.has_value());
EXPECT_EQ(index, data->activation_index.value());
} }
content::BrowserTaskEnvironment& task_environment() { content::BrowserTaskEnvironment& task_environment() {
...@@ -112,15 +134,23 @@ TEST_F(FullRestoreReadAndSaveTest, SaveAndReadRestoreData) { ...@@ -112,15 +134,23 @@ TEST_F(FullRestoreReadAndSaveTest, SaveAndReadRestoreData) {
FullRestoreSaveHandler* save_handler = FullRestoreSaveHandler::GetInstance(); FullRestoreSaveHandler* save_handler = FullRestoreSaveHandler::GetInstance();
base::OneShotTimer* timer = save_handler->GetTimerForTesting(); base::OneShotTimer* timer = save_handler->GetTimerForTesting();
// Add app launch info, and verity the timer starts. // Add app launch info, and verify the timer starts.
AddAppLaunchInfo(GetPath(), kId1); AddAppLaunchInfo(GetPath(), kId1);
EXPECT_TRUE(timer->IsRunning()); EXPECT_TRUE(timer->IsRunning());
// Add one more app launch info, and verity the timer is still running. // Add one more app launch info, and verify the timer is still running.
AddAppLaunchInfo(GetPath(), kId2); AddAppLaunchInfo(GetPath(), kId2);
EXPECT_TRUE(timer->IsRunning()); EXPECT_TRUE(timer->IsRunning());
// Simulate timeout, and verity the timer stops. // Simulate timeout, and verify the timer stops.
timer->FireNow();
CreateWindowInfo(kId2, kActivationIndex2);
task_environment().RunUntilIdle();
EXPECT_FALSE(timer->IsRunning());
// Modify the window info, and verify the timer starts.
CreateWindowInfo(kId1, kActivationIndex1);
EXPECT_TRUE(timer->IsRunning());
timer->FireNow(); timer->FireNow();
task_environment().RunUntilIdle(); task_environment().RunUntilIdle();
...@@ -142,9 +172,17 @@ TEST_F(FullRestoreReadAndSaveTest, SaveAndReadRestoreData) { ...@@ -142,9 +172,17 @@ TEST_F(FullRestoreReadAndSaveTest, SaveAndReadRestoreData) {
const auto app_restore_data_it1 = launch_list_it->second.find(kId1); const auto app_restore_data_it1 = launch_list_it->second.find(kId1);
EXPECT_TRUE(app_restore_data_it1 != launch_list_it->second.end()); EXPECT_TRUE(app_restore_data_it1 != launch_list_it->second.end());
const auto& data1 = app_restore_data_it1->second;
EXPECT_TRUE(data1->activation_index.has_value());
EXPECT_EQ(kActivationIndex1, data1->activation_index.value());
// Verify for |kId2| // Verify for |kId2|
const auto app_restore_data_it2 = launch_list_it->second.find(kId2); const auto app_restore_data_it2 = launch_list_it->second.find(kId2);
EXPECT_TRUE(app_restore_data_it2 != launch_list_it->second.end()); EXPECT_TRUE(app_restore_data_it2 != launch_list_it->second.end());
const auto& data2 = app_restore_data_it2->second;
EXPECT_TRUE(data2->activation_index.has_value());
EXPECT_EQ(kActivationIndex2, data2->activation_index.value());
} }
TEST_F(FullRestoreReadAndSaveTest, MultipleFilePaths) { TEST_F(FullRestoreReadAndSaveTest, MultipleFilePaths) {
...@@ -156,20 +194,28 @@ TEST_F(FullRestoreReadAndSaveTest, MultipleFilePaths) { ...@@ -156,20 +194,28 @@ TEST_F(FullRestoreReadAndSaveTest, MultipleFilePaths) {
ASSERT_TRUE(tmp_dir1.CreateUniqueTempDir()); ASSERT_TRUE(tmp_dir1.CreateUniqueTempDir());
ASSERT_TRUE(tmp_dir2.CreateUniqueTempDir()); ASSERT_TRUE(tmp_dir2.CreateUniqueTempDir());
// Add app launch info for |tmp_dir1|, and verity the timer starts. // Add app launch info for |tmp_dir1|, and verify the timer starts.
AddAppLaunchInfo(tmp_dir1.GetPath(), kId1); AddAppLaunchInfo(tmp_dir1.GetPath(), kId1);
EXPECT_TRUE(timer->IsRunning()); EXPECT_TRUE(timer->IsRunning());
// Add app launch info for |tmp_dir2|, and verity the timer is still running. // Add app launch info for |tmp_dir2|, and verify the timer is still running.
AddAppLaunchInfo(tmp_dir2.GetPath(), kId2); AddAppLaunchInfo(tmp_dir2.GetPath(), kId2);
EXPECT_TRUE(timer->IsRunning()); EXPECT_TRUE(timer->IsRunning());
// Simulate timeout, and verity the timer stops. // Simulate timeout, and verify the timer stops.
timer->FireNow();
CreateWindowInfo(kId2, kActivationIndex2);
task_environment().RunUntilIdle();
EXPECT_FALSE(timer->IsRunning());
// Modify the window info, and verify the timer starts.
CreateWindowInfo(kId1, kActivationIndex1);
EXPECT_TRUE(timer->IsRunning());
timer->FireNow(); timer->FireNow();
task_environment().RunUntilIdle(); task_environment().RunUntilIdle();
VerifyRestoreData(tmp_dir1.GetPath(), kId1); VerifyRestoreData(tmp_dir1.GetPath(), kId1, kActivationIndex1);
VerifyRestoreData(tmp_dir2.GetPath(), kId2); VerifyRestoreData(tmp_dir2.GetPath(), kId2, kActivationIndex2);
} }
} // namespace full_restore } // namespace full_restore
...@@ -124,6 +124,10 @@ void FullRestoreSaveHandler::SaveWindowInfo(const WindowInfo& window_info) { ...@@ -124,6 +124,10 @@ void FullRestoreSaveHandler::SaveWindowInfo(const WindowInfo& window_info) {
profile_path_to_restore_data_[it->second.first].ModifyWindowInfo( profile_path_to_restore_data_[it->second.first].ModifyWindowInfo(
it->second.second, window_id, window_info); it->second.second, window_id, window_info);
pending_save_profile_paths_.insert(it->second.first);
MaybeStartSaveTimer();
} }
void FullRestoreSaveHandler::Flush(const base::FilePath& profile_path) { void FullRestoreSaveHandler::Flush(const base::FilePath& profile_path) {
...@@ -173,23 +177,24 @@ void FullRestoreSaveHandler::Save() { ...@@ -173,23 +177,24 @@ void FullRestoreSaveHandler::Save() {
pending_save_profile_paths_.clear(); pending_save_profile_paths_.clear();
} }
void FullRestoreSaveHandler::OnSaveFinished(const base::FilePath& file_path) { void FullRestoreSaveHandler::OnSaveFinished(
save_running_.erase(file_path); const base::FilePath& profile_path) {
save_running_.erase(profile_path);
} }
FullRestoreFileHandler* FullRestoreSaveHandler::GetFileHandler( FullRestoreFileHandler* FullRestoreSaveHandler::GetFileHandler(
const base::FilePath& file_path) { const base::FilePath& profile_path) {
if (profile_path_to_file_handler_.find(file_path) == if (profile_path_to_file_handler_.find(profile_path) ==
profile_path_to_file_handler_.end()) { profile_path_to_file_handler_.end()) {
profile_path_to_file_handler_[file_path] = profile_path_to_file_handler_[profile_path] =
base::MakeRefCounted<FullRestoreFileHandler>(file_path); base::MakeRefCounted<FullRestoreFileHandler>(profile_path);
} }
return profile_path_to_file_handler_[file_path].get(); return profile_path_to_file_handler_[profile_path].get();
} }
base::SequencedTaskRunner* FullRestoreSaveHandler::BackendTaskRunner( base::SequencedTaskRunner* FullRestoreSaveHandler::BackendTaskRunner(
const base::FilePath& file_path) { const base::FilePath& profile_path) {
return GetFileHandler(file_path)->owning_task_runner(); return GetFileHandler(profile_path)->owning_task_runner();
} }
} // namespace full_restore } // namespace full_restore
...@@ -54,11 +54,11 @@ class COMPONENT_EXPORT(FULL_RESTORE) FullRestoreSaveHandler ...@@ -54,11 +54,11 @@ class COMPONENT_EXPORT(FULL_RESTORE) FullRestoreSaveHandler
// aura::WindowObserver: // aura::WindowObserver:
void OnWindowDestroyed(aura::Window* window) override; void OnWindowDestroyed(aura::Window* window) override;
// Save |app_launch_info| to the full restore file in |profile_path|. // Saves |app_launch_info| to the full restore file in |profile_path|.
void SaveAppLaunchInfo(const base::FilePath& profile_path, void SaveAppLaunchInfo(const base::FilePath& profile_path,
std::unique_ptr<AppLaunchInfo> app_launch_info); std::unique_ptr<AppLaunchInfo> app_launch_info);
// Save |window_info| to |profile_path_to_restore_data_|. // Saves |window_info| to |profile_path_to_restore_data_|.
void SaveWindowInfo(const WindowInfo& window_info); void SaveWindowInfo(const WindowInfo& window_info);
// Flushes the full restore file in |profile_path| with the current restore // Flushes the full restore file in |profile_path| with the current restore
...@@ -78,12 +78,13 @@ class COMPONENT_EXPORT(FULL_RESTORE) FullRestoreSaveHandler ...@@ -78,12 +78,13 @@ class COMPONENT_EXPORT(FULL_RESTORE) FullRestoreSaveHandler
// Passes |profile_path_to_restore_data_| to the backend for saving. // Passes |profile_path_to_restore_data_| to the backend for saving.
void Save(); void Save();
// Invoked when write to file operation for |file_path| is finished. // Invoked when write to file operation for |profile_path| is finished.
void OnSaveFinished(const base::FilePath& file_path); void OnSaveFinished(const base::FilePath& profile_path);
FullRestoreFileHandler* GetFileHandler(const base::FilePath& file_path); FullRestoreFileHandler* GetFileHandler(const base::FilePath& profile_path);
base::SequencedTaskRunner* BackendTaskRunner(const base::FilePath& file_path); base::SequencedTaskRunner* BackendTaskRunner(
const base::FilePath& profile_path);
// Records whether there are new updates for saving between each saving delay. // Records whether there are new updates for saving between each saving delay.
// |pending_save_profile_paths_| is cleared when Save is invoked. // |pending_save_profile_paths_| is cleared when Save is invoked.
......
...@@ -17,13 +17,13 @@ namespace full_restore { ...@@ -17,13 +17,13 @@ namespace full_restore {
DEFINE_UI_CLASS_PROPERTY_KEY(int32_t, kWindowIdKey, 0) DEFINE_UI_CLASS_PROPERTY_KEY(int32_t, kWindowIdKey, 0)
DEFINE_UI_CLASS_PROPERTY_KEY(int32_t, kRestoreWindowIdKey, 0) DEFINE_UI_CLASS_PROPERTY_KEY(int32_t, kRestoreWindowIdKey, 0)
void SaveAppLaunchInfo(const base::FilePath& profile_dir, void SaveAppLaunchInfo(const base::FilePath& profile_path,
std::unique_ptr<AppLaunchInfo> app_launch_info) { std::unique_ptr<AppLaunchInfo> app_launch_info) {
if (!ash::features::IsFullRestoreEnabled() || !app_launch_info) if (!ash::features::IsFullRestoreEnabled() || !app_launch_info)
return; return;
FullRestoreSaveHandler::GetInstance()->SaveAppLaunchInfo( FullRestoreSaveHandler::GetInstance()->SaveAppLaunchInfo(
profile_dir, std::move(app_launch_info)); profile_path, std::move(app_launch_info));
} }
void SaveWindowInfo(const WindowInfo& window_info) { void SaveWindowInfo(const WindowInfo& window_info) {
......
...@@ -35,7 +35,7 @@ extern const ui::ClassProperty<int32_t>* const kRestoreWindowIdKey; ...@@ -35,7 +35,7 @@ extern const ui::ClassProperty<int32_t>* const kRestoreWindowIdKey;
// Saves the app launch parameters to the full restore file. // Saves the app launch parameters to the full restore file.
COMPONENT_EXPORT(FULL_RESTORE) COMPONENT_EXPORT(FULL_RESTORE)
void SaveAppLaunchInfo(const base::FilePath& profile_dir, void SaveAppLaunchInfo(const base::FilePath& profile_path,
std::unique_ptr<AppLaunchInfo> app_launch_info); std::unique_ptr<AppLaunchInfo> app_launch_info);
// Saves the window information to the full restore file. // Saves the window information to the full restore file.
......
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