Commit 4e769099 authored by Wenzhao Zang's avatar Wenzhao Zang Committed by Commit Bot

cros: Notify CloudExternalDataPolicyObserver when policy doesn't exist

When the policy doesn't exist, an "OnExternalDataCleared" call could
be fired to notify clients, in case they still keep the old data from
a previous policy. It should be a no-op for clients that do not store
policy data.

Bug: 889108
Change-Id: I6061f3be1b650f026ea88f8d353b710fb0b3b16d
Reviewed-on: https://chromium-review.googlesource.com/c/1266695Reviewed-by: default avatarSean Kau <skau@chromium.org>
Reviewed-by: default avatarXiaoqian Dai <xdai@chromium.org>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Reviewed-by: default avatarAlexander Alekseev <alemate@chromium.org>
Commit-Queue: Wenzhao (Colin) Zang <wzang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#600430}
parent 033dff08
......@@ -1236,7 +1236,9 @@ void WallpaperController::RemoveUserWallpaper(
void WallpaperController::RemovePolicyWallpaper(
mojom::WallpaperUserInfoPtr user_info,
const std::string& wallpaper_files_id) {
DCHECK(IsPolicyControlled(user_info->account_id, user_info->is_ephemeral));
if (!IsPolicyControlled(user_info->account_id, user_info->is_ephemeral))
return;
// Updates the screen only when the user has logged in.
const bool show_wallpaper =
Shell::Get()->session_controller()->IsActiveUserSessionStarted();
......
......@@ -16,6 +16,7 @@
#include "ash/shell_test_api.h"
#include "ash/test/ash_test_base.h"
#include "ash/wallpaper/wallpaper_controller_observer.h"
#include "ash/wallpaper/wallpaper_utils/wallpaper_resizer.h"
#include "ash/wallpaper/wallpaper_view.h"
#include "ash/wallpaper/wallpaper_widget_controller.h"
#include "ash/wm/window_state.h"
......@@ -531,6 +532,8 @@ class WallpaperControllerTest : public AshTestBase {
controller_->decode_requests_for_testing_.clear();
}
void ClearWallpaper() { controller_->current_wallpaper_.reset(); }
WallpaperController* controller_; // Not owned.
base::ScopedTempDir user_data_dir_;
......@@ -1059,9 +1062,17 @@ TEST_F(WallpaperControllerTest, SetAndRemovePolicyWallpaper) {
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), POLICY);
// Log out the user and remove the policy wallpaper. Verify the wallpaper
// info is reset to default and the user is no longer policy controlled.
// Clear the wallpaper and log out the user. Verify the policy wallpaper is
// shown in the login screen.
ClearWallpaper();
ClearLogin();
controller_->ShowUserWallpaper(InitializeUser(account_id_1));
RunAllTasksUntilIdle();
EXPECT_EQ(controller_->GetWallpaperType(), POLICY);
EXPECT_TRUE(
controller_->IsPolicyControlled(account_id_1, false /*is_ephemeral=*/));
// Remove the policy wallpaper. Verify the wallpaper info is reset to default
// and the user is no longer policy controlled.
controller_->RemovePolicyWallpaper(InitializeUser(account_id_1),
wallpaper_files_id_1);
WaitUntilCustomWallpapersDeleted(account_id_1);
......@@ -1073,7 +1084,8 @@ TEST_F(WallpaperControllerTest, SetAndRemovePolicyWallpaper) {
EXPECT_EQ(wallpaper_info, default_wallpaper_info);
EXPECT_FALSE(
controller_->IsPolicyControlled(account_id_1, false /*is_ephemeral=*/));
// Verify the wallpaper is not updated since the user hasn't logged in.
// Verify the wallpaper is not updated since the user hasn't logged in (to
// avoid abrupt wallpaper change in login screen).
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), POLICY);
......@@ -1084,6 +1096,40 @@ TEST_F(WallpaperControllerTest, SetAndRemovePolicyWallpaper) {
EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
}
TEST_F(WallpaperControllerTest, RemovePolicyWallpaperNoOp) {
auto verify_custom_wallpaper_info = [&]() {
EXPECT_EQ(CUSTOMIZED, controller_->GetWallpaperType());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
WallpaperInfo wallpaper_info;
EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info,
false /*is_ephemeral=*/));
WallpaperInfo expected_wallpaper_info(
base::FilePath(wallpaper_files_id_1).Append(file_name_1).value(),
WALLPAPER_LAYOUT_CENTER, CUSTOMIZED, base::Time::Now().LocalMidnight());
EXPECT_EQ(expected_wallpaper_info, wallpaper_info);
};
// Set a custom wallpaper. Verify the user is not policy controlled and the
// wallpaper info is correct.
SimulateUserLogin(kUser1);
controller_->SetCustomWallpaper(
InitializeUser(account_id_1), wallpaper_files_id_1, file_name_1,
WALLPAPER_LAYOUT_CENTER, CreateImage(640, 480, kWallpaperColor),
false /*preview_mode=*/);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_FALSE(
controller_->IsPolicyControlled(account_id_1, false /*is_ephemeral=*/));
verify_custom_wallpaper_info();
// Verify RemovePolicyWallpaper() is a no-op when the user doesn't have a
// policy wallpaper.
controller_->RemovePolicyWallpaper(InitializeUser(account_id_1),
wallpaper_files_id_1);
RunAllTasksUntilIdle();
verify_custom_wallpaper_info();
}
TEST_F(WallpaperControllerTest, SetThirdPartyWallpaper) {
SetBypassDecode();
SimulateUserLogin(kUser1);
......
......@@ -759,6 +759,9 @@ void UserImageManagerImpl::OnExternalDataSet(const std::string& policy) {
void UserImageManagerImpl::OnExternalDataCleared(const std::string& policy) {
DCHECK_EQ(policy::key::kUserAvatarImage, policy);
if (!IsUserImageManaged())
return;
has_managed_image_ = false;
SetInitialUserImage();
TryToCreateImageSyncObserver();
......
......@@ -383,29 +383,6 @@ IN_PROC_BROWSER_TEST_F(WallpaperPolicyTest, DISABLED_SetResetClear) {
ASSERT_EQ(3, wallpaper_change_count_);
}
IN_PROC_BROWSER_TEST_F(WallpaperPolicyTest, PRE_PRE_PersistOverLogout) {
SetSystemSalt();
RegisterUser(testUsers_[0]);
StartupUtils::MarkOobeCompleted();
}
IN_PROC_BROWSER_TEST_F(WallpaperPolicyTest, PRE_PersistOverLogout) {
SetSystemSalt();
LoginUser(testUsers_[0]);
// Set wallpaper policy to red image.
InjectPolicy(0, kRedImageFileName);
// Run until wallpaper has changed to expected color.
RunUntilWallpaperChangeToColor(kRedImageColor);
StartupUtils::MarkOobeCompleted();
}
IN_PROC_BROWSER_TEST_F(WallpaperPolicyTest, PersistOverLogout) {
LoginUser(testUsers_[0]);
RunUntilWallpaperChangeToColor(kRedImageColor);
}
IN_PROC_BROWSER_TEST_F(WallpaperPolicyTest, PRE_DevicePolicyTest) {
SetSystemSalt();
RegisterUser(testUsers_[0]);
......
......@@ -74,7 +74,8 @@ CloudExternalDataPolicyObserver::PolicyServiceObserver::PolicyServiceObserver(
const PolicyMap::Entry* entry = policy_service_->GetPolicies(
PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
.Get(parent_->policy_);
if (entry)
// Notify |parent_| even when |entry| is null (i.e. the policy never existed
// or once existed but was cleared later).
parent_->HandleExternalDataPolicyUpdate(user_id_, entry);
}
}
......
......@@ -111,6 +111,7 @@ class CloudExternalDataPolicyObserverTest
std::unique_ptr<std::string> data) override;
void CreateObserver();
void RemoveObserver();
void ClearObservations();
......@@ -253,6 +254,10 @@ void CloudExternalDataPolicyObserverTest::CreateObserver() {
observer_->Init();
}
void CloudExternalDataPolicyObserverTest::RemoveObserver() {
observer_.reset();
}
void CloudExternalDataPolicyObserverTest::ClearObservations() {
set_calls_.clear();
cleared_calls_.clear();
......@@ -401,7 +406,7 @@ TEST_F(CloudExternalDataPolicyObserverTest,
EXPECT_TRUE(cleared_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
ASSERT_EQ(1u, set_calls_.size());
EXPECT_EQ(1u, set_calls_.size());
EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
ClearObservations();
......@@ -411,7 +416,7 @@ TEST_F(CloudExternalDataPolicyObserverTest,
EXPECT_TRUE(set_calls_.empty());
EXPECT_TRUE(cleared_calls_.empty());
ASSERT_EQ(1u, fetched_calls_.size());
EXPECT_EQ(1u, fetched_calls_.size());
EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
ClearObservations();
......@@ -436,7 +441,7 @@ TEST_F(CloudExternalDataPolicyObserverTest,
EXPECT_TRUE(cleared_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
ASSERT_EQ(1u, set_calls_.size());
EXPECT_EQ(1u, set_calls_.size());
EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
ClearObservations();
......@@ -504,7 +509,7 @@ TEST_F(CloudExternalDataPolicyObserverTest,
EXPECT_TRUE(cleared_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
ASSERT_EQ(1u, set_calls_.size());
EXPECT_EQ(1u, set_calls_.size());
EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
ClearObservations();
......@@ -515,7 +520,7 @@ TEST_F(CloudExternalDataPolicyObserverTest,
EXPECT_TRUE(set_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
ASSERT_EQ(1u, cleared_calls_.size());
EXPECT_EQ(1u, cleared_calls_.size());
EXPECT_EQ(device_local_account_user_id_, cleared_calls_.front());
ClearObservations();
......@@ -548,7 +553,7 @@ TEST_F(CloudExternalDataPolicyObserverTest,
EXPECT_TRUE(cleared_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
ASSERT_EQ(1u, set_calls_.size());
EXPECT_EQ(1u, set_calls_.size());
EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
ClearObservations();
......@@ -558,7 +563,7 @@ TEST_F(CloudExternalDataPolicyObserverTest,
EXPECT_TRUE(set_calls_.empty());
EXPECT_TRUE(cleared_calls_.empty());
ASSERT_EQ(1u, fetched_calls_.size());
EXPECT_EQ(1u, fetched_calls_.size());
EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
ClearObservations();
......@@ -585,7 +590,7 @@ TEST_F(CloudExternalDataPolicyObserverTest, ExistingDeviceLocalAccountSetSet) {
EXPECT_TRUE(cleared_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
ASSERT_EQ(1u, set_calls_.size());
EXPECT_EQ(1u, set_calls_.size());
EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
ClearObservations();
......@@ -596,7 +601,7 @@ TEST_F(CloudExternalDataPolicyObserverTest, ExistingDeviceLocalAccountSetSet) {
EXPECT_TRUE(cleared_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
ASSERT_EQ(1u, set_calls_.size());
EXPECT_EQ(1u, set_calls_.size());
EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
ClearObservations();
......@@ -607,7 +612,7 @@ TEST_F(CloudExternalDataPolicyObserverTest, ExistingDeviceLocalAccountSetSet) {
EXPECT_TRUE(set_calls_.empty());
EXPECT_TRUE(cleared_calls_.empty());
ASSERT_EQ(1u, fetched_calls_.size());
EXPECT_EQ(1u, fetched_calls_.size());
EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
EXPECT_EQ(avatar_policy_2_data_, fetched_calls_.front().second);
ClearObservations();
......@@ -616,7 +621,7 @@ TEST_F(CloudExternalDataPolicyObserverTest, ExistingDeviceLocalAccountSetSet) {
}
// Verifies that when the external data reference for a device-local account is
// initially not set, no notifications are emitted during login into the
// initially not set, a 'cleared' notification is emitted during login into the
// account. Further verifies that when the external data reference is then set,
// a corresponding notification is emitted only once and a fetch is started.
// Also verifies that when the fetch eventually succeeds, a notification
......@@ -635,7 +640,7 @@ TEST_F(CloudExternalDataPolicyObserverTest,
LogInAsDeviceLocalAccount(AccountId::FromUserEmail(kDeviceLocalAccount));
EXPECT_TRUE(set_calls_.empty());
EXPECT_TRUE(cleared_calls_.empty());
EXPECT_EQ(1u, cleared_calls_.size());
EXPECT_TRUE(fetched_calls_.empty());
ClearObservations();
......@@ -644,7 +649,7 @@ TEST_F(CloudExternalDataPolicyObserverTest,
EXPECT_TRUE(cleared_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
ASSERT_EQ(1u, set_calls_.size());
EXPECT_EQ(1u, set_calls_.size());
EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
ClearObservations();
......@@ -654,7 +659,7 @@ TEST_F(CloudExternalDataPolicyObserverTest,
EXPECT_TRUE(set_calls_.empty());
EXPECT_TRUE(cleared_calls_.empty());
ASSERT_EQ(1u, fetched_calls_.size());
EXPECT_EQ(1u, fetched_calls_.size());
EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
ClearObservations();
......@@ -712,7 +717,7 @@ TEST_F(CloudExternalDataPolicyObserverTest,
EXPECT_TRUE(cleared_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
ASSERT_EQ(1u, set_calls_.size());
EXPECT_EQ(1u, set_calls_.size());
EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
ClearObservations();
......@@ -722,7 +727,7 @@ TEST_F(CloudExternalDataPolicyObserverTest,
EXPECT_TRUE(set_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
ASSERT_EQ(1u, cleared_calls_.size());
EXPECT_EQ(1u, cleared_calls_.size());
EXPECT_EQ(device_local_account_user_id_, cleared_calls_.front());
ClearObservations();
......@@ -746,7 +751,7 @@ TEST_F(CloudExternalDataPolicyObserverTest, RegularUserFetchSuccess) {
EXPECT_TRUE(cleared_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
ASSERT_EQ(1u, set_calls_.size());
EXPECT_EQ(1u, set_calls_.size());
EXPECT_EQ(kRegularUserID, set_calls_.front());
ClearObservations();
......@@ -757,16 +762,16 @@ TEST_F(CloudExternalDataPolicyObserverTest, RegularUserFetchSuccess) {
EXPECT_TRUE(set_calls_.empty());
EXPECT_TRUE(cleared_calls_.empty());
ASSERT_EQ(1u, fetched_calls_.size());
EXPECT_EQ(1u, fetched_calls_.size());
EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
ClearObservations();
}
// Verifies that when the external data reference for a regular user is not set
// while the user is logging in, no notifications are emitted. Further verifies
// that when the external data reference is then cleared (which is a no-op),
// again, no notifications are emitted.
// while the user is logging in, a 'cleared' notifications is emitted. Further
// verifies that when the external data reference is then cleared (which is a
// no-op), no notifications are emitted.
TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearUnset) {
CreateObserver();
......@@ -775,7 +780,7 @@ TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearUnset) {
LogInAsRegularUser();
EXPECT_TRUE(set_calls_.empty());
EXPECT_TRUE(cleared_calls_.empty());
EXPECT_EQ(1u, cleared_calls_.size());
EXPECT_TRUE(fetched_calls_.empty());
ClearObservations();
......@@ -808,7 +813,7 @@ TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearSet) {
EXPECT_TRUE(cleared_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
ASSERT_EQ(1u, set_calls_.size());
EXPECT_EQ(1u, set_calls_.size());
EXPECT_EQ(kRegularUserID, set_calls_.front());
ClearObservations();
......@@ -819,15 +824,14 @@ TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearSet) {
EXPECT_TRUE(set_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
ASSERT_EQ(1u, cleared_calls_.size());
EXPECT_EQ(1u, cleared_calls_.size());
EXPECT_EQ(kRegularUserID, cleared_calls_.front());
ClearObservations();
}
// Verifies that when the external data reference for a regular user is not set
// while the user is logging in, no notifications are emitted. Further verifies
// that when the external data reference is then set, a corresponding
// while the user is logging in, a 'cleared' notifications is emitted. Further
// verifies that when the external data reference is then set, a corresponding
// notification is emitted and a fetch is started. Also verifies that when the
// fetch eventually succeeds, a notification containing the external data is
// emitted.
......@@ -839,7 +843,7 @@ TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetUnset) {
LogInAsRegularUser();
EXPECT_TRUE(set_calls_.empty());
EXPECT_TRUE(cleared_calls_.empty());
EXPECT_EQ(1u, cleared_calls_.size());
EXPECT_TRUE(fetched_calls_.empty());
ClearObservations();
......@@ -852,7 +856,7 @@ TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetUnset) {
EXPECT_TRUE(cleared_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
ASSERT_EQ(1u, set_calls_.size());
EXPECT_EQ(1u, set_calls_.size());
EXPECT_EQ(kRegularUserID, set_calls_.front());
ClearObservations();
......@@ -863,7 +867,7 @@ TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetUnset) {
EXPECT_TRUE(set_calls_.empty());
EXPECT_TRUE(cleared_calls_.empty());
ASSERT_EQ(1u, fetched_calls_.size());
EXPECT_EQ(1u, fetched_calls_.size());
EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
ClearObservations();
......@@ -888,7 +892,7 @@ TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetSet) {
EXPECT_TRUE(cleared_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
ASSERT_EQ(1u, set_calls_.size());
EXPECT_EQ(1u, set_calls_.size());
EXPECT_EQ(kRegularUserID, set_calls_.front());
ClearObservations();
......@@ -901,7 +905,7 @@ TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetSet) {
EXPECT_TRUE(cleared_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
ASSERT_EQ(1u, set_calls_.size());
EXPECT_EQ(1u, set_calls_.size());
EXPECT_EQ(kRegularUserID, set_calls_.front());
ClearObservations();
......@@ -912,10 +916,53 @@ TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetSet) {
EXPECT_TRUE(set_calls_.empty());
EXPECT_TRUE(cleared_calls_.empty());
ASSERT_EQ(1u, fetched_calls_.size());
EXPECT_EQ(1u, fetched_calls_.size());
EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
EXPECT_EQ(avatar_policy_2_data_, fetched_calls_.front().second);
ClearObservations();
}
// Tests that if external data reference for a regular user was cleared when
// the user logged out, the notification will still be emitted when the user
// logs back in.
TEST_F(CloudExternalDataPolicyObserverTest, RegularUserLogoutTest) {
SetRegularUserAvatarPolicy(avatar_policy_1_);
CreateObserver();
EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
.Times(1)
.WillOnce(SaveArg<1>(&fetch_callback_));
LogInAsRegularUser();
EXPECT_TRUE(cleared_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
EXPECT_EQ(1u, set_calls_.size());
EXPECT_EQ(kRegularUserID, set_calls_.front());
ClearObservations();
// Now simulate log out the user. Simply reset the external data policy
// observer.
RemoveObserver();
Mock::VerifyAndClear(&external_data_manager_);
EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
SetRegularUserAvatarPolicy("");
// Now simulate log back the user.
CreateObserver();
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
content::NotificationService::AllSources(),
content::Details<Profile>(profile_.get()));
// Test that clear notification is emitted.
EXPECT_TRUE(set_calls_.empty());
EXPECT_TRUE(fetched_calls_.empty());
EXPECT_EQ(1u, cleared_calls_.size());
EXPECT_EQ(kRegularUserID, cleared_calls_.front());
ClearObservations();
}
} // namespace policy
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