-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix math expressions on address bar (#3262)
Task/Issue URL: https://app.asana.com/0/1204006570077678/1208002970105121/f BSK PR: duckduckgo/BrowserServicesKit#952 **Description**: This pull request introduces a change on how we check for valid hostnames. The current regurlar expression had an issue where some mathematical expressions where valid hostnames. Also, following the philosophy about leaving the code a bit better when you touched it. I’ve added tests for the query submitted flow on the `OmniBar`. This PR fixes **Steps to test this PR**: 1. Open the app 2. Put `16385-12228.75` in the address bar 3. This should do a search on our SERP instead of trying to open a webpage 4. Test other usual web sites URLs and check those are working. **Definition of Done (Internal Only)**: * [x] Does this PR satisfy our [Definition of Done](https://app.asana.com/0/1202500774821704/1207634633537039/f)?
- Loading branch information
1 parent
c24b07e
commit 2d0fa40
Showing
3 changed files
with
154 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// | ||
// QuerySubmittedTests.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import XCTest | ||
import Suggestions | ||
|
||
@testable import DuckDuckGo | ||
|
||
class QuerySubmittedTests: XCTestCase { | ||
let mock = MockOmniBarDelegate() | ||
let sut = OmniBar.loadFromXib() | ||
|
||
override func setUp() { | ||
super.setUp() | ||
sut.omniDelegate = mock | ||
} | ||
|
||
override func tearDown() { | ||
mock.clear() | ||
super.tearDown() | ||
} | ||
|
||
func testValidAddressSubmissions() { | ||
let validQueries = [ | ||
("www.test.com", "http://www.test.com"), | ||
("http://example.com/path?query=123", "http://example.com/path?query=123"), | ||
(" www.test.com ", "http://www.test.com") | ||
] | ||
|
||
for (query, expected) in validQueries { | ||
assertQuerySubmission(query: query, expected: expected) | ||
} | ||
} | ||
|
||
func testInvalidAddressSubmissions() { | ||
let invalidQueries = [ | ||
"16385-12228.75", | ||
"invalid-url", | ||
"http://[::1]:80", | ||
"12345" | ||
] | ||
|
||
for query in invalidQueries { | ||
assertQuerySubmission(query: query, expected: query) | ||
} | ||
} | ||
|
||
func testSuggestionSelectionCallsDelegate() { | ||
mock.suggestion = .website(url: URL(string: "www.testing.com")!) | ||
|
||
sut.onQuerySubmitted() | ||
|
||
XCTAssertTrue(mock.wasOnOmniSuggestionSelectedCalled) | ||
XCTAssertFalse(mock.wasOnOmniQuerySubmittedCalled) | ||
} | ||
|
||
func testEmptyQueryDoesNotCallDelegate() { | ||
sut.textField.text = "" | ||
sut.onQuerySubmitted() | ||
|
||
XCTAssertFalse(mock.wasOnOmniQuerySubmittedCalled) | ||
XCTAssertFalse(mock.wasOnOmniSuggestionSelectedCalled) | ||
} | ||
|
||
func testBlankQueryDoesNotCallDelegate() { | ||
sut.textField.text = " " | ||
sut.onQuerySubmitted() | ||
|
||
XCTAssertFalse(mock.wasOnOmniQuerySubmittedCalled) | ||
XCTAssertFalse(mock.wasOnOmniSuggestionSelectedCalled) | ||
} | ||
|
||
// MARK: - Helper Methods | ||
|
||
private func assertQuerySubmission(query: String, expected: String) { | ||
sut.textField.text = query | ||
sut.onQuerySubmitted() | ||
|
||
XCTAssertEqual(mock.query, expected) | ||
XCTAssertFalse(mock.wasOnOmniSuggestionSelectedCalled) | ||
} | ||
} | ||
|
||
final class MockOmniBarDelegate: OmniBarDelegate { | ||
var query: String = "" | ||
var suggestion: Suggestion? | ||
var wasOnOmniQuerySubmittedCalled = false | ||
var wasOnOmniSuggestionSelectedCalled = false | ||
|
||
func onOmniQuerySubmitted(_ query: String) { | ||
wasOnOmniQuerySubmittedCalled = true | ||
self.query = query | ||
} | ||
|
||
func onOmniSuggestionSelected(_ suggestion: Suggestion) { | ||
wasOnOmniSuggestionSelectedCalled = true | ||
} | ||
|
||
func clear() { | ||
query = "" | ||
suggestion = nil | ||
wasOnOmniQuerySubmittedCalled = false | ||
wasOnOmniSuggestionSelectedCalled = false | ||
} | ||
|
||
func selectedSuggestion() -> Suggestion? { | ||
return suggestion | ||
} | ||
|
||
// MARK: - Unused methods | ||
|
||
func onEditingEnd() -> OmniBarEditingEndResult { | ||
return .dismissed | ||
} | ||
|
||
func onClearPressed() { | ||
} | ||
|
||
func onEnterPressed() { | ||
} | ||
|
||
func onVoiceSearchPressed() { | ||
} | ||
|
||
func onTextFieldWillBeginEditing(_ omniBar: DuckDuckGo.OmniBar, tapped: Bool) { | ||
} | ||
|
||
func onTextFieldDidBeginEditing(_ omniBar: DuckDuckGo.OmniBar) -> Bool { | ||
return false | ||
} | ||
} |