OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #import <EarlGrey/EarlGrey.h> |
| 6 #import <XCTest/XCTest.h> |
| 7 |
| 8 #include "base/strings/sys_string_conversions.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/infobars/core/confirm_infobar_delegate.h" |
| 11 #include "components/infobars/core/infobar.h" |
| 12 #include "components/infobars/core/infobar_manager.h" |
| 13 #import "ios/chrome/app/main_controller.h" |
| 14 #import "ios/chrome/browser/tabs/tab.h" |
| 15 #import "ios/chrome/browser/tabs/tab_model.h" |
| 16 #import "ios/chrome/test/app/chrome_test_util.h" |
| 17 #import "ios/chrome/test/app/tab_test_util.h" |
| 18 #import "ios/chrome/test/earl_grey/chrome_assertions.h" |
| 19 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 20 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 21 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 22 #import "ios/web/public/test/http_server.h" |
| 23 #include "ios/web/public/test/http_server_util.h" |
| 24 #include "url/gurl.h" |
| 25 #include "url/url_constants.h" |
| 26 |
| 27 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 28 #error "This file requires ARC support." |
| 29 #endif |
| 30 |
| 31 namespace { |
| 32 |
| 33 // The title of the test infobar. |
| 34 const char kTestInfoBarTitle[] = "TestInfoBar"; |
| 35 |
| 36 // Timeout for how long to wait for an infobar to appear or disapper. |
| 37 const CFTimeInterval kTimeout = 4.0; |
| 38 |
| 39 // An infobar that displays a single line of text and no buttons. |
| 40 class TestInfoBarDelegate : public ConfirmInfoBarDelegate { |
| 41 public: |
| 42 static bool Create(infobars::InfoBarManager* infobar_manager); |
| 43 |
| 44 // InfoBarDelegate implementation. |
| 45 InfoBarIdentifier GetIdentifier() const override { return TEST_INFOBAR; } |
| 46 |
| 47 // ConfirmInfoBarDelegate implementation. |
| 48 base::string16 GetMessageText() const override { |
| 49 return base::ASCIIToUTF16(kTestInfoBarTitle); |
| 50 } |
| 51 |
| 52 int GetButtons() const override { |
| 53 return ConfirmInfoBarDelegate::BUTTON_NONE; |
| 54 } |
| 55 }; |
| 56 |
| 57 bool TestInfoBarDelegate::Create(infobars::InfoBarManager* infobar_manager) { |
| 58 DCHECK(infobar_manager); |
| 59 return !!infobar_manager->AddInfoBar(infobar_manager->CreateConfirmInfoBar( |
| 60 std::unique_ptr<ConfirmInfoBarDelegate>(new TestInfoBarDelegate))); |
| 61 } |
| 62 |
| 63 // Returns the InfoBarManager for the current tab. Only works in normal |
| 64 // (non-incognito) mode. |
| 65 infobars::InfoBarManager* GetCurrentInfoBarManager() { |
| 66 MainController* main_controller = chrome_test_util::GetMainController(); |
| 67 return [[[[main_controller browserViewInformation] mainTabModel] currentTab] |
| 68 infoBarManager]; |
| 69 } |
| 70 |
| 71 // Adds a TestInfoBar to the current tab. |
| 72 bool AddTestInfoBarToCurrentTab() { |
| 73 infobars::InfoBarManager* manager = GetCurrentInfoBarManager(); |
| 74 return TestInfoBarDelegate::Create(manager); |
| 75 } |
| 76 |
| 77 // Verifies that a single TestInfoBar is either present or absent on the current |
| 78 // tab. |
| 79 void VerifyTestInfoBarVisibleForCurrentTab(bool visible) { |
| 80 // Expected values. |
| 81 bool expected_count = visible ? 1U : 0U; |
| 82 id<GREYMatcher> expected_visibility = |
| 83 visible ? grey_sufficientlyVisible() : grey_notVisible(); |
| 84 NSString* condition_name = |
| 85 visible ? @"Waiting for infobar to show" : @"Waiting for infobar to hide"; |
| 86 |
| 87 infobars::InfoBarManager* manager = GetCurrentInfoBarManager(); |
| 88 GREYAssertEqual(expected_count, manager->infobar_count(), |
| 89 @"Incorrect number of infobars."); |
| 90 [[GREYCondition |
| 91 conditionWithName:condition_name |
| 92 block:^BOOL { |
| 93 NSError* error = nil; |
| 94 [[EarlGrey |
| 95 selectElementWithMatcher: |
| 96 chrome_test_util::staticTextWithAccessibilityLabel( |
| 97 base::SysUTF8ToNSString(kTestInfoBarTitle))] |
| 98 assertWithMatcher:expected_visibility |
| 99 error:&error]; |
| 100 return error == nil; |
| 101 }] waitWithTimeout:kTimeout]; |
| 102 } |
| 103 |
| 104 } // namespace |
| 105 |
| 106 // Tests functionality related to infobars. |
| 107 @interface InfobarTestCase : ChromeTestCase |
| 108 @end |
| 109 |
| 110 @implementation InfobarTestCase |
| 111 |
| 112 // Tests that page infobars don't persist on navigation. |
| 113 - (void)testInfobarsDismissOnNavigate { |
| 114 web::test::SetUpFileBasedHttpServer(); |
| 115 |
| 116 // Open a new tab and navigate to the test page. |
| 117 const GURL testURL = web::test::HttpServer::MakeUrl( |
| 118 "http://ios/testing/data/http_server_files/pony.html"); |
| 119 [ChromeEarlGrey loadURL:testURL]; |
| 120 chrome_test_util::AssertMainTabCount(1U); |
| 121 |
| 122 // Add a test infobar to the current tab. Verify that the infobar is present |
| 123 // in the model and that the infobar view is visible on screen. |
| 124 GREYAssert(AddTestInfoBarToCurrentTab(), |
| 125 @"Failed to add infobar to test tab."); |
| 126 VerifyTestInfoBarVisibleForCurrentTab(true); |
| 127 |
| 128 // Navigate to a different page. Verify that the infobar is dismissed and no |
| 129 // longer visible on screen. |
| 130 [ChromeEarlGrey loadURL:GURL(url::kAboutBlankURL)]; |
| 131 VerifyTestInfoBarVisibleForCurrentTab(false); |
| 132 } |
| 133 |
| 134 // Tests that page infobars persist only on the tabs they are opened on, and |
| 135 // that navigation in other tabs doesn't affect them. |
| 136 - (void)testInfobarTabSwitch { |
| 137 const GURL destinationURL = web::test::HttpServer::MakeUrl( |
| 138 "http://ios/testing/data/http_server_files/destination.html"); |
| 139 const GURL ponyURL = web::test::HttpServer::MakeUrl( |
| 140 "http://ios/testing/data/http_server_files/pony.html"); |
| 141 web::test::SetUpFileBasedHttpServer(); |
| 142 |
| 143 // Create the first tab and navigate to the test page. |
| 144 [ChromeEarlGrey loadURL:destinationURL]; |
| 145 chrome_test_util::AssertMainTabCount(1U); |
| 146 |
| 147 // Create the second tab, navigate to the test page, and add the test infobar. |
| 148 chrome_test_util::OpenNewTab(); |
| 149 [ChromeEarlGrey loadURL:ponyURL]; |
| 150 chrome_test_util::AssertMainTabCount(2U); |
| 151 VerifyTestInfoBarVisibleForCurrentTab(false); |
| 152 GREYAssert(AddTestInfoBarToCurrentTab(), |
| 153 @"Failed to add infobar to second tab."); |
| 154 VerifyTestInfoBarVisibleForCurrentTab(true); |
| 155 |
| 156 // Switch back to the first tab and make sure no infobar is visible. |
| 157 chrome_test_util::SelectTabAtIndexInCurrentMode(0U); |
| 158 VerifyTestInfoBarVisibleForCurrentTab(false); |
| 159 |
| 160 // Navigate to a different URL in the first tab, to verify that this |
| 161 // navigation does not hide the infobar in the second tab. |
| 162 [ChromeEarlGrey loadURL:ponyURL]; |
| 163 |
| 164 // Close the first tab. Verify that there is only one tab remaining and its |
| 165 // infobar is visible. |
| 166 chrome_test_util::CloseCurrentTab(); |
| 167 chrome_test_util::AssertMainTabCount(1U); |
| 168 VerifyTestInfoBarVisibleForCurrentTab(true); |
| 169 } |
| 170 |
| 171 @end |
OLD | NEW |