summaryrefslogtreecommitdiffstats
path: root/tests/DumpRenderTree/src/com/android/dumprendertree/ReliabilityTest.java
blob: 22c458cf926a00c1e03bbc1497373225d1afe97b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.android.dumprendertree;

import android.os.Handler;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class ReliabilityTest extends ActivityInstrumentationTestCase2<ReliabilityTestActivity> {
    
    private static final String LOGTAG = "ReliabilityTest";
    private static final String PKG_NAME = "com.android.dumprendertree";
    private static final String TEST_LIST_FILE = "/sdcard/android/reliability_tests_list.txt";
    private static final String TEST_STATUS_FILE = "/sdcard/android/reliability_running_test.txt";
    private static final String TEST_TIMEOUT_FILE = "/sdcard/android/reliability_timeout_test.txt";
    private static final String TEST_DONE = "#DONE";
    static final String RELIABILITY_TEST_RUNNER_FILES[] = {
        "run_reliability_tests.py"
    };
    
    public ReliabilityTest() {
        super(PKG_NAME, ReliabilityTestActivity.class);
    }
    
    public void runReliabilityTest() throws Throwable {
        ReliabilityTestActivity activity = getActivity();
        LayoutTestsAutoRunner runner = (LayoutTestsAutoRunner)getInstrumentation();
        
        File testListFile = new File(TEST_LIST_FILE);
        if(!testListFile.exists())
            throw new FileNotFoundException("test list file not found.");
        
        BufferedReader listReader = new BufferedReader(
                new FileReader(testListFile));
        
        //always try to resume first, hence cleaning up status will be the
        //responsibility of driver scripts
        String lastUrl = readTestStatus();
        if(lastUrl != null && !TEST_DONE.equals(lastUrl))
            fastForward(listReader, lastUrl);
        
        String url = null;
        Handler handler = null;
        boolean timeoutFlag = false;
        long start, elapsed;
        
        //read from BufferedReader instead of populating a list in advance,
        //this will avoid excessive memory usage in case of a large list
        while((url = listReader.readLine()) != null) {
            url = url.trim();
            if(url.length() == 0)
                continue;
            start = System.currentTimeMillis();
            Log.v(LOGTAG, "Testing URL: " + url);
            updateTestStatus(url);
            activity.reset();
            //use message to send new URL to avoid interacting with
            //WebView in non-UI thread
            handler = activity.getHandler();
            handler.sendMessage(handler.obtainMessage(
                    ReliabilityTestActivity.MSG_NAVIGATE, 
                    runner.mTimeoutInMillis, runner.mDelay, url));
            timeoutFlag = activity.waitUntilDone();
            elapsed = System.currentTimeMillis() - start;
            if(elapsed < 1000) {
                Log.w(LOGTAG, "Page load finished in " + elapsed
                        + "ms, too soon?");
            } else {
                Log.v(LOGTAG, "Page load finished in " + elapsed + "ms");
            }
            if(timeoutFlag) {
                writeTimeoutFile(url);
            }
            System.runFinalization();
            System.gc();
            System.gc();
        }
        updateTestStatus(TEST_DONE);
        activity.finish();
        listReader.close();
    }
    
    public void copyRunnerAssetsToCache() {
        try {
            String out_dir = getActivity().getApplicationContext()
            .getCacheDir().getPath() + "/";

            for( int i=0; i< RELIABILITY_TEST_RUNNER_FILES.length; i++) {
                InputStream in = getActivity().getAssets().open(
                        RELIABILITY_TEST_RUNNER_FILES[i]);
                OutputStream out = new FileOutputStream(
                        out_dir + RELIABILITY_TEST_RUNNER_FILES[i]);

                byte[] buf = new byte[2048];
                int len;

                while ((len = in.read(buf)) >= 0 ) {
                    out.write(buf, 0, len);
                }
                out.close();
                in.close();
            }
        }catch (IOException e) {
            Log.e(LOGTAG, "Cannot extract scripts for testing.", e);
        }
    }
    
    private void updateTestStatus(String s) {
        // write last tested url into status file
        try {
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(TEST_STATUS_FILE));
            bos.write(s.getBytes());
            bos.close();
        } catch (IOException e) {
            Log.e(LOGTAG, "Cannot update file " + TEST_STATUS_FILE, e);
        }
    }
    
    private String readTestStatus() {
        // read out the test name it stopped last time.
        String status = null;
        File testStatusFile = new File(TEST_STATUS_FILE);
        if(testStatusFile.exists()) {
            try {
                BufferedReader inReader = new BufferedReader(
                        new FileReader(testStatusFile));
                status = inReader.readLine();
                inReader.close();
            } catch (IOException e) {
                Log.e(LOGTAG, "Error reading test status.", e);
            }
        }
        return status;
    }
    
    private void fastForward(BufferedReader testListReader, String lastUrl) {
        //fastforward the BufferedReader to the position right after last url
        if(lastUrl == null)
            return;
        
        String line = null;
        try {
            while((line = testListReader.readLine()) != null) {
                if(lastUrl.equals(line))
                    return;
            }
        } catch (IOException ioe) {
            Log.e(LOGTAG, "Error while reading test list.", ioe);
            return;
        }
    }

    private void writeTimeoutFile(String s) {
        //append to the file containing the list of timeout urls 
        try {
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(TEST_TIMEOUT_FILE, true));
            bos.write(s.getBytes());
            bos.write('\n');
            bos.close();
        } catch (Exception e) {
            Log.e(LOGTAG, "Cannot update file " + TEST_TIMEOUT_FILE, e);
        }
    }
}