blob: fbc5ea35d0cc0e398f92407b02bfb06903d4be9e [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) {}
23 ~Data() { *delete_setter_ = true; }
24
25 private:
26 bool* delete_setter_;
27
28 DISALLOW_COPY_AND_ASSIGN(Data);
29};
30
31} // namespace
32
James Cook317781a2017-07-18 02:08:0633using WindowUserDataTest = AshTestBase;
James Cookb0bf8e82017-04-09 17:01:4434
35// Verifies clear() deletes the data associated with a window.
36TEST_F(WindowUserDataTest, ClearDestroys) {
37 WindowUserData<Data> user_data;
Mitsuru Oshima931a7762021-02-12 01:50:2238 std::unique_ptr<aura::Window> window =
39 std::make_unique<aura::Window>(nullptr);
Scott Violetac410622018-08-11 18:33:2240 window->Init(ui::LAYER_NOT_DRAWN);
James Cookb0bf8e82017-04-09 17:01:4441 bool data_deleted = false;
Scott Violetac410622018-08-11 18:33:2242 user_data.Set(window.get(), std::make_unique<Data>(&data_deleted));
James Cookb0bf8e82017-04-09 17:01:4443 EXPECT_FALSE(data_deleted);
44 user_data.clear();
45 EXPECT_TRUE(data_deleted);
46}
47
48// Verifies Set() called with an existing window replaces the existing data.
49TEST_F(WindowUserDataTest, ReplaceDestroys) {
50 WindowUserData<Data> user_data;
Mitsuru Oshima931a7762021-02-12 01:50:2251 std::unique_ptr<aura::Window> window =
52 std::make_unique<aura::Window>(nullptr);
James Cookb0bf8e82017-04-09 17:01:4453 window->Init(ui::LAYER_NOT_DRAWN);
54 bool data1_deleted = false;
Mitsuru Oshima04b54d02017-10-09 14:22:4555 user_data.Set(window.get(), std::make_unique<Data>(&data1_deleted));
James Cookb0bf8e82017-04-09 17:01:4456 EXPECT_FALSE(data1_deleted);
57 bool data2_deleted = false;
Mitsuru Oshima04b54d02017-10-09 14:22:4558 user_data.Set(window.get(), std::make_unique<Data>(&data2_deleted));
James Cookb0bf8e82017-04-09 17:01:4459 EXPECT_TRUE(data1_deleted);
60 EXPECT_FALSE(data2_deleted);
61 ASSERT_EQ(1u, user_data.GetWindows().size());
62 EXPECT_EQ(window.get(), *user_data.GetWindows().begin());
63 window.reset();
64 EXPECT_TRUE(data2_deleted);
65 EXPECT_TRUE(user_data.GetWindows().empty());
66}
67
68// Verifies Set() with null deletes existing data.
69TEST_F(WindowUserDataTest, NullClears) {
70 WindowUserData<Data> user_data;
Mitsuru Oshima931a7762021-02-12 01:50:2271 std::unique_ptr<aura::Window> window =
72 std::make_unique<aura::Window>(nullptr);
Scott Violetac410622018-08-11 18:33:2273 window->Init(ui::LAYER_NOT_DRAWN);
James Cookb0bf8e82017-04-09 17:01:4474 bool data1_deleted = false;
Scott Violetac410622018-08-11 18:33:2275 user_data.Set(window.get(), std::make_unique<Data>(&data1_deleted));
James Cookb0bf8e82017-04-09 17:01:4476 EXPECT_FALSE(data1_deleted);
Scott Violetac410622018-08-11 18:33:2277 user_data.Set(window.get(), nullptr);
James Cookb0bf8e82017-04-09 17:01:4478 EXPECT_TRUE(data1_deleted);
79 EXPECT_TRUE(user_data.GetWindows().empty());
80}
81
82} // namespace ash