blob: 53b57c1d81bd9325a2dbf7a773ba9e67c4b188da [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"
Scott Violetac410622018-08-11 18:33:2211#include "ash/window_factory.h"
James Cookb0bf8e82017-04-09 17:01:4412#include "ash/window_user_data.h"
James Cookb0bf8e82017-04-09 17:01:4413#include "ui/aura/window.h"
14#include "ui/compositor/layer_type.h"
15
16namespace ash {
17namespace {
18
19// Class that sets a bool* to true from the destructor. Used to track
20// destruction.
21class Data {
22 public:
23 explicit Data(bool* delete_setter) : delete_setter_(delete_setter) {}
24 ~Data() { *delete_setter_ = true; }
25
26 private:
27 bool* delete_setter_;
28
29 DISALLOW_COPY_AND_ASSIGN(Data);
30};
31
32} // namespace
33
James Cook317781a2017-07-18 02:08:0634using WindowUserDataTest = AshTestBase;
James Cookb0bf8e82017-04-09 17:01:4435
36// Verifies clear() deletes the data associated with a window.
37TEST_F(WindowUserDataTest, ClearDestroys) {
38 WindowUserData<Data> user_data;
Scott Violetac410622018-08-11 18:33:2239 std::unique_ptr<aura::Window> window = window_factory::NewWindow();
40 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;
Scott Violetac410622018-08-11 18:33:2251 std::unique_ptr<aura::Window> window = window_factory::NewWindow();
James Cookb0bf8e82017-04-09 17:01:4452 window->Init(ui::LAYER_NOT_DRAWN);
53 bool data1_deleted = false;
Mitsuru Oshima04b54d02017-10-09 14:22:4554 user_data.Set(window.get(), std::make_unique<Data>(&data1_deleted));
James Cookb0bf8e82017-04-09 17:01:4455 EXPECT_FALSE(data1_deleted);
56 bool data2_deleted = false;
Mitsuru Oshima04b54d02017-10-09 14:22:4557 user_data.Set(window.get(), std::make_unique<Data>(&data2_deleted));
James Cookb0bf8e82017-04-09 17:01:4458 EXPECT_TRUE(data1_deleted);
59 EXPECT_FALSE(data2_deleted);
60 ASSERT_EQ(1u, user_data.GetWindows().size());
61 EXPECT_EQ(window.get(), *user_data.GetWindows().begin());
62 window.reset();
63 EXPECT_TRUE(data2_deleted);
64 EXPECT_TRUE(user_data.GetWindows().empty());
65}
66
67// Verifies Set() with null deletes existing data.
68TEST_F(WindowUserDataTest, NullClears) {
69 WindowUserData<Data> user_data;
Scott Violetac410622018-08-11 18:33:2270 std::unique_ptr<aura::Window> window = window_factory::NewWindow();
71 window->Init(ui::LAYER_NOT_DRAWN);
James Cookb0bf8e82017-04-09 17:01:4472 bool data1_deleted = false;
Scott Violetac410622018-08-11 18:33:2273 user_data.Set(window.get(), std::make_unique<Data>(&data1_deleted));
James Cookb0bf8e82017-04-09 17:01:4474 EXPECT_FALSE(data1_deleted);
Scott Violetac410622018-08-11 18:33:2275 user_data.Set(window.get(), nullptr);
James Cookb0bf8e82017-04-09 17:01:4476 EXPECT_TRUE(data1_deleted);
77 EXPECT_TRUE(user_data.GetWindows().empty());
78}
79
80} // namespace ash