1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.chrome.browser.page_info;
6 
7 import static androidx.test.espresso.Espresso.onView;
8 import static androidx.test.espresso.action.ViewActions.click;
9 import static androidx.test.espresso.assertion.ViewAssertions.matches;
10 import static androidx.test.espresso.matcher.ViewMatchers.isChecked;
11 import static androidx.test.espresso.matcher.ViewMatchers.isNotChecked;
12 import static androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;
13 import static androidx.test.espresso.matcher.ViewMatchers.withId;
14 
15 import static org.chromium.components.content_settings.PrefNames.COOKIE_CONTROLS_MODE;
16 
17 import android.support.test.InstrumentationRegistry;
18 
19 import androidx.test.espresso.matcher.ViewMatchers;
20 import androidx.test.filters.MediumTest;
21 
22 import org.junit.After;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.ClassRule;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 import org.chromium.base.metrics.RecordHistogram;
31 import org.chromium.base.test.util.CommandLineFlags;
32 import org.chromium.base.test.util.FlakyTest;
33 import org.chromium.chrome.browser.flags.ChromeSwitches;
34 import org.chromium.chrome.browser.profiles.Profile;
35 import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
36 import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
37 import org.chromium.components.content_settings.CookieControlsMode;
38 import org.chromium.components.page_info.PageInfoAction;
39 import org.chromium.components.page_info.R;
40 import org.chromium.components.user_prefs.UserPrefs;
41 import org.chromium.content_public.browser.test.util.TestThreadUtils;
42 import org.chromium.content_public.common.ContentSwitches;
43 import org.chromium.net.test.EmbeddedTestServer;
44 import org.chromium.ui.test.util.DisableAnimationsTestRule;
45 
46 /**
47  * Tests for CookieControlsView.
48  */
49 @RunWith(ChromeJUnit4ClassRunner.class)
50 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE,
51         ContentSwitches.HOST_RESOLVER_RULES + "=MAP * 127.0.0.1"})
52 public class CookieControlsViewTest {
53     @Rule
54     public ChromeTabbedActivityTestRule mActivityTestRule = new ChromeTabbedActivityTestRule();
55 
56     @ClassRule
57     public static DisableAnimationsTestRule disableAnimationsRule = new DisableAnimationsTestRule();
58 
59     private final String mPath = "/chrome/test/data/android/simple.html";
60     private EmbeddedTestServer mTestServer;
61 
loadUrlAndOpenPageInfo(String url)62     private void loadUrlAndOpenPageInfo(String url) {
63         mActivityTestRule.loadUrlInNewTab(url);
64         onView(withId(org.chromium.chrome.R.id.location_bar_status_icon)).perform(click());
65     }
66 
setThirdPartyCookieBlocking(@ookieControlsMode int value)67     private void setThirdPartyCookieBlocking(@CookieControlsMode int value) {
68         TestThreadUtils.runOnUiThreadBlocking(() -> {
69             UserPrefs.get(Profile.getLastUsedRegularProfile())
70                     .setInteger(COOKIE_CONTROLS_MODE, value);
71         });
72     }
73 
getTotalPageActionHistogramCount()74     private int getTotalPageActionHistogramCount() {
75         return RecordHistogram.getHistogramTotalCountForTesting("WebsiteSettings.Action");
76     }
77 
getPageActionHistogramCount(@ageInfoAction int action)78     private int getPageActionHistogramCount(@PageInfoAction int action) {
79         return RecordHistogram.getHistogramValueCountForTesting("WebsiteSettings.Action", action);
80     }
81 
82     @Before
setUp()83     public void setUp() throws InterruptedException {
84         mTestServer = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
85         mActivityTestRule.startMainActivityOnBlankPage();
86     }
87 
88     @After
tearDown()89     public void tearDown() {
90         mTestServer.destroy();
91     }
92 
93     /**
94      * Tests that CookieControlsView is hidden on a blank page.
95      */
96     @Test
97     @MediumTest
98     @FlakyTest(message = "https://crbug.com/1062645")
testHiddenOnBlankPage()99     public void testHiddenOnBlankPage() {
100         setThirdPartyCookieBlocking(CookieControlsMode.BLOCK_THIRD_PARTY);
101         onView(withId(org.chromium.chrome.R.id.location_bar_status_icon)).perform(click());
102         onView(withId(R.id.page_info_cookie_controls_view))
103                 .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE)));
104     }
105 
106     /**
107      * Tests that CookieControlsView is hidden if third-party cookie blocking is off.
108      */
109     @Test
110     @MediumTest
111     @FlakyTest(message = "https://crbug.com/1062645")
testHiddenWhenDisabled()112     public void testHiddenWhenDisabled() {
113         setThirdPartyCookieBlocking(CookieControlsMode.OFF);
114         loadUrlAndOpenPageInfo(mTestServer.getURLWithHostName("foo.com", mPath));
115         onView(withId(R.id.page_info_cookie_controls_view))
116                 .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE)));
117     }
118 
119     /**
120      * Tests that CookieControlsView can be instantiated and shown.
121      */
122     @Test
123     @MediumTest
124     @FlakyTest(message = "https://crbug.com/1062645")
testShow()125     public void testShow() {
126         setThirdPartyCookieBlocking(CookieControlsMode.BLOCK_THIRD_PARTY);
127         loadUrlAndOpenPageInfo(mTestServer.getURLWithHostName("foo.com", mPath));
128         onView(withId(R.id.page_info_cookie_controls_view))
129                 .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
130     }
131 
132     /**
133      * Tests that CookieControlsView updates on navigations.
134      */
135     @Test
136     @MediumTest
137     @FlakyTest(message = "https://crbug.com/1062645")
testUpdate()138     public void testUpdate() {
139         Assert.assertEquals(0, getTotalPageActionHistogramCount());
140 
141         setThirdPartyCookieBlocking(CookieControlsMode.BLOCK_THIRD_PARTY);
142         loadUrlAndOpenPageInfo(mTestServer.getURLWithHostName("foo.com", mPath));
143         Assert.assertEquals(1, getTotalPageActionHistogramCount());
144         Assert.assertEquals(1, getPageActionHistogramCount(PageInfoAction.PAGE_INFO_OPENED));
145         int switch_id = R.id.cookie_controls_block_cookies_switch;
146         onView(withId(switch_id)).check(matches(isChecked()));
147         onView(withId(switch_id)).perform(click());
148         onView(withId(switch_id)).check(matches(isNotChecked()));
149         Assert.assertEquals(2, getTotalPageActionHistogramCount());
150         Assert.assertEquals(
151                 1, getPageActionHistogramCount(PageInfoAction.PAGE_INFO_COOKIES_ALLOWED_FOR_SITE));
152 
153         // Load a different page.
154         loadUrlAndOpenPageInfo(mTestServer.getURLWithHostName("bar.com", mPath));
155         onView(withId(switch_id)).check(matches(isChecked()));
156 
157         // Go back to foo.com.
158         loadUrlAndOpenPageInfo(mTestServer.getURLWithHostName("foo.com", mPath));
159         onView(withId(switch_id)).check(matches(isNotChecked()));
160         onView(withId(switch_id)).perform(click());
161         onView(withId(switch_id)).check(matches(isChecked()));
162         Assert.assertEquals(5, getTotalPageActionHistogramCount());
163 
164         Assert.assertEquals(
165                 1, getPageActionHistogramCount(PageInfoAction.PAGE_INFO_COOKIES_BLOCKED_FOR_SITE));
166         Assert.assertEquals(3, getPageActionHistogramCount(PageInfoAction.PAGE_INFO_OPENED));
167     }
168 }
169