1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/base/scoped_visibility_tracker.h"
#include <utility>
#include "base/test/simple_test_tick_clock.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ui {
class ScopedVisibilityTrackerTest : public testing::Test {};
TEST_F(ScopedVisibilityTrackerTest, NeverVisible) {
auto tick_clock = std::make_unique<base::SimpleTestTickClock>();
ScopedVisibilityTracker tracker(tick_clock.get(), false /* is_shown */);
tick_clock->Advance(base::Minutes(10));
EXPECT_EQ(base::Minutes(0), tracker.GetForegroundDuration());
}
TEST_F(ScopedVisibilityTrackerTest, SimpleVisibility) {
auto tick_clock = std::make_unique<base::SimpleTestTickClock>();
ScopedVisibilityTracker tracker(tick_clock.get(), true /* is_shown */);
tick_clock->Advance(base::Minutes(10));
EXPECT_EQ(base::Minutes(10), tracker.GetForegroundDuration());
}
TEST_F(ScopedVisibilityTrackerTest, HiddenThenShown) {
auto tick_clock = std::make_unique<base::SimpleTestTickClock>();
ScopedVisibilityTracker tracker(tick_clock.get(), true /* is_shown */);
tick_clock->Advance(base::Minutes(1));
EXPECT_EQ(base::Minutes(1), tracker.GetForegroundDuration());
tracker.OnHidden();
tick_clock->Advance(base::Minutes(2));
EXPECT_EQ(base::Minutes(1), tracker.GetForegroundDuration());
tracker.OnShown();
tick_clock->Advance(base::Minutes(3));
EXPECT_EQ(base::Minutes(4), tracker.GetForegroundDuration());
}
TEST_F(ScopedVisibilityTrackerTest, InitiallyHidden) {
auto tick_clock = std::make_unique<base::SimpleTestTickClock>();
ScopedVisibilityTracker tracker(tick_clock.get(), false /* is_shown */);
tick_clock->Advance(base::Minutes(1));
EXPECT_EQ(base::Minutes(0), tracker.GetForegroundDuration());
tracker.OnShown();
tick_clock->Advance(base::Minutes(2));
EXPECT_EQ(base::Minutes(2), tracker.GetForegroundDuration());
}
// The object should be robust to double hidden and shown notification
TEST_F(ScopedVisibilityTrackerTest, DoubleNotifications) {
auto tick_clock = std::make_unique<base::SimpleTestTickClock>();
ScopedVisibilityTracker tracker(tick_clock.get(), false /* is_shown */);
tick_clock->Advance(base::Minutes(1));
EXPECT_EQ(base::Minutes(0), tracker.GetForegroundDuration());
tracker.OnHidden();
tick_clock->Advance(base::Minutes(1));
EXPECT_EQ(base::Minutes(0), tracker.GetForegroundDuration());
tracker.OnShown();
tick_clock->Advance(base::Minutes(2));
EXPECT_EQ(base::Minutes(2), tracker.GetForegroundDuration());
tracker.OnShown();
tick_clock->Advance(base::Minutes(2));
EXPECT_EQ(base::Minutes(4), tracker.GetForegroundDuration());
}
} // namespace ui
|