Commit e802774b authored by Tanmoy Mollik's avatar Tanmoy Mollik Committed by Chromium LUCI CQ

Create PrimaryAccountChangeEvent class for consent-aware primary account API

This cl creates the event change class that will be used to send primary
account change notifications to the observers. This cl also adds unit
tests for PrimaryAccountChangeEvent.

Design Doc: go/consent-aware-api-dd

Bug: 1046746
Change-Id: I7c82dd3ba8c3296ec457fbe94893550f27386a58
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2592999Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Commit-Queue: Tanmoy Mollik <triploblastic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#838025}
parent a55f9d74
......@@ -34,6 +34,8 @@ source_set("identity_manager") {
"load_credentials_state.h",
"primary_account_access_token_fetcher.cc",
"primary_account_access_token_fetcher.h",
"primary_account_change_event.cc",
"primary_account_change_event.h",
"primary_account_mutator.h",
"scope_set.h",
"set_accounts_in_cookie_result.h",
......@@ -100,6 +102,7 @@ source_set("unit_tests") {
"identity_test_environment_unittest.cc",
"identity_utils_unittest.cc",
"primary_account_access_token_fetcher_unittest.cc",
"primary_account_change_event_unittest.cc",
"primary_account_mutator_unittest.cc",
]
......
// Copyright 2020 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 "components/signin/public/identity_manager/primary_account_change_event.h"
namespace signin {
PrimaryAccountChangeEvent::State::State() = default;
PrimaryAccountChangeEvent::State::State(const State& other) = default;
PrimaryAccountChangeEvent::State::State(CoreAccountInfo account_info,
ConsentLevel consent_level)
: primary_account(account_info), consent_level(consent_level) {}
PrimaryAccountChangeEvent::State::~State() = default;
PrimaryAccountChangeEvent::State& PrimaryAccountChangeEvent::State::operator=(
const State& other) = default;
PrimaryAccountChangeEvent::PrimaryAccountChangeEvent() = default;
PrimaryAccountChangeEvent::PrimaryAccountChangeEvent(State previous_state,
State current_state)
: previous_state_(previous_state), current_state_(current_state) {}
PrimaryAccountChangeEvent::~PrimaryAccountChangeEvent() = default;
PrimaryAccountChangeEvent::Type PrimaryAccountChangeEvent::GetEventTypeFor(
ConsentLevel consent_level) const {
if (previous_state_ == current_state_)
return Type::kNone;
if (previous_state_.consent_level == ConsentLevel::kSync) {
// Cannot change the Sync account without signing out first.
DCHECK(previous_state_.primary_account == current_state_.primary_account ||
current_state_.primary_account.IsEmpty());
}
if (previous_state_.primary_account == current_state_.primary_account) {
// Cannot change the consent level for the empty account.
DCHECK(!previous_state_.primary_account.IsEmpty());
}
switch (consent_level) {
case ConsentLevel::kNotRequired:
if (previous_state_.primary_account != current_state_.primary_account) {
return current_state_.primary_account.IsEmpty() ? Type::kCleared
: Type::kSet;
}
return Type::kNone;
case ConsentLevel::kSync:
if (previous_state_.consent_level != current_state_.consent_level) {
return current_state_.consent_level == ConsentLevel::kSync
? Type::kSet
: Type::kCleared;
}
// Cannot change the Sync account without signing out first.
DCHECK_EQ(current_state_.consent_level, ConsentLevel::kNotRequired);
return Type::kNone;
}
}
const PrimaryAccountChangeEvent::State&
PrimaryAccountChangeEvent::GetCurrentState() const {
return current_state_;
}
const PrimaryAccountChangeEvent::State&
PrimaryAccountChangeEvent::GetPreviousState() const {
return previous_state_;
}
bool operator==(const PrimaryAccountChangeEvent::State& lhs,
const PrimaryAccountChangeEvent::State& rhs) {
return lhs.primary_account == rhs.primary_account &&
lhs.consent_level == rhs.consent_level;
}
} // namespace signin
\ No newline at end of file
// Copyright 2020 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.
#ifndef COMPONENTS_SIGNIN_PUBLIC_IDENTITY_MANAGER_PRIMARY_ACCOUNT_CHANGE_EVENT_H_
#define COMPONENTS_SIGNIN_PUBLIC_IDENTITY_MANAGER_PRIMARY_ACCOUNT_CHANGE_EVENT_H_
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/consent_level.h"
namespace signin {
class PrimaryAccountChangeEvent {
public:
// Used to denote the type of the change event.
enum class Type {
// No change.
kNone = 0,
// Primary account set or changed.
kSet,
// Primary account cleared.
kCleared
};
struct State {
State();
State(const State& other);
State(CoreAccountInfo account_info, ConsentLevel consent_level);
~State();
State& operator=(const State& other);
CoreAccountInfo primary_account;
ConsentLevel consent_level = ConsentLevel::kNotRequired;
};
PrimaryAccountChangeEvent();
PrimaryAccountChangeEvent(State previous_state, State current_state);
~PrimaryAccountChangeEvent();
// Returns primary account change event type for the corresponding
// consent_level. There can be 3 different event types.
// kNone - No change in primary account for the given consent_level.
// kSet - A new primary account is set or changed for the given consent_level.
// kCleared - The primary account set for the consent level is cleared.
Type GetEventTypeFor(ConsentLevel consent_level) const;
const State& GetPreviousState() const;
const State& GetCurrentState() const;
private:
State previous_state_, current_state_;
};
bool operator==(const PrimaryAccountChangeEvent::State& lhs,
const PrimaryAccountChangeEvent::State& rhs);
} // namespace signin
#endif // COMPONENTS_SIGNIN_PUBLIC_IDENTITY_MANAGER_PRIMARY_ACCOUNT_CHANGE_EVENT_H_
// Copyright 2020 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 "components/signin/public/identity_manager/primary_account_change_event.h"
#include "components/signin/public/identity_manager/consent_level.h"
#include "google_apis/gaia/core_account_id.h"
#include "testing/gtest/include/gtest/gtest.h"
using signin::ConsentLevel;
using signin::PrimaryAccountChangeEvent;
using Type = signin::PrimaryAccountChangeEvent::Type;
using State = signin::PrimaryAccountChangeEvent::State;
class PrimaryAccountChangeEventTest : public testing::Test {
public:
PrimaryAccountChangeEventTest() {
CoreAccountInfo account_info1 = GetCoreAccountInfoFrom("account1@test.com");
CoreAccountInfo account_info2 = GetCoreAccountInfoFrom("account2@test.com");
empty_not_required_ = State(CoreAccountInfo(), ConsentLevel::kNotRequired);
account1_not_required_ = State(account_info1, ConsentLevel::kNotRequired);
account2_not_required_ = State(account_info2, ConsentLevel::kNotRequired);
account1_sync_ = State(account_info1, ConsentLevel::kSync);
account2_sync_ = State(account_info2, ConsentLevel::kSync);
}
State empty_not_required_;
State account1_not_required_;
State account2_not_required_;
State account1_sync_;
State account2_sync_;
private:
CoreAccountInfo GetCoreAccountInfoFrom(const char* account_name) {
CoreAccountInfo account_info;
account_info.account_id = CoreAccountId(account_name);
account_info.gaia = account_info.email = account_name;
return account_info;
}
};
TEST_F(PrimaryAccountChangeEventTest, NoStateChange) {
PrimaryAccountChangeEvent event(empty_not_required_, empty_not_required_);
EXPECT_EQ(Type::kNone, event.GetEventTypeFor(ConsentLevel::kNotRequired));
EXPECT_EQ(Type::kNone, event.GetEventTypeFor(ConsentLevel::kSync));
event =
PrimaryAccountChangeEvent(account1_not_required_, account1_not_required_);
EXPECT_EQ(Type::kNone, event.GetEventTypeFor(ConsentLevel::kNotRequired));
EXPECT_EQ(Type::kNone, event.GetEventTypeFor(ConsentLevel::kSync));
event = PrimaryAccountChangeEvent(account1_sync_, account1_sync_);
EXPECT_EQ(Type::kNone, event.GetEventTypeFor(ConsentLevel::kNotRequired));
EXPECT_EQ(Type::kNone, event.GetEventTypeFor(ConsentLevel::kSync));
}
TEST_F(PrimaryAccountChangeEventTest,
ConsentLevelChangeFromNotRequiredToNotRequired) {
PrimaryAccountChangeEvent event(empty_not_required_, account1_not_required_);
EXPECT_EQ(Type::kSet, event.GetEventTypeFor(ConsentLevel::kNotRequired));
EXPECT_EQ(Type::kNone, event.GetEventTypeFor(ConsentLevel::kSync));
event =
PrimaryAccountChangeEvent(account1_not_required_, account2_not_required_);
EXPECT_EQ(Type::kSet, event.GetEventTypeFor(ConsentLevel::kNotRequired));
EXPECT_EQ(Type::kNone, event.GetEventTypeFor(ConsentLevel::kSync));
event =
PrimaryAccountChangeEvent(account1_not_required_, empty_not_required_);
EXPECT_EQ(Type::kCleared, event.GetEventTypeFor(ConsentLevel::kNotRequired));
EXPECT_EQ(Type::kNone, event.GetEventTypeFor(ConsentLevel::kSync));
}
TEST_F(PrimaryAccountChangeEventTest, ConsentLevelChangeFromNotRequiredToSync) {
PrimaryAccountChangeEvent event(empty_not_required_, account1_sync_);
EXPECT_EQ(Type::kSet, event.GetEventTypeFor(ConsentLevel::kNotRequired));
EXPECT_EQ(Type::kSet, event.GetEventTypeFor(ConsentLevel::kSync));
event = PrimaryAccountChangeEvent(account1_not_required_, account1_sync_);
EXPECT_EQ(Type::kNone, event.GetEventTypeFor(ConsentLevel::kNotRequired));
EXPECT_EQ(Type::kSet, event.GetEventTypeFor(ConsentLevel::kSync));
event = PrimaryAccountChangeEvent(account1_not_required_, account2_sync_);
EXPECT_EQ(Type::kSet, event.GetEventTypeFor(ConsentLevel::kNotRequired));
EXPECT_EQ(Type::kSet, event.GetEventTypeFor(ConsentLevel::kSync));
}
TEST_F(PrimaryAccountChangeEventTest, ConsentLevelChangeFromSyncToNotRequired) {
PrimaryAccountChangeEvent event(account1_sync_, account1_not_required_);
EXPECT_EQ(Type::kNone, event.GetEventTypeFor(ConsentLevel::kNotRequired));
EXPECT_EQ(Type::kCleared, event.GetEventTypeFor(ConsentLevel::kSync));
event = PrimaryAccountChangeEvent(account1_sync_, empty_not_required_);
EXPECT_EQ(Type::kCleared, event.GetEventTypeFor(ConsentLevel::kNotRequired));
EXPECT_EQ(Type::kCleared, event.GetEventTypeFor(ConsentLevel::kSync));
}
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