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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/test/browser_test.h"
#include "extensions/browser/api/idle/idle_manager.h"
#include "extensions/browser/api/idle/idle_manager_factory.h"
#include "extensions/browser/background_script_executor.h"
#include "extensions/common/extension.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "ui/base/idle/scoped_set_idle_state.h"
namespace extensions {
namespace {
constexpr char kExtensionRelativePath[] = "power/report_activity";
constexpr char kExtensionId[] = "dibbenaepdnglcjpgjmnefmjccpinang";
} // namespace
using ContextType = extensions::browser_test_util::ContextType;
class PowerApiTest : public ExtensionApiTest,
public testing::WithParamInterface<ContextType> {
public:
PowerApiTest() : ExtensionApiTest(GetParam()) {}
};
INSTANTIATE_TEST_SUITE_P(PersistentBackground,
PowerApiTest,
::testing::Values(ContextType::kPersistentBackground));
INSTANTIATE_TEST_SUITE_P(ServiceWorker,
PowerApiTest,
::testing::Values(ContextType::kServiceWorker));
// Verifies that chrome.power.reportActivity() correctly reports a user activity
// by observing idle states.
IN_PROC_BROWSER_TEST_P(PowerApiTest,
ReportActivityChangesIdleStateFromIdleToActive) {
ResultCatcher catcher;
ExtensionTestMessageListener ready_listener("ready");
ready_listener.set_extension_id(kExtensionId);
LoadExtension(test_data_dir_.AppendASCII(kExtensionRelativePath));
// Wait for idle state listener for be set up.
ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
{
// Wait for the state to change to idle.
ExtensionTestMessageListener idle_listener("idle");
ui::ScopedSetIdleState idle(ui::IDLE_STATE_IDLE);
ASSERT_TRUE(idle_listener.WaitUntilSatisfied());
// Allow the idle state to go out of scope to reset it.
// Otherwise the QueryState() call is always going to return
// the test state, even though it actually changed.
}
auto* idle_manager = IdleManagerFactory::GetForBrowserContext(profile());
auto threshold = idle_manager->GetThresholdForTest(kExtensionId);
ASSERT_EQ(idle_manager->QueryState(threshold), ui::IDLE_STATE_IDLE);
// Report activity.
BackgroundScriptExecutor script_executor(profile());
constexpr char kReportActivityScript[] = R"(chrome.power.reportActivity();)";
script_executor.BackgroundScriptExecutor::ExecuteScriptAsync(
kExtensionId, kReportActivityScript,
BackgroundScriptExecutor::ResultCapture::kNone);
// The test succeeds if the state goes from idle to active.
ASSERT_TRUE(catcher.GetNextResult());
// Test that the actual state is active.
ASSERT_EQ(idle_manager->QueryState(threshold), ui::IDLE_STATE_ACTIVE);
}
} // namespace extensions
|