File: peripherals_app_delegate_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (86 lines) | stat: -rw-r--r-- 3,383 bytes parent folder | download | duplicates (6)
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
// Copyright 2024 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/test/test_future.h"
#include "chrome/browser/apps/almanac_api_client/almanac_app_icon_loader.h"
#include "chrome/browser/ash/input_device_settings/peripherals_app_delegate_impl.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/testing_profile.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/package_id.h"
#include "content/public/test/browser_test.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {
namespace {

const apps::PackageId kTestPackageId(apps::PackageType::kWeb,
                                     "test.package.name");
constexpr char kAppName[] = "app_name";
constexpr char kTestDeviceKey[] = "0000:0001";

}  // namespace

class PeripheralsAppDelegateImplTest : public InProcessBrowserTest {
 public:
  void SetUpOnMainThread() override {
    InProcessBrowserTest::SetUpOnMainThread();
    TestingProfile::Builder profile_builder;
    profile_builder.SetSharedURLLoaderFactory(
        url_loader_factory_.GetSafeWeakWrapper());
    profile_ = profile_builder.Build();
    delegate_ = std::make_unique<PeripheralsAppDelegateImpl>();
    delegate()->set_profile_for_testing(profile_.get());
    ASSERT_TRUE(embedded_test_server()->Start());
    loader_ = std::make_unique<apps::AlmanacAppIconLoader>(*profile());
  }

  void TearDownOnMainThread() override {
    InProcessBrowserTest::TearDownOnMainThread();
    loader_.reset();
    delegate_.reset();
    profile_.reset();
  }

  TestingProfile* profile() { return profile_.get(); }

  PeripheralsAppDelegateImpl* delegate() { return delegate_.get(); }

  network::TestURLLoaderFactory url_loader_factory_;

 private:
  std::unique_ptr<TestingProfile> profile_;
  std::unique_ptr<PeripheralsAppDelegateImpl> delegate_;
  std::unique_ptr<apps::AlmanacAppIconLoader> loader_;
};

IN_PROC_BROWSER_TEST_F(PeripheralsAppDelegateImplTest,
                       GetCompanionAppResponse_Valid) {
  apps::proto::PeripheralsGetResponse response;
  response.set_package_id(kTestPackageId.ToString());
  response.set_name(kAppName);
  url_loader_factory_.AddResponse(delegate()->GetServerUrl().spec(),
                                  response.SerializeAsString(), net::HTTP_OK);
  base::test::TestFuture<const std::optional<mojom::CompanionAppInfo>&>
      app_info;
  delegate()->GetCompanionAppInfo(kTestDeviceKey, app_info.GetCallback());
  const auto info = app_info.Get();
  EXPECT_EQ(kAppName, info->app_name);
  EXPECT_EQ(kTestPackageId.ToString(), info->package_id);
}

IN_PROC_BROWSER_TEST_F(PeripheralsAppDelegateImplTest,
                       GetCompanionAppResponse_Invalid) {
  apps::proto::PeripheralsGetResponse response;
  url_loader_factory_.AddResponse(delegate()->GetServerUrl().spec(),
                                  response.SerializeAsString());
  base::test::TestFuture<const std::optional<mojom::CompanionAppInfo>&>
      app_info;
  delegate()->GetCompanionAppInfo(kTestDeviceKey, app_info.GetCallback());
  EXPECT_FALSE(app_info.Take().has_value());
}

}  // namespace ash