File: kiosk_app_update_service_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (236 lines) | stat: -rw-r--r-- 8,933 bytes parent folder | download | duplicates (5)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright 2013 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/ash/app_mode/kiosk_app_update_service.h"

#include <memory>
#include <string>

#include "ash/constants/ash_paths.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_path_override.h"
#include "base/test/test_future.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
#include "chrome/browser/ash/system/automatic_reboot_manager.h"
#include "chrome/browser/ash/system/automatic_reboot_manager_observer.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chromeos/ash/components/dbus/update_engine/update_engine_client.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/browser_test.h"
#include "extensions/common/extension.h"
#include "extensions/test/extension_test_message_listener.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::test::TestFuture;

namespace ash {

namespace {

// Maximum time for AutomaticRebootManager initialization to complete.
constexpr base::TimeDelta kAutomaticRebootManagerInitTimeout =
    base::Seconds(60);

// Blocks until `manager` is initialized and then sets `success_out` to true and
// runs `quit_closure`. If initialization does not occur within `timeout`, sets
// `success_out` to false and runs `quit_closure`.
void WaitForAutomaticRebootManagerInit(system::AutomaticRebootManager* manager,
                                       const base::TimeDelta& timeout,
                                       base::OnceClosure quit_closure,
                                       bool* success_out) {
  base::ScopedAllowBaseSyncPrimitivesForTesting allow_base_sync_primitives;
  *success_out = manager->WaitForInitForTesting(timeout);
  std::move(quit_closure).Run();
}

}  // namespace

class KioskAppUpdateServiceTest
    : public extensions::PlatformAppBrowserTest,
      public system::AutomaticRebootManagerObserver {
 public:
  KioskAppUpdateServiceTest() = default;
  KioskAppUpdateServiceTest(const KioskAppUpdateServiceTest&) = delete;
  KioskAppUpdateServiceTest& operator=(const KioskAppUpdateServiceTest&) =
      delete;
  ~KioskAppUpdateServiceTest() override = default;

  // extensions::PlatformAppBrowserTest overrides:
  void SetUpInProcessBrowserTestFixture() override {
    extensions::PlatformAppBrowserTest::SetUpInProcessBrowserTestFixture();

    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    const base::FilePath& temp_dir = temp_dir_.GetPath();

    const base::TimeDelta uptime = base::Hours(3);
    const std::string uptime_seconds =
        base::NumberToString(uptime.InSecondsF());
    const base::FilePath uptime_file = temp_dir.Append("uptime");
    ASSERT_TRUE(base::WriteFile(uptime_file, uptime_seconds));
    uptime_file_override_ = std::make_unique<base::ScopedPathOverride>(
        FILE_UPTIME, uptime_file, /*is_absolute=*/false, /*create=*/false);
  }

  void SetUpOnMainThread() override {
    extensions::PlatformAppBrowserTest::SetUpOnMainThread();

    app_ = LoadExtension(
        test_data_dir_.AppendASCII("api_test/runtime/on_restart_required"));

    // Fake app mode command line.
    base::CommandLine* command = base::CommandLine::ForCurrentProcess();
    command->AppendSwitch(switches::kForceAppMode);
    command->AppendSwitchASCII(switches::kAppId, app_->id());

    automatic_reboot_manager_ =
        g_browser_process->platform_part()->automatic_reboot_manager();

    // Wait for `automatic_reboot_manager_` to finish initializing.
    bool initialized = false;
    base::RunLoop run_loop;
    base::ThreadPool::PostTask(
        FROM_HERE,
        base::BindOnce(&WaitForAutomaticRebootManagerInit,
                       base::Unretained(automatic_reboot_manager_),
                       kAutomaticRebootManagerInitTimeout,
                       run_loop.QuitClosure(), base::Unretained(&initialized)));
    run_loop.Run();
    ASSERT_TRUE(initialized)
        << "AutomaticRebootManager not initialized in "
        << kAutomaticRebootManagerInitTimeout.InSeconds() << " seconds";

    automatic_reboot_manager_->AddObserver(this);
  }

  // system::AutomaticRebootManagerObserver:
  void OnRebootRequested(
      system::AutomaticRebootManagerObserver::Reason) override {
    if (test_waiter_) {
      test_waiter_->SetValue();
    }
  }

  void WillDestroyAutomaticRebootManager() override {
    automatic_reboot_manager_->RemoveObserver(this);
  }

  void CreateKioskAppUpdateService() {
    EXPECT_FALSE(update_service_);
    update_service_ = KioskAppUpdateServiceFactory::GetForProfile(profile());
    update_service_->Init(app_->id());
  }

  void FireAppUpdateAvailable() {
    update_service_->OnAppUpdateAvailable(*app_);
  }

  void FireUpdatedNeedReboot() {
    update_engine::StatusResult status;
    status.set_current_operation(update_engine::Operation::UPDATED_NEED_REBOOT);
    test_waiter_ = std::make_unique<TestFuture<void>>();
    automatic_reboot_manager_->UpdateStatusChanged(status);
    EXPECT_TRUE(test_waiter_->Wait());
  }

  void RequestPeriodicReboot() {
    test_waiter_ = std::make_unique<TestFuture<void>>();
    g_browser_process->local_state()->SetInteger(prefs::kUptimeLimit,
                                                 base::Hours(2).InSeconds());
    EXPECT_TRUE(test_waiter_->Wait());
  }

 private:
  base::ScopedTempDir temp_dir_;
  std::unique_ptr<base::ScopedPathOverride> uptime_file_override_;
  raw_ptr<const extensions::Extension> app_ = nullptr;  // Not owned.
  raw_ptr<KioskAppUpdateService, DanglingUntriaged> update_service_ =
      nullptr;  // Not owned.
  raw_ptr<system::AutomaticRebootManager, DanglingUntriaged>
      automatic_reboot_manager_ = nullptr;  // Not owned.
  std::unique_ptr<TestFuture<void>> test_waiter_;
};

// Verifies that the app is notified a reboot is required when an app update
// becomes available.
IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, AppUpdate) {
  base::HistogramTester histogram;

  CreateKioskAppUpdateService();

  ExtensionTestMessageListener listener("app_update");
  FireAppUpdateAvailable();
  EXPECT_TRUE(listener.WaitUntilSatisfied());

  histogram.ExpectUniqueSample(kKioskPrimaryAppInSessionUpdateHistogram,
                               /*sample=*/1,
                               /*expected_bucket_count=*/1);
}

