1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/session/fullscreen_controller.h"
#include <memory>
#include "ash/constants/app_types.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/test_shell_delegate.h"
#include "ash/wm/window_state.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "chromeos/ui/base/window_properties.h"
#include "chromeos/ui/wm/fullscreen/pref_names.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window.h"
namespace ash {
namespace {
const GURL kActiveUrl = GURL("https://wwww.test.com");
const GURL kEmptyUrl = GURL::EmptyGURL();
constexpr char kNonMatchingPattern[] = "google.com";
constexpr char kMatchingPattern[] = "test.com";
constexpr char kWildcardPattern[] = "*";
bool IsOfficialBuildWithoutDcheck() {
#if defined(OFFICIAL_BUILD) && !DCHECK_IS_ON()
return true;
#else
return false;
#endif
}
class FullscreenControllerTestBase : public AshTestBase {
public:
FullscreenControllerTestBase() = default;
FullscreenControllerTestBase(const FullscreenControllerTestBase&) = delete;
FullscreenControllerTestBase& operator=(const FullscreenControllerTestBase&) =
delete;
~FullscreenControllerTestBase() override = default;
// AshTestBase:
void SetUp() override {
// Create a test shell delegate which can return fake responses.
auto test_shell_delegate = std::make_unique<TestShellDelegate>();
test_shell_delegate_ = test_shell_delegate.get();
AshTestBase::SetUp(std::move(test_shell_delegate));
CreateFullscreenWindow();
}
void TearDown() override {
window_.reset();
AshTestBase::TearDown();
}
protected:
void CreateFullscreenWindow() {
window_ = CreateTestWindow();
window_->SetProperty(aura::client::kShowStateKey,
ui::SHOW_STATE_FULLSCREEN);
SetPropertiesForNewFullscreenWindow(window_.get());
window_state_ = WindowState::Get(window_.get());
}
virtual void SetPropertiesForNewFullscreenWindow(aura::Window* window) {}
void SetKeepFullscreenWithoutNotificationAllowList(
const std::string& pattern) {
base::Value::List list;
list.Append(pattern);
Shell::Get()->session_controller()->GetPrimaryUserPrefService()->SetList(
chromeos::prefs::kKeepFullscreenWithoutNotificationUrlAllowList,
std::move(list));
}
std::unique_ptr<aura::Window> window_;
raw_ptr<WindowState, DanglingUntriaged | ExperimentalAsh> window_state_ =
nullptr;
raw_ptr<TestShellDelegate, DanglingUntriaged> test_shell_delegate_ = nullptr;
};
class FullscreenControllerTest : public FullscreenControllerTestBase,
public testing::WithParamInterface<bool> {
protected:
void SetPropertiesForNewFullscreenWindow(aura::Window* window) override {
if (is_lacros_window_) {
window->SetProperty(aura::client::kAppType,
static_cast<int>(AppType::LACROS));
}
}
void SetUpShellDelegate(bool should_exit_fullscreen, GURL url = kActiveUrl) {
// The shell delegate will only retrieve the active URL for ash-chrome
// windows and return the empty URL for lacros-chrome windows.
if (is_lacros_window_) {
test_shell_delegate_->SetLastCommittedURLForWindow(kEmptyUrl);
test_shell_delegate_->SetShouldExitFullscreenBeforeLock(
should_exit_fullscreen);
} else {
test_shell_delegate_->SetLastCommittedURLForWindow(url);
}
}
bool is_lacros_window_ = GetParam();
};
// Test that full screen is exited after session unlock if the allow list pref
// is unset.
TEST_P(FullscreenControllerTest, ExitFullscreenIfUnsetPref) {
EXPECT_TRUE(window_state_->IsFullscreen());
base::RunLoop run_loop;
Shell::Get()->session_controller()->PrepareForLock(run_loop.QuitClosure());
GetSessionControllerClient()->LockScreen();
GetSessionControllerClient()->UnlockScreen();
run_loop.Run();
EXPECT_FALSE(window_state_->IsFullscreen());
}
// Test that full screen is exited after session unlock if the URL of the active
// window does not match any patterns from the allow list.
TEST_P(FullscreenControllerTest, ExitFullscreenIfNonMatchingPref) {
bool should_exit_fullscreen = true;
SetUpShellDelegate(should_exit_fullscreen);
SetKeepFullscreenWithoutNotificationAllowList(kNonMatchingPattern);
EXPECT_TRUE(window_state_->IsFullscreen());
base::RunLoop run_loop;
Shell::Get()->session_controller()->PrepareForLock(run_loop.QuitClosure());
GetSessionControllerClient()->LockScreen();
GetSessionControllerClient()->UnlockScreen();
run_loop.Run();
EXPECT_EQ(window_state_->IsFullscreen(), !should_exit_fullscreen);
}
// Test that full screen is not exited after session unlock if the URL of the
// active window matches a pattern from the allow list.
TEST_P(FullscreenControllerTest, KeepFullscreenIfMatchingPref) {
bool should_exit_fullscreen = false;
SetUpShellDelegate(should_exit_fullscreen);
// Set up the URL exempt list with one matching and one non-matching pattern.
base::Value::List list;
list.Append(kNonMatchingPattern);
list.Append(kMatchingPattern);
Shell::Get()->session_controller()->GetPrimaryUserPrefService()->SetList(
chromeos::prefs::kKeepFullscreenWithoutNotificationUrlAllowList,
std::move(list));
EXPECT_TRUE(window_state_->IsFullscreen());
base::RunLoop run_loop;
Shell::Get()->session_controller()->PrepareForLock(run_loop.QuitClosure());
GetSessionControllerClient()->LockScreen();
GetSessionControllerClient()->UnlockScreen();
run_loop.Run();
EXPECT_EQ(window_state_->IsFullscreen(), !should_exit_fullscreen);
}
// Test that full screen is not exited after session unlock if the allow list
// includes the wildcard character.
TEST_P(FullscreenControllerTest, KeepFullscreenIfWildcardPref) {
bool should_exit_fullscreen = false;
SetUpShellDelegate(should_exit_fullscreen);
SetKeepFullscreenWithoutNotificationAllowList(kWildcardPattern);
EXPECT_TRUE(window_state_->IsFullscreen());
base::RunLoop run_loop;
Shell::Get()->session_controller()->PrepareForLock(run_loop.QuitClosure());
GetSessionControllerClient()->LockScreen();
GetSessionControllerClient()->UnlockScreen();
run_loop.Run();
EXPECT_EQ(window_state_->IsFullscreen(), !should_exit_fullscreen);
}
// Test that full screen is exited after session unlock if the URL is not
// available.
TEST_P(FullscreenControllerTest, ExitFullscreenIfUnsetUrlUnsetPref) {
bool should_exit_fullscreen = true;
SetUpShellDelegate(should_exit_fullscreen, kEmptyUrl);
EXPECT_TRUE(window_state_->IsFullscreen());
base::RunLoop run_loop;
Shell::Get()->session_controller()->PrepareForLock(run_loop.QuitClosure());
GetSessionControllerClient()->LockScreen();
GetSessionControllerClient()->UnlockScreen();
run_loop.Run();
EXPECT_EQ(window_state_->IsFullscreen(), !should_exit_fullscreen);
}
// Test that full screen is not exited after session unlock if the allow list
// includes the wildcard character and the URL is not available.
TEST_P(FullscreenControllerTest, KeepFullscreenIfUnsetUrlWildcardPref) {
bool should_exit_fullscreen = false;
SetUpShellDelegate(should_exit_fullscreen, kEmptyUrl);
SetKeepFullscreenWithoutNotificationAllowList(kWildcardPattern);
EXPECT_TRUE(window_state_->IsFullscreen());
base::RunLoop run_loop;
Shell::Get()->session_controller()->PrepareForLock(run_loop.QuitClosure());
GetSessionControllerClient()->LockScreen();
GetSessionControllerClient()->UnlockScreen();
run_loop.Run();
EXPECT_EQ(window_state_->IsFullscreen(), !should_exit_fullscreen);
}
INSTANTIATE_TEST_SUITE_P(All, FullscreenControllerTest, testing::Bool());
using FullscreenControllerNotLacrosRelatedTest = FullscreenControllerTestBase;
TEST_F(FullscreenControllerNotLacrosRelatedTest,
KeepFullscreenIfNoExitPropertySet) {
window_->SetProperty(chromeos::kUseOverviewToExitFullscreen, true);
window_->SetProperty(chromeos::kNoExitFullscreenOnLock, true);
window_->SetProperty(aura::client::kAppType,
static_cast<int>(AppType::CROSTINI_APP));
ASSERT_TRUE(window_state_->IsFullscreen());
base::RunLoop run_loop;
Shell::Get()->session_controller()->PrepareForLock(run_loop.QuitClosure());
GetSessionControllerClient()->LockScreen();
EXPECT_TRUE(window_state_->IsFullscreen());
GetSessionControllerClient()->UnlockScreen();
run_loop.Run();
EXPECT_TRUE(window_state_->IsFullscreen());
}
TEST_F(FullscreenControllerNotLacrosRelatedTest,
NoExitPropertyNotAllowedIfOverviewPropertyIsNotSet) {
// CHECK macro discards error message for the official build with
// dcheck=false. See the definition in base/check.h for details.
std::string expected_error_message =
IsOfficialBuildWithoutDcheck() ? "" : "Property combination not allowed";
EXPECT_DEATH(
{
window_->SetProperty(chromeos::kUseOverviewToExitFullscreen, false);
window_->SetProperty(chromeos::kNoExitFullscreenOnLock, true);
ASSERT_TRUE(window_state_->IsFullscreen());
base::RunLoop run_loop;
Shell::Get()->session_controller()->PrepareForLock(
run_loop.QuitClosure());
GetSessionControllerClient()->LockScreen();
},
expected_error_message);
}
} // namespace
} // namespace ash
|