1 /*
2  * Copyright (C) 2022 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 
17 package com.android.bedstead.harrier;
18 
19 import android.content.ContentProvider;
20 import android.content.ContentValues;
21 import android.database.Cursor;
22 import android.database.MatrixCursor;
23 import android.net.Uri;
24 
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.concurrent.atomic.AtomicInteger;
27 
28 /**
29  * {@link ContentProvider} used to expose test results to Bedstead.
30  */
31 public class BedsteadRunResultsProvider extends ContentProvider {
32 
33     public static final AtomicInteger sNumberOfTests = new AtomicInteger(-1);
34     public static final ConcurrentHashMap<Integer, BedsteadResult> sResults =
35             new ConcurrentHashMap<>();
36 
37     @Override
onCreate()38     public boolean onCreate() {
39         return true;
40     }
41 
42     @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)43     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
44             String sortOrder) {
45         if (uri.getPath().equals("/numTests")) {
46             return getNumTests();
47         }
48         int target = Integer.parseInt(uri.getPath().substring(1));
49         if (!sResults.containsKey(target)) {
50             return null;
51         }
52 
53         MatrixCursor matrixCursor = new MatrixCursor(
54                 new String[] {
55                         "index", "testName", "result", "message",
56                         "stackTrace", "runTime", "isFinished"});
57         BedsteadResult result = sResults.get(target);
58         matrixCursor.addRow(new Object[]{
59                 result.mIndex, result.mTestName, result.mResult,
60                 result.mMessage, result.mStackTrace, result.mRuntime, result.mIsFinished});
61         if (result.mIsFinished) {
62             result.mHasBeenFetched = true;
63         }
64         return matrixCursor;
65     }
66 
getNumTests()67     private Cursor getNumTests() {
68         MatrixCursor matrixCursor = new MatrixCursor(new String[] {"tests"});
69         matrixCursor.addRow(new Object[]{sNumberOfTests.get()});
70         return matrixCursor;
71     }
72 
73     @Override
getType(Uri uri)74     public String getType(Uri uri) {
75         return null;
76     }
77 
78     @Override
insert(Uri uri, ContentValues values)79     public Uri insert(Uri uri, ContentValues values) {
80         // We do not allow inserting
81         return null;
82     }
83 
84     @Override
delete(Uri uri, String selection, String[] selectionArgs)85     public int delete(Uri uri, String selection, String[] selectionArgs) {
86         // We do not allow deleting
87         return 0;
88     }
89 
90     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)91     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
92         // We do not allow updating
93         return 0;
94     }
95 }
96