Flutter Engine
The Flutter Engine
ax_unique_id_unittest.cc
Go to the documentation of this file.
1// Copyright 2015 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 "ax_unique_id.h"
6
7#include <memory>
8
9#include "gtest/gtest.h"
10
11namespace ui {
12
13TEST(AXPlatformUniqueIdTest, IdsAreUnique) {
14 AXUniqueId id1, id2;
15 EXPECT_FALSE(id1 == id2);
16 EXPECT_GT(id2.Get(), id1.Get());
17}
18
19static const int32_t kMaxId = 100;
20
22 public:
25
26 private:
27 friend class AXUniqueId;
29};
30
33
34TEST(AXPlatformUniqueIdTest, UnassignedIdsAreReused) {
35 // Create a bank of ids that uses up all available ids.
36 // Then remove an id and replace with a new one. Since it's the only
37 // slot available, the id will end up having the same value, rather than
38 // starting over at 1.
39 std::unique_ptr<AXTestSmallBankUniqueId> ids[kMaxId];
40
41 for (int i = 0; i < kMaxId; i++) {
42 ids[i] = std::make_unique<AXTestSmallBankUniqueId>();
43 }
44
45 static int kIdToReplace = 10;
46 int32_t expected_id = ids[kIdToReplace]->Get();
47
48 // Delete one of the ids and replace with a new one.
49 ids[kIdToReplace] = nullptr;
50 ids[kIdToReplace] = std::make_unique<AXTestSmallBankUniqueId>();
51
52 // Expect that the original Id gets reused.
53 EXPECT_EQ(ids[kIdToReplace]->Get(), expected_id);
54}
55
56TEST(AXPlatformUniqueIdTest, DoesCreateCorrectId) {
57 constexpr int kLargerThanMaxId = kMaxId * 2;
58 std::unique_ptr<AXUniqueId> ids[kLargerThanMaxId];
59 // Creates and releases to fill up the internal static counter.
60 for (int i = 0; i < kLargerThanMaxId; i++) {
61 ids[i] = std::make_unique<AXUniqueId>();
62 }
63 for (int i = 0; i < kLargerThanMaxId; i++) {
64 ids[i].reset(nullptr);
65 }
66 // Creates an unique id whose max value is less than the internal
67 // static counter.
68 std::unique_ptr<AXTestSmallBankUniqueId> unique_id =
69 std::make_unique<AXTestSmallBankUniqueId>();
70
71 EXPECT_LE(unique_id->Get(), kMaxId);
72}
73
74} // namespace ui
int32_t Get() const
Definition: ax_unique_id.h:30
const GrXPFactory * Get(SkBlendMode mode)
TEST(AXEnumUtilTest, Event)
static const int32_t kMaxId
#define BASE_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:8