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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
// Copyright 2015 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 <string>
#include "base/command_line.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_utils.h"
#include "components/bookmarks/test/bookmark_test_helpers.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
class DurableStorageBrowserTest : public InProcessBrowserTest {
public:
DurableStorageBrowserTest() = default;
~DurableStorageBrowserTest() override = default;
void SetUpCommandLine(base::CommandLine*) override;
void SetUpOnMainThread() override;
protected:
content::RenderFrameHost* GetRenderFrameHost(Browser* browser) {
return browser->tab_strip_model()->GetActiveWebContents()->GetMainFrame();
}
content::RenderFrameHost* GetRenderFrameHost() {
return GetRenderFrameHost(browser());
}
void Bookmark(Browser* browser) {
bookmarks::BookmarkModel* bookmark_model =
BookmarkModelFactory::GetForProfile(browser->profile());
bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model);
bookmarks::AddIfNotBookmarked(bookmark_model, url_, base::ASCIIToUTF16(""));
}
void Bookmark() {
Bookmark(browser());
}
GURL url_;
private:
DISALLOW_COPY_AND_ASSIGN(DurableStorageBrowserTest);
};
void DurableStorageBrowserTest::SetUpCommandLine(
base::CommandLine* command_line) {
command_line->AppendSwitch(
switches::kEnableExperimentalWebPlatformFeatures);
}
void DurableStorageBrowserTest::SetUpOnMainThread() {
if (embedded_test_server()->Started())
return;
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
url_ = embedded_test_server()->GetURL("/durable/durability-permissions.html");
}
IN_PROC_BROWSER_TEST_F(DurableStorageBrowserTest, QueryNonBookmarkedPage) {
ui_test_utils::NavigateToURL(browser(), url_);
std::string permission_string;
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
GetRenderFrameHost(), "checkPermission()", &permission_string));
EXPECT_EQ("default", permission_string);
}
IN_PROC_BROWSER_TEST_F(DurableStorageBrowserTest, RequestNonBookmarkedPage) {
ui_test_utils::NavigateToURL(browser(), url_);
bool default_box_is_persistent = false;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
GetRenderFrameHost(), "requestPermission()", &default_box_is_persistent));
EXPECT_FALSE(default_box_is_persistent);
}
IN_PROC_BROWSER_TEST_F(DurableStorageBrowserTest, QueryBookmarkedPage) {
// Documents that the current behavior is to return "default" if script
// hasn't requested the durable permission, even if it would be autogranted.
Bookmark();
ui_test_utils::NavigateToURL(browser(), url_);
std::string permission_string;
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
GetRenderFrameHost(), "checkPermission()", &permission_string));
EXPECT_EQ("default", permission_string);
}
IN_PROC_BROWSER_TEST_F(DurableStorageBrowserTest, RequestBookmarkedPage) {
Bookmark();
ui_test_utils::NavigateToURL(browser(), url_);
bool default_box_is_persistent = false;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
GetRenderFrameHost(), "requestPermission()", &default_box_is_persistent));
EXPECT_TRUE(default_box_is_persistent);
}
IN_PROC_BROWSER_TEST_F(DurableStorageBrowserTest, BookmarkThenUnbookmark) {
Bookmark();
ui_test_utils::NavigateToURL(browser(), url_);
bool default_box_is_persistent = false;
std::string permission_string;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
GetRenderFrameHost(), "requestPermission()", &default_box_is_persistent));
EXPECT_TRUE(default_box_is_persistent);
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
GetRenderFrameHost(), "checkPermission()", &permission_string));
EXPECT_EQ("granted", permission_string);
bookmarks::BookmarkModel* bookmark_model =
BookmarkModelFactory::GetForProfile(browser()->profile());
bookmarks::RemoveAllBookmarks(bookmark_model, url_);
// Unbookmarking doesn't change the permission.
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
GetRenderFrameHost(), "checkPermission()", &permission_string));
EXPECT_EQ("granted", permission_string);
// Requesting after unbookmarking doesn't change the default box.
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
GetRenderFrameHost(), "requestPermission()", &default_box_is_persistent));
EXPECT_TRUE(default_box_is_persistent);
// Querying after requesting after unbookmarking still reports "granted".
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
GetRenderFrameHost(), "checkPermission()", &permission_string));
EXPECT_EQ("granted", permission_string);
}
IN_PROC_BROWSER_TEST_F(DurableStorageBrowserTest, FirstTabSeesResult) {
ui_test_utils::NavigateToURL(browser(), url_);
std::string permission_string;
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
GetRenderFrameHost(), "checkPermission()", &permission_string));
EXPECT_EQ("default", permission_string);
chrome::NewTab(browser());
ui_test_utils::NavigateToURL(browser(), url_);
Bookmark();
bool default_box_is_persistent = false;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
GetRenderFrameHost(), "requestPermission()", &default_box_is_persistent));
EXPECT_TRUE(default_box_is_persistent);
browser()->tab_strip_model()->ActivateTabAt(0, false);
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
GetRenderFrameHost(), "checkPermission()", &permission_string));
EXPECT_EQ("granted", permission_string);
}
IN_PROC_BROWSER_TEST_F(DurableStorageBrowserTest, Incognito) {
Browser* browser = CreateIncognitoBrowser();
ui_test_utils::NavigateToURL(browser, url_);
Bookmark(browser);
bool default_box_is_persistent = false;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(GetRenderFrameHost(browser),
"requestPermission()",
&default_box_is_persistent));
EXPECT_TRUE(default_box_is_persistent);
std::string permission_string;
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
GetRenderFrameHost(browser), "checkPermission()", &permission_string));
EXPECT_EQ("granted", permission_string);
}
|