[go: nahoru, domu]

Convert ScopedBlockPopupsPref to be compatible with EG2.

App calls made by this test class are moved into ChromeEarlGrey and made
to compile with EG2, so that ScopedBlockPopupsPref can continue to work
when moved into test-side code.

ChromeBrowserState was removed from ScopedBlockPopupsPref as I was not
sure if it would be passable over EDO safely, and all callsites of this
class use GetOriginalBrowserState(). Therefore, I moved getting the
ChromeBrowserState using GetOriginalBrowserState() to app-side in
CEGAppInterface.

Bug: 922813
Change-Id: I954d28309360b8b805a9943c7fde77df69f7df95
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1668512
Commit-Queue: ericale <ericale@chromium.org>
Reviewed-by: Eugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#672710}
diff --git a/ios/chrome/browser/ui/settings/BUILD.gn b/ios/chrome/browser/ui/settings/BUILD.gn
index 341e7f0aa8..4f2a6c4 100644
--- a/ios/chrome/browser/ui/settings/BUILD.gn
+++ b/ios/chrome/browser/ui/settings/BUILD.gn
@@ -373,6 +373,7 @@
     "//ios/chrome/browser/ui/table_view/cells",
     "//ios/chrome/browser/ui/util",
     "//ios/chrome/browser/ui/util:util",
+    "//ios/chrome/test:eg_test_support",
     "//ios/chrome/test:test_support",
     "//ios/chrome/test/app:test_support",
     "//ios/chrome/test/earl_grey:test_support",
diff --git a/ios/chrome/browser/ui/settings/block_popups_egtest.mm b/ios/chrome/browser/ui/settings/block_popups_egtest.mm
index ed29e5d..26e9aaf 100644
--- a/ios/chrome/browser/ui/settings/block_popups_egtest.mm
+++ b/ios/chrome/browser/ui/settings/block_popups_egtest.mm
@@ -17,7 +17,7 @@
 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h"
 #import "ios/chrome/test/earl_grey/chrome_matchers.h"
 #import "ios/chrome/test/earl_grey/chrome_test_case.h"
-#include "ios/chrome/test/scoped_block_popups_pref.h"
+#include "ios/chrome/test/earl_grey/scoped_block_popups_pref.h"
 #import "ios/web/public/test/http_server/http_server.h"
 #include "ios/web/public/test/http_server/http_server_util.h"
 #include "ui/base/l10n/l10n_util_mac.h"
@@ -126,8 +126,7 @@
   responses[openedWindowURL] = kOpenedWindowResponse;
   web::test::SetUpSimpleHttpServer(responses);
 
-  ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW,
-                                   GetOriginalBrowserState());
+  ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW);
   [ChromeEarlGrey loadURL:blockPopupsURL];
   [ChromeEarlGrey waitForMainTabCount:1];
 
@@ -156,8 +155,7 @@
   responses[openedWindowURL] = kOpenedWindowResponse;
   web::test::SetUpSimpleHttpServer(responses);
 
-  ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_BLOCK,
-                                   GetOriginalBrowserState());
+  ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_BLOCK);
   [ChromeEarlGrey loadURL:blockPopupsURL];
   [ChromeEarlGrey waitForMainTabCount:1];
 
@@ -185,8 +183,7 @@
 // revealed properly when the preference switch is toggled.
 - (void)testSettingsPageWithExceptions {
   std::string allowedPattern = "[*.]example.com";
-  ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_BLOCK,
-                                   GetOriginalBrowserState());
+  ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_BLOCK);
   ScopedBlockPopupsException exceptionSetter(allowedPattern);
 
   [ChromeEarlGreyUI openSettingsMenu];
diff --git a/ios/chrome/browser/web/browsing_egtest.mm b/ios/chrome/browser/web/browsing_egtest.mm
index 2ee385e..6acace4d 100644
--- a/ios/chrome/browser/web/browsing_egtest.mm
+++ b/ios/chrome/browser/web/browsing_egtest.mm
@@ -18,7 +18,7 @@
 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h"
 #import "ios/chrome/test/earl_grey/chrome_matchers.h"
 #import "ios/chrome/test/earl_grey/chrome_test_case.h"
-#include "ios/chrome/test/scoped_block_popups_pref.h"
+#include "ios/chrome/test/earl_grey/scoped_block_popups_pref.h"
 #include "ios/net/url_test_util.h"
 #import "ios/web/public/test/earl_grey/web_view_actions.h"
 #import "ios/web/public/test/earl_grey/web_view_matchers.h"
