Credential Manager API shouldn't save passwords from incognito.
[chromium-blink-merge.git] / components / copresence / copresence_state_unittest.cc
blob4817e7e426b07d0b70aece3d260a016b700d5c99
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.
5 #include <string>
6 #include <vector>
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"
15 using testing::ElementsAre;
16 using testing::Key;
17 using testing::SizeIs;
18 using testing::UnorderedElementsAre;
20 // TODO(ckehoe): Test start and end time tracking.
22 namespace google {
23 namespace protobuf {
25 bool operator==(const MessageLite& A, const MessageLite& B) {
26 std::string serializedA;
27 CHECK(A.SerializeToString(&serializedA));
29 std::string serializedB;
30 CHECK(B.SerializeToString(&serializedB));
32 return serializedA == serializedB;
35 } // namespace protobuf
36 } // namespace google
38 namespace copresence {
40 namespace {
42 const base::Time kStartTime = base::Time::FromDoubleT(10);
43 const base::Time kStopTime = base::Time::FromDoubleT(20);
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;
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;
64 } // namespace
66 class CopresenceStateTest : public CopresenceObserver,
67 public testing::Test {
68 public:
69 CopresenceStateTest() : directive_notifications_(0) {
70 state_.AddObserver(this);
73 protected:
74 CopresenceStateImpl state_;
76 int directive_notifications_;
77 std::vector<std::string> transmitted_updates_;
78 std::vector<std::string> received_updates_;
80 private:
81 // CopresenceObserver implementation.
82 void DirectivesUpdated() override {
83 directive_notifications_++;
85 void TokenTransmitted(const TransmittedToken& token) override {
86 transmitted_updates_.push_back(token.id);
88 void TokenReceived(const ReceivedToken& token) override {
89 received_updates_.push_back(token.id);
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);
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")));
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));
114 TEST_F(CopresenceStateTest, TransmittedTokens) {
115 state_.UpdateTransmittedToken(CreateToken<TransmittedToken>("A"));
116 state_.UpdateTransmittedToken(CreateToken<TransmittedToken>("B"));
118 EXPECT_THAT(transmitted_updates_, ElementsAre("A", "B"));
119 EXPECT_THAT(state_.transmitted_tokens(),
120 UnorderedElementsAre(Key("A"), Key("B")));
122 TransmittedToken tokenA = CreateToken<TransmittedToken>("A");
123 tokenA.stop_time = kStopTime;
124 state_.UpdateTransmittedToken(tokenA);
126 EXPECT_THAT(transmitted_updates_, ElementsAre("A", "B", "A"));
127 EXPECT_EQ(kStopTime, state_.transmitted_tokens().find("A")->second.stop_time);
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);
135 TEST_F(CopresenceStateTest, ReceivedTokens) {
136 state_.UpdateReceivedToken(CreateToken<ReceivedToken>("A"));
137 state_.UpdateReceivedToken(CreateToken<ReceivedToken>("B"));
139 EXPECT_THAT(received_updates_, ElementsAre("A", "B"));
140 EXPECT_THAT(state_.received_tokens(),
141 UnorderedElementsAre(Key("A"), Key("B")));
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);
149 } // namespace copresence