File: update_file_handler_command_unittest.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 (136 lines) | stat: -rw-r--r-- 5,139 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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/files/file_util.h"
#include "chrome/browser/web_applications/os_integration/os_integration_manager.h"
#include "chrome/browser/web_applications/os_integration/web_app_file_handler_manager.h"
#include "chrome/browser/web_applications/os_integration/web_app_protocol_handler_manager.h"
#include "chrome/browser/web_applications/test/fake_web_app_provider.h"
#include "chrome/browser/web_applications/test/os_integration_test_override_impl.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/test/web_app_test.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace web_app {
namespace {

class UpdateFileHandlerCommandTest : public WebAppTest {
 public:
  const char* kTestAppName = "test app";
  const GURL kTestAppUrl = GURL("https://example.com");
  const std::string kTestAppPolicyId = "https://example.com/";

  UpdateFileHandlerCommandTest() = default;
  ~UpdateFileHandlerCommandTest() override = default;

  void SetUp() override {
    WebAppTest::SetUp();
    {
      base::ScopedAllowBlockingForTesting allow_blocking;
      test_override_ = OsIntegrationTestOverrideImpl::OverrideForTesting();
    }
    provider_ = FakeWebAppProvider::Get(profile());

    auto file_handler_manager =
        std::make_unique<WebAppFileHandlerManager>(profile());
    auto protocol_handler_manager =
        std::make_unique<WebAppProtocolHandlerManager>(profile());
    auto os_integration_manager = std::make_unique<OsIntegrationManager>(
        profile(), std::move(file_handler_manager),
        std::move(protocol_handler_manager));

    provider_->SetOsIntegrationManager(std::move(os_integration_manager));
    test::AwaitStartWebAppProviderAndSubsystems(profile());
  }

  void TearDown() override {
    // Blocking required due to file operations in the shortcut override
    // destructor.
    test::UninstallAllWebApps(profile());
    {
      base::ScopedAllowBlockingForTesting allow_blocking;
      test_override_.reset();
    }
    WebAppTest::TearDown();
  }

  WebAppProvider* provider() { return provider_; }

 private:
  raw_ptr<FakeWebAppProvider, DanglingUntriaged> provider_ = nullptr;
  std::unique_ptr<OsIntegrationTestOverrideImpl::BlockingRegistration>
      test_override_;
};

TEST_F(UpdateFileHandlerCommandTest, UserChoiceAllowPersisted) {
  const webapps::AppId app_id =
      test::InstallDummyWebApp(profile(), kTestAppName, kTestAppUrl);
  EXPECT_EQ(
      provider()->registrar_unsafe().GetAppFileHandlerUserApprovalState(app_id),
      ApiApprovalState::kRequiresPrompt);

  base::RunLoop run_loop;
  provider()->scheduler().PersistFileHandlersUserChoice(
      app_id, /*allowed=*/true, run_loop.QuitClosure());
  run_loop.Run();

  EXPECT_EQ(
      provider()->registrar_unsafe().GetAppFileHandlerUserApprovalState(app_id),
      ApiApprovalState::kAllowed);
  EXPECT_TRUE(
      provider()->registrar_unsafe().ExpectThatFileHandlersAreRegisteredWithOs(
          app_id));
}

TEST_F(UpdateFileHandlerCommandTest, UserChoiceDisallowPersisted) {
  const webapps::AppId app_id =
      test::InstallDummyWebApp(profile(), kTestAppName, kTestAppUrl);
  EXPECT_EQ(
      provider()->registrar_unsafe().GetAppFileHandlerUserApprovalState(app_id),
      ApiApprovalState::kRequiresPrompt);

  base::RunLoop run_loop;
  provider()->scheduler().PersistFileHandlersUserChoice(
      app_id, /*allowed=*/false, run_loop.QuitClosure());
  run_loop.Run();

  EXPECT_EQ(
      provider()->registrar_unsafe().GetAppFileHandlerUserApprovalState(app_id),
      ApiApprovalState::kDisallowed);
  EXPECT_FALSE(
      provider()->registrar_unsafe().ExpectThatFileHandlersAreRegisteredWithOs(
          app_id));
}

#if BUILDFLAG(IS_CHROMEOS)
TEST_F(UpdateFileHandlerCommandTest, ApprovalStateOverridenByPolicy) {
  const webapps::AppId app_id =
      test::InstallDummyWebApp(profile(), kTestAppName, kTestAppUrl,
                               webapps::WebappInstallSource::EXTERNAL_POLICY);
  EXPECT_EQ(
      provider()->registrar_unsafe().GetAppFileHandlerUserApprovalState(app_id),
      ApiApprovalState::kRequiresPrompt);

  profile()->GetTestingPrefService()->SetDict(
      prefs::kDefaultHandlersForFileExtensions,
      base::Value::Dict().Set("pdf", kTestAppPolicyId));

  EXPECT_EQ(provider()->registrar_unsafe().GetAppFileHandlerApprovalState(
                app_id, "pdf"),
            ApiApprovalState::kAllowed);
  EXPECT_TRUE(
      provider()->registrar_unsafe().ExpectThatFileHandlersAreRegisteredWithOs(
          app_id));
}
#endif  // BUILDFLAG(IS_CHROMEOS)

}  // namespace
}  // namespace web_app