@@ -184,8 +184,7 @@
   responses[destinationURL] = "You've arrived!";
   web::test::SetUpSimpleHttpServer(responses);
 
-  ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW,
-                                   GetOriginalBrowserState());
+  ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW);
 
   [ChromeEarlGrey loadURL:URL];
   [ChromeEarlGrey waitForMainTabCount:1];
@@ -214,8 +213,7 @@
   responses[destinationURL] = "You've arrived!";
   web::test::SetUpSimpleHttpServer(responses);
 
-  ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW,
-                                   GetOriginalBrowserState());
+  ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW);
 
   [ChromeEarlGrey loadURL:URL];
   [ChromeEarlGrey waitForMainTabCount:1];
@@ -257,8 +255,7 @@
 
   web::test::SetUpSimpleHttpServer(responses);
 
-  ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW,
-                                   GetOriginalBrowserState());
+  ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW);
 
   [ChromeEarlGrey loadURL:URL];
   [ChromeEarlGrey waitForMainTabCount:1];
@@ -296,8 +293,7 @@
 
   web::test::SetUpSimpleHttpServer(responses);
 
-  ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW,
-                                   GetOriginalBrowserState());
+  ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW);
 
   [ChromeEarlGrey loadURL:URL];
   [ChromeEarlGrey waitForMainTabCount:1];
diff --git a/ios/chrome/browser/web/browsing_prevent_default_egtest.mm b/ios/chrome/browser/web/browsing_prevent_default_egtest.mm
index f13262b6..bd7d4b76 100644
--- a/ios/chrome/browser/web/browsing_prevent_default_egtest.mm
+++ b/ios/chrome/browser/web/browsing_prevent_default_egtest.mm
@@ -12,7 +12,7 @@
 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h"
 #import "ios/chrome/test/earl_grey/chrome_matchers.h"
 #import "ios/chrome/test/earl_grey/chrome_test_case.h"
-#include "ios/chrome/test/scoped_block_popups_pref.h"
+#include "ios/chrome/test/earl_grey/scoped_block_popups_pref.h"
 #import "ios/web/public/test/http_server/http_server.h"
 #include "ios/web/public/test/http_server/http_server_util.h"
 #include "url/url_constants.h"
@@ -48,8 +48,7 @@
 - (void)runTestAndVerifyNoNavigationForLinkID:(const std::string&)linkID {
   // Disable popup blocking, because that will mask failures that try to open
   // new tabs.
-  ScopedBlockPopupsPref scoper(CONTENT_SETTING_ALLOW,
-                               GetOriginalBrowserState());
+  ScopedBlockPopupsPref scoper(CONTENT_SETTING_ALLOW);
   web::test::SetUpFileBasedHttpServer();
 
   const GURL testURL = GetTestUrl();
@@ -89,8 +88,7 @@
 - (void)testPreventDefaultOverridesWindowOpen {
   // Disable popup blocking, because that will mask failures that try to open
   // new tabs.
-  ScopedBlockPopupsPref scoper(CONTENT_SETTING_ALLOW,
-                               GetOriginalBrowserState());
+  ScopedBlockPopupsPref scoper(CONTENT_SETTING_ALLOW);
   web::test::SetUpFileBasedHttpServer();
 
   const GURL testURL = GetTestUrl();
diff --git a/ios/chrome/browser/web/cache_egtest.mm b/ios/chrome/browser/web/cache_egtest.mm
index c9075a7c3..b592ffe 100644
--- a/ios/chrome/browser/web/cache_egtest.mm
+++ b/ios/chrome/browser/web/cache_egtest.mm
@@ -14,7 +14,7 @@
 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h"
 #import "ios/chrome/test/earl_grey/chrome_matchers.h"
 #import "ios/chrome/test/earl_grey/chrome_test_case.h"
-#include "ios/chrome/test/scoped_block_popups_pref.h"
+#include "ios/chrome/test/earl_grey/scoped_block_popups_pref.h"
 #include "ios/web/public/test/http_server/html_response_provider.h"
 #import "ios/web/public/test/http_server/http_server.h"
 #include "ios/web/public/test/http_server/http_server_util.h"
@@ -174,8 +174,7 @@
 
   // Open the first page in a new tab. Verify that cache was not used. Must
   // first allow popups.
-  ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW,
-                                   GetOriginalBrowserState());
+  ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW);
   [ChromeEarlGrey
       tapWebStateElementWithID:[NSString
                                    stringWithUTF8String:kCacheTestLinkID]];
