MOBILE-4047 behat: Add switch network connection step

main
Pau Ferrer Ocaña 2022-07-12 10:44:45 +02:00
parent bf78ca0e8f
commit 29d97cca3a
13 changed files with 117 additions and 93 deletions

View File

@ -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;
}
}
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 |

View File

@ -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

View File

@ -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 |

View File

@ -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."

View File

@ -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

View File

@ -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);
}
/**

View File

@ -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;
}
/**