Chromium Code Reviews| Index: components/browser_watcher/system_session_analyzer_win_unittest.cc |
| diff --git a/components/browser_watcher/system_session_analyzer_win_unittest.cc b/components/browser_watcher/system_session_analyzer_win_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..22a6416ee29ea7b0a12586dd30c0e5ddd5fcc157 |
| --- /dev/null |
| +++ b/components/browser_watcher/system_session_analyzer_win_unittest.cc |
| @@ -0,0 +1,118 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/browser_watcher/system_session_analyzer_win.h" |
| + |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/time/time.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace browser_watcher { |
| + |
| +// Ensure the fetcher retrieves events. Assumes the test host has at least 1 |
| +// prior session. |
| +TEST(SystemSessionEventFetcherTest, TestRetrieval) { |
|
Sigurður Ásgeirsson
2017/03/01 15:09:27
mmm - will this fail on every new test bot until i
manzagop (departed)
2017/03/01 21:07:45
Yeah, this will happen and will be annoying. I've
|
| + SystemSessionEventFetcher fetcher(1); |
| + |
| + std::vector<SystemSessionEventFetcher::EventInfo> events; |
| + ASSERT_TRUE(fetcher.FetchEvents(&events)); |
| + EXPECT_EQ(3U, events.size()); |
| +} |
| + |
| +// Ensure the fetcher's retrieved events conform to our expectations. This |
| +// conformance is validated using the SystemSessionAnalyzer. Note that the |
| +// ability of SystemSessionAnalyzer to validate the conformance is tested below. |
| +TEST(SystemSessionEventFetcherTest, NoEvent) { |
| + std::unique_ptr<SystemSessionEventFetcher> fetcher( |
| + new SystemSessionEventFetcher(1)); |
| + SystemSessionAnalyzer analyzer(std::move(fetcher)); |
| + EXPECT_EQ(SystemSessionAnalyzer::CLEAN, |
| + analyzer.IsSessionUnclean(base::Time::Now())); |
| +} |
| + |
|
Sigurður Ásgeirsson
2017/03/01 15:09:27
In Syzygy we use "Mock"XXX - is there a tradition
manzagop (departed)
2017/03/01 21:07:45
Actually, this is a stub. Mocks validate interacti
|
| +class FakeSystemSessionEventFetcher : public SystemSessionEventFetcher { |
| + public: |
| + FakeSystemSessionEventFetcher() : SystemSessionEventFetcher(10U) {} |
| + |
| + bool FetchEvents(std::vector<EventInfo>* event_infos) const override { |
| + DCHECK(event_infos); |
| + *event_infos = events_; |
| + return true; |
| + } |
| + |
| + void AddEvent(const EventInfo& info) { events_.push_back(info); } |
| + |
| + private: |
| + std::vector<EventInfo> events_; |
| +}; |
| + |
| +TEST(SystemSessionAnalyzerTest, StandardCase) { |
| + std::unique_ptr<FakeSystemSessionEventFetcher> fetcher( |
| + new FakeSystemSessionEventFetcher()); |
| + |
| + base::Time time = base::Time::Now(); |
| + fetcher->AddEvent({6005U, time}); |
|
Sigurður Ásgeirsson
2017/03/01 15:09:27
nit: can we have declarative constants for the mag
manzagop (departed)
2017/03/01 21:07:45
Done.
|
| + fetcher->AddEvent({6008U, time - base::TimeDelta::FromSeconds(10)}); |
| + fetcher->AddEvent({6005U, time - base::TimeDelta::FromSeconds(20)}); |
| + fetcher->AddEvent({6006U, time - base::TimeDelta::FromSeconds(22)}); |
| + fetcher->AddEvent({6005U, time - base::TimeDelta::FromSeconds(28)}); |
| + |
| + SystemSessionAnalyzer analyzer(std::move(fetcher)); |
| + |
| + EXPECT_EQ(SystemSessionAnalyzer::OUTSIDE_RANGE, |
| + analyzer.IsSessionUnclean(time - base::TimeDelta::FromSeconds(30))); |
| + EXPECT_EQ(SystemSessionAnalyzer::CLEAN, |
| + analyzer.IsSessionUnclean(time - base::TimeDelta::FromSeconds(25))); |
| + EXPECT_EQ(SystemSessionAnalyzer::UNCLEAN, |
| + analyzer.IsSessionUnclean(time - base::TimeDelta::FromSeconds(20))); |
| + EXPECT_EQ(SystemSessionAnalyzer::UNCLEAN, |
| + analyzer.IsSessionUnclean(time - base::TimeDelta::FromSeconds(15))); |
| + EXPECT_EQ(SystemSessionAnalyzer::UNCLEAN, |
| + analyzer.IsSessionUnclean(time - base::TimeDelta::FromSeconds(10))); |
| + EXPECT_EQ(SystemSessionAnalyzer::CLEAN, |
| + analyzer.IsSessionUnclean(time - base::TimeDelta::FromSeconds(5))); |
| + EXPECT_EQ(SystemSessionAnalyzer::CLEAN, |
| + analyzer.IsSessionUnclean(time + base::TimeDelta::FromSeconds(5))); |
| +} |
| + |
| +TEST(SystemSessionAnalyzerTest, NoEvent) { |
| + std::unique_ptr<SystemSessionEventFetcher> fetcher( |
| + new FakeSystemSessionEventFetcher()); |
| + SystemSessionAnalyzer analyzer(std::move(fetcher)); |
| + EXPECT_EQ(SystemSessionAnalyzer::FAILED, |
| + analyzer.IsSessionUnclean(base::Time::Now())); |
| +} |
| + |
| +TEST(SystemSessionAnalyzerTest, TimeInversion) { |
| + std::unique_ptr<FakeSystemSessionEventFetcher> fetcher( |
| + new FakeSystemSessionEventFetcher()); |
| + |
| + base::Time time = base::Time::Now(); |
| + fetcher->AddEvent({6005U, time}); |
| + fetcher->AddEvent({6006U, time + base::TimeDelta::FromSeconds(1)}); |
| + fetcher->AddEvent({6005U, time - base::TimeDelta::FromSeconds(1)}); |
| + |
| + SystemSessionAnalyzer analyzer(std::move(fetcher)); |
| + EXPECT_EQ(SystemSessionAnalyzer::FAILED, |
| + analyzer.IsSessionUnclean(base::Time::Now())); |
| +} |
| + |
| +TEST(SystemSessionAnalyzerTest, IdInversion) { |
| + std::unique_ptr<FakeSystemSessionEventFetcher> fetcher( |
| + new FakeSystemSessionEventFetcher()); |
| + |
| + base::Time time = base::Time::Now(); |
| + fetcher->AddEvent({6005U, time}); |
| + fetcher->AddEvent({6005U, time - base::TimeDelta::FromSeconds(1)}); |
| + fetcher->AddEvent({6006U, time - base::TimeDelta::FromSeconds(2)}); |
| + |
| + SystemSessionAnalyzer analyzer(std::move(fetcher)); |
| + EXPECT_EQ(SystemSessionAnalyzer::FAILED, |
| + analyzer.IsSessionUnclean(base::Time::Now())); |
| +} |
| + |
| +} // namespace browser_watcher |