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