• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.devicehealth.tests;
17 
18 import org.junit.Assert;
19 import org.junit.Before;
20 
21 import android.content.Context;
22 import android.os.DropBoxManager;
23 import android.support.test.InstrumentationRegistry;
24 
25 abstract class HealthCheckBase {
26 
27     private Context mContext;
28 
29     @Before
setUp()30     public void setUp() throws Exception {
31         mContext = InstrumentationRegistry.getInstrumentation().getContext();
32     }
33 
34     /**
35      * Check dropbox service for a particular label and assert if found
36      */
checkCrash(String label)37     protected void checkCrash(String label) {
38         DropBoxManager dropbox = (DropBoxManager) mContext
39                 .getSystemService(Context.DROPBOX_SERVICE);
40         Assert.assertNotNull("Unable access the DropBoxManager service", dropbox);
41 
42         long timestamp = 0;
43         DropBoxManager.Entry entry = null;
44         int crashCount = 0;
45         StringBuilder errorDetails = new StringBuilder("Error details:\n");
46         while (null != (entry = dropbox.getNextEntry(label, timestamp))) {
47             try {
48                 crashCount++;
49                 errorDetails.append(label);
50                 errorDetails.append(": ");
51                 errorDetails.append(entry.getText(70));
52                 errorDetails.append("    ...\n");
53             } finally {
54                 entry.close();
55             }
56             timestamp = entry.getTimeMillis();
57         }
58         Assert.assertEquals(errorDetails.toString(), 0, crashCount);
59     }
60 }
61