| OLD | NEW |
| (Empty) |
| 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/common/wm_window_user_data.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "ash/common/test/ash_test.h" | |
| 10 #include "ash/common/wm_shell.h" | |
| 11 #include "ash/common/wm_window.h" | |
| 12 #include "ash/common/wm_window_user_data.h" | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "ui/aura/window.h" | |
| 15 #include "ui/compositor/layer_type.h" | |
| 16 | |
| 17 namespace ash { | |
| 18 namespace { | |
| 19 | |
| 20 // Class that sets a bool* to true from the destructor. Used to track | |
| 21 // destruction. | |
| 22 class Data { | |
| 23 public: | |
| 24 explicit Data(bool* delete_setter) : delete_setter_(delete_setter) {} | |
| 25 ~Data() { *delete_setter_ = true; } | |
| 26 | |
| 27 private: | |
| 28 bool* delete_setter_; | |
| 29 | |
| 30 DISALLOW_COPY_AND_ASSIGN(Data); | |
| 31 }; | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 using WmWindowUserDataTest = AshTest; | |
| 36 | |
| 37 // Verifies clear() deletes the data associated with a window. | |
| 38 TEST_F(WmWindowUserDataTest, ClearDestroys) { | |
| 39 WmWindowUserData<Data> user_data; | |
| 40 std::unique_ptr<aura::Window> window( | |
| 41 base::MakeUnique<aura::Window>(nullptr, ui::wm::WINDOW_TYPE_UNKNOWN)); | |
| 42 window->Init(ui::LAYER_NOT_DRAWN); | |
| 43 bool data_deleted = false; | |
| 44 user_data.Set(WmWindow::Get(window.get()), | |
| 45 base::MakeUnique<Data>(&data_deleted)); | |
| 46 EXPECT_FALSE(data_deleted); | |
| 47 user_data.clear(); | |
| 48 EXPECT_TRUE(data_deleted); | |
| 49 } | |
| 50 | |
| 51 // Verifies Set() called with an existing window replaces the existing data. | |
| 52 TEST_F(WmWindowUserDataTest, ReplaceDestroys) { | |
| 53 WmWindowUserData<Data> user_data; | |
| 54 std::unique_ptr<aura::Window> window( | |
| 55 base::MakeUnique<aura::Window>(nullptr, ui::wm::WINDOW_TYPE_UNKNOWN)); | |
| 56 window->Init(ui::LAYER_NOT_DRAWN); | |
| 57 bool data1_deleted = false; | |
| 58 user_data.Set(WmWindow::Get(window.get()), | |
| 59 base::MakeUnique<Data>(&data1_deleted)); | |
| 60 EXPECT_FALSE(data1_deleted); | |
| 61 bool data2_deleted = false; | |
| 62 user_data.Set(WmWindow::Get(window.get()), | |
| 63 base::MakeUnique<Data>(&data2_deleted)); | |
| 64 EXPECT_TRUE(data1_deleted); | |
| 65 EXPECT_FALSE(data2_deleted); | |
| 66 ASSERT_EQ(1u, user_data.GetWindows().size()); | |
| 67 EXPECT_EQ(WmWindow::Get(window.get()), *user_data.GetWindows().begin()); | |
| 68 window.reset(); | |
| 69 EXPECT_TRUE(data2_deleted); | |
| 70 EXPECT_TRUE(user_data.GetWindows().empty()); | |
| 71 } | |
| 72 | |
| 73 // Verifies Set() with null deletes existing data. | |
| 74 TEST_F(WmWindowUserDataTest, NullClears) { | |
| 75 WmWindowUserData<Data> user_data; | |
| 76 std::unique_ptr<aura::Window> window( | |
| 77 base::MakeUnique<aura::Window>(nullptr, ui::wm::WINDOW_TYPE_UNKNOWN)); | |
| 78 window->Init(ui::LAYER_NOT_DRAWN); | |
| 79 bool data1_deleted = false; | |
| 80 user_data.Set(WmWindow::Get(window.get()), | |
| 81 base::MakeUnique<Data>(&data1_deleted)); | |
| 82 EXPECT_FALSE(data1_deleted); | |
| 83 user_data.Set(WmWindow::Get(window.get()), nullptr); | |
| 84 EXPECT_TRUE(data1_deleted); | |
| 85 EXPECT_TRUE(user_data.GetWindows().empty()); | |
| 86 } | |
| 87 | |
| 88 } // namespace ash | |
| OLD | NEW |