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