0

Tasks-Api: Fill in TestTasksDelegate

This CL fills in the body of TestTasksDelegate so that we can use it for
testing in Focus Mode.

Note: this is essentially the same as https://crrev.com/c/5319140, but
with an implementation that hopefully won't have to be reverted.

BUG=b:319307346

Change-Id: I00a49f6e1035bba6b8e3956f55edacdcb619b12c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5452732
Commit-Queue: Daniel Andersson <dandersson@chromium.org>
Reviewed-by: Richard Chui <richui@chromium.org>
Reviewed-by: Toni Barzic <tbarzic@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1287598}
This commit is contained in:
Daniel Andersson
2024-04-15 20:50:36 +00:00
committed by Chromium LUCI CQ
parent c72fbf2d15
commit 226af9daf6
2 changed files with 56 additions and 5 deletions

@ -5,6 +5,7 @@
#include "ash/api/tasks/test_tasks_delegate.h"
#include "ash/api/tasks/tasks_client.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "components/account_id/account_id.h"
@ -16,25 +17,32 @@ TestTasksDelegate::~TestTasksDelegate() = default;
void TestTasksDelegate::UpdateClientForProfileSwitch(
const AccountId& account_id) {
NOTIMPLEMENTED();
SetActiveAccount(account_id);
if (clients_.find(account_id) == clients_.end()) {
LOG(WARNING) << "No FakeTasksClient exists for active account";
} else {
GetClientForActiveAccount().InvalidateCache();
}
}
void TestTasksDelegate::GetTaskLists(
bool force_fetch,
TasksClient::GetTaskListsCallback callback) {
NOTIMPLEMENTED();
GetClientForActiveAccount().GetTaskLists(force_fetch, std::move(callback));
}
void TestTasksDelegate::GetTasks(const std::string& task_list_id,
bool force_fetch,
TasksClient::GetTasksCallback callback) {
NOTIMPLEMENTED();
GetClientForActiveAccount().GetTasks(task_list_id, force_fetch,
std::move(callback));
}
void TestTasksDelegate::AddTask(const std::string& task_list_id,
const std::string& title,
TasksClient::OnTaskSavedCallback callback) {
NOTIMPLEMENTED();
GetClientForActiveAccount().AddTask(task_list_id, title, std::move(callback));
}
void TestTasksDelegate::UpdateTask(const std::string& task_list_id,
@ -42,7 +50,26 @@ void TestTasksDelegate::UpdateTask(const std::string& task_list_id,
const std::string& title,
bool completed,
TasksClient::OnTaskSavedCallback callback) {
NOTIMPLEMENTED();
GetClientForActiveAccount().UpdateTask(task_list_id, task_id, title,
completed, std::move(callback));
}
void TestTasksDelegate::AddFakeTasksClient(
const AccountId& account_id,
std::unique_ptr<TasksClient> tasks_client) {
clients_.insert_or_assign(account_id, std::move(tasks_client));
}
void TestTasksDelegate::SetActiveAccount(const AccountId& account_id) {
active_account_id_ = account_id;
}
TasksClient& TestTasksDelegate::GetClientForActiveAccount() {
CHECK(active_account_id_.has_value());
auto it = clients_.find(*active_account_id_);
CHECK(it != clients_.end())
<< "No client registered for account: " << *active_account_id_;
return *it->second;
}
} // namespace ash::api

@ -5,11 +5,17 @@
#ifndef ASH_API_TASKS_TEST_TASKS_DELEGATE_H_
#define ASH_API_TASKS_TEST_TASKS_DELEGATE_H_
#include <optional>
#include "ash/api/tasks/tasks_delegate.h"
#include "ash/ash_export.h"
#include "base/containers/flat_map.h"
#include "components/account_id/account_id.h"
namespace ash::api {
class TasksClient;
class ASH_EXPORT TestTasksDelegate : public TasksDelegate {
public:
TestTasksDelegate();
@ -32,6 +38,24 @@ class ASH_EXPORT TestTasksDelegate : public TasksDelegate {
const std::string& title,
bool completed,
TasksClient::OnTaskSavedCallback callback) override;
// Helper function for adding pre-build `TasksClient` objects.
void AddFakeTasksClient(const AccountId& account_id,
std::unique_ptr<TasksClient> tasks_client);
// Switches the active account for the delegate.
void SetActiveAccount(const AccountId& account_id);
private:
// Returns a reference to the client registered for a current
// account. CHECK-fails if no client is registered for it.
TasksClient& GetClientForActiveAccount();
// The currently active account.
std::optional<AccountId> active_account_id_;
// Maps account IDs to clients.
base::flat_map<AccountId, std::unique_ptr<TasksClient>> clients_;
};
} // namespace ash::api