blob: 86cd0c2c4f868bd3b9253a296fe061222bcb0241 [file] [log] [blame]
James Cookb0bf8e82017-04-09 17:01:441// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ash/window_user_data.h"
6
7#include <memory>
8
Mitsuru Oshima9a6378d92019-11-01 00:48:159#include "ash/public/cpp/autotest_private_api_utils.h"
sky28eafdc2017-05-01 18:00:0710#include "ash/test/ash_test_base.h"
James Cookb0bf8e82017-04-09 17:01:4411#include "ash/window_user_data.h"
James Cookb0bf8e82017-04-09 17:01:4412#include "ui/aura/window.h"
13#include "ui/compositor/layer_type.h"
14
15namespace ash {
16namespace {
17
18// Class that sets a bool* to true from the destructor. Used to track
19// destruction.
20class Data {
21 public:
22 explicit Data(bool* delete_setter) : delete_setter_(delete_setter) {}
Peter Boströmec31a042021-09-16 23:37:3423
24 Data(const Data&) = delete;
25 Data& operator=(const Data&) = delete;
26
James Cookb0bf8e82017-04-09 17:01:4427 ~Data() { *delete_setter_ = true; }
28
29 private:
30 bool* delete_setter_;
James Cookb0bf8e82017-04-09 17:01:4431};
32
33} // namespace
34
James Cook317781a2017-07-18 02:08:0635using WindowUserDataTest = AshTestBase;
James Cookb0bf8e82017-04-09 17:01:4436
37// Verifies clear() deletes the data associated with a window.
38TEST_F(WindowUserDataTest, ClearDestroys) {
39 WindowUserData<Data> user_data;
Mitsuru Oshima931a7762021-02-12 01:50:2240 std::unique_ptr<aura::Window> window =
41 std::make_unique<aura::Window>(nullptr);
Scott Violetac410622018-08-11 18:33:2242 window->Init(ui::LAYER_NOT_DRAWN);
James Cookb0bf8e82017-04-09 17:01:4443 bool data_deleted = false;
Scott Violetac410622018-08-11 18:33:2244 user_data.Set(window.get(), std::make_unique<Data>(&data_deleted));
James Cookb0bf8e82017-04-09 17:01:4445 EXPECT_FALSE(data_deleted);
46 user_data.clear();
47 EXPECT_TRUE(data_deleted);
48}
49
50// Verifies Set() called with an existing window replaces the existing data.
51TEST_F(WindowUserDataTest, ReplaceDestroys) {
52 WindowUserData<Data> user_data;
Mitsuru Oshima931a7762021-02-12 01:50:2253 std::unique_ptr<aura::Window> window =
54 std::make_unique<aura::Window>(nullptr);
James Cookb0bf8e82017-04-09 17:01:4455 window->Init(ui::LAYER_NOT_DRAWN);
56 bool data1_deleted = false;
Mitsuru Oshima04b54d02017-10-09 14:22:4557 user_data.Set(window.get(), std::make_unique<Data>(&data1_deleted));
James Cookb0bf8e82017-04-09 17:01:4458 EXPECT_FALSE(data1_deleted);
59 bool data2_deleted = false;
Mitsuru Oshima04b54d02017-10-09 14:22:4560 user_data.Set(window.get(), std::make_unique<Data>(&data2_deleted));
James Cookb0bf8e82017-04-09 17:01:4461 EXPECT_TRUE(data1_deleted);
62 EXPECT_FALSE(data2_deleted);
63 ASSERT_EQ(1u, user_data.GetWindows().size());
64 EXPECT_EQ(window.get(), *user_data.GetWindows().begin());
65 window.reset();
66 EXPECT_TRUE(data2_deleted);
67 EXPECT_TRUE(user_data.GetWindows().empty());
68}
69
70// Verifies Set() with null deletes existing data.
71TEST_F(WindowUserDataTest, NullClears) {
72 WindowUserData<Data> user_data;
Mitsuru Oshima931a7762021-02-12 01:50:2273 std::unique_ptr<aura::Window> window =
74 std::make_unique<aura::Window>(nullptr);
Scott Violetac410622018-08-11 18:33:2275 window->Init(ui::LAYER_NOT_DRAWN);
James Cookb0bf8e82017-04-09 17:01:4476 bool data1_deleted = false;
Scott Violetac410622018-08-11 18:33:2277 user_data.Set(window.get(), std::make_unique<Data>(&data1_deleted));
James Cookb0bf8e82017-04-09 17:01:4478 EXPECT_FALSE(data1_deleted);
Scott Violetac410622018-08-11 18:33:2279 user_data.Set(window.get(), nullptr);
James Cookb0bf8e82017-04-09 17:01:4480 EXPECT_TRUE(data1_deleted);
81 EXPECT_TRUE(user_data.GetWindows().empty());
82}
83
84} // namespace ash