Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "ui/aura/mus/user_activity_forwarder.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 14 #include "services/ui/common/task_runner_test_base.h" | |
| 15 #include "services/ui/public/interfaces/user_activity_monitor.mojom.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 #include "ui/base/user_activity/user_activity_detector.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // Fake implementation of ui::mojom::UserActivityMonitor for testing that just | |
| 22 // supports tracking and notifying observers. | |
| 23 class FakeUserActivityMonitor : public ui::mojom::UserActivityMonitor { | |
| 24 public: | |
| 25 FakeUserActivityMonitor() : binding_(this) {} | |
| 26 ~FakeUserActivityMonitor() override {} | |
| 27 | |
| 28 ui::mojom::UserActivityMonitorPtr GetPtr() { | |
| 29 return binding_.CreateInterfacePtrAndBind(); | |
| 30 } | |
| 31 | |
| 32 // Notifies all observers about user activity. | |
| 33 // ui::TaskRunnerTestBase::RunUntilIdle() must be called after this method in | |
| 34 // order for observers to receive notifications. | |
| 35 void NotifyUserActivityObservers() { | |
| 36 for (auto& observer : activity_observers_) | |
| 37 observer->OnUserActivity(); | |
| 38 } | |
| 39 | |
| 40 // ui::mojom::UserActivityMonitor: | |
| 41 void AddUserActivityObserver( | |
| 42 uint32_t delay_between_notify_secs, | |
| 43 ui::mojom::UserActivityObserverPtr observer) override { | |
| 44 activity_observers_.push_back(std::move(observer)); | |
| 45 } | |
| 46 void AddUserIdleObserver(uint32_t idleness_in_minutes, | |
| 47 ui::mojom::UserIdleObserverPtr observer) override { | |
| 48 NOTREACHED() << "Unexpected AddUserIdleObserver call"; | |
| 49 } | |
| 50 | |
| 51 private: | |
| 52 mojo::Binding<ui::mojom::UserActivityMonitor> binding_; | |
| 53 std::vector<ui::mojom::UserActivityObserverPtr> activity_observers_; | |
| 54 | |
| 55 DISALLOW_COPY_AND_ASSIGN(FakeUserActivityMonitor); | |
| 56 }; | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 namespace aura { | |
| 61 | |
| 62 class UserActivityForwarderTest : public ui::TaskRunnerTestBase { | |
| 63 public: | |
| 64 UserActivityForwarderTest() {} | |
| 65 ~UserActivityForwarderTest() override {} | |
| 66 | |
| 67 protected: | |
| 68 std::unique_ptr<FakeUserActivityMonitor> monitor_; | |
| 69 std::unique_ptr<ui::UserActivityDetector> detector_; | |
| 70 std::unique_ptr<UserActivityForwarder> forwarder_; | |
| 71 | |
| 72 private: | |
| 73 // ui::TaskRunnerTestBase: | |
| 74 void SetUp() override { | |
| 75 ui::TaskRunnerTestBase::SetUp(); | |
| 76 monitor_ = base::MakeUnique<FakeUserActivityMonitor>(); | |
| 77 detector_ = base::MakeUnique<ui::UserActivityDetector>(); | |
| 78 forwarder_ = base::MakeUnique<UserActivityForwarder>(monitor_->GetPtr(), | |
| 79 detector_.get()); | |
| 80 | |
| 81 // Run pending tasks so |monitor_| receives |forwarder_|'s registration. | |
| 82 RunUntilIdle(); | |
|
James Cook
2017/01/20 03:38:49
nit: The test might be easier to read if all this
Daniel Erat
2017/01/20 16:49:45
sure, sounds fine to me -- i don't anticipate addi
| |
| 83 } | |
| 84 | |
| 85 DISALLOW_COPY_AND_ASSIGN(UserActivityForwarderTest); | |
| 86 }; | |
| 87 | |
| 88 TEST_F(UserActivityForwarderTest, ForwardActivityToDetector) { | |
| 89 base::TimeTicks now = base::TimeTicks::FromInternalValue(1000); | |
| 90 detector_->set_now_for_test(now); | |
| 91 monitor_->NotifyUserActivityObservers(); | |
| 92 RunUntilIdle(); | |
| 93 EXPECT_EQ(now, detector_->last_activity_time()); | |
| 94 | |
| 95 now += base::TimeDelta::FromSeconds(10); | |
| 96 detector_->set_now_for_test(now); | |
| 97 monitor_->NotifyUserActivityObservers(); | |
| 98 RunUntilIdle(); | |
| 99 EXPECT_EQ(now, detector_->last_activity_time()); | |
| 100 } | |
| 101 | |
| 102 } // namespace aura | |
|
James Cook
2017/01/20 03:38:49
Thanks for adding test at the mojo level!
Daniel Erat
2017/01/20 16:49:45
no problem!
| |
| OLD | NEW |