Merge "New test cases test_browsing_4g & test_browsing_wifi"
diff --git a/acts/framework/acts/test_utils/tel/tel_data_utils.py b/acts/framework/acts/test_utils/tel/tel_data_utils.py
index 4ff6550..0daea65 100644
--- a/acts/framework/acts/test_utils/tel/tel_data_utils.py
+++ b/acts/framework/acts/test_utils/tel/tel_data_utils.py
@@ -15,6 +15,8 @@
 #   limitations under the License.
 
 import time
+import random
+import re
 
 from acts.utils import rand_ascii_str
 from acts.test_utils.tel.tel_subscription_utils import \
@@ -46,7 +48,9 @@
 from acts.test_utils.tel.tel_test_utils import WIFI_CONFIG_APBAND_5G
 from acts.test_utils.tel.tel_test_utils import get_service_state_by_adb
 from acts.test_utils.tel.tel_test_utils import wait_for_state
-
+from acts.test_utils.tel.tel_test_utils import get_mobile_data_usage
+from acts.test_utils.tel.tel_test_utils import get_wifi_usage
+from acts.test_utils.tel.tel_test_utils import check_is_wifi_connected
 
 def wifi_tethering_cleanup(log, provider, client_list):
     """Clean up steps for WiFi Tethering.
@@ -495,3 +499,104 @@
         ad.log.error("No Internet access after changing Data SIM.")
         return False
     return True
+
+def browsing_test(log, ad, wifi_ssid=None, pass_threshold_in_mb = 1.0):
+    """ Ramdomly browse 6 among 23 selected web sites. The idle time is used to
+    simulate visit duration and normally distributed with the mean 35 seconds
+    and std dev 15 seconds, which means 95% of visit duration will be between
+    5 and 65 seconds. DUT will enter suspend mode when idle time is greater than
+    35 seconds.
+
+    Args:
+        log: log object.
+        ad: android object.
+        pass_threshold_in_mb: minimum traffic of browsing 6 web sites in MB for
+            test pass
+
+    Returns:
+        True if the total traffic of Chrome for browsing 6 web sites is greater
+        than pass_threshold_in_mb. Otherwise False.
+    """
+    web_sites = [
+        "http://tw.yahoo.com",
+        "http://24h.pchome.com.tw",
+        "http://www.mobile01.com",
+        "https://www.android.com/phones/",
+        "http://www.books.com.tw",
+        "http://www.udn.com.tw",
+        "http://news.baidu.com",
+        "http://www.google.com",
+        "http://www.cnn.com",
+        "http://www.nytimes.com",
+        "http://www.amazon.com",
+        "http://www.wikipedia.com",
+        "http://www.ebay.com",
+        "http://www.youtube.com",
+        "http://espn.go.com",
+        "http://www.sueddeutsche.de",
+        "http://www.bild.de",
+        "http://www.welt.de",
+        "http://www.lefigaro.fr",
+        "http://www.accuweather.com",
+        "https://www.flickr.com",
+        "http://world.taobao.com",
+        "http://www.theguardian.com"]
+
+    wifi_connected = False
+    if wifi_ssid and check_is_wifi_connected(ad.log, ad, wifi_ssid):
+        wifi_connected = True
+        usage_level_at_start = get_wifi_usage(ad, apk="com.android.chrome")
+    else:
+        usage_level_at_start = get_mobile_data_usage(ad, apk="com.android.chrome")
+
+    for web_site in random.sample(web_sites, 6):
+        ad.log.info("Browsing %s..." % web_site)
+        ad.adb.shell(
+            "am start -a android.intent.action.VIEW -d %s --es "
+            "com.android.browser.application_id com.android.browser" % web_site)
+
+        idle_time = round(random.normalvariate(35, 15))
+        if idle_time < 2:
+            idle_time = 2
+        elif idle_time > 90:
+            idle_time = 90
+
+        ad.log.info(
+            "Idle time before browsing next web site: %s sec." % idle_time)
+
+        if idle_time > 35:
+            time.sleep(35)
+            rest_idle_time = idle_time-35
+            if rest_idle_time < 3:
+                rest_idle_time = 3
+            ad.log.info("Let device go to sleep for %s sec." % rest_idle_time)
+            ad.droid.wakeLockRelease()
+            ad.droid.goToSleepNow()
+            time.sleep(rest_idle_time)
+            ad.log.info("Wake up device.")
+            ad.droid.wakeLockAcquireBright()
+            ad.droid.wakeUpNow()
+            time.sleep(3)
+        else:
+            time.sleep(idle_time)
+
+    if wifi_connected:
+        usage_level = get_wifi_usage(ad, apk="com.android.chrome")
+    else:
+        usage_level = get_mobile_data_usage(ad, apk="com.android.chrome")
+
+    try:
+        usage = round((usage_level - usage_level_at_start)/1024/1024, 2)
+        if usage < pass_threshold_in_mb:
+            ad.log.error(
+                "Usage of browsing '%s MB' is smaller than %s " % (
+                    usage, pass_threshold_in_mb))
+            return False
+        else:
+            ad.log.info("Usage of browsing: %s MB" % usage)
+            return True
+    except Exception as e:
+        ad.log.error(e)
+        usage = "unknown"
+        ad.log.info("Usage of browsing: %s MB" % usage)
+        return False
diff --git a/acts/framework/acts/test_utils/tel/tel_test_utils.py b/acts/framework/acts/test_utils/tel/tel_test_utils.py
index 1a94e51..bd5997c 100644
--- a/acts/framework/acts/test_utils/tel/tel_test_utils.py
+++ b/acts/framework/acts/test_utils/tel/tel_test_utils.py
@@ -3722,6 +3722,37 @@
             ad.adb.shell("rm %s" % file_path, ignore_status=True)
 
 
+def get_wifi_usage(ad, sid=None, apk=None):
+    if not sid:
+        sid = ad.droid.subscriptionGetDefaultDataSubId()
+    current_time = int(time.time() * 1000)
+    begin_time = current_time - 10 * 24 * 60 * 60 * 1000
+    end_time = current_time + 10 * 24 * 60 * 60 * 1000
+
+    if apk:
+        uid = ad.get_apk_uid(apk)
+        ad.log.debug("apk %s uid = %s", apk, uid)
+        try:
+            return ad.droid.connectivityQueryDetailsForUid(
+                TYPE_WIFI,
+                ad.droid.telephonyGetSubscriberIdForSubscription(sid),
+                begin_time, end_time, uid)
+        except:
+            return ad.droid.connectivityQueryDetailsForUid(
+                ad.droid.telephonyGetSubscriberIdForSubscription(sid),
+                begin_time, end_time, uid)
+    else:
+        try:
+            return ad.droid.connectivityQuerySummaryForDevice(
+                TYPE_WIFI,
+                ad.droid.telephonyGetSubscriberIdForSubscription(sid),
+                begin_time, end_time)
+        except:
+            return ad.droid.connectivityQuerySummaryForDevice(
+                ad.droid.telephonyGetSubscriberIdForSubscription(sid),
+                begin_time, end_time)
+
+
 def get_mobile_data_usage(ad, sid=None, apk=None):
     if not sid:
         sid = ad.droid.subscriptionGetDefaultDataSubId()
diff --git a/acts/tests/google/tel/live/TelLiveDataTest.py b/acts/tests/google/tel/live/TelLiveDataTest.py
index e1a6910..46fb618 100644
--- a/acts/tests/google/tel/live/TelLiveDataTest.py
+++ b/acts/tests/google/tel/live/TelLiveDataTest.py
@@ -56,6 +56,7 @@
     WAIT_TIME_DATA_STATUS_CHANGE_DURING_WIFI_TETHERING
 from acts.test_utils.tel.tel_defines import WAIT_TIME_TETHERING_AFTER_REBOOT
 from acts.test_utils.tel.tel_data_utils import airplane_mode_test
+from acts.test_utils.tel.tel_data_utils import browsing_test
 from acts.test_utils.tel.tel_data_utils import change_data_sim_and_verify_data
 from acts.test_utils.tel.tel_data_utils import data_connectivity_single_bearer
 from acts.test_utils.tel.tel_data_utils import tethering_check_internet_connection
@@ -119,6 +120,7 @@
 from acts.test_utils.tel.tel_voice_utils import phone_setup_csfb
 from acts.test_utils.tel.tel_voice_utils import phone_setup_voice_general
 from acts.test_utils.tel.tel_voice_utils import phone_setup_volte
+from acts.test_utils.tel.tel_voice_utils import phone_setup_4g
 from acts.utils import disable_doze
 from acts.utils import enable_doze
 from acts.utils import rand_ascii_str
@@ -3305,4 +3307,31 @@
         return self._test_data_stall_detection_recovery(nw_type="cellular",
                                                 validation_type="recovery")
 
-        """ Tests End """
+    @test_tracker_info(uuid="d705d653-c810-42eb-bd07-3313f99be2fa")
+    @TelephonyBaseTest.tel_test_wrap
+    def test_browsing_4g(self):
+        ad = self.android_devices[0]
+        self.log.info("Connect to LTE and verify internet connection.")
+        if not phone_setup_4g(self.log, ad):
+            return False
+        if not verify_internet_connection(self.log, ad):
+            return False
+
+        return browsing_test(self.log, self.android_devices[0])
+
+    @test_tracker_info(uuid="71088cb1-5ccb-4d3a-8e6a-03fac9bf31cc")
+    @TelephonyBaseTest.tel_test_wrap
+    def test_browsing_wifi(self):
+        ad = self.android_devices[0]
+        self.log.info("Connect to Wi-Fi and verify internet connection.")
+        if not ensure_wifi_connected(self.log, ad, self.wifi_network_ssid,
+                                     self.wifi_network_pass):
+            return False
+        if not wait_for_wifi_data_connection(self.log, ad, True):
+            return False
+        if not verify_internet_connection(self.log, ad):
+            return False
+
+        return browsing_test(self.log, self.android_devices[0], wifi_ssid=self.wifi_network_ssid)
+
+    """ Tests End """