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.
5 #include "base/sequence_token.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 TEST(SequenceTokenTest, IsValid) {
12 EXPECT_FALSE(SequenceToken().IsValid());
13 EXPECT_TRUE(SequenceToken::Create().IsValid());
16 TEST(SequenceTokenTest, OperatorEquals) {
17 const SequenceToken invalid_a;
18 const SequenceToken invalid_b;
19 const SequenceToken valid_a = SequenceToken::Create();
20 const SequenceToken valid_b = SequenceToken::Create();
22 EXPECT_FALSE(invalid_a == invalid_a);
23 EXPECT_FALSE(invalid_a == invalid_b);
24 EXPECT_FALSE(invalid_a == valid_a);
25 EXPECT_FALSE(invalid_a == valid_b);
27 EXPECT_FALSE(valid_a == invalid_a);
28 EXPECT_FALSE(valid_a == invalid_b);
29 EXPECT_EQ(valid_a, valid_a);
30 EXPECT_FALSE(valid_a == valid_b);
33 TEST(SequenceTokenTest, OperatorNotEquals) {
34 const SequenceToken invalid_a;
35 const SequenceToken invalid_b;
36 const SequenceToken valid_a = SequenceToken::Create();
37 const SequenceToken valid_b = SequenceToken::Create();
39 EXPECT_NE(invalid_a, invalid_a);
40 EXPECT_NE(invalid_a, invalid_b);
41 EXPECT_NE(invalid_a, valid_a);
42 EXPECT_NE(invalid_a, valid_b);
44 EXPECT_NE(valid_a, invalid_a);
45 EXPECT_NE(valid_a, invalid_b);
46 EXPECT_FALSE(valid_a != valid_a);
47 EXPECT_NE(valid_a, valid_b);
50 TEST(SequenceTokenTest, GetForCurrentThread) {
51 const SequenceToken token = SequenceToken::Create();
53 EXPECT_FALSE(SequenceToken::GetForCurrentThread().IsValid());
56 ScopedSetSequenceTokenForCurrentThread
57 scoped_set_sequence_token_for_current_thread(token);
58 EXPECT_TRUE(SequenceToken::GetForCurrentThread().IsValid());
59 EXPECT_EQ(token, SequenceToken::GetForCurrentThread());
62 EXPECT_FALSE(SequenceToken::GetForCurrentThread().IsValid());
65 TEST(SequenceTokenTest, ToInternalValue) {
66 const SequenceToken token1 = SequenceToken::Create();
67 const SequenceToken token2 = SequenceToken::Create();
69 // Confirm that internal values are unique.
70 EXPECT_NE(token1.ToInternalValue(), token2.ToInternalValue());
73 // Expect a default-constructed TaskToken to be invalid and not equal to
74 // another invalid TaskToken.
75 TEST(TaskTokenTest, InvalidDefaultConstructed) {
76 EXPECT_FALSE(TaskToken().IsValid());
77 EXPECT_NE(TaskToken(), TaskToken());
80 // Expect a TaskToken returned by TaskToken::GetForCurrentThread() outside the
81 // scope of a ScopedSetSequenceTokenForCurrentThread to be invalid.
82 TEST(TaskTokenTest, InvalidOutsideScope) {
83 EXPECT_FALSE(TaskToken::GetForCurrentThread().IsValid());
86 // Expect an invalid TaskToken not to be equal with a valid TaskToken.
87 TEST(TaskTokenTest, ValidNotEqualsInvalid) {
88 ScopedSetSequenceTokenForCurrentThread
89 scoped_set_sequence_token_for_current_thread(SequenceToken::Create());
90 TaskToken valid = TaskToken::GetForCurrentThread();
92 EXPECT_NE(valid, invalid);
95 // Expect TaskTokens returned by TaskToken::GetForCurrentThread() in the scope
96 // of the same ScopedSetSequenceTokenForCurrentThread instance to be
97 // valid and equal with each other.
98 TEST(TaskTokenTest, EqualInSameScope) {
99 ScopedSetSequenceTokenForCurrentThread
100 scoped_set_sequence_token_for_current_thread(SequenceToken::Create());
102 const TaskToken token_a = TaskToken::GetForCurrentThread();
103 const TaskToken token_b = TaskToken::GetForCurrentThread();
105 EXPECT_TRUE(token_a.IsValid());
106 EXPECT_TRUE(token_b.IsValid());
107 EXPECT_EQ(token_a, token_b);
110 // Expect TaskTokens returned by TaskToken::GetForCurrentThread() in the scope
111 // of different ScopedSetSequenceTokenForCurrentThread instances to be
112 // valid but not equal to each other.
113 TEST(TaskTokenTest, NotEqualInDifferentScopes) {
118 ScopedSetSequenceTokenForCurrentThread
119 scoped_set_sequence_token_for_current_thread(SequenceToken::Create());
120 token_a = TaskToken::GetForCurrentThread();
123 ScopedSetSequenceTokenForCurrentThread
124 scoped_set_sequence_token_for_current_thread(SequenceToken::Create());
125 token_b = TaskToken::GetForCurrentThread();
128 EXPECT_TRUE(token_a.IsValid());
129 EXPECT_TRUE(token_b.IsValid());
130 EXPECT_NE(token_a, token_b);