<commit_before>/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.focus.activity;

import android.content.Context;
import android.preference.PreferenceManager;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;

import static android.support.test.espresso.action.ViewActions.click;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mozilla.focus.activity.TestHelper.waitingTime;
import static org.mozilla.focus.fragment.FirstrunFragment.FIRSTRUN_PREF;

/**
 * This test browses to a test site and makes sure that no traces are left on disk.
 *
 * The test uses a whitelist so it might fail as soon as you store new files on disk.
 */
@RunWith(AndroidJUnit4.class)
public class WebViewDataTest {
    private static final List<String> WHITELIST_DATA_DIR_CONTENTS = Arrays.asList(
            "cache", // We assert that this folder is empty
            "code_cache",
            "shared_prefs",
            "app_dxmaker_cache",
            "telemetry",
            "databases",
            "app_webview"
    );

    // We expect those folders to exist but they should be empty.
    private static final List<String> WHITELIST_EMPTY_FOLDERS = Collections.singletonList(
            "app_textures"
    );

    private static final String TEST_PATH = "/copper/truck/destroy?smoke=violet#bizarre";

    /**
     * If any of those strings show up in local files then we consider the browsing session to be leaked.
     */
    private static final List<String> TEST_TRACES = Arrays.asList(
            // URL fragments
            "localhost",
            "copper",
            "truck",
            "destroy",
            "smoke",
            "violet",
            "bizarre",
            // Content from the test page
            "groovy",
            "rabbits",
            "gigantic",
            "experience",
            // Content from cookies
            "birthday",<commit_msg>Closes #1842: Whitelist AS profiler files.<commit_after>/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.focus.activity;

import android.content.Context;
import android.preference.PreferenceManager;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;

import static android.support.test.espresso.action.ViewActions.click;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mozilla.focus.activity.TestHelper.waitingTime;
import static org.mozilla.focus.fragment.FirstrunFragment.FIRSTRUN_PREF;

/**
 * This test browses to a test site and makes sure that no traces are left on disk.
 *
 * The test uses a whitelist so it might fail as soon as you store new files on disk.
 */
@RunWith(AndroidJUnit4.class)
public class WebViewDataTest {
    private static final List<String> WHITELIST_DATA_DIR_CONTENTS = Arrays.asList(
            "cache", // We assert that this folder is empty
            "code_cache",
            "shared_prefs",
            "app_dxmaker_cache",
            "telemetry",
            "databases",
            "app_webview",

            // Android Studio will inject these files if you:
            // - Build with Android Studio 3.0+
            // - Have opened the "Android Profiler" tab at least once since the AS process started
            // - Run on an API 26+ device (or maybe when explicitly enabling advanced debugging on older devices)
            //
            // This should only affect local builds and we don't want to risk breaking the profiler so
            // we whitelist them. Additional details around when these files are added can be found in:
            //   https://github.com/mozilla-mobile/focus-android/issues/1842#issuecomment-348038392
            "libperfa_x86.so",
            "perfa.jar",
            "perfd"
    );

    // We expect those folders to exist but they should be empty.
    private static final List<String> WHITELIST_EMPTY_FOLDERS = Collections.singletonList(
            "app_textures"
    );

    private static final String TEST_PATH = "/copper/truck/destroy?smoke=violet#bizarre";

    /**
     * If any of those strings show up in local files then we consider the browsing session to be leaked.
     */
    private static final List<String> TEST_TRACES = Arrays.asList(
            // URL fragments
            "localhost",
            "copper",
            "truck",
            "destroy",
            "smoke",
            "violet",
            "bizarre",
            // Content from the test page
            "groovy",
            "rabbits",
            "gigantic",
            "experience",
            // Content from cookies
            "birthday",
{'fraction_non_alphanumeric': 0.092187967674349, 'fraction_numerical': 0.00673451062556121, 'mean_word_length': 2.8585450346420322, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7061094', 'n_tokens_mistral': 1926, 'n_tokens_neox': 1875, 'n_words': 560}