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

Side by Side Diff: remoting/client/client_telemetry_logger_unittest.cc

Issue 2643483003: [Remoting Android] Refactor ClientTelemetryLogger (Closed)
Patch Set: PTAL Created 3 years, 11 months 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "remoting/client/client_telemetry_logger.h" 5 #include "remoting/client/client_telemetry_logger.h"
6 6
7 #include <deque> 7 #include <deque>
8 #include <string> 8 #include <string>
9 9
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/memory/weak_ptr.h"
11 #include "remoting/protocol/connection_to_host.h" 12 #include "remoting/protocol/connection_to_host.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 namespace { 15 namespace {
15 16
16 // Returns true if |actual| has all fields that |expected| has and the values 17 // Returns true if |actual| has all fields that |expected| has and the values
17 // also match. |actual| can have fields that |expected| doesn't have but not the 18 // also match. |actual| can have fields that |expected| doesn't have but not the
18 // other way around. 19 // other way around.
19 static bool Contains(const remoting::ChromotingEvent& actual, 20 static bool Contains(const remoting::ChromotingEvent& actual,
20 const remoting::ChromotingEvent& expected) { 21 const remoting::ChromotingEvent& expected) {
(...skipping 19 matching lines...) Expand all
40 41
41 namespace remoting { 42 namespace remoting {
42 43
43 // The fake log writer will pass the test IFF: 44 // The fake log writer will pass the test IFF:
44 // 1. The caller logs an entry when the writer expects an entry to be logged. 45 // 1. The caller logs an entry when the writer expects an entry to be logged.
45 // 2. The caller logs an entry with all key-value pairs found in the expected 46 // 2. The caller logs an entry with all key-value pairs found in the expected
46 // entry. 47 // entry.
47 // 3. There are no more expected log entries when the writer destructs. 48 // 3. There are no more expected log entries when the writer destructs.
48 class FakeLogWriter : public ChromotingEventLogWriter { 49 class FakeLogWriter : public ChromotingEventLogWriter {
49 public: 50 public:
51 FakeLogWriter();
52
50 ~FakeLogWriter() override { 53 ~FakeLogWriter() override {
51 EXPECT_TRUE(expected_events_.empty()) << "Sent less logs than expected."; 54 EXPECT_TRUE(expected_events_.empty()) << "Sent less logs than expected.";
52 } 55 }
53 56
54 // Add the event to |expected_events_|. Log() will only succeed IFF the actual 57 // Add the event to |expected_events_|. Log() will only succeed IFF the actual
55 // entry has all fields that the expected entry (first entry in 58 // entry has all fields that the expected entry (first entry in
56 // |expected_events|) has and the values also match. The actual entry can have 59 // |expected_events|) has and the values also match. The actual entry can have
57 // more fields in addition to those in the expected entry. 60 // more fields in addition to those in the expected entry.
58 void AddExpectedEvent(const ChromotingEvent& entry); 61 void AddExpectedEvent(const ChromotingEvent& entry);
59 62
63 base::WeakPtr<FakeLogWriter> GetWeakPtr();
64
60 // ChromotingEventLogWriter overrides. 65 // ChromotingEventLogWriter overrides.
61 void Log(const ChromotingEvent& entry) override; 66 void Log(const ChromotingEvent& entry) override;
62 void SetAuthToken(const std::string& auth_token) override; 67 void SetAuthToken(const std::string& auth_token) override;
63 void SetAuthClosure(const base::Closure& closure) override; 68 void SetAuthClosure(const base::Closure& closure) override;
64 69
65 const std::string& auth_token() const { return auth_token_; } 70 const std::string& auth_token() const { return auth_token_; }
66 const base::Closure& auth_closure() const { return auth_closure_; } 71 const base::Closure& auth_closure() const { return auth_closure_; }
67 72
68 private: 73 private:
69 std::deque<ChromotingEvent> expected_events_; 74 std::deque<ChromotingEvent> expected_events_;
70 std::string auth_token_; 75 std::string auth_token_;
71 base::Closure auth_closure_; 76 base::Closure auth_closure_;
77 base::WeakPtrFactory<FakeLogWriter> weak_factory_;
72 }; 78 };
73 79
80 FakeLogWriter::FakeLogWriter() : weak_factory_(this) {
81 }
82
74 void FakeLogWriter::AddExpectedEvent(const ChromotingEvent& entry) { 83 void FakeLogWriter::AddExpectedEvent(const ChromotingEvent& entry) {
75 expected_events_.push_back(entry); 84 expected_events_.push_back(entry);
76 } 85 }
77 86
87 base::WeakPtr<FakeLogWriter> FakeLogWriter::GetWeakPtr() {
88 return weak_factory_.GetWeakPtr();
89 }
90
78 void FakeLogWriter::Log(const ChromotingEvent& entry) { 91 void FakeLogWriter::Log(const ChromotingEvent& entry) {
79 ASSERT_FALSE(expected_events_.empty()) 92 ASSERT_FALSE(expected_events_.empty())
80 << "Trying to send more logs than expected"; 93 << "Trying to send more logs than expected";
81 ASSERT_TRUE(Contains(entry, expected_events_.front())) 94 ASSERT_TRUE(Contains(entry, expected_events_.front()))
82 << "Unexpected log being sent."; 95 << "Unexpected log being sent.";
83 expected_events_.pop_front(); 96 expected_events_.pop_front();
84 } 97 }
85 98
86 void FakeLogWriter::SetAuthToken(const std::string& auth_token) { 99 void FakeLogWriter::SetAuthToken(const std::string& auth_token) {
87 auth_token_ = auth_token; 100 auth_token_ = auth_token;
88 } 101 }
89 102
90 void FakeLogWriter::SetAuthClosure(const base::Closure& closure) { 103 void FakeLogWriter::SetAuthClosure(const base::Closure& closure) {
91 auth_closure_ = closure; 104 auth_closure_ = closure;
92 } 105 }
93 106
94 class ClientTelemetryLoggerTest : public testing::Test { 107 class ClientTelemetryLoggerTest : public testing::Test {
95 public: 108 public:
96 // testing::Test override. 109 // testing::Test override.
97 void SetUp() override; 110 void SetUp() override;
98 111
99 protected: 112 protected:
100 // |log_writer_| will be owned by |logger_| and freed when |logger_| 113 std::unique_ptr<FakeLogWriter> log_writer_;
101 // destructs. Feel free to use this reference in the test.
102 FakeLogWriter* log_writer_ = nullptr;
103 std::unique_ptr<ClientTelemetryLogger> logger_; 114 std::unique_ptr<ClientTelemetryLogger> logger_;
104 }; 115 };
105 116
106 void ClientTelemetryLoggerTest::SetUp() { 117 void ClientTelemetryLoggerTest::SetUp() {
107 log_writer_ = new FakeLogWriter(); 118 log_writer_.reset(new FakeLogWriter());
108 logger_.reset(new ClientTelemetryLogger(ChromotingEvent::Mode::ME2ME)); 119 logger_.reset(new ClientTelemetryLogger(log_writer_->GetWeakPtr(),
109 logger_->StartForTest(base::WrapUnique(log_writer_)); 120 ChromotingEvent::Mode::ME2ME));
110 } 121 }
111 122
112 TEST_F(ClientTelemetryLoggerTest, LogSessionStateChange) { 123 TEST_F(ClientTelemetryLoggerTest, LogSessionStateChange) {
113 ChromotingEvent event(ChromotingEvent::Type::SESSION_STATE); 124 ChromotingEvent event(ChromotingEvent::Type::SESSION_STATE);
114 event.SetEnum("session_state", ChromotingEvent::SessionState::CONNECTED); 125 event.SetEnum("session_state", ChromotingEvent::SessionState::CONNECTED);
115 event.SetEnum("connection_error", ChromotingEvent::ConnectionError::NONE); 126 event.SetEnum("connection_error", ChromotingEvent::ConnectionError::NONE);
116 log_writer_->AddExpectedEvent(event); 127 log_writer_->AddExpectedEvent(event);
117 logger_->LogSessionStateChange(ChromotingEvent::SessionState::CONNECTED, 128 logger_->LogSessionStateChange(ChromotingEvent::SessionState::CONNECTED,
118 ChromotingEvent::ConnectionError::NONE); 129 ChromotingEvent::ConnectionError::NONE);
119 130
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 178
168 // kMaxSessionIdAgeDays = 1. Fake the generation time to be 2 days ago and 179 // kMaxSessionIdAgeDays = 1. Fake the generation time to be 2 days ago and
169 // force it to expire. 180 // force it to expire.
170 logger_->SetSessionIdGenerationTimeForTest(base::TimeTicks::Now() - 181 logger_->SetSessionIdGenerationTimeForTest(base::TimeTicks::Now() -
171 base::TimeDelta::FromDays(2)); 182 base::TimeDelta::FromDays(2));
172 protocol::PerformanceTracker perf_tracker; 183 protocol::PerformanceTracker perf_tracker;
173 logger_->LogStatistics(&perf_tracker); 184 logger_->LogStatistics(&perf_tracker);
174 EXPECT_NE(last_id, logger_->session_id()); 185 EXPECT_NE(last_id, logger_->session_id());
175 } 186 }
176 187
177 TEST_F(ClientTelemetryLoggerTest, PassesThroughAuthTokenAndClosure) {
178 std::string token("some token");
179 base::Closure closure = base::Bind(&base::DoNothing);
180 logger_->SetAuthToken(token);
181 logger_->SetAuthClosure(closure);
182 EXPECT_EQ(token, log_writer_->auth_token());
183 EXPECT_TRUE(closure.Equals(log_writer_->auth_closure()));
184 }
185
186 } // namespace remoting 188 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698