summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/signin/remove_local_account_browsertest.cc
blob: 259f3d2857ee24700dc98d1c158badd00f363fdd (plain)
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
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "base/run_loop.h"
#include "base/test/bind.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "components/signin/public/identity_manager/accounts_in_cookie_jar_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_test_utils.h"
#include "components/signin/public/identity_manager/test_identity_manager_observer.h"
#include "content/public/test/browser_test.h"
#include "google_apis/gaia/fake_gaia.h"
#include "google_apis/gaia/gaia_switches.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_response.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/ash/login/test/network_portal_detector_mixin.h"
#endif

namespace {

using testing::Contains;
using testing::Not;

MATCHER_P(ListedAccountMatchesGaiaId, gaia_id, "") {
  return arg.gaia_id == std::string(gaia_id);
}

const char kTestGaiaId[] = "123";

class RemoveLocalAccountTest : public MixinBasedInProcessBrowserTest {
 protected:
  RemoveLocalAccountTest()
      : embedded_test_server_(net::EmbeddedTestServer::TYPE_HTTPS) {
    embedded_test_server_.RegisterRequestHandler(base::BindRepeating(
        &FakeGaia::HandleRequest, base::Unretained(&fake_gaia_)));
  }

  ~RemoveLocalAccountTest() override = default;

  signin::IdentityManager* identity_manager() {
    return IdentityManagerFactory::GetForProfile(browser()->profile());
  }

  signin::AccountsInCookieJarInfo WaitUntilAccountsInCookieUpdated() {
    signin::TestIdentityManagerObserver observer(identity_manager());
    base::RunLoop run_loop;
    observer.SetOnAccountsInCookieUpdatedCallback(run_loop.QuitClosure());
    run_loop.Run();
    return observer.AccountsInfoFromAccountsInCookieUpdatedCallback();
  }

  // MixinBasedInProcessBrowserTest:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    MixinBasedInProcessBrowserTest::SetUpCommandLine(command_line);
    ASSERT_TRUE(embedded_test_server_.InitializeAndListen());
    const GURL base_url = embedded_test_server_.base_url();
    command_line->AppendSwitchASCII(switches::kGaiaUrl, base_url.spec());
  }

  void SetUpOnMainThread() override {
    MixinBasedInProcessBrowserTest::SetUpOnMainThread();
    fake_gaia_.Initialize();

    FakeGaia::MergeSessionParams params;
    params.signed_out_gaia_ids.push_back(kTestGaiaId);
    fake_gaia_.UpdateMergeSessionParams(params);

    embedded_test_server_.StartAcceptingConnections();

#if BUILDFLAG(IS_CHROMEOS_ASH)
    // ChromeSigninClient uses chromeos::DelayNetworkCall() which requires
    // simulating being online.
    network_portal_detector_.SimulateDefaultNetworkState(
        ash::NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE);
#endif
  }

  FakeGaia fake_gaia_;
  net::EmbeddedTestServer embedded_test_server_;

#if BUILDFLAG(IS_CHROMEOS_ASH)
  ash::NetworkPortalDetectorMixin network_portal_detector_{&mixin_host_};
#endif
};

IN_PROC_BROWSER_TEST_F(RemoveLocalAccountTest, ShouldNotifyObservers) {
  // To enforce an initial ListAccounts fetch and the corresponding notification
  // to observers, make the current list as stale. This is done for the purpose
  // of documenting assertions on the AccountsInCookieJarInfo passed to
  // observers during notification.
  signin::SetFreshnessOfAccountsInGaiaCookie(identity_manager(),
                                             /*accounts_are_fresh=*/false);

  ASSERT_FALSE(identity_manager()->GetAccountsInCookieJar().accounts_are_fresh);
  const signin::AccountsInCookieJarInfo
      cookie_jar_info_in_initial_notification =
          WaitUntilAccountsInCookieUpdated();
  ASSERT_TRUE(cookie_jar_info_in_initial_notification.accounts_are_fresh);
  ASSERT_THAT(cookie_jar_info_in_initial_notification.signed_out_accounts,
              Contains(ListedAccountMatchesGaiaId(kTestGaiaId)));

  const signin::AccountsInCookieJarInfo initial_cookie_jar_info =
      identity_manager()->GetAccountsInCookieJar();
  ASSERT_TRUE(initial_cookie_jar_info.accounts_are_fresh);
  ASSERT_THAT(initial_cookie_jar_info.signed_out_accounts,
              Contains(ListedAccountMatchesGaiaId(kTestGaiaId)));

  // Open a FakeGaia page that issues the desired HTTP response header with
  // Google-Accounts-RemoveLocalAccount.
  chrome::AddTabAt(browser(),
                   fake_gaia_.GetDummyRemoveLocalAccountURL(kTestGaiaId),
                   /*index=*/0,
                   /*foreground=*/true);

  // Wait until observers are notified with OnAccountsInCookieUpdated().
  const signin::AccountsInCookieJarInfo
      cookie_jar_info_in_updated_notification =
          WaitUntilAccountsInCookieUpdated();

  EXPECT_TRUE(cookie_jar_info_in_updated_notification.accounts_are_fresh);
  EXPECT_THAT(cookie_jar_info_in_updated_notification.signed_out_accounts,
              Not(Contains(ListedAccountMatchesGaiaId(kTestGaiaId))));

  const signin::AccountsInCookieJarInfo updated_cookie_jar_info =
      identity_manager()->GetAccountsInCookieJar();
  EXPECT_TRUE(updated_cookie_jar_info.accounts_are_fresh);
  EXPECT_THAT(updated_cookie_jar_info.signed_out_accounts,
              Not(Contains(ListedAccountMatchesGaiaId(kTestGaiaId))));
}

}  // namespace