diff --git a/ios/chrome/test/BUILD.gn b/ios/chrome/test/BUILD.gn
index 6aee749d..fd3c012 100644
--- a/ios/chrome/test/BUILD.gn
+++ b/ios/chrome/test/BUILD.gn
@@ -31,8 +31,6 @@
     "ios_chrome_unit_test_suite.mm",
     "root_view_controller_test.h",
     "root_view_controller_test.mm",
-    "scoped_block_popups_pref.h",
-    "scoped_block_popups_pref.mm",
     "scoped_key_window.h",
     "testing_application_context.h",
     "testing_application_context.mm",
@@ -77,7 +75,7 @@
   ]
   deps = [
     ":test_support",
-    "//base",
+    "//base:base",
     "//ios/third_party/earl_grey:earl_grey+link",
   ]
 }
diff --git a/ios/chrome/test/earl_grey/BUILD.gn b/ios/chrome/test/earl_grey/BUILD.gn
index 53868f6f6..cf617cc 100644
--- a/ios/chrome/test/earl_grey/BUILD.gn
+++ b/ios/chrome/test/earl_grey/BUILD.gn
@@ -219,6 +219,8 @@
     "chrome_test_case.mm",
     "chrome_test_case_app_interface.h",
     "chrome_test_case_app_interface.mm",
+    "scoped_block_popups_pref.h",
+    "scoped_block_popups_pref.mm",
   ]
 
   deps = [
@@ -232,6 +234,7 @@
     "//components/ukm/ios:features",
     "//components/unified_consent",
     "//ios/chrome/app/strings",
+    "//ios/chrome/browser/content_settings:content_settings",
     "//ios/chrome/browser/ntp:features",
     "//ios/chrome/browser/ui:feature_flags",
     "//ios/chrome/browser/ui/authentication:authentication",
@@ -331,6 +334,7 @@
     "//components/ukm/ios:features",
     "//components/unified_consent",
     "//ios/chrome/app/strings",
+    "//ios/chrome/browser/content_settings:content_settings",
     "//ios/chrome/browser/ntp:features",
     "//ios/chrome/browser/ui:feature_flags",
     "//ios/chrome/browser/ui/authentication/cells",
@@ -394,6 +398,8 @@
     "chrome_test_case.h",
     "chrome_test_case.mm",
     "chrome_test_case_app_interface.h",
+    "scoped_block_popups_pref.h",
+    "scoped_block_popups_pref.mm",
   ]
 
   deps = [
diff --git a/ios/chrome/test/earl_grey/chrome_earl_grey.h b/ios/chrome/test/earl_grey/chrome_earl_grey.h
index 85a49e5..7cf6f02 100644
--- a/ios/chrome/test/earl_grey/chrome_earl_grey.h
+++ b/ios/chrome/test/earl_grey/chrome_earl_grey.h
@@ -345,6 +345,16 @@
 // Returns YES if WebPaymentsModifiers feature is enabled.
 - (BOOL)isWebPaymentsModifiersEnabled WARN_UNUSED_RESULT;
 
+#pragma mark - Popup Blocking
+
+// Gets the current value of the popup content setting preference for the
+// original browser state.
+- (ContentSetting)popupPrefValue;
+
+// Sets the popup content setting preference to the given value for the original
+// browser state.
+- (void)setPopupPrefValue:(ContentSetting)value;
+
 @end
 
 // Helpers that only compile under EarlGrey 1 are included in this "EG1"
diff --git a/ios/chrome/test/earl_grey/chrome_earl_grey.mm b/ios/chrome/test/earl_grey/chrome_earl_grey.mm
index 4c446155..7ccbf94 100644
--- a/ios/chrome/test/earl_grey/chrome_earl_grey.mm
+++ b/ios/chrome/test/earl_grey/chrome_earl_grey.mm
@@ -558,6 +558,16 @@
   return [ChromeEarlGreyAppInterface isWebPaymentsModifiersEnabled];
 }
 
+#pragma mark - ScopedBlockPopupsPref
+
+- (ContentSetting)popupPrefValue {
+  return [ChromeEarlGreyAppInterface popupPrefValue];
+}
+
+- (void)setPopupPrefValue:(ContentSetting)value {
+  return [ChromeEarlGreyAppInterface setPopupPrefValue:value];
+}
+
 @end
 
 // The helpers below only compile under EarlGrey1.
diff --git a/ios/chrome/test/earl_grey/chrome_earl_grey_app_interface.h b/ios/chrome/test/earl_grey/chrome_earl_grey_app_interface.h
index 0e23703..e699009 100644
--- a/ios/chrome/test/earl_grey/chrome_earl_grey_app_interface.h
+++ b/ios/chrome/test/earl_grey/chrome_earl_grey_app_interface.h
@@ -283,6 +283,16 @@
 // Returns YES if WebPaymentsModifiers feature is enabled.
 + (BOOL)isWebPaymentsModifiersEnabled WARN_UNUSED_RESULT;
 
+#pragma mark - Popup Blocking
+
+// Gets the current value of the popup content setting preference for the
+// original browser state.
++ (ContentSetting)popupPrefValue;
+
+// Sets the popup content setting preference to the given value for the original
+// browser state.
++ (void)setPopupPrefValue:(ContentSetting)value;
+
 @end
 
 #endif  // IOS_CHROME_TEST_EARL_GREY_CHROME_EARL_GREY_APP_INTERFACE_H_
diff --git a/ios/chrome/test/earl_grey/chrome_earl_grey_app_interface.mm b/ios/chrome/test/earl_grey/chrome_earl_grey_app_interface.mm
index 1d4228b0..579e1ad 100644
--- a/ios/chrome/test/earl_grey/chrome_earl_grey_app_interface.mm
+++ b/ios/chrome/test/earl_grey/chrome_earl_grey_app_interface.mm
@@ -6,8 +6,10 @@
 #import "base/test/ios/wait_util.h"
 
 #include "base/strings/sys_string_conversions.h"
+#include "components/content_settings/core/browser/host_content_settings_map.h"
 #import "components/payments/core/features.h"
 #import "components/ukm/ios/features.h"
+#include "ios/chrome/browser/content_settings/host_content_settings_map_factory.h"
 #import "ios/chrome/browser/ntp/features.h"
 #import "ios/chrome/browser/ui/ui_feature_flags.h"
 #import "ios/chrome/test/app/bookmarks_test_util.h"
@@ -486,4 +488,19 @@
       payments::features::kWebPaymentsModifiers);
 }
 
+#pragma mark - ScopedBlockPopupsPref
+
++ (ContentSetting)popupPrefValue {
+  return ios::HostContentSettingsMapFactory::GetForBrowserState(
+             chrome_test_util::GetOriginalBrowserState())
+      ->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, NULL);
+}
+
++ (void)setPopupPrefValue:(ContentSetting)value {
+  DCHECK(value == CONTENT_SETTING_BLOCK || value == CONTENT_SETTING_ALLOW);
+  ios::HostContentSettingsMapFactory::GetForBrowserState(
+      chrome_test_util::GetOriginalBrowserState())
+      ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, value);
+}
+
 @end
