blob: 352d5844d4867a3ab6e6ed02933fa67d431fcee9 [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
sky28eafdc2017-05-01 18:00:079#include "ash/test/ash_test_base.h"
James Cookb0bf8e82017-04-09 17:01:4410#include "ash/window_user_data.h"
James Cookb0bf8e82017-04-09 17:01:4411#include "ui/aura/window.h"
12#include "ui/compositor/layer_type.h"
13
14namespace ash {
15namespace {
16
17// Class that sets a bool* to true from the destructor. Used to track
18// destruction.
19class Data {
20 public:
21 explicit Data(bool* delete_setter) : delete_setter_(delete_setter) {}
22 ~Data() { *delete_setter_ = true; }
23
24 private:
25 bool* delete_setter_;
26
27 DISALLOW_COPY_AND_ASSIGN(Data);
28};
29
30} // namespace
31
James Cook317781a2017-07-18 02:08:0632using WindowUserDataTest = AshTestBase;
James Cookb0bf8e82017-04-09 17:01:4433
34// Verifies clear() deletes the data associated with a window.
35TEST_F(WindowUserDataTest, ClearDestroys) {
36 WindowUserData<Data> user_data;
Thiago Farina627fba952017-05-24 21:15:2637 aura::Window window(nullptr, aura::client::WINDOW_TYPE_UNKNOWN);
James Cookb0bf8e82017-04-09 17:01:4438 window.Init(ui::LAYER_NOT_DRAWN);
39 bool data_deleted = false;
Mitsuru Oshima04b54d02017-10-09 14:22:4540 user_data.Set(&window, std::make_unique<Data>(&data_deleted));
James Cookb0bf8e82017-04-09 17:01:4441 EXPECT_FALSE(data_deleted);
42 user_data.clear();
43 EXPECT_TRUE(data_deleted);
44}
45
46// Verifies Set() called with an existing window replaces the existing data.
47TEST_F(WindowUserDataTest, ReplaceDestroys) {
48 WindowUserData<Data> user_data;
Mitsuru Oshima04b54d02017-10-09 14:22:4549 std::unique_ptr<aura::Window> window(std::make_unique<aura::Window>(
Thiago Farina627fba952017-05-24 21:15:2650 nullptr, aura::client::WINDOW_TYPE_UNKNOWN));
James Cookb0bf8e82017-04-09 17:01:4451 window->Init(ui::LAYER_NOT_DRAWN);
52 bool data1_deleted = false;
Mitsuru Oshima04b54d02017-10-09 14:22:4553 user_data.Set(window.get(), std::make_unique<Data>(&data1_deleted));
James Cookb0bf8e82017-04-09 17:01:4454 EXPECT_FALSE(data1_deleted);
55 bool data2_deleted = false;
Mitsuru Oshima04b54d02017-10-09 14:22:4556 user_data.Set(window.get(), std::make_unique<Data>(&data2_deleted));
James Cookb0bf8e82017-04-09 17:01:4457 EXPECT_TRUE(data1_deleted);
58 EXPECT_FALSE(data2_deleted);
59 ASSERT_EQ(1u, user_data.GetWindows().size());
60 EXPECT_EQ(window.get(), *user_data.GetWindows().begin());
61 window.reset();
62 EXPECT_TRUE(data2_deleted);
63 EXPECT_TRUE(user_data.GetWindows().empty());
64}
65
66// Verifies Set() with null deletes existing data.
67TEST_F(WindowUserDataTest, NullClears) {
68 WindowUserData<Data> user_data;
Thiago Farina627fba952017-05-24 21:15:2669 aura::Window window(nullptr, aura::client::WINDOW_TYPE_UNKNOWN);
James Cookb0bf8e82017-04-09 17:01:4470 window.Init(ui::LAYER_NOT_DRAWN);
71 bool data1_deleted = false;
Mitsuru Oshima04b54d02017-10-09 14:22:4572 user_data.Set(&window, std::make_unique<Data>(&data1_deleted));
James Cookb0bf8e82017-04-09 17:01:4473 EXPECT_FALSE(data1_deleted);
74 user_data.Set(&window, nullptr);
75 EXPECT_TRUE(data1_deleted);
76 EXPECT_TRUE(user_data.GetWindows().empty());
77}
78
79} // namespace ash