Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: components/copresence/copresence_state_unittest.cc

Issue 806853003: Revert of Revert of Adding CopresenceState (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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 <string>
6 #include <vector>
7
8 #include "base/time/time.h"
9 #include "components/copresence/copresence_state_impl.h"
10 #include "components/copresence/proto/data.pb.h"
11 #include "components/copresence/public/copresence_observer.h"
12 #include "components/copresence/tokens.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14
15 using testing::ElementsAre;
16 using testing::Key;
17 using testing::SizeIs;
18 using testing::UnorderedElementsAre;
19
20 // TODO(ckehoe): Test start and end time tracking.
21
22 namespace google {
23 namespace protobuf {
24
25 bool operator==(const MessageLite& A, const MessageLite& B) {
26 std::string serializedA;
27 CHECK(A.SerializeToString(&serializedA));
28
29 std::string serializedB;
30 CHECK(B.SerializeToString(&serializedB));
31
32 return serializedA == serializedB;
33 }
34
35 } // namespace protobuf
36 } // namespace google
37
38 namespace copresence {
39
40 namespace {
41
42 const base::Time kStartTime = base::Time::FromDoubleT(10);
43 const base::Time kStopTime = base::Time::FromDoubleT(20);
44
45 Directive CreateDirective(const std::string& token, bool transmit) {
46 Directive directive;
47 TokenInstruction* instruction = directive.mutable_token_instruction();
48 instruction->set_token_id(token);
49 instruction->set_medium(AUDIO_ULTRASOUND_PASSBAND);
50 if (transmit)
51 instruction->set_token_instruction_type(TRANSMIT);
52 return directive;
53 }
54
55 template<typename TokenType>
56 TokenType CreateToken(const std::string& id) {
57 TokenType token;
58 token.id = id;
59 token.medium = AUDIO_ULTRASOUND_PASSBAND;
60 token.start_time = kStartTime;
61 return token;
62 }
63
64 } // namespace
65
66 class CopresenceStateTest : public CopresenceObserver,
67 public testing::Test {
68 public:
69 CopresenceStateTest() : directive_notifications_(0) {
70 state_.AddObserver(this);
71 }
72
73 protected:
74 CopresenceStateImpl state_;
75
76 int directive_notifications_;
77 std::vector<std::string> transmitted_updates_;
78 std::vector<std::string> received_updates_;
79
80 private:
81 // CopresenceObserver implementation.
82 void DirectivesUpdated() override {
83 directive_notifications_++;
84 }
85 void TokenTransmitted(const TransmittedToken& token) override {
86 transmitted_updates_.push_back(token.id);
87 }
88 void TokenReceived(const ReceivedToken& token) override {
89 received_updates_.push_back(token.id);
90 }
91 };
92
93 TEST_F(CopresenceStateTest, Directives) {
94 std::vector<Directive> directives;
95 directives.push_back(CreateDirective("transmit 1", true));
96 directives.push_back(CreateDirective("transmit 2", true));
97 directives.push_back(CreateDirective("receive", false));
98 state_.UpdateDirectives(directives);
99
100 EXPECT_EQ(1, directive_notifications_);
101 EXPECT_EQ(directives, state_.active_directives());
102 EXPECT_THAT(transmitted_updates_, ElementsAre("transmit 1", "transmit 2"));
103 EXPECT_THAT(state_.transmitted_tokens(),
104 UnorderedElementsAre(Key("transmit 1"), Key("transmit 2")));
105
106 directives.clear();
107 directives.push_back(CreateDirective("transmit 1", true));
108 state_.UpdateDirectives(directives);
109 EXPECT_EQ(2, directive_notifications_);
110 EXPECT_EQ(directives, state_.active_directives());
111 EXPECT_THAT(state_.transmitted_tokens(), SizeIs(2));
112 }
113
114 TEST_F(CopresenceStateTest, TransmittedTokens) {
115 state_.UpdateTransmittedToken(CreateToken<TransmittedToken>("A"));
116 state_.UpdateTransmittedToken(CreateToken<TransmittedToken>("B"));
117
118 EXPECT_THAT(transmitted_updates_, ElementsAre("A", "B"));
119 EXPECT_THAT(state_.transmitted_tokens(),
120 UnorderedElementsAre(Key("A"), Key("B")));
121
122 TransmittedToken tokenA = CreateToken<TransmittedToken>("A");
123 tokenA.stop_time = kStopTime;
124 state_.UpdateTransmittedToken(tokenA);
125
126 EXPECT_THAT(transmitted_updates_, ElementsAre("A", "B", "A"));
127 EXPECT_EQ(kStopTime, state_.transmitted_tokens().find("A")->second.stop_time);
128
129 state_.UpdateReceivedToken(CreateToken<ReceivedToken>("B"));
130 EXPECT_THAT(transmitted_updates_, ElementsAre("A", "B", "A", "B"));
131 EXPECT_TRUE(state_.transmitted_tokens().find("B")
132 ->second.broadcast_confirmed);
133 }
134
135 TEST_F(CopresenceStateTest, ReceivedTokens) {
136 state_.UpdateReceivedToken(CreateToken<ReceivedToken>("A"));
137 state_.UpdateReceivedToken(CreateToken<ReceivedToken>("B"));
138
139 EXPECT_THAT(received_updates_, ElementsAre("A", "B"));
140 EXPECT_THAT(state_.received_tokens(),
141 UnorderedElementsAre(Key("A"), Key("B")));
142
143 state_.UpdateTokenStatus("A", copresence::VALID);
144 EXPECT_THAT(received_updates_, ElementsAre("A", "B", "A"));
145 EXPECT_EQ(ReceivedToken::VALID,
146 state_.received_tokens().find("A")->second.valid);
147 }
148
149 } // namespace copresence
OLDNEW
« no previous file with comments | « components/copresence/copresence_state_impl.cc ('k') | components/copresence/handlers/audio/audio_directive_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698