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 using UserActivityForwarderTest = ui::TaskRunnerTestBase; |
| 63 |
| 64 TEST_F(UserActivityForwarderTest, ForwardActivityToDetector) { |
| 65 FakeUserActivityMonitor monitor; |
| 66 ui::UserActivityDetector detector; |
| 67 UserActivityForwarder forwarder(monitor.GetPtr(), &detector); |
| 68 |
| 69 // Run pending tasks so |monitor| receives |forwarder|'s registration. |
| 70 RunUntilIdle(); |
| 71 |
| 72 base::TimeTicks now = base::TimeTicks::FromInternalValue(1000); |
| 73 detector.set_now_for_test(now); |
| 74 monitor.NotifyUserActivityObservers(); |
| 75 RunUntilIdle(); |
| 76 EXPECT_EQ(now, detector.last_activity_time()); |
| 77 |
| 78 now += base::TimeDelta::FromSeconds(10); |
| 79 detector.set_now_for_test(now); |
| 80 monitor.NotifyUserActivityObservers(); |
| 81 RunUntilIdle(); |
| 82 EXPECT_EQ(now, detector.last_activity_time()); |
| 83 } |
| 84 |
| 85 } // namespace aura |
OLD | NEW |