summaryrefslogtreecommitdiff
path: root/chromium/weblayer/browser/cookie_manager_browsertest.cc
blob: 0d98a159a747d04f39f5e3499cebfff2927e2ed7 (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
// Copyright 2020 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 "base/test/bind_test_util.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "weblayer/browser/profile_impl.h"
#include "weblayer/public/cookie_manager.h"
#include "weblayer/test/weblayer_browser_test.h"
#include "weblayer/test/weblayer_browser_test_utils.h"

namespace weblayer {

class CookieManagerBrowserTest : public WebLayerBrowserTest {
 public:
  const std::vector<net::CookieChangeInfo>& WaitForChanges(size_t num) {
    if (change_infos_.size() >= num)
      return change_infos_;

    num_to_wait_for_ = num;
    run_loop_ = std::make_unique<base::RunLoop>();
    run_loop_->Run();
    return change_infos_;
  }

  void OnCookieChanged(const net::CookieChangeInfo& info) {
    change_infos_.push_back(info);
    if (run_loop_ && num_to_wait_for_ == change_infos_.size())
      run_loop_->Quit();
  }

  void Reset() { change_infos_.clear(); }

  bool SetCookie(const std::string& cookie) {
    GURL base_url = embedded_test_server()->base_url();
    base::RunLoop run_loop;
    bool final_result = false;
    GetProfile()->GetCookieManager()->SetCookie(
        base_url, cookie,
        base::BindLambdaForTesting([&run_loop, &final_result](bool result) {
          final_result = result;
          run_loop.Quit();
        }));
    run_loop.Run();
    return final_result;
  }

 private:
  size_t num_to_wait_for_ = 0;
  std::vector<net::CookieChangeInfo> change_infos_;
  std::unique_ptr<base::RunLoop> run_loop_;
};

IN_PROC_BROWSER_TEST_F(CookieManagerBrowserTest, CookieChanged) {
  EXPECT_TRUE(embedded_test_server()->Start());
  NavigateAndWaitForCompletion(
      embedded_test_server()->GetURL("/simple_page.html"), shell());

  GURL base_url = embedded_test_server()->base_url();
  auto subscription =
      GetProfile()->GetCookieManager()->AddCookieChangedCallback(
          base_url, nullptr,
          base::BindRepeating(&CookieManagerBrowserTest::OnCookieChanged,
                              base::Unretained(this)));

  ASSERT_TRUE(SetCookie("foo=bar"));
  const auto& changes = WaitForChanges(1);
  ASSERT_EQ(changes.size(), 1u);
  const auto& cookie = changes[0].cookie;
  EXPECT_EQ(cookie.Name(), "foo");
  EXPECT_EQ(cookie.Value(), "bar");
}

IN_PROC_BROWSER_TEST_F(CookieManagerBrowserTest,
                       CookieChangedRemoveSubscription) {
  EXPECT_TRUE(embedded_test_server()->Start());
  NavigateAndWaitForCompletion(
      embedded_test_server()->GetURL("/simple_page.html"), shell());

  GURL base_url = embedded_test_server()->base_url();
  std::string cookie1 = "cookie1";
  auto subscription1 =
      GetProfile()->GetCookieManager()->AddCookieChangedCallback(
          base_url, &cookie1,
          base::BindRepeating(&CookieManagerBrowserTest::OnCookieChanged,
                              base::Unretained(this)));
  std::string cookie2 = "cookie2";
  auto subscription2 =
      GetProfile()->GetCookieManager()->AddCookieChangedCallback(
          base_url, &cookie2,
          base::BindRepeating(&CookieManagerBrowserTest::OnCookieChanged,
                              base::Unretained(this)));

  ASSERT_TRUE(SetCookie("cookie1=something"));
  {
    const auto& changes = WaitForChanges(1);
    ASSERT_EQ(changes.size(), 1u);
    EXPECT_EQ(changes[0].cookie.Name(), cookie1);
  }

  Reset();
  subscription1 = nullptr;

  // Set cookie1 first and then cookie2. We should only receive a cookie change
  // event for cookie2.
  ASSERT_TRUE(SetCookie("cookie1=other"));
  ASSERT_TRUE(SetCookie("cookie2=something"));
  {
    const auto& changes = WaitForChanges(1);
    ASSERT_EQ(changes.size(), 1u);
    EXPECT_EQ(changes[0].cookie.Name(), cookie2);
  }
}

}  // namespace weblayer