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

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

Issue 764673003: 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 google::protobuf::MessageLite;
16 using testing::ElementsAre;
17 using testing::Key;
18 using testing::SizeIs;
19 using testing::UnorderedElementsAre;
20
21 // TODO(ckehoe): Test start and end time tracking.
22
23 namespace google {
24 namespace protobuf {
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 }
xiyuan 2014/12/19 04:03:02 nit: } // namespace protobuf
Charlie 2014/12/19 05:25:39 Done.
35 }
xiyuan 2014/12/19 04:03:02 nit: } // namespace google
Charlie 2014/12/19 05:25:39 Done.
36
37 namespace copresence {
38
39 namespace {
40
41 const double kStartTime = 10;
42
43 Directive CreateDirective(const std::string& token, bool transmit) {
44 Directive directive;
45 TokenInstruction* instruction = directive.mutable_token_instruction();
46 instruction->set_token_id(token);
47 instruction->set_medium(AUDIO_ULTRASOUND_PASSBAND);
48 if (transmit)
49 instruction->set_token_instruction_type(TRANSMIT);
50 return directive;
51 }
52
53 template<typename TokenType>
54 TokenType CreateToken(const std::string& id) {
55 TokenType token;
56 token.id = id;
57 token.medium = AUDIO_ULTRASOUND_PASSBAND;
58 token.start_time = base::Time::FromDoubleT(kStartTime);
59 return token;
60 }
61
62 } // namespace
63
64 class CopresenceStateTest : public CopresenceObserver,
65 public testing::Test {
66 public:
67 CopresenceStateTest() : directive_notifications_(0) {
68 state_.AddObserver(this);
69 }
70
71 protected:
72 CopresenceStateImpl state_;
73
74 int directive_notifications_;
75 std::vector<std::string> transmitted_updates_;
76 std::vector<std::string> received_updates_;
77
78 private:
79 // CopresenceObserver implementation.
80 void DirectivesUpdated() override {
81 directive_notifications_++;
xiyuan 2014/12/19 04:03:01 nit: ++directive_notifications_;
Charlie 2014/12/19 05:25:39 Style guide says this can be either before or afte
82 }
83 void TokenTransmitted(const TransmittedToken& token) override {
84 transmitted_updates_.push_back(token.id);
85 }
86 void TokenReceived(const ReceivedToken& token) override {
87 received_updates_.push_back(token.id);
88 }
89 };
90
91 TEST_F(CopresenceStateTest, Directives) {
92 std::vector<Directive> directives;
93 directives.push_back(CreateDirective("transmit 1", true));
94 directives.push_back(CreateDirective("transmit 2", true));
95 directives.push_back(CreateDirective("receive", false));
96 state_.UpdateDirectives(directives);
97
98 EXPECT_EQ(1, directive_notifications_);
99 EXPECT_EQ(directives, state_.active_directives());
100 EXPECT_THAT(transmitted_updates_, ElementsAre("transmit 1", "transmit 2"));
101 EXPECT_THAT(state_.transmitted_tokens(),
102 UnorderedElementsAre(Key("transmit 1"), Key("transmit 2")));
103
104 directives.clear();
105 directives.push_back(CreateDirective("transmit 1", true));
106 state_.UpdateDirectives(directives);
107 EXPECT_EQ(2, directive_notifications_);
108 EXPECT_EQ(directives, state_.active_directives());
109 EXPECT_THAT(state_.transmitted_tokens(), SizeIs(2));
110 }
111
112 TEST_F(CopresenceStateTest, TransmittedTokens) {
113 state_.UpdateTransmittedToken(CreateToken<TransmittedToken>("A"));
114 state_.UpdateTransmittedToken(CreateToken<TransmittedToken>("B"));
115
116 EXPECT_THAT(transmitted_updates_, ElementsAre("A", "B"));
117 EXPECT_THAT(state_.transmitted_tokens(),
118 UnorderedElementsAre(Key("A"), Key("B")));
119
120 const base::Time kStopTime = base::Time::FromDoubleT(20);
121 TransmittedToken tokenA = CreateToken<TransmittedToken>("A");
122 tokenA.stop_time = kStopTime;
123 state_.UpdateTransmittedToken(tokenA);
124
125 EXPECT_THAT(transmitted_updates_, ElementsAre("A", "B", "A"));
126 EXPECT_EQ(kStopTime, state_.transmitted_tokens().find("A")->second.stop_time);
127
128 state_.UpdateReceivedToken(CreateToken<ReceivedToken>("B"));
129 EXPECT_THAT(transmitted_updates_, ElementsAre("A", "B", "A", "B"));
130 EXPECT_TRUE(state_.transmitted_tokens().find("B")
131 ->second.broadcast_confirmed);
132 }
133
134 TEST_F(CopresenceStateTest, ReceivedTokens) {
135 state_.UpdateReceivedToken(CreateToken<ReceivedToken>("A"));
136 state_.UpdateReceivedToken(CreateToken<ReceivedToken>("B"));
137
138 EXPECT_THAT(received_updates_, ElementsAre("A", "B"));
139 EXPECT_THAT(state_.received_tokens(),
140 UnorderedElementsAre(Key("A"), Key("B")));
141
142 state_.UpdateTokenStatus("A", copresence::VALID);
143 EXPECT_THAT(received_updates_, ElementsAre("A", "B", "A"));
144 EXPECT_EQ(ReceivedToken::VALID,
145 state_.received_tokens().find("A")->second.valid);
146 }
147
148 } // namespace copresence
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698