James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame^] | 1 | // 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 | |
| 9 | #include "ash/test/ash_test.h" |
| 10 | #include "ash/window_user_data.h" |
| 11 | #include "base/memory/ptr_util.h" |
| 12 | #include "ui/aura/window.h" |
| 13 | #include "ui/compositor/layer_type.h" |
| 14 | |
| 15 | namespace ash { |
| 16 | namespace { |
| 17 | |
| 18 | // Class that sets a bool* to true from the destructor. Used to track |
| 19 | // destruction. |
| 20 | class 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 | |
| 33 | using WindowUserDataTest = AshTest; |
| 34 | |
| 35 | // Verifies clear() deletes the data associated with a window. |
| 36 | TEST_F(WindowUserDataTest, ClearDestroys) { |
| 37 | WindowUserData<Data> user_data; |
| 38 | aura::Window window(nullptr, ui::wm::WINDOW_TYPE_UNKNOWN); |
| 39 | window.Init(ui::LAYER_NOT_DRAWN); |
| 40 | bool data_deleted = false; |
| 41 | user_data.Set(&window, base::MakeUnique<Data>(&data_deleted)); |
| 42 | EXPECT_FALSE(data_deleted); |
| 43 | user_data.clear(); |
| 44 | EXPECT_TRUE(data_deleted); |
| 45 | } |
| 46 | |
| 47 | // Verifies Set() called with an existing window replaces the existing data. |
| 48 | TEST_F(WindowUserDataTest, ReplaceDestroys) { |
| 49 | WindowUserData<Data> user_data; |
| 50 | std::unique_ptr<aura::Window> window( |
| 51 | base::MakeUnique<aura::Window>(nullptr, ui::wm::WINDOW_TYPE_UNKNOWN)); |
| 52 | window->Init(ui::LAYER_NOT_DRAWN); |
| 53 | bool data1_deleted = false; |
| 54 | user_data.Set(window.get(), base::MakeUnique<Data>(&data1_deleted)); |
| 55 | EXPECT_FALSE(data1_deleted); |
| 56 | bool data2_deleted = false; |
| 57 | user_data.Set(window.get(), base::MakeUnique<Data>(&data2_deleted)); |
| 58 | 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. |
| 68 | TEST_F(WindowUserDataTest, NullClears) { |
| 69 | WindowUserData<Data> user_data; |
| 70 | aura::Window window(nullptr, ui::wm::WINDOW_TYPE_UNKNOWN); |
| 71 | window.Init(ui::LAYER_NOT_DRAWN); |
| 72 | bool data1_deleted = false; |
| 73 | user_data.Set(&window, base::MakeUnique<Data>(&data1_deleted)); |
| 74 | EXPECT_FALSE(data1_deleted); |
| 75 | user_data.Set(&window, nullptr); |
| 76 | EXPECT_TRUE(data1_deleted); |
| 77 | EXPECT_TRUE(user_data.GetWindows().empty()); |
| 78 | } |
| 79 | |
| 80 | } // namespace ash |