1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/window_user_data.h"
9 #include "ash/public/cpp/autotest_private_api_utils.h"
10 #include "ash/test/ash_test_base.h"
11 #include "ui/aura/window.h"
12 #include "ui/compositor/layer_type.h"
17 // Class that sets a bool* to true from the destructor. Used to track
21 explicit Data(bool* delete_setter) : delete_setter_(delete_setter) {}
23 Data(const Data&) = delete;
24 Data& operator=(const Data&) = delete;
26 ~Data() { *delete_setter_ = true; }
34 using WindowUserDataTest = AshTestBase;
36 // Verifies clear() deletes the data associated with a window.
37 TEST_F(WindowUserDataTest, ClearDestroys) {
38 WindowUserData<Data> user_data;
39 std::unique_ptr<aura::Window> window =
40 std::make_unique<aura::Window>(nullptr);
41 window->Init(ui::LAYER_NOT_DRAWN);
42 bool data_deleted = false;
43 user_data.Set(window.get(), std::make_unique<Data>(&data_deleted));
44 EXPECT_FALSE(data_deleted);
46 EXPECT_TRUE(data_deleted);
49 // Verifies Set() called with an existing window replaces the existing data.
50 TEST_F(WindowUserDataTest, ReplaceDestroys) {
51 WindowUserData<Data> user_data;
52 std::unique_ptr<aura::Window> window =
53 std::make_unique<aura::Window>(nullptr);
54 window->Init(ui::LAYER_NOT_DRAWN);
55 bool data1_deleted = false;
56 user_data.Set(window.get(), std::make_unique<Data>(&data1_deleted));
57 EXPECT_FALSE(data1_deleted);
58 bool data2_deleted = false;
59 user_data.Set(window.get(), std::make_unique<Data>(&data2_deleted));
60 EXPECT_TRUE(data1_deleted);
61 EXPECT_FALSE(data2_deleted);
62 ASSERT_EQ(1u, user_data.GetWindows().size());
63 EXPECT_EQ(window.get(), *user_data.GetWindows().begin());
65 EXPECT_TRUE(data2_deleted);
66 EXPECT_TRUE(user_data.GetWindows().empty());
69 // Verifies Set() with null deletes existing data.
70 TEST_F(WindowUserDataTest, NullClears) {
71 WindowUserData<Data> user_data;
72 std::unique_ptr<aura::Window> window =
73 std::make_unique<aura::Window>(nullptr);
74 window->Init(ui::LAYER_NOT_DRAWN);
75 bool data1_deleted = false;
76 user_data.Set(window.get(), std::make_unique<Data>(&data1_deleted));
77 EXPECT_FALSE(data1_deleted);
78 user_data.Set(window.get(), nullptr);
79 EXPECT_TRUE(data1_deleted);
80 EXPECT_TRUE(user_data.GetWindows().empty());