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