File: ambient_client_impl_unittest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (149 lines) | stat: -rw-r--r-- 5,853 bytes parent folder | download
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
// Copyright 2020 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/ui/ash/ambient/ambient_client_impl.h"

#include <memory>

#include "ash/constants/ash_features.h"
#include "ash/public/cpp/test/test_image_downloader.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

constexpr char kTestProfileName[] = "user@gmail.com";
constexpr char16_t kTestProfileName16[] = u"user@gmail.com";
constexpr char kTestGaiaId[] = "1234567890";

class AmbientClientImplTest : public testing::Test {
 public:
  AmbientClientImplTest() = default;
  ~AmbientClientImplTest() override = default;

  void SetUp() override {
    ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
    profile_manager_ = std::make_unique<TestingProfileManager>(
        TestingBrowserProcess::GetGlobal());
    ASSERT_TRUE(profile_manager_->SetUp());

    profile_ = profile_manager_->CreateTestingProfile(
        kTestProfileName, /*prefs=*/{}, kTestProfileName16,
        /*avatar_id=*/0,
        IdentityTestEnvironmentProfileAdaptor::
            GetIdentityTestEnvironmentFactories());
    identity_test_env_adaptor_ =
        std::make_unique<IdentityTestEnvironmentProfileAdaptor>(profile_);
    user_manager_enabler_ = std::make_unique<user_manager::ScopedUserManager>(
        std::make_unique<ash::FakeChromeUserManager>());
    image_downloader_ = std::make_unique<ash::TestImageDownloader>();

    ambient_client_ = std::make_unique<AmbientClientImpl>();
  }

  void TearDown() override {
    ambient_client_.reset();
    user_manager_enabler_.reset();
    identity_test_env_adaptor_.reset();
    profile_ = nullptr;
    profile_manager_->DeleteTestingProfile(kTestProfileName);
    profile_manager_.reset();
  }

 protected:
  AmbientClientImpl& ambient_client() { return *ambient_client_; }
  TestingProfile* profile() { return profile_; }

  ash::FakeChromeUserManager* GetFakeUserManager() const {
    return static_cast<ash::FakeChromeUserManager*>(
        user_manager::UserManager::Get());
  }

  void AddAndLoginUser(const AccountId& account_id) {
    GetFakeUserManager()->AddUser(account_id);
    GetFakeUserManager()->LoginUser(account_id);
    GetFakeUserManager()->SwitchActiveUser(account_id);
    MaybeMakeAccountAsPrimaryAccount(account_id);
  }

  ash::TestImageDownloader& image_downloader() { return *image_downloader_; }
  signin::IdentityTestEnvironment* identity_test_env() {
    return identity_test_env_adaptor_->identity_test_env();
  }

 private:
  void MaybeMakeAccountAsPrimaryAccount(const AccountId& account_id) {
    if (!identity_test_env()->identity_manager()->HasPrimaryAccount(
            signin::ConsentLevel::kSignin)) {
      identity_test_env()->MakePrimaryAccountAvailable(
          account_id.GetUserEmail(), signin::ConsentLevel::kSignin);
    }
  }

  content::BrowserTaskEnvironment task_environment_;
  base::ScopedTempDir data_dir_;
  std::unique_ptr<TestingProfileManager> profile_manager_;
  // Owned by |profile_manager_|
  raw_ptr<TestingProfile, ExperimentalAsh> profile_ = nullptr;
  std::unique_ptr<IdentityTestEnvironmentProfileAdaptor>
      identity_test_env_adaptor_;
  std::unique_ptr<user_manager::ScopedUserManager> user_manager_enabler_;
  std::unique_ptr<ash::TestImageDownloader> image_downloader_;
  std::unique_ptr<AmbientClientImpl> ambient_client_;
};

TEST_F(AmbientClientImplTest, AllowedByPrimaryUser) {
  AddAndLoginUser(AccountId::FromUserEmailGaiaId(
      profile()->GetProfileUserName(), kTestGaiaId));
  EXPECT_TRUE(ash::AmbientClient::Get()->IsAmbientModeAllowed());
}

TEST_F(AmbientClientImplTest, DisallowedByNonPrimaryUser) {
  AddAndLoginUser(
      AccountId::FromUserEmailGaiaId("user2@gmail.com", kTestGaiaId));
  AddAndLoginUser(AccountId::FromUserEmailGaiaId(
      profile()->GetProfileUserName(), kTestGaiaId));
  EXPECT_FALSE(ash::AmbientClient::Get()->IsAmbientModeAllowed());
}

TEST_F(AmbientClientImplTest, DisallowedByEmailDomain) {
  AddAndLoginUser(
      AccountId::FromUserEmailGaiaId("user@gmailtest.com", kTestGaiaId));
  EXPECT_FALSE(ash::AmbientClient::Get()->IsAmbientModeAllowed());
}

TEST_F(AmbientClientImplTest, DownloadImage) {
  identity_test_env()->SetAutomaticIssueOfAccessTokens(true);
  AddAndLoginUser(AccountId::FromUserEmailGaiaId(
      profile()->GetProfileUserName(), kTestGaiaId));
  ambient_client().DownloadImage("test_url", base::DoNothing());
  base::RunLoop().RunUntilIdle();

  EXPECT_FALSE(image_downloader().last_request_headers().IsEmpty());
  std::string out;
  image_downloader().last_request_headers().GetHeader("Authorization", &out);
  EXPECT_EQ("Bearer access_token", out);
}

TEST_F(AmbientClientImplTest, DownloadImageMultipleTimes) {
  identity_test_env()->SetAutomaticIssueOfAccessTokens(true);
  AddAndLoginUser(AccountId::FromUserEmailGaiaId(
      profile()->GetProfileUserName(), kTestGaiaId));
  // make sure multiple images can download at the same time.
  ambient_client().DownloadImage("test_url_1", base::DoNothing());
  ambient_client().DownloadImage("test_url_2", base::DoNothing());
  ambient_client().DownloadImage("test_url_3", base::DoNothing());

  EXPECT_EQ(3u, ambient_client().token_fetchers_for_testing().size());

  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(0u, ambient_client().token_fetchers_for_testing().size());
}