diff --git a/ios/chrome/test/earl_grey/scoped_block_popups_pref.h b/ios/chrome/test/earl_grey/scoped_block_popups_pref.h
new file mode 100644
index 0000000..20d41bf
--- /dev/null
+++ b/ios/chrome/test/earl_grey/scoped_block_popups_pref.h
@@ -0,0 +1,27 @@
+// Copyright 2017 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.
+
+#ifndef IOS_CHROME_TEST_EARL_GREY_SCOPED_BLOCK_POPUPS_PREF_H_
+#define IOS_CHROME_TEST_EARL_GREY_SCOPED_BLOCK_POPUPS_PREF_H_
+
+#include "base/macros.h"
+#include "components/content_settings/core/common/content_settings.h"
+
+// ScopedBlockPopupsPref modifies the block popups preference for the original
+// browser state and resets the preference to its original value when this
+// object goes out of scope.
+class ScopedBlockPopupsPref {
+ public:
+  explicit ScopedBlockPopupsPref(ContentSetting setting);
+  ~ScopedBlockPopupsPref();
+
+ private:
+  // Saves the original pref setting so that it can be restored when the scoper
+  // is destroyed.
+  ContentSetting original_setting_;
+
+  DISALLOW_COPY_AND_ASSIGN(ScopedBlockPopupsPref);
+};
+
+#endif  // IOS_CHROME_TEST_EARL_GREY_SCOPED_BLOCK_POPUPS_PREF_H_
diff --git a/ios/chrome/test/earl_grey/scoped_block_popups_pref.mm b/ios/chrome/test/earl_grey/scoped_block_popups_pref.mm
new file mode 100644
index 0000000..7786c0d
--- /dev/null
+++ b/ios/chrome/test/earl_grey/scoped_block_popups_pref.mm
@@ -0,0 +1,20 @@
+// Copyright 2017 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 "ios/chrome/test/earl_grey/scoped_block_popups_pref.h"
+
+#import "ios/chrome/test/earl_grey/chrome_earl_grey.h"
+
+#if !defined(__has_feature) || !__has_feature(objc_arc)
+#error "This file requires ARC support."
+#endif
+
+ScopedBlockPopupsPref::ScopedBlockPopupsPref(ContentSetting setting)
+    : original_setting_([ChromeEarlGrey popupPrefValue]) {
+  [ChromeEarlGrey setPopupPrefValue:setting];
+}
+
+ScopedBlockPopupsPref::~ScopedBlockPopupsPref() {
+  [ChromeEarlGrey setPopupPrefValue:original_setting_];
+}
diff --git a/ios/chrome/test/scoped_block_popups_pref.h b/ios/chrome/test/scoped_block_popups_pref.h
deleted file mode 100644
index fd307a9..0000000
--- a/ios/chrome/test/scoped_block_popups_pref.h
+++ /dev/null
@@ -1,41 +0,0 @@
-// Copyright 2017 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.
-
-#ifndef IOS_CHROME_TEST_SCOPED_BLOCK_POPUPS_PREF_H_
-#define IOS_CHROME_TEST_SCOPED_BLOCK_POPUPS_PREF_H_
-
-#include "base/macros.h"
-#include "components/content_settings/core/common/content_settings.h"
-
-namespace ios {
-class ChromeBrowserState;
-}
-
-// ScopedBlockPopupsPref modifies the block popups preference
-// and resets the preference to its original value when this object goes out of
-// scope.
-class ScopedBlockPopupsPref {
- public:
-  explicit ScopedBlockPopupsPref(ContentSetting setting,
-                                 ios::ChromeBrowserState* browser_state);
-  ~ScopedBlockPopupsPref();
-
- private:
-  // Gets the current value of the preference.
-  ContentSetting GetPrefValue();
-
-  // Sets the preference to the given value.
-  void SetPrefValue(ContentSetting setting);
-
-  // Saves the browser state, which is required when getting and setting prefs.
-  ios::ChromeBrowserState* browser_state_;
-
-  // Saves the original pref setting so that it can be restored when the scoper
-  // is destroyed.
-  ContentSetting original_setting_;
-
-  DISALLOW_COPY_AND_ASSIGN(ScopedBlockPopupsPref);
-};
-
-#endif  // IOS_CHROME_TEST_SCOPED_BLOCK_POPUPS_PREF_H_
diff --git a/ios/chrome/test/scoped_block_popups_pref.mm b/ios/chrome/test/scoped_block_popups_pref.mm
deleted file mode 100644
index 96ca9e4..0000000
--- a/ios/chrome/test/scoped_block_popups_pref.mm
+++ /dev/null
@@ -1,34 +0,0 @@
-// Copyright 2017 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 "ios/chrome/test/scoped_block_popups_pref.h"
-
-#include "components/content_settings/core/browser/host_content_settings_map.h"
-#include "ios/chrome/browser/content_settings/host_content_settings_map_factory.h"
-
-#if !defined(__has_feature) || !__has_feature(objc_arc)
-#error "This file requires ARC support."
-#endif
-
-ScopedBlockPopupsPref::ScopedBlockPopupsPref(
-    ContentSetting setting,
-    ios::ChromeBrowserState* browser_state)
-    : browser_state_(browser_state), original_setting_(GetPrefValue()) {
-  SetPrefValue(setting);
-}
-
-ScopedBlockPopupsPref::~ScopedBlockPopupsPref() {
-  SetPrefValue(original_setting_);
-}
-
-ContentSetting ScopedBlockPopupsPref::GetPrefValue() {
-  return ios::HostContentSettingsMapFactory::GetForBrowserState(browser_state_)
-      ->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, NULL);
-}
-
-void ScopedBlockPopupsPref::SetPrefValue(ContentSetting setting) {
-  DCHECK(setting == CONTENT_SETTING_BLOCK || setting == CONTENT_SETTING_ALLOW);
-  ios::HostContentSettingsMapFactory::GetForBrowserState(browser_state_)
-      ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, setting);
-}