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 "base/sequence_token.h"
6 
7 #include "testing/gtest/include/gtest/gtest.h"
8 
9 namespace base {
10 
TEST(SequenceTokenTest,IsValid)11 TEST(SequenceTokenTest, IsValid) {
12   EXPECT_FALSE(SequenceToken().IsValid());
13   EXPECT_TRUE(SequenceToken::Create().IsValid());
14 }
15 
TEST(SequenceTokenTest,OperatorEquals)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();
21 
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);
26 
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);
31 }
32 
TEST(SequenceTokenTest,OperatorNotEquals)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();
38 
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);
43 
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);
48 }
49 
TEST(SequenceTokenTest,GetForCurrentThread)50 TEST(SequenceTokenTest, GetForCurrentThread) {
51   const SequenceToken token = SequenceToken::Create();
52 
53   EXPECT_FALSE(SequenceToken::GetForCurrentThread().IsValid());
54 
55   {
56     ScopedSetSequenceTokenForCurrentThread
57         scoped_set_sequence_token_for_current_thread(token);
58     EXPECT_TRUE(SequenceToken::GetForCurrentThread().IsValid());
59     EXPECT_EQ(token, SequenceToken::GetForCurrentThread());
60   }
61 
62   EXPECT_FALSE(SequenceToken::GetForCurrentThread().IsValid());
63 }
64 
TEST(SequenceTokenTest,ToInternalValue)65 TEST(SequenceTokenTest, ToInternalValue) {
66   const SequenceToken token1 = SequenceToken::Create();
67   const SequenceToken token2 = SequenceToken::Create();
68 
69   // Confirm that internal values are unique.
70   EXPECT_NE(token1.ToInternalValue(), token2.ToInternalValue());
71 }
72 
73 // Expect a default-constructed TaskToken to be invalid and not equal to
74 // another invalid TaskToken.
TEST(TaskTokenTest,InvalidDefaultConstructed)75 TEST(TaskTokenTest, InvalidDefaultConstructed) {
76   EXPECT_FALSE(TaskToken().IsValid());
77   EXPECT_NE(TaskToken(), TaskToken());
78 }
79 
80 // Expect a TaskToken returned by TaskToken::GetForCurrentThread() outside the
81 // scope of a ScopedSetSequenceTokenForCurrentThread to be invalid.
TEST(TaskTokenTest,InvalidOutsideScope)82 TEST(TaskTokenTest, InvalidOutsideScope) {
83   EXPECT_FALSE(TaskToken::GetForCurrentThread().IsValid());
84 }
85 
86 // Expect an invalid TaskToken not to be equal with a valid TaskToken.
TEST(TaskTokenTest,ValidNotEqualsInvalid)87 TEST(TaskTokenTest, ValidNotEqualsInvalid) {
88   ScopedSetSequenceTokenForCurrentThread
89       scoped_set_sequence_token_for_current_thread(SequenceToken::Create());
90   TaskToken valid = TaskToken::GetForCurrentThread();
91   TaskToken invalid;
92   EXPECT_NE(valid, invalid);
93 }
94 
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.
TEST(TaskTokenTest,EqualInSameScope)98 TEST(TaskTokenTest, EqualInSameScope) {
99   ScopedSetSequenceTokenForCurrentThread
100       scoped_set_sequence_token_for_current_thread(SequenceToken::Create());
101 
102   const TaskToken token_a = TaskToken::GetForCurrentThread();
103   const TaskToken token_b = TaskToken::GetForCurrentThread();
104 
105   EXPECT_TRUE(token_a.IsValid());
106   EXPECT_TRUE(token_b.IsValid());
107   EXPECT_EQ(token_a, token_b);
108 }
109 
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.
TEST(TaskTokenTest,NotEqualInDifferentScopes)113 TEST(TaskTokenTest, NotEqualInDifferentScopes) {
114   TaskToken token_a;
115   TaskToken token_b;
116 
117   {
118     ScopedSetSequenceTokenForCurrentThread
119         scoped_set_sequence_token_for_current_thread(SequenceToken::Create());
120     token_a = TaskToken::GetForCurrentThread();
121   }
122   {
123     ScopedSetSequenceTokenForCurrentThread
124         scoped_set_sequence_token_for_current_thread(SequenceToken::Create());
125     token_b = TaskToken::GetForCurrentThread();
126   }
127 
128   EXPECT_TRUE(token_a.IsValid());
129   EXPECT_TRUE(token_b.IsValid());
130   EXPECT_NE(token_a, token_b);
131 }
132 
133 }  // namespace base
134