ImportantFileWriterAndroidTest.java revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.content.browser;
6
7import android.test.InstrumentationTestCase;
8import android.test.suitebuilder.annotation.SmallTest;
9
10import org.chromium.base.ImportantFileWriterAndroid;
11import org.chromium.base.test.util.Feature;
12import org.chromium.content.app.LibraryLoader;
13import org.chromium.content.common.CommandLine;
14import org.chromium.content.common.ProcessInitException;
15import org.chromium.content_shell_apk.ContentShellApplication;
16
17import java.io.DataInputStream;
18import java.io.File;
19import java.io.FileInputStream;
20import java.io.IOException;
21import java.lang.Throwable;
22
23
24/**
25 * Tests for {@Link ImportantFileWriterAndroid}
26 *
27 * Note that this assumes that the underlying native atomic write functions
28 * work, so is not attempting to test that writes are atomic. Instead it is just
29 * testing that the Java code is calling the native code correctly.
30 */
31public class ImportantFileWriterAndroidTest extends InstrumentationTestCase {
32
33    void loadJni() {
34        getInstrumentation().runOnMainSync(new Runnable() {
35            @Override
36            public void run() {
37                ContentShellApplication.initializeApplicationParameters();
38                try {
39                    LibraryLoader.ensureInitialized();
40                } catch (ProcessInitException e) {
41                    throw new Error(e);
42                }
43            }
44        });
45    }
46
47    private void checkFile(File testFile, byte[] data) {
48        assertTrue(testFile.exists());
49        try {
50            byte[] fileData = new byte[(int) testFile.length()];
51            DataInputStream dis =
52                    new DataInputStream(new FileInputStream(testFile));
53            dis.readFully(fileData);
54            dis.close();
55            assertEquals("Data length wrong", data.length, fileData.length);
56            for(int i = 0; i < data.length; i++) {
57                assertEquals("Data byte wrong", data[i], fileData[i]);
58            }
59        } catch (IOException e) {
60            fail("Failed to read file");
61        }
62
63    }
64
65    @SmallTest
66    @Feature({"Android-AppBase"})
67    public void testAtomicWrite() {
68        // Try writing a file that can't be created.
69        byte[] data1 = {0,1,2,3,4,5,6,7,8,9};
70        assertFalse("Writing bad file succeded",
71                ImportantFileWriterAndroid.writeFileAtomically(
72                        "/junk/junk", data1));
73        File dir = getInstrumentation().getTargetContext().getFilesDir();
74        File testFile = new File(dir, "ImportantFileTest");
75
76        // Make sure the file doesn't already exist
77        if(testFile.exists()) {
78            assertTrue(testFile.delete());
79        }
80
81        // Write a new file
82        assertTrue("Writing new file failed",
83                ImportantFileWriterAndroid.writeFileAtomically(
84                        testFile.getAbsolutePath(), data1));
85        checkFile(testFile, data1);
86        byte[] data2 = {10, 20, 30, 40, 50, 60, 70, 80};
87
88        // Overwrite an existing file
89        assertTrue("Writing exiting file failed",
90                ImportantFileWriterAndroid.writeFileAtomically(
91                        testFile.getAbsolutePath(), data2));
92        checkFile(testFile, data2);
93
94        // Done, tidy up
95        assertTrue(testFile.delete());
96    }
97
98    @Override
99    public void setUp() throws Exception {
100        loadJni();
101    }
102}
103