// Verifies that the app is notified a reboot is required when an OS update is
// applied while Chrome is running and the policy to reboot after update is
// enabled.
IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, OsUpdate) {
  CreateKioskAppUpdateService();

  g_browser_process->local_state()->SetBoolean(prefs::kRebootAfterUpdate, true);
  ExtensionTestMessageListener listener("os_update");
  FireUpdatedNeedReboot();
  EXPECT_TRUE(listener.WaitUntilSatisfied());
}

// Verifies that the app is notified a reboot is required when a periodic reboot
// is requested while Chrome is running.
IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, Periodic) {
  CreateKioskAppUpdateService();

  ExtensionTestMessageListener listener("periodic");
  RequestPeriodicReboot();
  EXPECT_TRUE(listener.WaitUntilSatisfied());
}

// Verifies that the app is notified a reboot is required when an OS update was
// applied before Chrome was started and the policy to reboot after update is
// enabled.
IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, StartAfterOsUpdate) {
  g_browser_process->local_state()->SetBoolean(prefs::kRebootAfterUpdate, true);
  FireUpdatedNeedReboot();

  ExtensionTestMessageListener listener("os_update");
  CreateKioskAppUpdateService();
  EXPECT_TRUE(listener.WaitUntilSatisfied());
}

// Verifies that the app is notified a reboot is required when a periodic reboot
// was requested before Chrome was started.
IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, StartAfterPeriodic) {
  RequestPeriodicReboot();

  ExtensionTestMessageListener listener("periodic");
  CreateKioskAppUpdateService();
  EXPECT_TRUE(listener.WaitUntilSatisfied());
}

}  // namespace ash