summaryrefslogtreecommitdiff
path: root/chromium/content/browser/idle/idle_browsertest.cc
blob: 06ae09ff4dc780401005e8c300e1b4bf65ef7a17 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2019 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 "content/browser/idle/idle_manager.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "testing/gmock/include/gmock/gmock.h"

using ::testing::NiceMock;

namespace content {

namespace {

class MockIdleTimeProvider : public IdleManager::IdleTimeProvider {
 public:
  MockIdleTimeProvider() = default;
  ~MockIdleTimeProvider() override = default;

  MOCK_METHOD1(CalculateIdleState, ui::IdleState(base::TimeDelta));
  MOCK_METHOD0(CalculateIdleTime, base::TimeDelta());
  MOCK_METHOD0(CheckIdleStateIsLocked, bool());

 private:
  DISALLOW_COPY_AND_ASSIGN(MockIdleTimeProvider);
};

class IdleTest : public ContentBrowserTest {
 public:
  IdleTest() = default;
  ~IdleTest() override = default;

  void SetUpCommandLine(base::CommandLine* command_line) override {
    ContentBrowserTest::SetUpCommandLine(command_line);
    command_line->AppendSwitchASCII(switches::kEnableBlinkFeatures,
                                    "IdleDetection");
  }
};

}  // namespace

IN_PROC_BROWSER_TEST_F(IdleTest, Start) {
  EXPECT_TRUE(NavigateToURL(shell(), GetTestUrl(nullptr, "simple_page.html")));

  auto mock_time_provider = std::make_unique<NiceMock<MockIdleTimeProvider>>();
  auto* rph = static_cast<RenderProcessHostImpl*>(
      shell()->web_contents()->GetMainFrame()->GetProcess());
  IdleManager* idle_mgr =
      static_cast<StoragePartitionImpl*>(rph->GetStoragePartition())
          ->GetIdleManager();

  // Test that statuses are updated after idleDetector.start().
  std::string script = R"(
    (async () => {
        let idleDetector = new IdleDetector({threshold: 60});
        await idleDetector.start();
        return new Promise(function(resolve) {
          let states = [];
          idleDetector.addEventListener('change', e => {
            let {user, screen} = idleDetector.state;
            states.push(`${user}-${screen}`)
            if (states.length >= 3) {
              let states_str = states.join(',');
              resolve(states_str);
            }
          });
        });
    }) ();
  )";

  EXPECT_CALL(*mock_time_provider, CalculateIdleTime())
      // Initial state of the system.
      .WillOnce(testing::Return(base::TimeDelta::FromSeconds(0)))
      // Simulates a user going idle.
      .WillOnce(testing::Return(base::TimeDelta::FromSeconds(60)))
      // Simulates a screen getting locked after the user goes idle.
      .WillOnce(testing::Return(base::TimeDelta::FromSeconds(60)))
      // Simulates a user going back to active.
      .WillRepeatedly(testing::Return(base::TimeDelta::FromSeconds(0)));

  EXPECT_CALL(*mock_time_provider, CheckIdleStateIsLocked())
      // Initial state of the system.
      .WillOnce(testing::Return(false))
      // Simulates unlocked screen while user goes idle.
      .WillOnce(testing::Return(false))
      // Simulates a screen getting locked after the user goes idle.
      .WillOnce(testing::Return(true))
      // Simulates an unlocked screen as user goes back to active.
      .WillRepeatedly(testing::Return(false));

  idle_mgr->SetIdleTimeProviderForTest(std::move(mock_time_provider));

  std::string result = EvalJs(shell(), script).ExtractString();
  std::vector<std::string> states = base::SplitString(
      result, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);

  EXPECT_EQ("idle-unlocked", states.at(0));
  EXPECT_EQ("idle-locked", states.at(1));
  EXPECT_EQ("active-unlocked", states.at(2));
}

}  // namespace content