From 29d97cca3af7d201b79b6183822146d7901a12d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Tue, 12 Jul 2022 10:44:45 +0200 Subject: [PATCH] MOBILE-4047 behat: Add switch network connection step --- .../tests/behat/behat_app.php | 24 +++++++++- .../messages/tests/behat/basic_usage.feature | 12 ++--- .../assign/tests/behat/basic_usage.feature | 8 ++-- .../choice/tests/behat/basic_usage.feature | 12 ++--- src/addons/mod/data/tests/behat/sync.feature | 16 +++---- .../mod/forum/tests/behat/basic_usage.feature | 34 ++++++------- .../mod/forum/tests/behat/navigation.feature | 4 +- .../glossary/tests/behat/basic_usage.feature | 10 ++-- .../glossary/tests/behat/navigation.feature | 4 +- .../survey/tests/behat/basic_usage.feature | 8 ++-- .../comments/tests/behat/basic_usage.feature | 24 +++++----- src/core/services/app.ts | 6 +-- src/core/services/network.ts | 48 ++++++++++--------- 13 files changed, 117 insertions(+), 93 deletions(-) diff --git a/local_moodleappbehat/tests/behat/behat_app.php b/local_moodleappbehat/tests/behat/behat_app.php index 0f3b92d5e..1ce830d2a 100644 --- a/local_moodleappbehat/tests/behat/behat_app.php +++ b/local_moodleappbehat/tests/behat/behat_app.php @@ -841,9 +841,31 @@ class behat_app extends behat_app_helper { * @Given /^I switch offline mode to "(true|false)"$/ * @param string $offline New value for navigator online mode * @throws DriverException If the navigator.online mode is not available + * @deprecated since 4.1 use i_switch_network_connection instead. */ public function i_switch_offline_mode(string $offline) { - $this->runtime_js("network.setForceOffline($offline)"); + $this->i_switch_network_connection($offline == 'true' ? 'offline' : 'wifi'); + } + + /** + * Switch network connection. + * + * @When /^I switch network connection to (wifi|cellular|offline)$/ + * @param string $more New network mode. + * @throws DriverException If the navigator.online mode is not available + */ + public function i_switch_network_connection(string $mode) { + switch ($mode) { + case 'wifi': + $this->runtime_js("network.setForceConnectionMode('$mode');"); + break; + case 'cellular': + $this->runtime_js("network.setForceConnectionMode('$mode');"); + break; + case 'offline': + $this->runtime_js("network.setForceConnectionMode('none');"); + break; + } } } diff --git a/src/addons/messages/tests/behat/basic_usage.feature b/src/addons/messages/tests/behat/basic_usage.feature index 0fcc067db..bd35f8756 100755 --- a/src/addons/messages/tests/behat/basic_usage.feature +++ b/src/addons/messages/tests/behat/basic_usage.feature @@ -163,7 +163,7 @@ Feature: Test basic usage of messages in app And I set the field "Search" to "student1" in the app And I press "Search" "button" in the app And I press "Student1 student1" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I set the field "New message" to "heeey student" in the app And I press "Send" in the app Then I should find "heeey student" in the app @@ -172,7 +172,7 @@ Feature: Test basic usage of messages in app And I press "Send" in the app Then I should find "byee" in the app - When I switch offline mode to "false" + When I switch network connection to wifi And I press the back button in the app And I press "Student1 student1" in the app Then I should find "heeey student" in the app @@ -192,14 +192,14 @@ Feature: Test basic usage of messages in app And I set the field "Search" to "student1" in the app And I press "Search" "button" in the app And I press "Student1 student1" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I set the field "New message" to "heeey student" in the app And I press "Send" in the app And I set the field "New message" to "byee" in the app And I press "Send" in the app Then I should find "byee" in the app - When I switch offline mode to "false" + When I switch network connection to wifi And I run cron tasks in the app Given I entered the app as "student1" @@ -346,12 +346,12 @@ Feature: Test basic usage of messages in app And I press "Send" in the app Then I should find "self conversation online" in the app - When I switch offline mode to "true" + When I switch network connection to offline And I set the field "New message" to "self conversation offline" in the app And I press "Send" in the app Then I should find "self conversation offline" in the app - When I switch offline mode to "false" + When I switch network connection to wifi And I press the back button in the app And I press "Student1 student1" in the app And I press "Display options" in the app diff --git a/src/addons/mod/assign/tests/behat/basic_usage.feature b/src/addons/mod/assign/tests/behat/basic_usage.feature index ec41e1017..446e32a74 100755 --- a/src/addons/mod/assign/tests/behat/basic_usage.feature +++ b/src/addons/mod/assign/tests/behat/basic_usage.feature @@ -104,14 +104,14 @@ Feature: Test basic usage of assignment activity in app Scenario: Add submission offline (online text) & Submit for grading offline & Sync submissions Given I entered the assign activity "assignment1" on course "Course 1" as "student1" in the app When I press "Add submission" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I set the field "Online text submissions" to "Submission test" in the app And I press "Save" in the app And I press "Submit assignment" in the app And I press "OK" in the app Then I should find "This Assignment has offline data to be synchronised." in the app - When I switch offline mode to "false" + When I switch network connection to wifi And I press the back button in the app And I press "assignment1" in the app And I press "Information" in the app @@ -122,7 +122,7 @@ Feature: Test basic usage of assignment activity in app Scenario: Edit an offline submission before synchronising it Given I entered the assign activity "assignment1" on course "Course 1" as "student1" in the app When I press "Add submission" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I set the field "Online text submissions" to "Submission test original offline" in the app And I press "Save" in the app Then I should find "This Assignment has offline data to be synchronised." in the app @@ -139,7 +139,7 @@ Feature: Test basic usage of assignment activity in app And I press "OK" in the app Then I should find "This Assignment has offline data to be synchronised." in the app - When I switch offline mode to "false" + When I switch network connection to wifi And I press the back button in the app And I press "assignment1" in the app Then I should find "Submitted for grading" in the app diff --git a/src/addons/mod/choice/tests/behat/basic_usage.feature b/src/addons/mod/choice/tests/behat/basic_usage.feature index 753181599..aae180c51 100755 --- a/src/addons/mod/choice/tests/behat/basic_usage.feature +++ b/src/addons/mod/choice/tests/behat/basic_usage.feature @@ -72,7 +72,7 @@ Feature: Test basic usage of choice activity in app | choice | Test single choice name | Test single choice description | C1 | choice1 | Option 1, Option 2, Option 3 | 0 | 0 | 1 | And I entered the choice activity "Test single choice name" on course "Course 1" as "student1" in the app When I select "Option 1" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I select "Option 2" in the app And I press "Save my choice" in the app Then I should find "Are you sure" in the app @@ -85,7 +85,7 @@ Feature: Test basic usage of choice activity in app And I should not find "Option 2: 1" in the app And I should not find "Option 3: 0" in the app - When I switch offline mode to "false" + When I switch network connection to wifi And I press the back button in the app And I press "Test single choice name" in the app Then I should find "Test single choice description" in the app @@ -103,7 +103,7 @@ Feature: Test basic usage of choice activity in app | choice | Test single choice name | Test single choice description | C1 | choice1 | Option 1, Option 2, Option 3 | 0 | 0 | 1 | And I entered the choice activity "Test single choice name" on course "Course 1" as "student1" in the app When I select "Option 1" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I select "Option 2" in the app And I press "Save my choice" in the app Then I should find "Are you sure" in the app @@ -114,7 +114,7 @@ Feature: Test basic usage of choice activity in app And I should not find "Option 2: 1" in the app And I should not find "Option 3: 0" in the app - When I switch offline mode to "false" + When I switch network connection to wifi And I run cron tasks in the app And I wait loading to finish in the app Then I should find "Option 1: 0" in the app @@ -133,7 +133,7 @@ Feature: Test basic usage of choice activity in app Then I should find "Downloaded" within "Test single choice name" "ion-item" in the app And I press the back button in the app - When I switch offline mode to "true" + When I switch network connection to offline And I press "Test multi choice name" in the app Then I should find "There was a problem connecting to the site. Please check your connection and try again." in the app @@ -152,7 +152,7 @@ Feature: Test basic usage of choice activity in app And I should not find "Option 2: 1" in the app And I should not find "Option 3: 0" in the app - When I switch offline mode to "false" + When I switch network connection to wifi And I press the back button in the app And I press "Test single choice name" in the app Then I should find "Option 1: 0" in the app diff --git a/src/addons/mod/data/tests/behat/sync.feature b/src/addons/mod/data/tests/behat/sync.feature index 699739215..00fcf382d 100644 --- a/src/addons/mod/data/tests/behat/sync.feature +++ b/src/addons/mod/data/tests/behat/sync.feature @@ -33,7 +33,7 @@ Feature: Users can store entries in database activities when offline and sync wh Scenario: Create entry (offline) Given I entered the data activity "Web links" on course "Course 1" as "student1" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I should find "No entries in database" in the app When I press "Add entries" in the app And I set the following fields to these values in the app: @@ -44,7 +44,7 @@ Feature: Users can store entries in database activities when offline and sync wh And I should find "Moodle community site" in the app And I should find "This Database has offline data to be synchronised" in the app And I press the back button in the app - And I switch offline mode to "false" + And I switch network connection to wifi And I press "Web links" near "General" in the app And I should find "https://moodle.org/" in the app And I should find "Moodle community site" in the app @@ -63,7 +63,7 @@ Feature: Users can store entries in database activities when offline and sync wh And I press "Information" in the app And I press "Download" in the app And I wait until the page is ready - And I switch offline mode to "true" + And I switch network connection to offline When I press "Edit" in the app And I set the following fields to these values in the app: | URL | https://moodlecloud.com/ | @@ -75,7 +75,7 @@ Feature: Users can store entries in database activities when offline and sync wh And I should find "Moodle Cloud" in the app And I should find "This Database has offline data to be synchronised" in the app And I press the back button in the app - And I switch offline mode to "false" + And I switch network connection to wifi And I press "Web links" near "General" in the app And I should not find "https://moodle.org/" in the app And I should not find "Moodle community site" in the app @@ -85,7 +85,7 @@ Feature: Users can store entries in database activities when offline and sync wh And I press "Information" in the app And I press "Refresh" in the app And I wait until the page is ready - And I switch offline mode to "true" + And I switch network connection to offline And I press "Delete" in the app And I should find "Are you sure you want to delete this entry?" in the app And I press "Delete" in the app @@ -93,7 +93,7 @@ Feature: Users can store entries in database activities when offline and sync wh And I should find "Moodle Cloud" in the app And I should find "This Database has offline data to be synchronised" in the app And I press the back button in the app - And I switch offline mode to "false" + And I switch network connection to wifi And I press "Web links" near "General" in the app And I should not find "https://moodlecloud.com/" in the app And I should not find "Moodle Cloud" in the app @@ -112,7 +112,7 @@ Feature: Users can store entries in database activities when offline and sync wh And I press "Information" in the app And I press "Download" in the app And I wait until the page is ready - When I switch offline mode to "true" + When I switch network connection to offline And I press "Delete" in the app And I should find "Are you sure you want to delete this entry?" in the app And I press "Delete" in the app @@ -121,7 +121,7 @@ Feature: Users can store entries in database activities when offline and sync wh And I should find "This Database has offline data to be synchronised" in the app And I press "Restore" in the app And I press the back button in the app - And I switch offline mode to "false" + And I switch network connection to wifi And I press "Web links" near "General" in the app Then I should find "https://moodle.org/" in the app And I should find "Moodle community site" in the app diff --git a/src/addons/mod/forum/tests/behat/basic_usage.feature b/src/addons/mod/forum/tests/behat/basic_usage.feature index afdb62ebc..eb5750689 100755 --- a/src/addons/mod/forum/tests/behat/basic_usage.feature +++ b/src/addons/mod/forum/tests/behat/basic_usage.feature @@ -96,7 +96,7 @@ Feature: Test basic usage of forum activity in app Then I should find "Reply" in the app When I press the back button in the app - And I switch offline mode to "true" + And I switch network connection to offline And I press "Initial discussion" in the app And I press "Reply" in the app And I set the field "Message" to "not sent reply" in the app @@ -110,7 +110,7 @@ Feature: Test basic usage of forum activity in app Then I should find "Not sent" in the app And I should find "This Discussion has offline data to be synchronised" in the app - When I switch offline mode to "false" + When I switch network connection to wifi And I press the back button in the app And I press "Initial discussion" in the app Then I should not find "Not sent" in the app @@ -118,7 +118,7 @@ Feature: Test basic usage of forum activity in app Scenario: Edit a not sent new discussion offline Given I entered the forum activity "Test forum name" on course "Course 1" as "student1" in the app - When I switch offline mode to "true" + When I switch network connection to offline And I press "Add discussion topic" in the app And I set the following fields to these values in the app: | Subject | Auto-test | @@ -129,7 +129,7 @@ Feature: Test basic usage of forum activity in app And I press "Post to forum" in the app Then I should find "This Forum has offline data to be synchronised." in the app - When I switch offline mode to "false" + When I switch network connection to wifi And I press "Auto-test" in the app Then I should find "Post to forum" in the app @@ -151,7 +151,7 @@ Feature: Test basic usage of forum activity in app Then I should find "Edit" in the app When I press "Edit" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I set the field "Message" to "Auto-test message edited" in the app And I press "Save changes" in the app Then I should find "There was a problem connecting to the site. Please check your connection and try again." in the app @@ -163,7 +163,7 @@ Feature: Test basic usage of forum activity in app And I press "Edit" in the app Then I should find "There was a problem connecting to the site. Please check your connection and try again." in the app - When I switch offline mode to "false" + When I switch network connection to wifi And I press "OK" in the app And I press "Edit" in the app And I set the field "Message" to "Auto-test message edited" in the app @@ -183,7 +183,7 @@ Feature: Test basic usage of forum activity in app When I press "Delete" in the app And I press "Cancel" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I press "Display options" near "Reply" in the app Then I should find "Delete" in the app @@ -192,7 +192,7 @@ Feature: Test basic usage of forum activity in app When I press "OK" in the app And I close the popup in the app - And I switch offline mode to "false" + And I switch network connection to wifi And I press "Display options" near "Reply" in the app And I press "Delete" in the app And I press "Delete" in the app @@ -215,14 +215,14 @@ Feature: Test basic usage of forum activity in app When I press "Auto-test" in the app And I press "None" near "Auto-test message" in the app And I press "1" near "Cancel" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I press "None" near "test2" in the app And I press "0" near "Cancel" in the app Then I should find "Data stored in the device because it couldn't be sent. It will be sent automatically later." in the app And I should find "Average of ratings: -" in the app And I should find "Average of ratings: 1" in the app - When I switch offline mode to "false" + When I switch network connection to wifi And I press the back button in the app Then I should find "This Forum has offline data to be synchronised." in the app @@ -244,7 +244,7 @@ Feature: Test basic usage of forum activity in app Scenario: Reply a post offline Given I entered the forum activity "Test forum name" on course "Course 1" as "student1" in the app When I press "Initial discussion" in the app - And I switch offline mode to "true" + And I switch network connection to offline Then I should find "Reply" in the app When I press "Reply" in the app @@ -255,7 +255,7 @@ Feature: Test basic usage of forum activity in app And I should find "Not sent" in the app When I press the back button in the app - And I switch offline mode to "false" + And I switch network connection to wifi And I press "Initial discussion" in the app Then I should find "Initial discussion message" in the app And I should find "ReplyMessage" in the app @@ -263,7 +263,7 @@ Feature: Test basic usage of forum activity in app Scenario: New discussion offline & Sync Forum Given I entered the forum activity "Test forum name" on course "Course 1" as "student1" in the app - When I switch offline mode to "true" + When I switch network connection to offline And I press "Add discussion topic" in the app And I set the following fields to these values in the app: | Subject | DiscussionSubject | @@ -273,7 +273,7 @@ Feature: Test basic usage of forum activity in app And I should find "Not sent" in the app And I should find "This Forum has offline data to be synchronised." in the app - When I switch offline mode to "false" + When I switch network connection to wifi And I press the back button in the app And I press "Test forum name" in the app And I press "Information" in the app @@ -286,7 +286,7 @@ Feature: Test basic usage of forum activity in app Scenario: New discussion offline & Auto-sync forum Given I entered the forum activity "Test forum name" on course "Course 1" as "student1" in the app - When I switch offline mode to "true" + When I switch network connection to offline And I press "Add discussion topic" in the app And I set the following fields to these values in the app: | Subject | DiscussionSubject | @@ -296,7 +296,7 @@ Feature: Test basic usage of forum activity in app And I should find "Not sent" in the app And I should find "This Forum has offline data to be synchronised." in the app - When I switch offline mode to "false" + When I switch network connection to wifi And I run cron tasks in the app And I wait loading to finish in the app Then I should not find "Not sent" in the app @@ -314,7 +314,7 @@ Feature: Test basic usage of forum activity in app Then I should find "Downloaded" within "Test forum name" "ion-item" in the app When I press the back button in the app - And I switch offline mode to "true" + And I switch network connection to offline And I press "Test forum name" in the app Then I should find "Initial discussion" in the app diff --git a/src/addons/mod/forum/tests/behat/navigation.feature b/src/addons/mod/forum/tests/behat/navigation.feature index 80bdef26b..89dae9dee 100644 --- a/src/addons/mod/forum/tests/behat/navigation.feature +++ b/src/addons/mod/forum/tests/behat/navigation.feature @@ -103,7 +103,7 @@ Feature: Test forum navigation # Offline When I press the back button in the app And I press "Add discussion topic" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I set the following fields to these values in the app: | Subject | Offline discussion 1 | | Message | Offline discussion 1 message | @@ -199,7 +199,7 @@ Feature: Test forum navigation # Offline When I press "Add discussion topic" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I set the following fields to these values in the app: | Subject | Offline discussion 1 | | Message | Offline discussion 1 message | diff --git a/src/addons/mod/glossary/tests/behat/basic_usage.feature b/src/addons/mod/glossary/tests/behat/basic_usage.feature index c3103b0a3..a2209d84b 100644 --- a/src/addons/mod/glossary/tests/behat/basic_usage.feature +++ b/src/addons/mod/glossary/tests/behat/basic_usage.feature @@ -111,7 +111,7 @@ Feature: Test basic usage of glossary in app When I press "Course downloads" in the app When I press "Download" within "Test glossary" "ion-item" in the app And I press the back button in the app - And I switch offline mode to "true" + And I switch network connection to offline And I press "Test glossary" in the app Then the header should be "Test glossary" in the app And I should find "Cucumber" in the app @@ -156,7 +156,7 @@ Feature: Test basic usage of glossary in app Scenario: Sync Given I entered the glossary activity "Test glossary" on course "Course 1" as "student1" in the app And I press "Add a new entry" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I set the following fields to these values in the app: | Concept | Broccoli | | Definition | Brassica oleracea var. italica | @@ -181,7 +181,7 @@ Feature: Test basic usage of glossary in app And I should find "Entries to be synced" in the app And I should find "This Glossary has offline data to be synchronised." in the app - When I switch offline mode to "false" + When I switch network connection to wifi And I press "Information" in the app And I press "Synchronise now" in the app Then the header should be "Test glossary" in the app @@ -211,13 +211,13 @@ Feature: Test basic usage of glossary in app # Rate entries as teacher2 Given I entered the glossary activity "Test glossary" on course "Course 1" as "teacher2" in the app And I press "Cucumber" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I press "None" in the app And I press "0" in the app Then I should find "Data stored in the device because it couldn't be sent. It will be sent automatically later." in the app And I should find "Average of ratings: 1" in the app - When I switch offline mode to "false" + When I switch network connection to wifi And I press the back button in the app Then I should find "This Glossary has offline data to be synchronised." in the app diff --git a/src/addons/mod/glossary/tests/behat/navigation.feature b/src/addons/mod/glossary/tests/behat/navigation.feature index 04b988ecf..309c0a1f0 100644 --- a/src/addons/mod/glossary/tests/behat/navigation.feature +++ b/src/addons/mod/glossary/tests/behat/navigation.feature @@ -173,7 +173,7 @@ Feature: Test glossary navigation When I press the back button in the app And I press "Clear search" in the app And I press "Add a new entry" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I set the following fields to these values in the app: | Concept | Tomato | | Definition | Tomato is a fruit | @@ -274,7 +274,7 @@ Feature: Test glossary navigation # Offline When I press "Clear search" in the app And I press "Add a new entry" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I set the following fields to these values in the app: | Concept | Tomato | | Definition | Tomato is a fruit | diff --git a/src/addons/mod/survey/tests/behat/basic_usage.feature b/src/addons/mod/survey/tests/behat/basic_usage.feature index b6e0755c4..f12395a58 100755 --- a/src/addons/mod/survey/tests/behat/basic_usage.feature +++ b/src/addons/mod/survey/tests/behat/basic_usage.feature @@ -233,12 +233,12 @@ Feature: Test basic usage of survey activity in app | activity | name | intro | template | course | idnumber | groupmode | | survey | Test survey critical incidents | Test survey1 | 5 | C1 | survey1 | 0 | Given I entered the survey activity "Test survey critical incidents" on course "Course 1" as "student1" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I press "Submit" in the app And I press "OK" in the app Then I should see "This Survey has offline data to be synchronised." - When I switch offline mode to "false" + When I switch network connection to wifi And I press the back button in the app And I press "Test survey critical incidents" in the app And I press "Information" in the app @@ -255,7 +255,7 @@ Feature: Test basic usage of survey activity in app And I press "Course downloads" in the app And I press "Download" within "Test survey critical incidents" "ion-item" in the app And I press the back button in the app - And I switch offline mode to "true" + And I switch network connection to offline And I press "Test survey name" in the app Then I should see "There was a problem connecting to the site. Please check your connection and try again." @@ -266,7 +266,7 @@ Feature: Test basic usage of survey activity in app And I press "OK" in the app Then I should see "This Survey has offline data to be synchronised." - When I switch offline mode to "false" + When I switch network connection to wifi And I run cron tasks in the app Then I should not see "This Survey has offline data to be synchronised." And I should see "You have completed this survey." diff --git a/src/core/features/comments/tests/behat/basic_usage.feature b/src/core/features/comments/tests/behat/basic_usage.feature index 9fef313be..01ebe7a67 100644 --- a/src/core/features/comments/tests/behat/basic_usage.feature +++ b/src/core/features/comments/tests/behat/basic_usage.feature @@ -91,7 +91,7 @@ Feature: Test basic usage of comments in app And I press "Save" in the app And I press "More" in the app And I press "Comments (0)" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I set the field "Add a comment..." to "comment test" in the app And I press "Send" in the app Then I should find "Data stored in the device because it couldn't be sent. It will be sent automatically later." in the app @@ -100,7 +100,7 @@ Feature: Test basic usage of comments in app When I press the back button in the app And I press "Comments (0)" in the app - And I switch offline mode to "false" + And I switch network connection to wifi And I press "Display options" in the app And I press "Synchronise now" in the app And I close the popup in the app @@ -109,7 +109,7 @@ Feature: Test basic usage of comments in app When I press the back button in the app And I press "Comments (1)" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I press "Toggle delete buttons" in the app And I press "Delete" in the app And I press "Delete" near "Cancel" in the app @@ -120,7 +120,7 @@ Feature: Test basic usage of comments in app When I press the back button in the app And I press "Comments (1)" in the app - And I switch offline mode to "false" + And I switch network connection to wifi And I press "Display options" in the app And I press "Synchronise now" in the app And I close the popup in the app @@ -179,7 +179,7 @@ Feature: Test basic usage of comments in app And I press "Save" in the app And I press "potato" in the app And I press "Comments (0)" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I set the field "Add a comment..." to "comment test" in the app And I press "Send" in the app Then I should find "Data stored in the device because it couldn't be sent. It will be sent automatically later." in the app @@ -188,7 +188,7 @@ Feature: Test basic usage of comments in app When I press the back button in the app And I press "Comments (0)" in the app - And I switch offline mode to "false" + And I switch network connection to wifi And I press "Display options" in the app And I press "Synchronise now" in the app And I close the popup in the app @@ -197,7 +197,7 @@ Feature: Test basic usage of comments in app When I press the back button in the app And I press "Comments (1)" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I press "Toggle delete buttons" in the app And I press "Delete" in the app And I press "Delete" near "Cancel" in the app @@ -208,7 +208,7 @@ Feature: Test basic usage of comments in app When I press the back button in the app And I press "Comments (1)" in the app - And I switch offline mode to "false" + And I switch network connection to wifi And I press "Display options" in the app And I press "Synchronise now" in the app And I close the popup in the app @@ -262,7 +262,7 @@ Feature: Test basic usage of comments in app And I should find "Blog body" in the app When I press "Comments (0)" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I set the field "Add a comment..." to "comment test" in the app And I press "Send" in the app Then I should find "Data stored in the device because it couldn't be sent. It will be sent automatically later." in the app @@ -271,7 +271,7 @@ Feature: Test basic usage of comments in app When I press the back button in the app And I press "Comments (0)" in the app - And I switch offline mode to "false" + And I switch network connection to wifi And I press "Display options" in the app And I press "Synchronise now" in the app And I close the popup in the app @@ -280,7 +280,7 @@ Feature: Test basic usage of comments in app When I press the back button in the app And I press "Comments (1)" in the app - And I switch offline mode to "true" + And I switch network connection to offline And I press "Toggle delete buttons" in the app And I press "Delete" in the app And I press "Delete" near "Cancel" in the app @@ -291,7 +291,7 @@ Feature: Test basic usage of comments in app When I press the back button in the app And I press "Comments (1)" in the app - And I switch offline mode to "false" + And I switch network connection to wifi And I press "Display options" in the app And I press "Synchronise now" in the app And I close the popup in the app diff --git a/src/core/services/app.ts b/src/core/services/app.ts index a734676e8..9a3632274 100644 --- a/src/core/services/app.ts +++ b/src/core/services/app.ts @@ -30,7 +30,7 @@ import { CoreDatabaseTable } from '@classes/database/database-table'; import { CorePromisedValue } from '@classes/promised-value'; import { Subscription } from 'rxjs'; import { CorePlatform } from '@services/platform'; -import { CoreNetwork } from '@services/network'; +import { CoreNetwork, CoreNetworkConnection } from '@services/network'; /** * Factory to provide some global functionalities, like access to the global app database. @@ -644,10 +644,10 @@ export class CoreAppProvider { * Set value of forceOffline flag. If true, the app will think the device is offline. * * @param value Value to set. - * @deprecated since 4.1.0. Use CoreNetwork instead. + * @deprecated since 4.1.0. Use CoreNetwork.setForceConnectionMode instead. */ setForceOffline(value: boolean): void { - CoreNetwork.setForceOffline(value); + CoreNetwork.setForceConnectionMode(value ? CoreNetworkConnection.NONE : CoreNetworkConnection.WIFI); } /** diff --git a/src/core/services/network.ts b/src/core/services/network.ts index 44cb095e3..72f9ec98f 100644 --- a/src/core/services/network.ts +++ b/src/core/services/network.ts @@ -18,7 +18,7 @@ import { Network } from '@ionic-native/network/ngx'; import { makeSingleton } from '@singletons'; import { Observable, Subject, merge } from 'rxjs'; -enum CoreNetworkConnection { +export enum CoreNetworkConnection { UNKNOWN = 'unknown', ETHERNET = 'ethernet', WIFI = 'wifi', @@ -39,9 +39,21 @@ export class CoreNetworkService extends Network { protected connectObservable = new Subject<'connected'>(); protected disconnectObservable = new Subject<'disconnected'>(); - protected forceOffline = false; + protected forceConnectionMode?: CoreNetworkConnection; protected online = false; + get connectionType(): CoreNetworkConnection { + if (this.forceConnectionMode !== undefined) { + return this.forceConnectionMode; + } + + if (CorePlatform.isMobile()) { + return this.type as CoreNetworkConnection; + } + + return this.online ? CoreNetworkConnection.WIFI : CoreNetworkConnection.NONE; + } + /** * Initialize the service. */ @@ -81,12 +93,13 @@ export class CoreNetworkService extends Network { } /** - * Set value of forceOffline flag. If true, the app will think the device is offline. + * Set value of forceConnectionMode flag. + * The app will think the device is offline or limited connection. * * @param value Value to set. */ - setForceOffline(value: boolean): void { - this.forceOffline = !!value; + setForceConnectionMode(value: CoreNetworkConnection): void { + this.forceConnectionMode = value; this.fireObservable(); } @@ -105,24 +118,15 @@ export class CoreNetworkService extends Network { * @return Whether the app is online. */ checkOnline(): void { - if (this.forceOffline) { + if (this.forceConnectionMode === CoreNetworkConnection.NONE) { this.online = false; - this.type = CoreNetworkConnection.NONE; return; } - if (!CorePlatform.isMobile()) { - this.online = navigator.onLine; - this.type = this.online - ? CoreNetworkConnection.WIFI - : CoreNetworkConnection.NONE; + const type = this.connectionType; - return; - } - - let online = this.type !== null && this.type !== CoreNetworkConnection.NONE && - this.type !== CoreNetworkConnection.UNKNOWN; + let online = type !== null && type !== CoreNetworkConnection.NONE && type !== CoreNetworkConnection.UNKNOWN; // Double check we are not online because we cannot rely 100% in Cordova APIs. if (!online && navigator.onLine) { @@ -179,18 +183,16 @@ export class CoreNetworkService extends Network { * @return Whether the device uses a limited connection. */ isNetworkAccessLimited(): boolean { - if (!CorePlatform.isMobile()) { - return false; - } - - const limited: string[] = [ + const limited: CoreNetworkConnection[] = [ CoreNetworkConnection.CELL_2G, CoreNetworkConnection.CELL_3G, CoreNetworkConnection.CELL_4G, CoreNetworkConnection.CELL, ]; - return limited.indexOf(this.type) > -1; + const type = this.connectionType; + + return limited.indexOf(type) > -1; } /**