Search code examples
androidbrowser-history

Android programmatically manipulate Browser History for unittests


I want to unittest my classes, for that purpose I first call:

Browser.clearHistory(getContext().getContentResolver());

But how can I now add some entries as test-data for my unittest?


Solution

  • i've created a function for that, which calls urls in chrome

    private void openUrlInChrome(String url) {
        Intent intent = new Intent("android.intent.action.MAIN");
    
        // force system to use chrome and not standard browser
        intent.setComponent(ComponentName.unflattenFromString("com.android.chrome/com.android.chrome.Main"));
    
        intent.addCategory("android.intent.category.LAUNCHER");
    
        // is needed because this test is not an activity
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
        intent.setData(Uri.parse(url));
        getContext().startActivity(intent);
        // wait until chrome has opened the pages
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }